25
More on Primary and Foreign Keys Please see speaker notes for additional information!

More on Primary and Foreign Keys Please see speaker notes for additional information!

Embed Size (px)

Citation preview

Page 1: More on Primary and Foreign Keys Please see speaker notes for additional information!

More on Primary and Foreign Keys

Please see speaker notes for additional information!

Page 2: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM student00;

STUD NAME STADR CITY ST ZIP PHONE SSNUM MA---- -------------------- -------------------- -------------------- -- ----- ---------- --------- --ENROLLED---------1111 Stephen Daniels 345 Midland Ave Yonkers NY 03456 9145553456 036574678 BU09-SEP-00

1212 Jennifer Ames 12 Ave F Fall River MA 02740 5085558343 034759850 CI02-SEP-00

2222 Carl Hersey 12 Ave F Fall River MA 02740 5085558343 045673945 BU02-SEP-00

2345 Mary Stanton 156 West St Taunton MA 04567 5085552090 035678090 CI05-SEP-00

3333 John Richards 76 Main St Fall River MA 02456 5085556498 035656432 CI06-SEP-00

Student systemStudent system

This is the student00 table that holds information about the individual student.

We will establish the primary key as studentidno.

SQL> DESC student00; Name Null? Type ------------------------------- -------- ---- STUDENTIDNO VARCHAR2(4) NAME VARCHAR2(20) STADR VARCHAR2(20) CITY VARCHAR2(20) STATE VARCHAR2(2) ZIP VARCHAR2(5) PHONE VARCHAR2(10) SSNUM VARCHAR2(9) MAJORCODE VARCHAR2(2) ENROLLED DATE

Page 3: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM stucourse00;

STUD COURS SEMTA GR---- ----- ----- --1111 CIS11 F2000 A-1111 MAR11 F2000 A1111 CIS44 S2000 A1212 CIS44 S2000 A2222 CIS44 S2000 A2222 MAN11 F2000 A-3333 CIS44 F2000 B3333 CIS44 F2000 B3333 CIS50 F2000 B+3333 CIS56 S2000 A-2345 CIS50 F2000 I1111 CIS50 S2000 A+

Student systemStudent system

SQL> DESC stucourse00 Name Null? Type ------------------------------- -------- ---- STUDENTIDNO VARCHAR2(4) COURSECD VARCHAR2(5) SEMTAKEN VARCHAR2(5) GRADE VARCHAR2(2)

This is the stucourse00 table that contains information on the courses the student has taken.

The key to this table will be the studentidno combined with the coursecd combined with the semtaken.

Page 4: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM major00;

MA MAJORNAME CHAIR-- ------------------------------ --------------------BU Business Administration AdamsCI Computer Information Systems Grocer

SQL> DESC major00; Name Null? Type ------------------------------- -------- ---- MAJORCODE VARCHAR2(2) MAJORNAME VARCHAR2(30) CHAIR VARCHAR2(20)

Student systemStudent system

This is the table that holds information about the major itself.

The primary key for this table will be the majorcode.

Page 5: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM course00;

COURS COURSENAME CREDITS----- ------------------------------ ---------CIS11 Intro to Computer Info Systems 3CIS44 Internet User/Developer 3CIS50 Oracle and SQL 3CIS56 Visual Basic 3MAN11 Intro to Management 3MAR11 Marketing Principles 3

6 rows selected.

SQL> DESC course00 Name Null? Type ------------------------------- -------- ---- COURSECD VARCHAR2(5) COURSENAME VARCHAR2(30) CREDITS NUMBER(1)

Student systemStudent system

This is the table that holds information about the courses themselves.

The primary key for this table will be the coursecd.

Page 6: More on Primary and Foreign Keys Please see speaker notes for additional information!

Student systemStudent system

SQL> ALTER TABLE student00 2 ADD CONSTRAINT studentidno_pk PRIMARY KEY (studentidno);

Table altered.

SQL> DESC student00 Name Null? Type ------------------------------- -------- ---- STUDENTIDNO NOT NULL VARCHAR2(4) NAME VARCHAR2(20) STADR VARCHAR2(20) CITY VARCHAR2(20) STATE VARCHAR2(2) ZIP VARCHAR2(5) PHONE VARCHAR2(10) SSNUM VARCHAR2(9) MAJORCODE VARCHAR2(2) ENROLLED DATE

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUDENT00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - -----------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT STUDENTIDNO_PK P STUDENT00

ENABLED

The ALTER statement is used to add the PRIMARY KEY to the student00 table.

