Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 Copyright © 2006, Oracle. All rights reserved. Relational Data Model Part II.

Similar presentations


Presentation on theme: "3 Copyright © 2006, Oracle. All rights reserved. Relational Data Model Part II."— Presentation transcript:

1 3 Copyright © 2006, Oracle. All rights reserved. Relational Data Model Part II

2 3.2 - 2 How to Include Constraints (Integrity Rules) When Creating Tables The following constraint types are valid in SQL: – NOT NULL – PRIMARY KEY – UNIQUE (Alternate Key) – FOREIGN KEY – CHECK Constraint name facilitates the identification of a constraint if a violation occurs when a data row is being inserted, updated or deleted.

3 3.2 - 3 NOT NULL Constraint Ensures that null values are not permitted for a column. NOT NULL constraint (No row can contain a null value for this column.) (business rule) Absence of NOT NULL constraint (Any row can contain a null value for this column.) NOT NULL Constraint (business rule) …

4 3.2 - 4 PRIMARY KEY Constraint Departments PRIMARY KEY INSERT (a new department) Not allowed (null value) Not allowed (not unique, 50 already exists) … case 1 : case 2 :

5 3.2 - 5 Defining Constraints (Examples for PRIMARY KEY and NULL Constraints) Column-level constraint: Table-level constraint: CREATE TABLE Employees ( emp_id CHAR(6) CONSTRAINT employee_pk PRIMARY KEY, f_name VARCHAR(20), job_id VARCHAR(10) NOT NULL,...); CREATE TABLE Employees ( emp_id CHAR(6), f_name VARCHAR(20), job_id VARCHAR(10) NOT NULL,..., CONSTRAINT employee_pk PRIMARY KEY (emp_id),... );

6 3.2 - 6 Defining Constraints (An Example for Composite Primary Key) Table-level constraint: CREATE TABLE Enrollment ( OfferNo INTEGER, StdSSN CHAR(11), EnGrade DECIMAL(3,2), CONSTRAINT PKEnrollment PRIMARY KEY ( OfferNo, StdSSN ),... );

7 3.2 - 7 UNIQUE Constraint Employees UNIQUE constraint INSERT INTO Not allowed: already exists Allowed … case 1 : case 2 : PRIMARY KEY

8 3.2 - 8 UNIQUE Constraint CREATE TABLE Employees ( emp_id CHAR(6), l_name VARCHAR(25) NOT NULL, email VARCHAR(25), CONSTRAINT emp_email_uk UNIQUE(email) ); Table-level constraint:

9 3.2 - 9 DEPARTMENTS EMPLOYEES FOREIGN KEY INSERT (a new employee) PRIMARY KEY Not allowed (9 does not exist) : R1 Allowed case 1: case 2: FOREIGN KEY Constraint 90

10 3.2 - 10 FOREIGN KEY Constraint CREATE TABLE Employees ( emp_id CHAR(6), l_name VARCHAR(25) NOT NULL, email VARCHAR(25), hire_date DATE NOT NULL, dept_id CHAR(4),..., CONSTRAINT emp_pk PRIMARY KEY (emp_id), CONSTRAINT emp_dept_id_fk FOREIGN KEY (dept_id) REFERENCES Departments(dept_id), CONSTRAINT emp_email_uk UNIQUE(email)); CREATE TABLE Departments ( dept_id CHAR(4), dept_name VARCHAR(30) NOT NULL, loc_id CHAR(4), CONSTRAINT dept_pk PRIMARY KEY (dept_id) ); (dept_id) is optional in this case. Note: Create table Departments before table Employees

11 3.2 - 11 CREATE TABLE Employees ( emp_id CHAR(6), l_name VARCHAR(25) NOT NULL, salary INTEGER CONSTRAINT employees_salary_min_ck CHECK (salary > 0),... ); CHECK Constraint Defines a condition for a particular column, that each row must satisfy.

