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

Slides:



Advertisements
Similar presentations
Relational database - student system As always please use speaker notes!
Advertisements

Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Lists, Views, Describing tables Kris Pepper. Where are we Finishing up integrity, sets and lists, and now adding in views and describe. Finishing homework.
Sections 10 – Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL Use of Functions Character functions Please use speaker notes for additional information!
Oracle Data Definition Language (DDL)
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
Chapter Four Objectives Introduction to SQL Types of SQL statements Concepts of DDL & DML Creating Tables Restrictions on Tables Data Definition Language(DDL)
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Exceptions in PL/SQL Please use speaker notes for additional information!
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
Introduction to Oracle - SQL Additional information is available in speaker notes!
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database in Access Student System As always please use speaker notes!
Relational Database Management System(RDBMS) Structured Query Language(SQL)
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
More about maintaining a table Use speaker notes for additional information!
Unbound data fields, find, filter etc. using Data Environment Please use speaker notes for additional information!
Database Programming Sections 9 & 10 – DDL Data Definition Language,
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamental of Database Systems
Including Constraints
SQL: Schema Definition and Constraints Chapter 6 week 6
MySQL - Creating donorof database offline
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Managing Objects with Data Dictionary Views
“Manipulating Data” Lecture 6.
Introduction to Views and Reports
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
“Manipulating Data” Lecture 6.
Oracle Data Definition Language (DDL)
Structured Query Language – The Basics
SQL NOT NULL Constraint
Presentation transcript:

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 Stephen Daniels 345 Midland Ave Yonkers NY BU 09-SEP Jennifer Ames 12 Ave F Fall River MA CI 02-SEP Carl Hersey 12 Ave F Fall River MA BU 02-SEP Mary Stanton 156 West St Taunton MA CI 05-SEP John Richards 76 Main St Fall River MA CI 06-SEP-00 Student 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

SQL> SELECT * FROM stucourse00; STUD COURS SEMTA GR CIS11 F2000 A MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A CIS44 F2000 B 3333 CIS50 F2000 B CIS56 S2000 A CIS50 F2000 I 1111 CIS50 S2000 A+ Student 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.

SQL> SELECT * FROM major00; MA MAJORNAME CHAIR BU Business Administration Adams CI Computer Information Systems Grocer SQL> DESC major00; Name Null? Type MAJORCODE VARCHAR2(2) MAJORNAME VARCHAR2(30) CHAIR VARCHAR2(20) Student system This is the table that holds information about the major itself. The primary key for this table will be the majorcode.

SQL> SELECT * FROM course00; COURS COURSENAME CREDITS CIS11 Intro to Computer Info Systems 3 CIS44 Internet User/Developer 3 CIS50 Oracle and SQL 3 CIS56 Visual Basic 3 MAN11 Intro to Management 3 MAR11 Marketing Principles 3 6 rows selected. SQL> DESC course00 Name Null? Type COURSECD VARCHAR2(5) COURSENAME VARCHAR2(30) CREDITS NUMBER(1) Student system This is the table that holds information about the courses themselves. The primary key for this table will be the coursecd.

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

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 CIS11 F2000 A MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A CIS44 F2000 B 3333 CIS50 F2000 B CIS56 S2000 A CIS50 F2000 I 1111 CIS50 S2000 A+ 12 rows selected. Student 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.

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 CIS11 F2000 A MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A CIS50 F2000 B CIS56 S2000 A CIS50 F2000 I 1111 CIS50 S2000 A 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.

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

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

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

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

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

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 system

SQL> SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS 2 WHERE TABLE_NAME IN ('STUDENT00', 'STUCOURSE00', 'COURSE00', 'MAJOR00'); TABLE_NAME CONSTRAINT_NAME COURSE00 COURSECD_PK MAJOR00 MAJORCODE_PK STUCOURSE00 THEKEY_PK STUCOURSE00 COURSECD_FK STUCOURSE00 STUDENTIDNO_FK STUDENT00 STUDENTIDNO_PK STUDENT00 MAJORCODE_FK 7 rows selected. Student 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.

