Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Chen, Business Database Systems JustLee DataBase Referential Integrity Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga.

Similar presentations


Presentation on theme: "Dr. Chen, Business Database Systems JustLee DataBase Referential Integrity Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga."— Presentation transcript:

1 Dr. Chen, Business Database Systems 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

2 Dr. Chen, Business Database Systems Customer#LastNameFirstNameAddressCityStateZipReferredRegionEmail NUMBER(4)VARCHAR2(10) VARCHAR2(20)VARCHAR2(12)VARCHAR2(2)VARCHAR2(5)NUMBER(4)CHAR(2)VARCHAR2(30) Order#Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) 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”

3 Dr. Chen, Business Database Systems Customers and Orders Entities with Referential Integrity

4 Dr. Chen, Business Database Systems Customer#LastNameFirstNameAddressCityStateZipReferredRegionEmail NUMBER(4)VARCHAR2(10) VARCHAR2(20)VARCHAR2(12)VARCHAR2(2)VARCHAR2(5)NUMBER(4)CHAR(2)VARCHAR2(30) Order#Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) DATE VARCHAR2(18)VARCHAR2(15)VARCHAR2(2)VARCHAR2(5)NUMBER(4,2) CUSTOMERS ORDERS pk fk Q2: Identify the relationship (“cardinality”) between “CUSTOMERS” and “ORDERS” Q1: Identify pk and fk

5 Dr. Chen, Business Database Systems ORDERS pk fk pk Q1: Can we “create” ORDERS#1 (100) if CUSTOMERS#5 (1005) is not created? ? Why?

6 Dr. Chen, Business Database Systems ORDERS fk pk Q2: Can we “delete” CUSTOMERS#5 (1005) if ORDERS#1 (100) is still in the database? Why?

7 Dr. Chen, Business Database Systems ORDERS fk pk 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) Referential Integrity Do we have (or even want) to manually enforce the data (referential) integrity? Yes/No. Why? How?

8 Dr. Chen, Business Database Systems 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?

9 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”

10 Dr. Chen, Business Database Systems JustLee DDL (Original one) See “Partial Solution” on the next slide

11 Dr. Chen, Business Database Systems Partial Solution

12 Dr. Chen, Business Database Systems JustLee DDL (Original one)

13 Dr. Chen, Business Database Systems Customer# LastNameFirstNameAddressCityStateZipReferredRegionEmail NUMBER(4)VARCHAR2(10) VARCHAR2 (20) VARCHAR2(12)VARCHAR 2(2) VARCHAR2 (5) NUMBER( 4) CHAR(2)VARCHAR2( 30) Order# Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) DATE VARCHAR2(18)VARCHAR2(15)VARCHAR2(2)NUMBER(4)NUMBER(4,2) CUSTOMERS pk ORDERS fkpk ORDERS Order# (pk). CUSTOMERS Customer# (pk). ORDERS Order# (pk)., CUSTOMERS Customer# (pk).

14 Dr. Chen, Business Database Systems 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 Customer# LastNameFirstNameAddress…Region Order# Customer#OrderDate…ShipZipShipCost customers orders pk fk pk

15 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# LastNameFirstNameAddress…Region Order# Customer#OrderDate…ShipZipShipCost customers orders pk fk pk

16 Dr. Chen, Business Database Systems customers orders pk fk pk customer#LastName…ReferredRegion 1001MORALESNULLSE …… 1005GIRARDNULLNW 1020FALAHNULLNE Order#customer#… ShipZip ShipCost 10001005981142.00 ……… 10031001323284.00 10121007490026.00 Referential Integrity

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

18 Dr. Chen, Business Database Systems

19 Customer# LastNameFirstNameAddressCityStateZipReferredRegionEmail NUMBER(4)VARCHAR2(10) VARCHAR2 (20) VARCHAR2(12)VARCHAR 2(2) VARCHAR2 (5) NUMBER( 4) CHAR(2)VARCHAR2( 30) Order# Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) 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')) ); CUSTOMERS pk ORDERS fkpk

20 Dr. Chen, Business Database Systems Customer# LastNameFirstNameAddressCityStateZipReferredRegionEmail NUMBER(4)VARCHAR2(10) VARCHAR2 (20) VARCHAR2(12)VARCHAR 2(2) VARCHAR2 (5) NUMBER( 4) CHAR(2)VARCHAR2( 30) Order# Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) 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#)); CUSTOMERS pk ORDERS fk pk

21 Dr. Chen, Business Database Systems Order# Customer#OrderDateShipDateShipStreetShipCityShipStateShipZipShipCost NUMBER(4) DATE VARCHAR2(18)VARCHAR2(15)VARCHAR2(2)NUMBER(4)NUMBER(4,2) ORDERS Order# Item#ISBNQuantityPaidEach NUMBER(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 TitlePubDatePubIDCostRetailDiscountCategory VARCHAR2(10)VARCHAR2(30)DATENUMBER(2)NUMBER(5,2) NUMBER(4,2)VARCHAR2(12) BOOKS pk fkCpk, fkcpk pk


Download ppt "Dr. Chen, Business Database Systems JustLee DataBase Referential Integrity Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga."

Similar presentations


Ads by Google