Sie sind auf Seite 1von 21

JustLee DataBase

Referential Integrity
Jason C. H. Chen, Ph.D.
Professor of MIS
School of Business Administration
Gonzaga University
Spokane, WA 99258
chen@jepson.gonzaga.edu
Dr. Chen, Business Database Systems

Q1: Identify pk and fk

CUSTOMERS
Customer#

LastName

FirstName

Address

City

State

Zip

Referred

Region

Email

NUMBER(4)

VARCHAR2(10)

VARCHAR2(10)

VARCHAR2(20)

VARCHAR2(12)

VARCHAR2(2)

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)

VARCHAR2(5)

NUMBER(4,2)

Q2: Identify the relationship (cardinality) between


CUSTOMERS and ORDERS

Dr. Chen, Business Database Systems

Customers and Orders Entities


with Referential Integrity

Dr. Chen, Business Database Systems

Q1: Identify pk and fk

CUSTOMERS

pk

Customer#

LastName

FirstName

Address

City

State

Zip

Referred

Region

Email

NUMBER(4)

VARCHAR2(10)

VARCHAR2(10)

VARCHAR2(20)

VARCHAR2(12)

VARCHAR2(2)

VARCHAR2(5)

NUMBER(4)

CHAR(2)

VARCHAR2(30)

pk

ORDERS

fk

Order#

Customer#

OrderDate

ShipDate

ShipStreet

ShipCity

ShipState

ShipZip

ShipCost

NUMBER(4)

NUMBER(4)

DATE

DATE

VARCHAR2(18)

VARCHAR2(15)

VARCHAR2(2)

VARCHAR2(5)

NUMBER(4,2)

Q2: Identify the relationship (cardinality) between


CUSTOMERS and ORDERS

Dr. Chen, Business Database Systems

Q1: Can we create ORDERS#1 (100) if CUSTOMERS#5 (1005) is not created? ? Why?

pk

fk

pk

Dr. Chen, Business Database Systems

ORDERS

Q2: Can we delete CUSTOMERS#5 (1005) if ORDERS#1 (100) is still in the database? Why?

pk

fk

pk

Dr. Chen, Business Database Systems

ORDERS

Referential Integrity

pk

pk

fk

ORDERS

Order of Entering Data:


CUSTOMERS ORDERS
The table with pk (e.g., customers)
should be created first before the
table with fk (orders)

Order of Deleting Data:


ORDERS CUSTOMERS
The table with fk (orders) should be
deleted first before the table with pk
(customers)

Do we have (or even want) to manually enforce the data (referential) integrity?
Dr. Chen, Business Database Systems

Yes/No. Why? How?

pk

Customers#5

Can we delete
customers#5 if
orders#1 is still in
the database?
Why?

pk
orders#1

Can we create
orders#1 if
customers#5 is
not created?
Why?

Dr. Chen, Business Database Systems

fk

Assignment on JLDB for


Referential Integrity
1) Identify pk, fk etc.
2) Identify Order of Entering Data
3) Identify Order of Deleting Data

Dr. Chen, Business Database Systems

JustLee DDL (Original one)

Dr. Chen, Business Database Systems

See Partial Solution on the next slide

Partial Solution

Dr. Chen, Business Database Systems

JustLee DDL (Original one)

Dr. Chen, Business Database Systems

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

Email

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)

CUSTOMERS
Customer# (pk)
.
.

CUSTOMERS
Customer# (pk)
.
.

Dr. Chen, Business Database Systems

ORDERS
Order# (pk)
.
,

ORDERS
Order# (pk)
.
.

Using the FOREIGN KEY Constraint


Referential Integrity
Requires a value to exist in the referenced column
of another table
NULL values are allowed
Enforces referential integrity
Maps to the PRIMARY KEY in parent table
customers
pk

pk

Customer#

orders
Order#

Dr. Chen, Business Database Systems

LastName

FirstName

Address

Region

fk
Customer# OrderDate

ShipZip

ShipCost

Using the FOREIGN KEY Constraint


Referential Integrity (cont.)
You cannot delete a value in a parent table (pk)
referenced by a row in a child table (fk)

customers
pk

pk

Customer#

orders
Order#

Dr. Chen, Business Database Systems

LastName

FirstName

Address

Region

fk
Customer# OrderDate

ShipZip

ShipCost

Referential Integrity
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, Business Database Systems

Referential Integrity
The table with pk (e.g., customers) should
be created first before the table with fk
(orders)
The table with fk (orders) should be deleted
first before the table with pk (customers)
How to maintain the Referential Integrity
in an efficient way?
Answer: include constraints in the DDL.
Dr. Chen, Business Database Systems

STUDENT
pk
s_id
100

105
FACULTY
pk
f_id
1

5
LOCATION
pk
loc_id
45

57

TABLE-1
(FACULTY)

s_last
Miller

s_pin
8891

Connoly

9188

f_last
Cox
Brown

fk
loc_id
53

57

bldg_code
CR

room
101

LIB

222

fk
f_id
1

f_image
cox.jpg

brown.jpg

capacity
150

TABLE-
(STUDENT)

RULES:
1. You cant add a record to TABLE- (or the table with fk,
e.g., STUDENT) 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., FACULTY) if there is a
record in TABLE- (or the table with fk).
Order of entering data into the database: LOCATION FACULTY STUDENT
Order of deleting data from the database:STUDENT FACULTY LOCATION
Dr. Chen, Business Database Systems

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

Email

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)

CREATE TABLE Customers


(Customer# NUMBER(4),
LastName VARCHAR2(10) NOT NULL,
FirstName VARCHAR2(10) NOT NULL,
Address VARCHAR2(20),
City VARCHAR2(12),
State VARCHAR2(2),
Zip VARCHAR2(5),
Referred NUMBER(4),
Region CHAR(2),
Email VARCHAR2(30),
CONSTRAINT customers_customer#_pk PRIMARY KEY(customer#),
CONSTRAINT customers_region_ck
CHECK (region IN ('N', 'NW', 'NE', 'S', 'SE', 'SW', 'W', 'E')) );
Dr. Chen, Business Database Systems

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

Email

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)

CREATE TABLE Orders


(Order# NUMBER(4),
Customer# NUMBER(4),
OrderDate DATE NOT NULL,
ShipDate DATE,
ShipStreet VARCHAR2(18),
ShipCity VARCHAR2(15),
ShipState VARCHAR2(2),
ShipZip VARCHAR2(5),
ShipCost NUMBER(4,2),
CONSTRAINT orders_order#_pk PRIMARY KEY(order#),
CONSTRAINT orders_customer#_fk FOREIGN KEY (customer#)
REFERENCES customers(customer#));
Dr. Chen, Business Database Systems

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)

Cpk, fk
Order#
NUMBER(4)

cpk
Item#
NUMBER(2)

fk

ORDERITEMS

ISBN
VARCHAR2(10)

Quantity
NUMBER(3)

PaidEach
NUMBER(5,2)

CREATE TABLE ORDERITEMS


( Order# NUMBER(4),
Item# NUMBER(2),
ISBN VARCHAR2(10),
Quantity NUMBER(3) NOT NULL,
PaidEach NUMBER(5,2) NOT NULL,
CONSTRAINT orderitems_order#_item#_pk PRIMARY KEY (order#,
item#),
CONSTRAINT orderitems_order#_fk FOREIGN KEY (order#)
REFERENCES orders (order#) ,
CONSTRAINT orderitems_isbn_fk FOREIGN KEY (isbn)
REFERENCES books (isbn) ,
CONSTRAINT oderitems_quantity_ck CHECK (quantity > 0) );
Dr. Chen, Business Database Systems