Page 7: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> ALTER TABLE stucourse00 2 ADD CONSTRAINT thekey_pk PRIMARY KEY (studentidno, coursecd, semtaken);ALTER TABLE stucourse00*ERROR at line 1:ORA-02437: cannot enable (SCOTT.THEKEY_PK) - primary key violated

SQL> select * from stucourse00;

STUD COURS SEMTA GR---- ----- ----- --1111 CIS11 F2000 A-1111 MAR11 F2000 A1111 CIS44 S2000 A1212 CIS44 S2000 A2222 CIS44 S2000 A2222 MAN11 F2000 A-3333 CIS44 F2000 B3333 CIS44 F2000 B3333 CIS50 F2000 B+3333 CIS56 S2000 A-2345 CIS50 F2000 I1111 CIS50 S2000 A+

12 rows selected.

Student systemStudent system

I have a problem here, there are two courses with the same studentidno, same coursecd and same semtaken. When I try to create the key, I get an error because the primary key would be violated.

Page 8: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> DELETE FROM stucourse00 2 WHERE studentidno = '3333' AND coursecd = 'CIS44';

2 rows deleted.

SQL> INSERT INTO stucourse00 2 VALUES('3333','CIS44','F2000','B');

1 row created.

SQL> SELECT * FROM stucourse00;

STUD COURS SEMTA GR---- ----- ----- --1111 CIS11 F2000 A-1111 MAR11 F2000 A1111 CIS44 S2000 A1212 CIS44 S2000 A2222 CIS44 S2000 A2222 MAN11 F2000 A-3333 CIS50 F2000 B+3333 CIS56 S2000 A-2345 CIS50 F2000 I1111 CIS50 S2000 A+3333 CIS44 F2000 B

11 rows selected.

SQL> ALTER TABLE stucourse00 2 ADD CONSTRAINT thekey_pk PRIMARY KEY (studentidno, coursecd, semtaken);

Table altered.

SQL> DESC stucourse00 Name Null? Type ------------------------------- -------- ---- STUDENTIDNO NOT NULL VARCHAR2(4) COURSECD NOT NULL VARCHAR2(5) SEMTAKEN NOT NULL VARCHAR2(5) GRADE VARCHAR2(2)

I deleted the identical records and added one back in.

The ALTER creates the Primary Key made up of three fields: studentidno, coursecd, semtaken.

Page 9: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> ALTER TABLE major00 2 ADD CONSTRAINT majorcode_pk PRIMARY KEY (majorcode);

Table altered.

SQL> DESC major00; Name Null? Type ------------------------------- -------- ---- MAJORCODE NOT NULL VARCHAR2(2) MAJORNAME VARCHAR2(30) CHAIR VARCHAR2(20)

SQL> ALTER TABLE course00 2 ADD CONSTRAINT coursecd_pk PRIMARY KEY (coursecd);

Table altered.

Student systemStudent system

Here I am creating the primary key for the major00 table which is majorcode.

SQL> DESC course00 Name Null? Type ------------------------------- -------- ---- COURSECD NOT NULL VARCHAR2(5) COURSENAME VARCHAR2(30) CREDITS NUMBER(1)

Here I am creating the primary key for the course00 table which is coursecd.

Page 10: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUCOURSE00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - --------------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT THEKEY_PK P STUCOURSE00

ENABLED

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'MAJOR00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - --------------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT MAJORCODE_PK P MAJOR00

ENABLED

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'COURSE00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - --------------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT COURSECD_PK P COURSE00

ENABLED

Page 11: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> DESC student00; Name Null? Type ------------------------------- -------- ---- STUDENTIDNO NOT NULL VARCHAR2(4) NAME VARCHAR2(20) STADR VARCHAR2(20) CITY VARCHAR2(20) STATE VARCHAR2(2) ZIP VARCHAR2(5) PHONE VARCHAR2(10) SSNUM VARCHAR2(9) MAJORCODE VARCHAR2(2) ENROLLED DATE

SQL> DESC major00; Name Null? Type ------------------------------- -------- ---- MAJORCODE NOT NULL VARCHAR2(2) MAJORNAME VARCHAR2(30) CHAIR VARCHAR2(20)

SQL>ALTER TABLE student00 2 ADD CONSTRAINT majorcode_fk FOREIGN KEY(majorcode) REFERENCES major00;

Table altered.

Student systemStudent system

I want to establish majorcode on the student00 table as a foreign key linking to majorcode on the major00 table.

The alter statement optionally names the key that is being established. The required portions of the add constraint are to establish that it is a foreign key, the name of the foreign key on the table student00 where it is a foreign key and to establish the table that I want to link to. Note the link is to the primary key already established on the major00 table.

