I'm using Microsoft SQL server 2008 and I need to create a constraint that will prevent a [Tx] from being created/updated if the [Driver] is not allowed to either the source [Shipper] or destination [Consignee] site.
Tables:
- Drivers (holds typical driver info)
- Shipper/Consignee (source/destination site - holds locations the driver goes from/to)
- DriverForbiddenSite (holds sites the driver is not allowed to)
- Schedule (hold driver, and when)
- Tx (a transaction has source site{shipper}, destination site {consignee}, cargo, schedule)
Here is a query that will show any record that violates the rule:
select * from DriverForbiddenSite dfs
join Schedule sch on dfs.driverId=sch.driverId
join Tx tx on sch.scheduleId=tx.scheduleId
left join Shipper shi on shi.shipperId=tx.shipperId
left join Consignee con on con.consigneeId=tx.consigneeId
where dfs.transloadLocationId=shi.transloadLocationId or dfs.transloadLocationId=con.transloadLocationId
How would be best to prevent Tx's from being created if the Driver is not allowed on the Site?
Thanks