Upload
lora-gallagher
View
233
Download
0
Embed Size (px)
Citation preview
Dr. Chen, Business Database Systems
JustLee DataBaseReferential Integrity
Jason C. H. Chen, Ph.D.Professor of MIS
School of Business AdministrationGonzaga UniversitySpokane, WA 99258
Dr. Chen, Business Database Systems
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)
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)
CUSTOMERS
ORDERS
Q1: Identify pk and fk
Q2: Identify the relationship (“cardinality”) between “CUSTOMERS” and “ORDERS”
Dr. Chen, Business Database Systems
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)
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)
CUSTOMERS
ORDERS
pk
pk fk
Q2: Identify the relationship (“cardinality”) between “CUSTOMERS” and “ORDERS”
Q1: Identify pk and fk
Dr. Chen, Business Database Systems
ORDERS
pk
fkpk
Q1: Can we “create” ORDERS#1 (100) if CUSTOMERS#5 (1005) is not created? ? Why?
Dr. Chen, Business Database Systems
ORDERSfk
pk
pk
Q2: Can we “delete” CUSTOMERS#5 (1005) if ORDERS#1 (100) is still in the database? Why?
Dr. Chen, Business Database Systems
ORDERSfk
pk
pk
Order of Entering Data: CUSTOMERS ORDERSThe table with pk (e.g., customers) should be created first before the table with fk (orders)
Order of Deleting Data: ORDERS CUSTOMERSThe table with fk (orders) should be deleted first before the table with pk (customers)
Referential Integrity
Do we have (or even want) to manually enforce the data (referential) integrity?Yes/No. Why? How?
Dr. Chen, Business Database Systems
pk
pk fk
Customers#5
orders#1
Can we “create” orders#1 if customers#5 is not created? Why?
Can we “delete” customers#5 if orders#1 is still in the database? Why?
Dr. Chen, Business Database Systems
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) See “Partial Solution” on the next slide
Dr. Chen, Business Database Systems
Customer# LastName FirstName Address City State Zip Referred Region EmailNUMBER(4)
VARCHAR2(10) VARCHAR2(10)
VARCHAR2(20)
VARCHAR2(12) VARCHAR2(2)
VARCHAR2(5)
NUMBER(4)
CHAR(2) VARCHAR2(30)
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)
CUSTOMERSpk
ORDERSfkpk
ORDERSOrder# (pk)
.
.
CUSTOMERSCustomer# (pk)
.
.
ORDERSOrder# (pk)
.,
CUSTOMERSCustomer# (pk)
.
.
Dr. Chen, Business Database Systems
Using the FOREIGN KEY ConstraintReferential 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
Customer# LastName FirstName Address … Region
Order# Customer# OrderDate … ShipZip ShipCost
customers
orders
pk
fkpk
Dr. Chen, Business Database Systems
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)
Customer# LastName FirstName Address … Region
Order# Customer# OrderDate … ShipZip ShipCost
customers
orders
pk
fkpk
Dr. Chen, Business Database Systems
customers
RULES: 1. You can’t 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 can’t 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
orders
pk
fkpk
customer# LastName … Referred Region1001 MORALES NULL SE… …
1005 GIRARD NULL NW1020 FALAH NULL NE
Order# customer# … ShipZip ShipCost1000 1005 98114 2.00… … …
1003 1001 32328 4.001012 1007 49002 6.00
Referential Integrity
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 fk
s_id s_last … s_pin f_id 100 Miller 8891 1 … …
105 Connoly 9188 3 FACULTY pk fk
f_id f_last … loc_id … f_image 1 Cox 53 cox.jpg … … … 5 Brown 57 brown.jpg
LOCATION pk
loc_id bldg_code room capacity 45 CR 101 150 … … 57 LIB 222 1
TABLE-1 TABLE- (FACULTY) (STUDENT)
RULES: 1. You can’t 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 can’t 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
Customer# LastName FirstName Address City State Zip Referred Region EmailNUMBER(4)
VARCHAR2(10) VARCHAR2(10)
VARCHAR2(20)
VARCHAR2(12) VARCHAR2(2)
VARCHAR2(5)
NUMBER(4)
CHAR(2) VARCHAR2(30)
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(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')) );
CUSTOMERSpk
ORDERSfkpk
Dr. Chen, Business Database Systems
Customer# LastName FirstName Address City State Zip Referred Region EmailNUMBER(4)
VARCHAR2(10) VARCHAR2(10)
VARCHAR2(20)
VARCHAR2(12) VARCHAR2(2)
VARCHAR2(5)
NUMBER(4)
CHAR(2) VARCHAR2(30)
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(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#));
CUSTOMERSpk
ORDERSfkpk
Dr. Chen, Business Database Systems
Order# Customer# OrderDate ShipDate ShipStreet ShipCity ShipState ShipZip ShipCostNUMBER(4) NUMBER(4) DATE DATE VARCHAR2(18) VARCHAR2(15) VARCHAR2(2) NUMBER(4) NUMBER(4,2)
ORDERS
Order# Item# ISBN Quantity PaidEachNUMBER(4) NUMBER(2) VARCHAR2(10) NUMBER(3) NUMBER(5,2)
ORDERITEMS
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) );
ISBN Title PubDate PubID Cost Retail Discount CategoryVARCHAR2(10) VARCHAR2(30) DATE NUMBER(2) NUMBER(5,2) NUMBER(5,2) NUMBER(4,2) VARCHAR2(12)
BOOKSpk
fkCpk, fk cpk
pk