Page 12: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUDENT00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - -------------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT STUDENTIDNO_PK P STUDENT00

ENABLED

SCOTT MAJORCODE_FK R STUDENT00

SCOTT MAJORCODE_PK NO ACTION ENABLED

Student systemStudent system

Page 13: More on Primary and Foreign Keys Please see speaker notes for additional information!

Student systemStudent system

SQL> ALTER TABLE student00 2 ADD CONSTRAINT major_fk FOREIGN KEY(majorcode) REFERENCES experiment_major;ADD CONSTRAINT major_fk FOREIGN KEY(majorcode) REFERENCES experiment_major *ERROR at line 2:ORA-02268: referenced table does not have a primary key

SQL> CREATE INDEX major_index ON experiment_major(majorcode);

Index created.

SQL> ALTER TABLE student00 2 ADD CONSTRAINT major_fk FOREIGN KEY(majorcode) REFERENCES experiment_major;ADD CONSTRAINT major_fk FOREIGN KEY(majorcode) REFERENCES experiment_major *ERROR at line 2:ORA-02268: referenced table does not have a primary key

SQL> DESC experiment_major; Name Null? Type ------------------------------- -------- ---- MAJORCODE VARCHAR2(2) MAJORNAME VARCHAR2(30) CHAIR VARCHAR2(20)

I created the experiment_major table to experiment with. It is set up just like major00.

Because experiment_major does not have a primary key, I can not establish a foreign key link to it. I then created an index in experiment_major to see if I could link to that, again it was rejected.

Page 14: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUCOURSE00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - -----------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT THEKEY_PK P STUCOURSE00

ENABLED

SQL> ALTER TABLE stucourse00 2 ADD CONSTRAINT coursecd_FK FOREIGN KEY(coursecd) REFERENCES course00;

Table altered.

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUCOURSE00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - -----------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT THEKEY_PK P STUCOURSE00

ENABLED

SCOTT COURSECD_FK R STUCOURSE00

SCOTT COURSECD_PK NO ACTION ENABLED

Page 15: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> ALTER TABLE stucourse00 2 ADD CONSTRAINT studentidno_FK FOREIGN KEY(studentidno) REFERENCES student00;

Table altered.

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'STUCOURSE00';

OWNER CONSTRAINT_NAME C TABLE_NAME------------------------------ ------------------------------ - -------------------SEARCH_CONDITION--------------------------------------------------------------------------------R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS------------------------------ ------------------------------ --------- --------SCOTT THEKEY_PK P STUCOURSE00

ENABLED

SCOTT COURSECD_FK R STUCOURSE00

SCOTT COURSECD_PK NO ACTION ENABLED

SCOTT STUDENTIDNO_FK R STUCOURSE00

SCOTT STUDENTIDNO_PK NO ACTION ENABLED

Student systemStudent system

Page 16: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS 2 WHERE TABLE_NAME IN ('STUDENT00', 'STUCOURSE00', 'COURSE00', 'MAJOR00');

TABLE_NAME CONSTRAINT_NAME------------------------------ ------------------------------COURSE00 COURSECD_PKMAJOR00 MAJORCODE_PKSTUCOURSE00 THEKEY_PKSTUCOURSE00 COURSECD_FKSTUCOURSE00 STUDENTIDNO_FKSTUDENT00 STUDENTIDNO_PKSTUDENT00 MAJORCODE_FK

7 rows selected.

Student systemStudent system

A very useful way to find out what constraints are active in the system that is being used is the SELECT below. Here I can see the keys for all four tables.

Page 17: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT studentidno, name 2 FROM student00;

STUD NAME---- --------------------1111 Stephen Daniels1212 Jennifer Ames2222 Carl Hersey2345 Mary Stanton3333 John Richards

Student systemStudent system

SQL> INSERT INTO stucourse00 2 VALUES ('1234','CIS11','S2000','C');INSERT INTO stucourse00 *ERROR at line 1:ORA-02291: integrity constraint (SCOTT.STUDENTIDNO_FK) violated - parent key not found

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS 2 WHERE TABLE_NAME IN ('STUDENT00', 'STUCOURSE00');

TABLE_NAME CONSTRAINT_NAME------------------------------ ------------------------------STUCOURSE00 THEKEY_PKSTUCOURSE00 COURSECD_FKSTUCOURSE00 STUDENTIDNO_FKSTUDENT00 STUDENTIDNO_PKSTUDENT00 MAJORCODE_FK

The stucourse00 table has a foreign key on the studentidno which is linked to the primary key on the student00 table. This means that I cannot add a record to the stucourse00 table unless the studentidno of the new record already exists on the student00.