SQL> SELECT studentidno, name 2 FROM student00; STUD NAME Stephen Daniels 1212 Jennifer Ames 2222 Carl Hersey 2345 Mary Stanton 3333 John Richards Student 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_PK STUCOURSE00 COURSECD_FK STUCOURSE00 STUDENTIDNO_FK STUDENT00 STUDENTIDNO_PK STUDENT00 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.

Student system SQL> SELECT * 2 FROM course00; COURS COURSENAME CREDITS CIS11 Intro to Computer Info Systems 3 CIS44 Internet User/Developer 3 CIS50 Oracle and SQL 3 CIS56 Visual Basic 3 MAN11 Intro to Management 3 MAR11 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_PK STUCOURSE00 THEKEY_PK STUCOURSE00 COURSECD_FK STUCOURSE00 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.

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 3 CIS44 Internet User/Developer 3 CIS50 Oracle and SQL 3 CIS56 Visual Basic 3 MAN11 Intro to Management 3 MAR11 Marketing Principles 3 CIS40 Lotus Notes 3 7 rows selected. Student system SQL> INSERT INTO stucourse00 2 VALUES ('3333','CIS40','S2000','C'); 1 row created. SQL> SELECT * FROM stucourse00; STUD COURS SEMTA GR CIS11 F2000 A MAR11 F2000 A 1111 CIS44 S2000 A 1212 CIS44 S2000 A 2222 CIS44 S2000 A 2222 MAN11 F2000 A CIS40 S2000 C 3333 CIS50 F2000 B CIS56 S2000 A CIS50 F2000 I 1111 CIS50 S2000 A 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.

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 COURSECD COURSECD_PK COURSECD MAJORCODE_FK MAJORCODE MAJORCODE_PK MAJORCODE STUDENTIDNO_FK STUDENTIDNO STUDENTIDNO_PK STUDENTIDNO THEKEY_PK STUDENTIDNO THEKEY_PK COURSECD THEKEY_PK SEMTAKEN 9 rows selected. Student 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.

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 COURSECD COURSE00 COURSECD_PK COURSECD STUDENT00 MAJORCODE_FK MAJORCODE MAJOR00 MAJORCODE_PK MAJORCODE STUCOURSE00 STUDENTIDNO_FK STUDENTIDNO STUDENT00 STUDENTIDNO_PK STUDENTIDNO STUCOURSE00 THEKEY_PK STUDENTIDNO STUCOURSE00 THEKEY_PK COURSECD STUCOURSE00 THEKEY_PK SEMTAKEN 9 rows selected. Student system This includes the table name in case there is possibility for confusion.

SQL> ALTER TABLE course00 2 DISABLE CONSTRAINT coursecd_pk 3 CASCADE; Student system SQL> INSERT INTO stucourse00 2 VALUES ('2222','MAR55','S2000','C+'); SQL> SELECT * FROM course00; COURS COURSENAME CREDITS CIS11 Intro to Computer Info Systems 3 CIS44 Internet User/Developer 3 CIS50 Oracle and SQL 3 CIS56 Visual Basic 3 MAN11 Intro to Management 3 MAR11 Marketing Principles 3 CIS40 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.

Student 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 MAR55 S2000 C+ The parent is on course00 and the child is on stucourse00.

Student 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 COURSECD COURSE00 COURSECD_PK COURSECD STUCOURSE00 STUDENTIDNO_FK STUDENTIDNO STUCOURSE00 THEKEY_PK STUDENTIDNO STUCOURSE00 THEKEY_PK COURSECD STUCOURSE00 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 STUDENTIDNO STUCOURSE00 THEKEY_PK STUDENTIDNO STUCOURSE00 THEKEY_PK COURSECD STUCOURSE00 THEKEY_PK SEMTAKEN The primary key for course00 is dropped and the cascade also drops all keys that link to it.

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 COURSECD COURSE00 COURSECD_PK COURSECD STUCOURSE00 STUDENTIDNO_FK STUDENTIDNO STUCOURSE00 THEKEY_PK STUDENTIDNO STUCOURSE00 THEKEY_PK COURSECD STUCOURSE00 THEKEY_PK SEMTAKEN Student system The primary key and foreign key that I dropped have now been put back.