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

Slides:



Advertisements
Similar presentations
Assignment Design Methodology A structured approach that uses procedures, techniques, tools, and documentation aids to support and facilitate the.
Advertisements

Dr. Chen, Oracle Database System (Oracle) 1 Chapter 12 Subqueries and Merge Statements (up to p.451) Jason C. H. Chen, Ph.D. Professor of MIS School of.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
The Database Approach u Emphasizes the integration of data across the organization.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Data Integrity Constraints
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
Database Architecture The Relational Database Model.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Oracle Data Definition Language (DDL)
School of Business Administration
XP Class Objectives – 9/10 and 9/12 Learn how to design a small database Understand the goals of a database Understand the terminology of database design.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 4 Constraints Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University Spokane,
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Chapter 5 Data Manipulation and Transaction Control
Relational DB Components
Copyright  Oracle Corporation, All rights reserved. 4 CMIS Powell Oracle Designer: Creating the Database Design CMIS Powell.
Mapping from Data Model (ERD) to Relational Model
Concepts of Database Management Seventh Edition Chapter 4 Keys and Relationship.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
MIS 301 Information Systems in Organizations Dave Salisbury ( )
CIS 3051 PHP – Chapter 27 Building A Shopping Cart.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Dr. Chen, Oracle Database System (Oracle) 1 HW Discussion Chapter 5 (#9) –Why need two “deletes”? SQL> --9. SQL> SQL> DELETE FROM orderitems 2 WHERE order#
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 6 Additional Database Objects (up to p.195 and all in the pptx file) Jason C. H. Chen, Ph.D. Professor.
Data Integrity An empty database is a correct database.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.
Relational Theory and Design
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Chapter 7 SQL: Data Definition Pearson Education © 2009.
Normalization Is the gradual and sequential process of efficiently organizing data in a database that follows the rules listed in the previous slide –
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
CS263 Lecture 5: Logical Database Design Can express the structure of a relation by a Tuple, a shorthand notation Name of the relation is followed (in.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Presenter: Rueen-Fang Wang, Ph.D., P.E. Fix Implementation Deficiencies of Database Structure Presenter: Rueen-Fang Wang, Ph.D., P.E. 1.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Chapter 3: Relational Databases
Howard Paul. Sequential Access Index Files and Data File Random Access.
Concepts of Database Management Seventh Edition Chapter 4 Keys and Relationship.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Getting started with Accurately Storing Data
CompSci 280 S Introduction to Software Development
CSIS 115 Database Design and Applications for Business
Database Constraints Ashima Wadhwa.
CS 3630 Database Design and Implementation
Tables and Their Characteristics
Order Database – ER Diagram
Lecture # 13 (After 1st Exam)
Constraints AND Examples
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Entity relationship diagrams
Constraints.
Mapping an ERD to a Relational Database
Constraints AND Examples
Presentation transcript:

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

Dr. Chen, Business Database Systems Customer#LastNameFirstNameAddressCityStateZipReferredRegion 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”

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

Dr. Chen, Business Database Systems Customer#LastNameFirstNameAddressCityStateZipReferredRegion 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

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

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?

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?

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?

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 Partial Solution

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

Dr. Chen, Business Database Systems Customer# LastNameFirstNameAddressCityStateZipReferredRegion 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).

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

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

Dr. Chen, Business Database Systems customers orders pk fk pk customer#LastName…ReferredRegion 1001MORALESNULLSE …… 1005GIRARDNULLNW 1020FALAHNULLNE Order#customer#… ShipZip ShipCost ……… 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

Customer# LastNameFirstNameAddressCityStateZipReferredRegion 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), 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

Dr. Chen, Business Database Systems Customer# LastNameFirstNameAddressCityStateZipReferredRegion 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

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