1 --Column ID of table A is primary key:
6 CREATE UNIQUE INDEX AI ON A (ID);
8 --Columns REFB of table B and REFC of C are foreign keys referencing ID of A:
13 CREATE INDEX BI ON B (REFB);
18 CREATE INDEX CI ON C (REFC);
20 --Trigger for table A:
22 CREATE TRIGGER AT BEFORE DELETE OR UPDATE ON A FOR EACH ROW
24 check_foreign_key (2, 'cascade', 'ID', 'B', 'REFB', 'C', 'REFC');
26 2 - means that check must be performed for foreign keys of 2 tables.
27 cascade - defines that corresponding keys must be deleted.
28 ID - name of primary key column in triggered table (A). You may
29 use as many columns as you need.
30 B - name of (first) table with foreign keys.
31 REFB - name of foreign key column in this table. You may use as many
32 columns as you need, but number of key columns in referenced
33 table (A) must be the same.
34 C - name of second table with foreign keys.
35 REFC - name of foreign key column in this table.
38 --Trigger for table B:
40 CREATE TRIGGER BT BEFORE INSERT OR UPDATE ON B FOR EACH ROW
42 check_primary_key ('REFB', 'A', 'ID');
45 REFB - name of foreign key column in triggered (B) table. You may use as
46 many columns as you need, but number of key columns in referenced
47 table must be the same.
48 A - referenced table name.
49 ID - name of primary key column in referenced table.
52 --Trigger for table C:
54 CREATE TRIGGER CT BEFORE INSERT OR UPDATE ON C FOR EACH ROW
56 check_primary_key ('REFC', 'A', 'ID');
60 INSERT INTO A VALUES (10);
61 INSERT INTO A VALUES (20);
62 INSERT INTO A VALUES (30);
63 INSERT INTO A VALUES (40);
64 INSERT INTO A VALUES (50);
66 INSERT INTO B VALUES (1); -- invalid reference
67 INSERT INTO B VALUES (10);
68 INSERT INTO B VALUES (30);
69 INSERT INTO B VALUES (30);
71 INSERT INTO C VALUES (11); -- invalid reference
72 INSERT INTO C VALUES (20);
73 INSERT INTO C VALUES (20);
74 INSERT INTO C VALUES (30);
76 DELETE FROM A WHERE ID = 10;
77 DELETE FROM A WHERE ID = 20;
78 DELETE FROM A WHERE ID = 30;