80 likes | 191 Views
Dilemmas in Integrity Constraints (Which First: Chicken or Egg?). Chicken-egg dilemmas. Assume that we would like to create the following tables: PROF( prof-id , affiliated - dept-id ) DEPT( dept-id , head-prof-id )
E N D
Dilemmas in Integrity Constraints (Which First: Chicken or Egg?)
Chicken-egg dilemmas • Assume that we would like to create the following tables: • PROF(prof-id, affiliated-dept-id)DEPT(dept-id, head-prof-id) • We want to set affiliated-dept-id as a foreign key on DEPT, and head-prof-id as a foreign key on PROF. • Will the following declarations work? • CREATE TABLE PROF ( prof-id INTEGER, affiliated-dept-id CHAR(20), PRIMARY KEY (prof-id), FOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id)) CREATE TABLE DEPT ( dept-id CHAR(20), head-prof-id INTEGER, PRIMARY KEY (dept-id), FOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id))
Chicken-egg dilemmas (cont.) • CREATE TABLE PROF ( prof-id INTEGER, affiliated-dept-id CHAR(20), PRIMARY KEY (prof-id), FOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id)) CREATE TABLE DEPT ( dept-id CHAR(20), head-prof-id INTEGER, PRIMARY KEY (dept-id), FOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id)) • No. • Dilemma 1: if we run the definition of PROF first, DEPT does not exist, so the FOREIGN KEY statement is invalid. Similarly, neither can we run the definition of DEPT first. • To solve this problem, we should create tables first, and then, add constraints – see next.
Solving Dilemma 1 • CREATE TABLE PROF ( prof-id INTEGER, affiliated-dept-id CHAR(20), PRIMARY KEY (prof-id))CREATE TABLE DEPT ( dept-id CHAR(20), head-prof-id INTEGER, PRIMARY KEY (dept-id)) • ALTER TABLE PROF ADD CONSTRAINT PROF-CONSTFOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id) ALTER TABLE DEPT ADD CONSTRAINT DEPT-CONSTFOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id) constraint names
Chicken-egg dilemmas (cont.) • ALTER TABLE PROF ADD CONSTRAINT PROF-CONSTFOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id)) ALTER TABLE DEPT ADD CONSTRAINT DEPT-CONSTFOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id)) • Dilemma 2: Should you insert tuples into PROF or DEPT first? • Answer: Neither works. • Let us say the first tuple is inserted into PROF as(1, ‘D1’). • But – where is ‘D1’?
Chicken-egg dilemmas (cont.) • ALTER TABLE PROF ADD CONSTRAINT PROF-CONSTFOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id)) ALTER TABLE DEPT ADD CONSTRAINT DEPT-CONSTFOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id)) • Dilemma 2: Should you insert tuples into PROF or DEPT first? • Answer: Neither works. • This dilemma arises because, by default, the database checks the foreign key constraint right after an insertion. • To solve the problem, we should ask the database to defer the checking, until we instruct it to do so.
Chicken-egg dilemmas (cont.) • ALTER TABLE PROF ADD CONSTRAINT PROF-CONSTFOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id) INITIALLY DEFERRED DEFERRABLE; ALTER TABLE DEPT ADD CONSTRAINT DEPT-CONSTFOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id) INITIALLY DEFERRED DEFERRABLE; • INSERT INTO PROF VALUES (1, ‘D1’); • INSERT INTO DEPT VALUES (‘D1’, 1); • COMMIT; • The database checks the integrity constraint at this time.
Solving Dilemma 2 • ALTER TABLE PROF ADD CONSTRAINT PROF-CONSTFOREIGN KEY (affiliated-dept-id) REFERENCES DEPT(dept-id) INITIALLY DEFERRED DEFERRABLE; ALTER TABLE DEPT ADD CONSTRAINT DEPT-CONSTFOREIGN KEY (head-prof-id) REFERENCES PROF(prof-id) INITIALLY DEFERRED DEFERRABLE; • INSERT INTO PROF VALUES (1, ‘D1’); • INSERT INTO DEPT VALUES (‘D1’, 1); • COMMIT; -- the database checks the foreign key constraint • INSERT INTO PROF VALUES (2, ‘D2’); • COMMIT; -- the database checks the foreign key constraint • The database replies:“Transaction rolled back. Constraint violated” • Consequence: (2, ‘D2’) is not inserted.