Académique Documents
Professionnel Documents
Culture Documents
Constraints
Jason C. H. Chen, Ph.D.
Professor of MIS
School of Business
Gonzaga University
Spokane, WA 99258 USA
chen@jepson.gonzaga.edu
Dr. Chen, Oracle Database System (Oracle)
Data
Integrity
How to
achieve it?
Referential
Integrity
Objectives
Explain the purpose of constraints in a table
Distinguish among PRIMARY KEY, FOREIGN
KEY, UNIQUE, CHECK, and NOT NULL
constraints and the appropriate use for each
constraint
Understand how constraints can be created when
creating a table or modifying an existing table
Distinguish between creating constraints at the
column level and table level
Objectives (continued)
Create PRIMARY KEY constraints for a single
column and a composite primary key (cpk)
Create a FOREIGN KEY constraint
Create a UNIQUE constraint
Create a CHECK constraint
Create a NOT NULL constraint using the ALTER
TABLEMODIFY command
Include constraints during table creation
Use DISABLE and ENABLE commands
Use the DROP command
Dr. Chen, Oracle Database System (Oracle)
Referential Integrity
Can we delete customers#5 if orders#1 is still in the
database? Why?
customers
pk
Customers#5
pk
orders#1
customer#
1001
1005
1020
orders
Order#
1000
1003
1012
LastName
MORALES
GIRARD
FALAH
Referred
NULL
NULL
NULL
Region
SE
NW
NE
fk
customer#
1005
1001
1007
ShipZip
ShipCost
98114
32328
49002
2.00
4.00
6.00
pk
Customers#5
Can we delete
customers#5 if
orders#1 is still in
the database?
Why?
Can we insert a
different customer
with Duplicate pk?
pk
fk
orders#1
Can we create
orders#1 if
customers#5 is
not created?
Why?
customers
pk
pk
customer#
1001
1005
1020
orders
Order#
1000
1003
1012
LastName
MORALES
GIRARD
FALAH
Referred
NULL
NULL
NULL
Region
SE
NW
NE
fk
customer#
1005
1001
1007
ShipZip
ShipCost
98114
32328
49002
2.00
4.00
6.00
RULES:
1. You cant add 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)
L
Dr. Chen, Oracle Database System (Oracle)
10
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)
11
Types of Constraints
Integrity constraints: define primary and foreign
keys
Value constraints: define specific data values or
data ranges that must be inserted into columns and
whether values must be unique or not NULL
Table constraint: restricts the data value with
respect to all other values in the table
Field (column) constraint: limits the value that
can be placed in a specific field, irrespective of
values that exist in other table records
12
13
Integrity Constraints
14
Creating a Table
);
Dr. Chen, Oracle Database System (Oracle)
15
Creating Constraints
When
During table creation
After table creation, by modifying the existing
table
How
Column level approach
Table level approach
16
Figure 4-1
Figure 4-2
17
pk
CUSTOMERS
Customer# LastName
FirstName
Address
City
NUMBER(4) VARCHAR2(10)
VARCHAR2
(10)
VARCHAR2
(20)
VARCHAR2(12) VARCHAR
2(2)
pk
fk
State
Zip
Referred
Region
VARCHAR2
(5)
NUMBER(
4)
CHAR(2)
VARCHAR2(
30)
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)
18
pk
CUSTOMERS
Customer# LastName
FirstName
Address
City
NUMBER(4) VARCHAR2(10)
VARCHAR2
(10)
VARCHAR2
(20)
VARCHAR2(12) VARCHAR
2(2)
pk
fk
State
Zip
Referred
Region
VARCHAR2
(5)
NUMBER(
4)
CHAR(2)
VARCHAR2(
30)
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)
19
pk
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)
fk
Order#
NUMBER(4)
cpk
Item#
NUMBER(2)
fk
ORDERITEMS
ISBN
VARCHAR2(10)
Quantity
NUMBER(3)
PaidEach
NUMBER(5,2)
20
Your Job
You need to study and understand all
CREATE TABLE SQL commands in
JLDB_Build_4.sql
21
Enforcement of Constraints
All constraints are enforced at the table
level
If a data value violates a constraint, the
entire row is rejected
22
Constraint Types
Table 4-1
Constraint types
23
Your Turn
24
25
at the Table-Level
Practice:
Type in one of the command.
26
27
Figure 4-3 Syntax of the ALTER TABLE command to add a PRIMARY KEY constraint
28
Figure 4-5
29
Figure 4-7
Dr. Chen, Oracle Database System (Oracle)
Drop Contraint
ALTER TABLE orderitems
DROP CONSTRAINT
orderitems_order#item#_pk PRIMARY
KEY (order#, item#);
31
Optional
(variable name)
32
Customer#
LastName
orders
Order#
FirstName
Address
Region
fk
Customer# OrderDate
ShipZip
ShipCost
33
customers
pk
Customer#
LastName
orders
Order#
FirstName
Address
Region
fk
Customer# OrderDate
ShipZip
ShipCost
34
customers
pk Customer#
orders
Order#
LastName
FirstName
Address
Region
fk
Customer# OrderDate
ShipZip
ShipCost
RULES:
1. You cant add 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)
35
36
37
38
39
40
41
Practice
Lets try to create additional tables
JustLee Books would like to create some new
tables to store office equipment inventory data.
EQUIP
DEPT
DeptID
Dname
Fax
EquipID
Edesc
Purchdate
Rating
DeptID
EtypeID
ETYPES
EtypeID
Etypename
42
pk
DeptID
Dname
Fax
fk
EquipID
Edesc
Purchdate
Rating
DeptID
EtypeID fk
pk ETYPES
EtypeID
Etypename
unique
NOT NULL
43
44
45
46
47
48
Disabling Constraints
-- chapter 4, Figure 4-36; p. 123
ALTER TABLE equip
DISABLE CONSTRAINT equip_rating_ck;
ALTER TABLE equip
ENABLE CONSTRAINT equip_rating_ck;
49
Dropping Constraints
-- chapter 4, Figure 4-38; p. 124
ALTER TABLE equip
DROP CONSTRAINT equip_rating_ck;
-- chapter 4, Figure 4-39; p. 125
ALTER TABLE customers
DROP PRIMARY KEY;
-- chapter 4, Figure 4-40; p. 125
ALTER TABLE customers
DROP PRIMARY KEY CASCADE;
50
HW!!
Practice all the examples in the text.
A Script file is available on the Bb (file name:
Ch4Queries.sql)
After completing all examples, do the HW (hint: see
the tables below for the final schema and the sample
output on the Bb under Assignments
store_reps
pk
rep_id
NUMBER(5)
pk
last
VARCHAR2(15)
first
VARCHAR2(10)
cpk, fk
name
VARCHAR2(30)
rep_contracts
store_id
NUMBER(8)
Dr. Chen, Oracle Database System (Oracle)
name
VARCHAR2(5)
base_salary
NUMBER(7,2)
fk
book_stores
store_id
NUMBER(8)
comm
CHAR(1)
contact
VARCHAR2(30)
cpk
quarter
CHAR(3)
rep_id
NUMBER(5)
cpk, fk
rep_id
NUMBER(5)
51
52
53
End of chapter 4
54