Enforce functional dependencies by using triggers in SQL99 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 &lt;&gt; N.C;
  IF(counter&gt;0)
    THEN raise_exception ('AB-&gt;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?
An instead of trigger is exactly that; it won't perform an insert even if data satisfy the constraint, so it won't be appropriate.

I understand that yours is a contrived example and that in real life you'll use a unique constraint to enforce such a rule, but the exists predicate nevertheless might be more efficient:

BEGIN 
  IF EXISTS (
    SELECT 1 FROM R
    WHERE R.A = N.A AND R.B = N.B AND R. C &lt;&gt; N.C;
  )
    THEN raise_exception ('AB-&gt;C on R was violated');
END;