12 3.2 - 12 CREATE TABLE : Example CREATE TABLE Employees ( emp_id CHAR(6), f_name VARCHAR(20) NOT NULL, l_name VARCHAR(25) NOT NULL, email VARCHAR(25) NOT NULL, hire_date DATE NOT NULL, salary INTEGER CONSTRAINT emp_salary_ck CHECK (salary > 0), commission_pct DECIMAL(2,2), dept_id INTEGER, CONSTRAINT emp_pk PRIMARY KEY (emp_id), CONSTRAINT emp_email_uk UNIQUE (email), CONSTRAINT emp_dept_id_fk FOREIGN KEY (dept_id) REFERENCES Departments(dept_id) );

13 3.2 - 13 University Database (Representation of Referential Integrity)

14 3.2 - 14 Creating University Database (CREATE TABLE Student & Course) CREATE TABLE Student ( StdSSN CHAR(11), StdFirstNameVARCHAR(50), StdLastNameVARCHAR(50), StdCityVARCHAR(50), StdStateCHAR(2), StdZipCHAR(10), StdClassCHAR(6), StdGPADECIMAL(3,2), CONSTRAINT Student_pk PIMARY KEY (StdSSN) ); CREATE TABLE Course ( CourseNo CHAR(6), CrsDesc VARCHAR(250), CrsUnitsCHAR(2), CONSTRAINT Course_pk PRIMARY KEY (CourseNo) );

15 3.2 - 15 Creating University Database (CREATE TABLE Faculty) CREATE TABLE Faculty ( FacSSN CHAR(11), FacFirstNameVARCHAR(50) NOT NULL, FacLastNameVARCHAR(50) NOT NULL, FacCityVARCHAR(50) NOT NULL, FacStateCHAR(2) NOT NULL, FacZipCodeCHAR(10) NOT NULL, FacHireDateDATE, CONSTRAINT Faculty_pk PRIMARY KEY (FacSSN) );

16 3.2 - 16 Creating University Database (CREATE TABLE Offering) CREATE TABLE Offering ( OfferNo CHAR(6), CourseNoCHAR(6) NOT NULL, OffTermCHAR(6) NOT NULL, OffYearINTEGER NOT NULL, OffLocation VARCHAR(50), OffDaysCHAR(6), OffTimeDATE, FacSSNCHAR(11), CONSTRAINT Offering_pk PRIMARY KEY (OfferNo), CONSTRAINT Offering_CourseNo_fk FOREIGN KEY (CourseNo) REFERENCES Course (CourseNo), CONSTRAINT Offering_FacSSN_fk FOREIGN KEY (FacSSN) REFERENCES Faculty (FacSSN) );

17 3.2 - 17 Creating University Database (CREATE TABLE Enrollment) CREATE TABLE Enrollment ( StdSSNCHAR(11), OfferNo NUMBER(6), EnrGradeNUMBER(3), CONSTRAINT Enrollment_pk PRIMARY KEY (StdSSN, OfferNo), CONSTRAINT Enrollment_StdSSN_fk FOREIGN KEY (StdSSN) REFERENCES Student (StdSSN), CONSTRAINT Enrollment_OfferNo_fk FOREIGN KEY (OfferNo) REFERENCES Offering (OfferNo) );

18 3.2 - 18 Self-Referencing Relationships ( 自我參照關係 ) Foreign key that references the same table Represents relationships among records of the same table Not common but important in specialized situations

19 3.2 - 19 Self-Referencing Example EmpSSNEmpFirstNameEmpLastNameEmpRankEmpSalaryEmpSupervisor 098-76-5432LEONARDVINCEASST$35,000654-32-1098 543-21-0987VICTORIAEMMANUELPROF$120,000 654-32-1098LEONARDFIBONASSC$70,000543-21-0987 876-54-3210CRISTOPHERCOLANASST$40,000654-32-1098 123-45-6789JOHNSMITHASSC$75,000543-21-0987 Table Name: Employee FK PK 543-21-0987 654-32-1098 098-76-5432 876-54-3210 123-45-6789 record self-referencing architecture

20 3.2 - 20 Self-Referencing Example CREATE TABLE Employee ( EmpSSN CHAR(11), EmpFirstNameVARCHAR(50) NOT NULL, EmpLastNameVARCHAR(50) NOT NULL, EmpCityVARCHAR(50) NOT NULL, EmpStateCHAR(2) NOT NULL, EmpZipCodeCHAR(10) NOT NULL, EmpHireDateDATE, EmpSupervisor CHAR(11), CONSTRAINT employee_pk PRIMARY KEY (FacSSN), CONSTRAINT employee_fk FOREIGN KEY (EmpSupervisor) REFERENCES Employee (EmpSSN) );

