1 / 8

Dilemmas in Integrity Constraints (Which First: Chicken or Egg?)

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 )

anika-combs
Télécharger la présentation

Dilemmas in Integrity Constraints (Which First: Chicken or Egg?)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dilemmas in Integrity Constraints (Which First: Chicken or Egg?)

  2. 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))

  3. 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.

  4. 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

  5. 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’?

  6. 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.

  7. 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.

  8. 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.

More Related