I have a Table called Couple which has the following Fields CoupleId, HusbandId , WifeId ,
StartDate , EndDate
The previous Table contains informations about couple, The CoupleId is a primary key
HusbandId and WifeId are Foreign keys form another Table Named Person
StartDate and EndDate represent the start/end date of marriage that happens between a male with HusbandId and female with WifeId
in my problem i have a condition that w female can’t married to two or more male at same time “Synchronous marriage”
and i have the following query
SELECT
DISTINCT A.WifeID
FROM
Couple A
INNER JOIN Couple B
ON A.WifeID = B.WifeID
AND A.HusbandID <> B.HusbandID
AND A.StartDate < B.EndDate
AND A.EndDate > B.StartDate;
which returns any female who has Synchronous marriage ” Married to two or more male at same time”
I want to write trigger which fire when somebody want to insert or update the couple
table ,and i want this trigger allow the modification only if it make no fault information on it ( for example if someone insert a row on couple table and this row make the wife with #4 married to tow or more person at same time, this insertion must not complete because it make wrong info in the table)
can any one help me at this point ?
You can solve this without a trigger. Make a Composite primary key of these three keys in the
Couplestable like so:This will ensure the uniqueness of the
HusbandIdandWifeIdfor eachcoupleId.Here is a demo for the schema design for these two tables:
SQL Fiddle Demo.
If you want to do this using a TRIGGER, try this:
Updated SQL Fiddle Demo using a trigger.
This is just an example, you have to provide an update statement in the
ELSEblock if you need.