21 3.2 - 21 Graphical Representation of Self-Referencing MS Access can only indirectly show self-referencing relationship.

22 3.2 - 22 Referenced Rows ( 被參照的記錄 ) A row of a parent table is referenced if there are rows in a child table with FK values identical to its PK value of the row. CourseNo CrsDescCrsUnits IS320FUNDAMENTALS OF BUSINESS PROGRAMMING4 IS460SYSTEMS ANALYSIS4 IS470BUSINESS DATA COMMUNICATIONS4 IS480FUNDAMENTALS OF DATABASE MANAGEMENT4 OfferNo CourseNo OffLocationOffTimeOffDays 1111IS320BLM30210:30 AMMW 1234IS320BLM30210:30 AMMW 5678IS480BLM30210:30 AMMW Table Name: Offering Table Name: Course

23 3.2 - 23 When to Ensure Referential Integrity? When deleting a referenced row When updating the PK of a referenced row CourseNo CrsDescCrsUnits IS320FUNDAMENTALS OF BUSINESS PROGRAMMING4 IS460SYSTEMS ANALYSIS4 IS470BUSINESS DATA COMMUNICATIONS4 IS480FUNDAMENTALS OF DATABASE MANAGEMENT4 OfferNo CourseNo OffLocationOffTimeOffDays 1111IS320BLM30210:30 AMMW 1234IS320BLM30210:30 AMMW 5678IS480BLM30210:30 AMMW Table Name: Offering Table Name: Course

24 3.2 - 24 Possible Choices for Defining Related Tables (For Ensuring Referential Integrity) Restrict ( 限制 ) –Do not permit any action on the referenced parent row Cascade ( 連鎖 ) –Perform the same action on related child rows Nullify ( 空值化 ) –Set the foreign key of related child rows to null –Only valid if foreign keys accept null values Default ( 預設值 ) –Set the FK column of related child rows to its default value

25 3.2 - 25 SQL Syntax Example of Possible Actions (For Ensuring Referential Integrity) CREATE TABLE Enrollment ( OfferNoNUMBER(6) NOT NULL, StdSSNCHAR(11) NOT NULL, EnrGradeNUMBER(3,2), CONSTRAINT enrollment_pk PRIMARY KEY (OfferNo, StdSSN), CONSTRAINT enrollment_offerNo_fk FOREIGN KEY (OfferNo) REFERENCES Offering (OfferNo) ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT enrollment_stdSSN_fk FOREIGN KEY (StdSSN) REFERENCES Student (StdSSN) ON DELETE RESTRICT ON UPDATE CASCADE ); The actions can be specified in SQL using ON DELETE and ON UPDATE clauses. The clauses are part of FK constraints of the child table.

26 3.2 - 26 Update Operations On Tables INSERT a record. DELETE a record. UPDATE a record Integrity constraints should not be violated by these update operations.

27 3.2 - 27 CREATE TABLE : Employees CREATE TABLE Employees ( emp_id CHAR(6), f_name VARCHAR(20) NOT NULL, l_name VARCHAR(25) NOT NULL, email VARCHAR(25) NOT NULL, hire_date DATE NOT NULL, salary INTEGER CONSTRAINT emp_salary_ck CHECK (salary > 0), commission_pct DECIMAL(2,2), dept_id INTEGER, CONSTRAINT emp_email_uk UNIQUE (email), CONSTRAINT emp_pk PRIMARY KEY (emp_id), CONSTRAINT emp_dept_id_fk FOREIGN KEY (dept_id) REFERENCES Departments(dept_id) ON DELETE RESTRICT ON UPDATE CASCADE );

28 3.2 - 28 UPDATE employees * ERROR at line 1: ORA-02291: integrity constraint (employees.emp_dept_id_fk) violated - parent key not found UPDATE employees SET dept_id = 55 WHERE dept_id = 110; Example of Violating Constraints ( UPDATE records ) The message shows that department 55 does not exist in parent table Department.