Page 18: More on Primary and Foreign Keys Please see speaker notes for additional information!

Student systemStudent system SQL> SELECT * 2 FROM course00;

COURS COURSENAME CREDITS----- ------------------------------ ---------CIS11 Intro to Computer Info Systems 3CIS44 Internet User/Developer 3CIS50 Oracle and SQL 3CIS56 Visual Basic 3MAN11 Intro to Management 3MAR11 Marketing Principles 3

SQL> INSERT INTO stucourse00 2 VALUES ('3333','CIS40','S2000','C');INSERT INTO stucourse00 *ERROR at line 1:ORA-02291: integrity constraint (SCOTT.COURSECD_FK) violated - parent key not found

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS 2 WHERE TABLE_NAME IN ('STUCOURSE00','COURSE00');

TABLE_NAME CONSTRAINT_NAME------------------------------ ------------------------------COURSE00 COURSECD_PKSTUCOURSE00 THEKEY_PKSTUCOURSE00 COURSECD_FKSTUCOURSE00 STUDENTIDNO_FK

This time the studentidno was okay, but I attempted to add a course that did not exist when I added the child record to stucourse00. Therefore the parent violation of coursecd_fk.

Page 19: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> INSERT INTO course00 2 VALUES('CIS40', 'Lotus Notes', 3);

1 row created.

SQL> SELECT * FROM course00;

COURS COURSENAME CREDITS----- ------------------------------ ---------CIS11 Intro to Computer Info Systems 3CIS44 Internet User/Developer 3CIS50 Oracle and SQL 3CIS56 Visual Basic 3MAN11 Intro to Management 3MAR11 Marketing Principles 3CIS40 Lotus Notes 3

7 rows selected.

Student systemStudent system

SQL> INSERT INTO stucourse00 2 VALUES ('3333','CIS40','S2000','C');

1 row created.

SQL> SELECT * FROM stucourse00;

STUD COURS SEMTA GR---- ----- ----- --1111 CIS11 F2000 A-1111 MAR11 F2000 A1111 CIS44 S2000 A1212 CIS44 S2000 A2222 CIS44 S2000 A2222 MAN11 F2000 A-3333 CIS40 S2000 C3333 CIS50 F2000 B+3333 CIS56 S2000 A-2345 CIS50 F2000 I1111 CIS50 S2000 A+3333 CIS44 F2000 B

I have now added a course, CIS40 to the course00 table.

Now when I attempt to add a record to stucourse00 table it is successful because studentidno 3333 exists on student00 and because of the insert above, CIS40 now exists on course00.

Page 20: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT CONSTRAINT_NAME, COLUMN_NAME 2 from USER_CONS_COLUMNS 3 WHERE TABLE_NAME 4 IN ('STUDENT00', 'STUCOURSE00', 'COURSE00', 'MAJOR00');

CONSTRAINT_NAME COLUMN_NAME------------------------------ ------------------------------COURSECD_FK COURSECDCOURSECD_PK COURSECDMAJORCODE_FK MAJORCODEMAJORCODE_PK MAJORCODESTUDENTIDNO_FK STUDENTIDNOSTUDENTIDNO_PK STUDENTIDNOTHEKEY_PK STUDENTIDNOTHEKEY_PK COURSECDTHEKEY_PK SEMTAKEN

9 rows selected.

Student systemStudent system

This shows the constraint name and the columns involved. Note that for THEKEY_PK there are three columns listed because this key is made up of three columns.

Page 21: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME 2 FROM USER_CONS_COLUMNS 3 WHERE TABLE_NAME 4 IN ('STUDENT00', 'STUCOURSE00', 'COURSE00', 'MAJOR00');

TABLE_NAME CONSTRAINT_NAME COLUMN_NAME------------------------------ ------------------------------ ----------------STUCOURSE00 COURSECD_FK COURSECDCOURSE00 COURSECD_PK COURSECDSTUDENT00 MAJORCODE_FK MAJORCODEMAJOR00 MAJORCODE_PK MAJORCODESTUCOURSE00 STUDENTIDNO_FK STUDENTIDNOSTUDENT00 STUDENTIDNO_PK STUDENTIDNOSTUCOURSE00 THEKEY_PK STUDENTIDNOSTUCOURSE00 THEKEY_PK COURSECDSTUCOURSE00 THEKEY_PK SEMTAKEN

9 rows selected.

Student systemStudent system

This includes the table name in case there is possibility for confusion.

Page 22: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> ALTER TABLE course00 2 DISABLE CONSTRAINT coursecd_pk 3 CASCADE;

