Professional Documents
Culture Documents
required
SQL> --7.
SQL> CREATE TABLE book_pricing (id, cost, retail, category)
2 AS (SELECT isbn, cost, retail, category
3 FROM books);
Table created.
SQL> --7b.(version 2)
SQL>
SQL> DROP TABLE book_pricing CASCADE CONSTRAINTS;
Table dropped.
SQL> required
SQL> CREATE TABLE book_pricing
2 AS (SELECT isbn as id, cost, retail, category
3 FROM books);
Table created.
Referential
Integrity
(Constraints)
orders fk
pk
Order# customer# ShipZip ShipCost
orders#1 1000 1005 98114 2.00
1003 1001 32328 4.00
1012 1007 49002 6.00
orders fk
pk
Order# customer# ShipZip ShipCost
1000 1005 98114 2.00
1003 1001 32328 4.00
1012 1007 49002 6.00
RULES:
1. You cant add/insert a record to TABLE- (or the table with fk, e.g., orders)
unless there is a corresponding record in TABLE-1 (or the table with pk).
2. You cant delete a record in TABLE-1 (or the table with pk, e.g.,
customers) if there is a record in TABLE- (or the table with fk).
Order of entering data into the database: customers orders
Order of deleting data from the database: orders customers
Dr. Chen, Oracle Database System (Oracle) 12
Review from Last Class
L
Dr. Chen, Oracle Database System (Oracle) 13
What is a Constraint?
A rule used to enforce business rules, practices,
and policies
A rule used to ensure accuracy and integrity of
data
A mechanism used to protect
the relationship between data within an Oracle
table, or
the correspondence between data in two different
tables.
For example, the state entered must be one of the 50
states in the U.S.
Dr. Chen, Oracle Database System (Oracle) 14
Types of Constraints
<tablename>_<fieldname>_<constraint id>
Where <constraint id> is:
pk PRIMARY KEY
fk REFERENCES <tablename> (pk)
ck CHECK <condition to be checked>
(note that cc stands for CHECK CONDITION)
nn NOT NULL
uk UNIQUE
e.g.,
s_id NUMBER (6) CONSTRAINT student_s_id_pk PRIMARY KEY;
Dr. Chen, Oracle Database System (Oracle) 16
Integrity Constraints
When
During table creation
After table creation, by modifying the existing
table
How
Column level approach
Table level approach
pk fk ORDERS
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCost
NUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)
pk fk ORDERS
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCost
NUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)
pk BOOKS
ISBN Title PubDate PubID Cost Retail Discount Category
VARCHAR2(10) VARCHAR2(30) DATE NUMBER(2) NUMBER(5,2) NUMBER(5,2) NUMBER(4,2) VARCHAR2(12)
cpk ORDERITEMS
fk fk
Order# Item# ISBN Quantity PaidEach
NUMBER(4) NUMBER(2) VARCHAR2(10) NUMBER(3) NUMBER(5,2)
Figure 4-3 Syntax of the ALTER TABLE command to add a PRIMARY KEY constraint
EQUIP
DEPT ETYPES
EquipID
DeptID Edesc EtypeID
Dname Purchdate Etypename
Fax Rating
DeptID
EtypeID
pk book_stores fk
Upload the SQL and spooled files (*.sql and *.txt) to the Bb
(under Assignments & Projects) by the deadline