29 3.2 - 29 Example of Violating Constraints ( DELETE a record ) You cannot delete a referenced row of a (parent) table if the FK of its child table is ON DELETE RESTRICT DELETE FROM departments WHERE dept_id = 60; DELETE FROM departments * ERROR at line 1: ORA-02292: integrity constraint (employees.emp_dept_id_fk) violated - child record found The message shows that department 60 has associated child records in child table employees.

30 3.2 - 30 The INSERT Operation ( INSERT a record ) Violate entity integrity (Rejected) Because NOT NULL constraint on SSN is violated. Why ? INSERT INTO EMPLOYEE VALUES (’Cecilia’, ’F’, ’Kolonsky’, null, ’1960-01-01’, ’123 Riley Street’, ’M’, 40000, null, 4); PK

31 3.2 - 31 The INSERT Operation ( INSERT a record ) Violate the PK constraint (Rejected) Because UNIQUE constraint is violated. Why ?. INSERT INTO EMPLOYEE VALUES (’Alicia’, ’J’, ’Zelaya’, ’999887777’, ’1960-04-05’, ’6357 Windy Lane, Katy, TX’, ’F’, 2800, ’987654321’, 4);

32 3.2 - 32 The INSERT Operation ( INSERT a record ) Violate the referential integrity constraint (Rejected) Why? Because Department No 7 does not exist. INSERT INTO EMPLOYEE VALUES (’Cecilia’, ’F’, ’Kolonsky’, ‘677678989’, ’1960-01-01’, ‘123 Riley Street’, ’M’, 40000, ‘987654321’, 7 ); FK

33 3.2 - 33 The INSERT Operation ( INSERT a record ) Are all constraints satisfied? INSERT INTO EMPLOYEE VALUES (’Cecilia’, ’F’, ’Kolonsky’, ‘677678989’, ’1960-01-01’, ‘123 Riley Street’, ’M’, 40000, null, 4 );

34 3.2 - 34 The DELETE Operation ( DELETE child records ) This deletion is acceptable. DELETE RESTRICT, OK ? DELET FROM WORK_ON WHERE ESSN = ’999887777’; Child table Parent table FK

35 3.2 - 35 The DELETE Operation ( DELETE a referenced parent record ) This deletion is not acceptable, if …. DELET FROM EMPLOYEE WHERE SSN = ’999887777’; Child table Parent table A referenced row The FK of child table is ON DELETE RESTRICT

36 3.2 - 36 The UPDATE Operation (Update non-PK, non-FK Column) This operation is acceptable. UPDATE EMPLOYEE SET SALARY = 28000 WHERE SSN = ’999887777’;

37 3.2 - 37 The UPDATE Operation (Update FK Column of Child Table) This operation is acceptable. UPDATE EMPLOYEE SET DNO = 1 WHERE SSN = ’999887777’; Child/Parent table Parent/Child table PK FK

38 3.2 - 38 The UPDATE Operation (Update FK Column of Child Table) Unacceptable, because it violates referential integrity constraint. Why ? UPDATE EMPLOYEE SET DNO = 7 WHERE SSN = ’999887777’; Child/Parent table Parent/Child table PK FK

39 3.2 - 39 The UPDATE Operation (Update PK Column of Parent Table) Unacceptable, because it violates PK constraints (entity integrity). Why ? UPDATE EMPLOYEE SET SSN = ’999887777’ WHERE SSN = ’987654321’; Child/Parent table Parent/Child table PK FK

40 3.2 - 40 The UPDATE Operation Updating an column that is neither a PK nor a FK usually causes no problems –Except UNIQUE, CHECK & NOT NULL Modifying a PK value is similar to deleting one record and inserting another in its place. If a FK of the child table is modified, the DBMS must make sure that the new value refers to an existing record in its parent table (or is null).

41 3.2 - 41 分組專題 (DDL: Data Definition Language) 下一週第一堂上課時交 分組專題 #1 第三章 71 頁 Problems : 1, 2, 3, 4, 5, 6 ( 使用 SQL:2003 語法 ) (SQL:2003 語法 : 參考第三章 74 頁 Appendix 3.B) ( 範例 : 參考第三章 73 頁 Appendix 3.A)


Download ppt "3 Copyright © 2006, Oracle. All rights reserved. Relational Data Model Part II."

Similar presentations


Ads by Google