Student systemStudent system

SQL> INSERT INTO stucourse00 2 VALUES ('2222','MAR55','S2000','C+');

SQL> SELECT * FROM course00;

COURS COURSENAME CREDITS----- ------------------------------ ---------CIS11 Intro to Computer Info Systems 3CIS44 Internet User/Developer 3CIS50 Oracle and SQL 3CIS56 Visual Basic 3MAN11 Intro to Management 3MAR11 Marketing Principles 3CIS40 Lotus Notes 3

SQL> ALTER TABLE course00 2 ENABLE CONSTRAINT coursecd_pk;

Table altered.

The disable clause turns of the coursecd_pk constraint and the cascade takes care of foreign keys that link to it.

I am now enabling the coursecd_pk on course00.

Page 23: More on Primary and Foreign Keys Please see speaker notes for additional information!

Student systemStudent system

SQL> ALTER TABLE stucourse00 2 ENABLE CONSTRAINT coursecd_fk;ALTER TABLE stucourse00*ERROR at line 1:ORA-02298: cannot enable (SCOTT.COURSECD_FK) - parent keys not found

When I try to enable the foreign key associated with coursecd on the stucourse00 table, I am refused because of the MAR55 record I added.

SQL> INSERT INTO course00 2 VALUES('MAR55','MARKETING',3);

1 row created.

SQL> ALTER TABLE stucourse00 2 ENABLE CONSTRAINT coursecd_fk;

Table altered.

I inserted the MAR55 course into course00 and re-executed the enable, it worked because the parent was now found.

SQL> SELECT * FROM course00 WHERE coursecd = 'MAR55';

COURS COURSENAME CREDITS----- ------------------------------ ---------MAR55 MARKETING 3

SQL> SELECT * FROM stucourse00 WHERE coursecd = 'MAR55';

STUD COURS SEMTA GR---- ----- ----- --2222 MAR55 S2000 C+

The parent is on course00 and the child is on stucourse00.

Page 24: More on Primary and Foreign Keys Please see speaker notes for additional information!

Student systemStudent system

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME 2 FROM USER_CONS_COLUMNS 3 WHERE TABLE_NAME 4 IN ('STUCOURSE00', 'COURSE00');

TABLE_NAME CONSTRAINT_NAME COLUMN_NAME------------------------------ ------------------------------ ----------------STUCOURSE00 COURSECD_FK COURSECDCOURSE00 COURSECD_PK COURSECDSTUCOURSE00 STUDENTIDNO_FK STUDENTIDNOSTUCOURSE00 THEKEY_PK STUDENTIDNOSTUCOURSE00 THEKEY_PK COURSECDSTUCOURSE00 THEKEY_PK SEMTAKEN

SQL> ALTER TABLE course00 2 DROP PRIMARY KEY 3 CASCADE;

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME 2 FROM USER_CONS_COLUMNS 3 WHERE TABLE_NAME 4 IN ('COURSE00', 'STUCOURSE00');

TABLE_NAME CONSTRAINT_NAME COLUMN_NAME------------------------------ ------------------------------ --------------STUCOURSE00 STUDENTIDNO_FK STUDENTIDNOSTUCOURSE00 THEKEY_PK STUDENTIDNOSTUCOURSE00 THEKEY_PK COURSECDSTUCOURSE00 THEKEY_PK SEMTAKEN

The primary key for course00 is dropped and the cascade also drops all keys that link to it.

Page 25: More on Primary and Foreign Keys Please see speaker notes for additional information!

SQL> ALTER TABLE course00 2 ADD CONSTRAINT coursecd_pk PRIMARY KEY(coursecd);

Table altered.

SQL> ALTER TABLE stucourse00 2 ADD CONSTRAINT coursecd_fk FOREIGN KEY(coursecd) REFERENCES course00;

Table altered.

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME 2 FROM USER_CONS_COLUMNS 3 WHERE TABLE_NAME IN ('COURSE00', 'STUCOURSE00');

TABLE_NAME CONSTRAINT_NAME COLUMN_NAME------------------------------ ------------------------------ ---------------STUCOURSE00 COURSECD_FK COURSECDCOURSE00 COURSECD_PK COURSECDSTUCOURSE00 STUDENTIDNO_FK STUDENTIDNOSTUCOURSE00 THEKEY_PK STUDENTIDNOSTUCOURSE00 THEKEY_PK COURSECDSTUCOURSE00 THEKEY_PK SEMTAKEN

Student systemStudent systemThe primary key and foreign key that I dropped have now been put back.