I am looking for a better way to add a constraint to a table, which is actually a unique index for the relationship between the record and the rest of the records in this table.
Imagine the following table describing the patrol of various guards (from the previous watchman scenario)
PK PatrolID Integer
FK GuardID Integer
Starts DateTime
Ends DateTime
Let's start with a constraint indicating that the start and end times should be logical:
Ends >= Starts
However, I want to add one more logical restriction: a specific guard (GuardID) cannot be in two places at the same time, that is, for any record, the period indicated at the beginning / end should not overlap with the period defined for any other guard.
I can think of two ways to approach this:
INSTEAD OF INSERT. INSERTED, . - , . , , - , SQL Server, , UPDATE. INSERTED, .
, , - CONSTRAINT, , PatrolID, GuardID, Starts and Ends. WHERE EXISTS , GuardID/Starts/Ends, PatrolID. , .
? - - , , / ( , , , , ). (, - INDEX?)