The question is as below:
For R(A, B, C, D, E), write a trigger to enforce AB->C on insert.
This is my solution:
CREATE TRIGGER fdEnforceInsert
BEFORE INSERT ON R
REFERENCING NEW ROW AS N
FOR EACH ROW
DECLARE counter INT
BEGIN
SELECT COUNT(*) INTO counter
FROM R
WHERE R.A = N.A AND R.B = N.B AND R. C <> N.C;
IF(counter>0)
THEN raise_exception ('AB->C on R was violated');
END;
One thing I'd like to ask is whether to use "before" or "instead of" in the second line?