1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Relational State Constraints These slides.

Slides:



Advertisements
Similar presentations
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Natural Joins These slides are licensed.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Oracle Data Definition Language (DDL)
CSC271 Database Systems Lecture # 6. Summary: Previous Lecture  Relational model terminology  Mathematical relations  Database relations  Properties.
The Relational Model These slides are based on the slides of your text book.
Week 6 Lecture Normalization
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen M:N Relationships & Bridge Classes These.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relationship Classes & N-ary Relationships.
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Dependent & Identifying Relationships.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Normal Forms These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Conceptual State Constraints These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Advanced Normalization These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen :1 Relationships These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Union Subclasses & Disjoint 1:M Relationships.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Design Sections 11 Database relationship, Integrity, keys, mapping conceptual model to logical/physical model Previous Section 12 – DDLesson11Fa12.ppt.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational Mapping with Constraints &
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen The Relational Model & Relational Mapping.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subclasses These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Object-Relational Database Programming.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Roles & Constraints These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Inner Joins These slides are licensed.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Functional Dependencies & Normalization.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Logical Database Design and the Rational Model
Chapter 6: Integrity (and Security)
COP Introduction to Database Structures
Including Constraints
Database Systems Instructor Name: Lecture-12.
Translation of ER-diagram into Relational Schema
Copyright © Ellis Cohen Consistency & Initialization
Database Design: Relational Model
Copyright © Ellis Cohen
Presentation transcript:

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Constraints These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see

2 © Ellis Cohen Overview of Lecture Relational State Constraints Check Constraints Non Null Constraints Simple Uniqueness Constraints Composite Primary Keys Composite Uniqueness Constraints Simple Foreign Key Constraints Deletion Integrity Composite Foreign Key Constraints Foreign Keys for Unique Attributes Defining and Changing Constraints

3 © Ellis Cohen Relational State Constraints

4 © Ellis Cohen State Constraints State Constraint Specifies an invariant property of the database state (something that must always be true and must be able to be checked at ANY ARBITRARY TIME!) Conceptual State Constraint A state constraint written informally in terms of the conceptual model (about entity classes, not tables!) Relational State Constraint A state constraint written formally, in SQL, in terms of the relational model. The SQL standard includes built-in support for relational state constraints.

5 © Ellis Cohen Relational State Constraints Check Constraints (CHECK, NOT NULL) Ensures that each tuple in a table satisfies a condition Uniqueness Constraints (UNIQUE, PRIMARY KEY) Ensures that all values in a column are unique Foreign Key Constraints (REFERENCES) Ensures that a value in one column (or group of columns) matches a value in a referenced unique column (or group of columns) Relational State Assertions (ASSERTION) Ensures that an arbitrary condition (possibly involving multiple tables) remains satisfied

6 © Ellis Cohen Examples of Relational Constraints CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) not null); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30) not null, streetvarchar(40), cityvarchar(20), statechar(2) check (state <> 'CA'), zipchar(5), jobvarchar(9), deptnonumber(3) references Depts, mgrnumber(4) references Emps, salnumber (11,2), check (empno <> mgr) )

7 © Ellis Cohen Brief Textual Relational Model Depts( deptno, dname ) dnamenot null Emps( empno, ename, street, city, state, zip, deptno, mgr, sal ) enamenot null check (state <> 'CA') deptno references Depts mgr references Emps check (empno <> mgr) SHORTHAND LIST OF CONSTRAINTS Parenthesized list of all attributes, with PK underlined Followed by bulleted list of constraints

8 © Ellis Cohen Check Constraints

9 © Ellis Cohen Checking a Tuple at a Time Check constraints can be used for enforcing any state constraint that can be checked by considering –a single tuple at a time –of a single table Examples –Every employee makes more than $600 –No employee has the job PEON or JESTER –Every DEPTMGR makes more than $2000

10 © Ellis Cohen Check as Column Constraint CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), streetvarchar(40), cityvarchar(20), statechar(2) check (state <> 'CA'), zipchar(5), jobvarchar(9), deptnonumber(3) references Depts, mgrnumber(4) references Emps, salnumber (11,2) ) Column constraint Associate constraint with the one column it names

11 © Ellis Cohen Check as Table Constraint CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), streetvarchar(40), cityvarchar(20), statechar(2), zipchar(5), jobvarchar(9), deptnonumber(3) references Depts, mgrnumber(4) references Emps, salnumber (11,2), check( state <> 'CA' ) ) No constraint Table constraints are not associated with a particular column. They are just placed at the end of the table definition Move constraint to end of table definition

12 © Ellis Cohen Required Table Constraints CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), streetvarchar(40), cityvarchar(20), statechar(2) check (state <> 'CA'), zipchar(5), jobvarchar(9), deptnonumber(3) references Depts, mgrnumber(4) references Emps, salnumber (11,2), check (empno <> mgr) ); Column constraint Table constraint A constraint that names multiple columns MUST be a table constraint!

13 © Ellis Cohen WHERE vs CHECK Both WHERE clauses and CHECK constraints use boolean expressions – when applied to a tuple, the result can be TRUE or FALSE WHERE clauses are used in queries to filter the tuples returned WHERE state <> 'CA' CHECK constraint are used in table definitions. The DB makes sure that every tuple in the table satisfies the expression CHECK( state <> 'CA' )

14 © Ellis Cohen Upgrading Attributes to Classes Employee College collegeName … empno ename Employee empno ename collegeName It can be useful to upgrade attributes to classes. WHY?

15 © Ellis Cohen Upgrading adds Referential Integrity Employee College Emps empno ename collegeName Colleges collegeName … collegeName … empno ename Visual Relational Model (Relational Schema) Employee empno ename collegeName Emps empno ename collegeName Visual CONCEPTUAL Model (Crow Magnum) The Colleges table can contain a list of all known colleges, with standard spellings for the collegeNames. It ensures that an employee didn’t go to a "fake" college, and that all employees who went to the same college have their collegeName spelled the same way. Also allows adding college-specific attributes without redundancy or a surrogate PK How would this checking be done without Colleges?

16 © Ellis Cohen Referential Integrity vs Checking Relational Model Employee empno ename collegeName Emps empno ename collegeName CONCEPTUAL Model + Conceptual State Constraint: collegeName must be one of Amherst, Bates, Bowdoin, … Emps empnoprimary key enamenot null collegeNameCHECK( collegeName IN ('Amherst', 'Bates', 'Bowdoin', … ) ) For large # of values, this approach is slower than using referential integrity. Also, making changes requires altering the CHECK constraint, which is more complicated than modifying the Colleges table

17 © Ellis Cohen Complex Check Constraints Check constraints can be used to enforce any constraint that only involves checking a tuple at a time. What's the CHECK constraint for Every DEPTMGR makes $1500 or more (Assume that every employee has a job and a salary) Remember that every tuple in the table (not just those for DEPTMGR's) must satisfy the CHECK constraint

18 © Ellis Cohen Check Constraint Solution Think about how you'd look at a specific tuple to see if the constraint is satisfied 1.Check if the job is DEPTMGR. If not, OK 2.Otherwise, check that sal ≥ 1500 CHECK( job != 'DEPTMGR' OR sal >= 1500 )

19 © Ellis Cohen Alternate Check Constraint Solution Which tuples should be excluded 1.Those in which the job = 'DEPTMGR' 2.And where sal < 1500 ) CHECK( NOT ( job = 'DEPTMGR' AND sal < 1500 ) ) CHECK( job != 'DEPTMGR' OR sal >= 1500 ) Equivalent by DeMorgan's Law

20 © Ellis Cohen Not Null Constraints

21 © Ellis Cohen Not Null Constraints in Relational Schemas Depts deptno ! dname ! Solid underline always indicates a primary key, which implies not null We use an exclamation mark to denote non-null attributes (not needed for primary keys) NOTE: Most non-reference attributes in actual applications will be non-null! CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) not null ) The database will not allow dname to be NULL

22 © Ellis Cohen Relational Mapping of Required Attributes Dept deptno dname ! Depts deptno dname ! Visual CONCEPTUAL Model (Crow Magnum ER Diagram) Visual RELATIONAL Model (Relational Schema) Means that every department must have a name Means that the dname attribute is never NULL

23 © Ellis Cohen NOT NULL dnamevarchar(20) not null IS JUST AN ABBREVIATION FOR dnamevarchar(20) check(dname IS NOT NULL) But the not null constraint is more efficient

24 © Ellis Cohen Check Constraints and NULL Consider CHECK( sal > 100 ) If sal is 50 (sal > 100) is FALSE  NOT ALLOWED If sal is 200 (sal > 100) is TRUE  OK If sal is NULL (i.e. UNKNOWN) (sal > 100) is NULL (i.e. UNKNOWN)  OK The DB does not ensure that Check Constraints are TRUE The DB does ensure that Check Constraints are not FALSE

25 © Ellis Cohen Disallowing NULLs sal number(11,2) CHECK( sal > 100 AND sal IS NOT null ) sal number(11,2) NOT NULL CHECK( sal > 100 ) -- clearer and more efficient Suppose we want to ensure that every employee's salary is greater than 100 and not NULL

26 © Ellis Cohen Three-Valued Logic ORTRUENULLFALSE TRUE NULLTRUENULL FALSETRUENULLFALSE xNOT x TRUEFALSE NULL FALSETRUE ANDTRUENULLFALSE TRUE NULLFALSE NULL FALSE These make perfect sense if you treat NULL as UNKNOWN

27 © Ellis Cohen Check/NULL Constraint Problem CHECK( job != 'DEPTMGR' OR sal >= 1500 ) Suppose sal can be NULL, but not when the job is DEPTMGR Every dept manager makes 1500 or more

28 © Ellis Cohen Check Constraints w NULL Check by looking at each employee tuple 1) Check if they are a DEPTMGR, if NOT, OK 2) If they are, then check that their sal is NOT NULL and that they make ≥ 1500 (assuming every employee has a job) CHECK( job != 'DEPTMGR' OR (sal IS NOT NULL AND sal >= 1500 ) ) Suppose job can be NULL: Revise the CHECK constraint

29 © Ellis Cohen Checks with Nullable Attributes CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ) CREAT TABLE Emps( empnonumber(4) primary key, enamevarchar(30), jobvarchar(20), salnumber(11,2), addrvarchar(80), deptnonumber(3) references Depts, mgrnumber(4) references Emps, CHECK( job != 'DEPTMGR' OR job IS NULL OR (sal ≥ 1500 AND sal IS NOT NULL) ) Every dept manager makes 1500 or more Note: both job and sal can be NULL

30 © Ellis Cohen Simple Uniqueness Constraints

31 © Ellis Cohen Uniqueness Constraints UNIQUE All the values in the column must be different PRIMARY KEY This identifies this column as the primary means of identifying a row. It also implies NOT NULL and UNIQUE CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) unique not null ) Any column (or group of columns) whose values are not null and unique is called a candidate key (so both deptno & dname are candidate keys)

32 © Ellis Cohen Uniqueness Constraints in Relational Schemas Depts deptno dname ! Solid underline always indicates a primary key, which implies unique and not null We use the unipop symbol to denote uniqueness for fields which are not primary keys not null

33 © Ellis Cohen Relational Mapping of Unique Attributes Dept deptno dname ! {unique} Depts deptno dname ! Visual CONCEPTUAL Model (Crow Magnum ER Diagram) Visual RELATIONAL Model (Relational Schema) Means that every department must have a name which is unique Means that the dname attribute is unique & never NULL

34 © Ellis Cohen Unique Table Constraints CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) unique not null ) CREATE TABLE Depts( deptnonumber(3), dnamevarchar(20) not null, primary key(deptno), unique(dname) ) Equivalent Primary Key Table Constraint Unique Table Constraint

35 © Ellis Cohen Composite Primary Keys

36 © Ellis Cohen Composite Primary Keys Uniqueness & Primary Key Constraints can be –Column Constraints (for single-column constraints) –Table Constraints (required for composite constraints) Suppose each Dept is uniquely identified by –a divno (a division number), and –a deptno, which is only unique within each division Depts (divno, deptno, dname) dname not null unique Composite Primary Key

37 © Ellis Cohen Composite Primary Key Example divnodeptnodname 110NY SALES 130NY ACCOUNTING 150NY SUPPORT 210MA SALES 242MA ACCOUNTING 250MA PARTYING 350NJ MARKETING unique non-null composite primary key

38 © Ellis Cohen SQL Composite Primary Keys CREATE TABLE Depts ( divnonumber(3), deptnonumber(3), dnamevarchar(20) not null unique, primary key( divno, deptno ) ) CREATE TABLE Depts ( divnonumber(3) primary key, deptnonumber(3) primary key, dnamevarchar(20) not null unique ) Not legal, says that divno is a primary key and also deptno is a primary key But a table can only have one primary key Composite primary keys must be written as table constraints

39 © Ellis Cohen Composite Primary Key in Relational Schemas Depts divno deptno dname ! divno + deptno are both underlined, so together they indicate a composite primary key unique not null

40 © Ellis Cohen Relational Mapping of Composite Primary Keys Dept divno deptno dname ! {unique} Visual CONCEPTUAL Model (Crow Magnum ER Diagram) Visual RELATIONAL Model (Relational Schema) divno + deptno are both underlined, so together they indicate a composite primary key divno deptno dname ! Depts divno + deptno are both underlined, so together they indicate a composite primary key

41 © Ellis Cohen Composite Uniqueness Constraints

42 © Ellis Cohen Composite Unique Constraint Example divnodeptnodname 110SALES 130ACCOUNTING 150SUPPORT 210SALES 242ACCOUNTING 250PARTYING 350MARKETING composite primary key non-null Combination of divno & dname is unique

43 © Ellis Cohen Composite Unique Constraints Suppose the department name is also unique only within a division CREATE TABLE Depts ( divnonumber(3), deptnonumber(3), dnamevarchar(20) not null, primary key( divno, deptno ), unique( divno, dname ) ) Table constraint

44 © Ellis Cohen Multiple Unique Constraints CREATE TABLE Depts ( divnonumber(3) unique, deptnonumber(3), dnamevarchar(20) not null unique, primary key( divno, deptno ) ) Is this legal? If not, why not? If so, what would it mean, and would it make sense?

45 © Ellis Cohen Legal but Wrong CREATE TABLE Depts ( divnonumber(3) unique, deptnonumber(3), dnamevarchar(20) not null unique, primary key( divno, deptno ) ) Says that every dept has a unique value for divno AND also a unique value for dname If every dept has a unique value for divno, then every division only has at most a single dept!

46 © Ellis Cohen Composite Uniqueness Constraints in Relational Schemas Depts divno deptno dname ! divno + deptno are both underlined, so together they indicate a composite primary key We use the multipop symbol to denote composite unique fields not null

47 © Ellis Cohen Relational Mapping of Composite Unique Constraints Dept divno deptno dname ! Visual CONCEPTUAL Model (Crow Magnum ER Diagram) Visual RELATIONAL Model (Relational Schema) + A division number and department name together uniquely identify a department Composite uniqueness can be represented visually in the Relational Schema and corresponds to a built-in SQL construct Composite uniqueness cannot be shown visually in the ER model. It must be stated explicitly! divno deptno dname ! Depts

48 © Ellis Cohen Simple Foreign Key Constraints

49 © Ellis Cohen Mapping Simple Foreign Keys DeptEmployee works for deptno dname empno ename addr Emps empno ename addr deptno Depts deptno dname Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema)

50 © Ellis Cohen Foreign Key Column Constraint CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), deptnonumber(3) references Depts ) Foreign Key Column Constraint

51 © Ellis Cohen Foreign Key Table Constraint CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), deptnonumber(3), foreign key(deptno) references Depts ) Foreign Key Table Constraint

52 © Ellis Cohen Simple References Depts( deptno, dname ) Emps( empno, ename, addr, deptno ) deptno references Depts Column Constraint (Simple Foreign Key Constraint) Emps empno ename addr deptno Depts deptno dname VISUAL Relational Model (Relational Schema) Brief TEXTUAL Relational Model (TRex)

53 © Ellis Cohen Mandatory Child Participation? DeptEmployee works for deptno dname empno ename addr Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) Emps empno ename addr deptno Depts deptno dname How would mandatory child participation be represented in the relational model?

54 © Ellis Cohen Mandatory Child Participation DeptEmployee works for deptno dname empno ename addr Visual CONCEPTUAL Model (Crow Magnum) RELATIONAL Model Emps empno ename addr deptno ! Depts deptno dname Emps( empno, ename, addr, deptno ) deptno not null references Depts

55 © Ellis Cohen Mandatory Parent Participation? DeptEmployee works for deptno dname empno ename addr Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) Emps empno ename addr deptno Depts deptno dname How would mandatory parent participation be represented in the relational model?

56 © Ellis Cohen Mandatory Parent Participation DeptEmployee works for deptno dname empno ename addr Visual CONCEPTUAL Model (Crow Magnum) RELATIONAL Model Emps empno ename addr deptno Depts deptno dname There's no way to represent mandatory parent participation in the standard relational schema. We'll need to use state assertions instead!

57 © Ellis Cohen Deletion Integrity Rules Note: These reflect other business rules (resulting in pre- and post-conditions) They are NOT state constraints

58 © Ellis Cohen Side Effects of Deletion in 1:M Relationships Suppose a department has employees: Should we allow the department to be deleted? If so, what might we want to have happen to those employees when that department is deleted? Dept Employee works for Every 1:M relationship requires some thought about what happens when a parent instance is deleted. Child Entity ClassParent Entity Class

59 © Ellis Cohen Deletion Integrity Rules Restricted Deletion: Don't allow a department to be deleted if it has employees (pre-condition) Deassignment: When a department is deleted, all employees in that department become unassigned Lifetime Dependency (a.k.a. Cascading Delete): When a department is deleted, all employees in that department are deleted Transfer: When a department is deleted, all employees in that department are moved to other departments Special Transfer: When a department is removed, all employees in that department are moved to the Party Department Dept Employee works for Possible business rules involving Dept deletion (NOTE: None of these are state constraints!) Built-in SQL support

60 © Ellis Cohen Restricted Deletion Dept Employee works for Emps empno ename addr deptno Depts deptno dname Emps( empno, ename, addr, deptno ) deptno references Depts The default deletion integrity rule is Restricted Deletion (results in pre-condition for operations that delete departments) A department with employees CANNOT be deleted

61 © Ellis Cohen Deassignment Dept Employee works for Emps empno ename addr deptno Depts deptno dname Emps( empno, ename, addr, deptno ) deptno references Depts on delete set null + Business Rule: When a dept is deleted, all employees in the dept become unassigned (results in post-conditions for operations that delete depts) Not represented in relational schema, only in textual representation & SQL

62 © Ellis Cohen Deassignment in SQL CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(40), deptnonumber(3) references Depts on delete set null )

63 © Ellis Cohen Lifetime Dependency (Cascading Delete) Dept Employee works for Emps empno ename addr deptno Depts deptno dname Emps( empno, ename, addr, deptno ) deptno references Depts on delete cascade + Business Rule: When a dept is deleted, all employees in the dept are deleted as well

64 © Ellis Cohen Mandatory Child Participation & Deletion Integrity DeptEmployee works for deptno dname empno ename addr Suppose every employee MUST be assigned to a department. How does this affect the possibilities for deletion integrity rules?

65 © Ellis Cohen Mandatory Child Participation Prevents Deassignment! DeptEmployee works for deptno dname empno ename addr Visual CONCEPTUAL Model (Crow Magnum) RELATIONAL Model Emps empno ename addr ! deptno ! Depts deptno dname An employee's deptno can never be NULL, so Deassignment deptno not null references Depts on delete set null doesn't make any sense, since deleting a department, would deassign all employees in the dept, setting their deptno's to NULL!

66 © Ellis Cohen Lifetime Dependency & Mandatory Child Participation Dept Employee works for Emps empno ename addr ! deptno ! Depts deptno dname Emps( empno, ename, addr, deptno ) deptno not null references Depts on delete cascade + Business Rule: When a dept is deleted, all employees in the dept are deleted as well

67 © Ellis Cohen Dependency + Particiption in SQL CREATE TABLE Depts( deptnonumber(3) primary key, dnamevarchar(20) ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), deptnonumber(3) not null references Depts on delete cascade )

68 © Ellis Cohen Composite Foreign Key Constraints

69 © Ellis Cohen Mapping Composite Keys DeptEmployee works for divno deptno dname empno ename addr Emps empno ename addr divno deptno dname Visual CONCEPTUAL Model: Crow Magnum ER Diagram Visual RELATIONAL Model: Relational Schema composite PRIMARY KEY Depts Composite Reference composite PRIMARY KEY composite FOREIGN KEY

70 © Ellis Cohen Composite Foreign Keys Emps empno ename addr divno deptno dname Depts Depts( divno, deptno, dname ) primary key( divno, deptno ) Emps( empno, ename, addr, divno, deptno ) foreign key( divno, deptno ) references Depts Table Constraint (Composite Foreign Key Constraint) A table constraint MUST be used!

71 © Ellis Cohen Composite Foreign Keys in SQL Emps empno ename addr divno deptno dname Depts CREATE TABLE Depts ( divnonumber(3), deptnonumber(3), dnamevarchar(20), primary key( divno, deptno ) ) CREATE TABLE Emps ( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), divnonumber(3), deptnonumber(3), foreign key( divno, deptno ) references Depts )

72 © Ellis Cohen Foreign Key Rule Relational Schemas use foreign key constraints. A foreign key constraint MUST refer to an attribute or group of attributes which are unique -- generally, the relation's primary key *

73 © Ellis Cohen Simple vs Composite Foreign Keys Emp Dept empno deptno Emps empno deptno Depts deptno … Emp Dept empno divno deptno Emps empno divno deptno Depts divno deptno …

74 © Ellis Cohen Composite Child Participation Emp Dept empno deptno Emps empno deptno ! Depts deptno … Emp Dept empno divno deptno Emps empno divno ! deptno ! Depts divno deptno …

75 © Ellis Cohen Composite Participation & Cascading Delete Emps empno ename addr ! ! divno ! deptno ! divno deptno dname Depts Depts( divno, deptno, dname ) primary key( divno, deptno ) Emps( empno, ename, addr, divno, deptno ) divno, deptno not null foreign key( divno, deptno ) references Depts on delete cascade

76 © Ellis Cohen Composite Natural Joins List the name & department name of each employee Emps empno ename addr divno deptno dname Depts SELECT ename, dname FROM (Emps NATURAL JOIN Depts) SELECT ename, dname FROM Emps, Depts WHERE Emps.divno = Depts.divno AND Emps.deptno = Depts.deptno

77 © Ellis Cohen Adding Surrogate Primary Keys Both deptid and (divno + deptno) are candidate keys. However, deptid is chosen to be the primary key Emps empno ename addr divno deptno dname Depts Emps empno ename addr deptid deptid divno ! deptno ! dname Depts If you want to avoid composite references (this is an option, not a requirement), you can add a surrogate primary key, either in the conceptual model, or during mapping Surrogate primary key: A new attribute added to an entity class / relation and used in place of the original primary key. composite UNIQUE constraint

78 © Ellis Cohen Relational Model with Surrogate Key CREATE TABLE Depts( deptidnumber(5) primary key, divnonumber(3) not null, deptnonumber(3) not null, dnamevarchar(20) unique not null, unique( divno, deptno ) ) CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30) not null, addrvarchar(80), deptidnumber(5) references Depts ) Emps empno ename ! addr deptid deptid divno ! deptno ! dname ! Depts composite UNIQUE constraint

79 © Ellis Cohen Foreign Keys for Unique Attributes

80 © Ellis Cohen Foreign Keys for Unique Attributes Emps empno ename addr dname Depts deptno dname ! Emps empno ename addr deptno Depts deptno dname Foreign keys need not refer to primary keys. They can refer to any attribute which is unique. For example, if a department's dname was unique, then the 1:M relationship between employees and departments could be represented by using dname as the foreign key.

81 © Ellis Cohen Unique Foreign Key Constraint CREATE TABLE Depts( divnodeptnonumber(3) primary key, dnamevarchar(20) not null unique ); CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), dnamenumber(3) references Depts(dname) )

82 © Ellis Cohen Foreign Keys + Unique Emps empno ename addr dname Depts deptno dname ! Emps empno ename addr dname Depts deptno dname !

83 © Ellis Cohen Foreign Keys for Composite Unique Attributes Emps empno ename addr divno dname Depts divno deptno dname Emps empno ename addr divno deptno Depts divno deptno dname Composite foreign keys can refer to composite unique attributes

84 © Ellis Cohen Composite Unique Foreign Key Constraint CREATE TABLE Depts ( divnonumber(3), deptnonumber(3), dnamevarchar(20) not null, primary key( divno, deptno ), unique( divno, dname ) ) CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30), addrvarchar(80), divnonumber(3), dnamenumber(3), foreign key( divno, deptno ) references Depts( divno, dname ) )

85 © Ellis Cohen Foreign Keys + Unique Emps empno ename addr divno dname Depts divno deptno dname Emps empno ename addr divno dname Depts divno deptno dname

86 © Ellis Cohen Defining and Changing Constraints

87 © Ellis Cohen Tables with Constraints CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(30) not null, streetvarchar(40), cityvarchar(20), statechar(2) check (state <> 'CA'), zipchar(5), deptnonumber(3) references Depts, mgrnumber(4) references Emps, check (empno <> mgr) );

88 © Ellis Cohen Named Constraints CREATE TABLE Emps( empnonumber(4) CONSTRAINT Emps_pk primary key, enamevarchar(30) CONSTRAINT Emps_ename not null, streetvarchar(40), cityvarchar(20), statechar(2) CONSTRAINT Emps_CA check (state <> 'CA'), zipchar(5), deptnonumber(3) CONSTRAINT Emps_fk_deptno references Depts, mgrnumber(4) CONSTRAINT Emps_fk_mgr references Emps, CONSTRAINT Emps_SelfManage check (empno <> mgr) ); Why name? So the database can tell you the name of a constraint which is violated

89 © Ellis Cohen Enabling & Disabling Constraints ALTER TABLE Emps MODIFY CONSTRAINT Emps_CA DISABLE –Disables Emps_CA constraint ALTER TABLE Emps MODIFY CONSTRAINT Emps_CA ENABLE –Enables Emps_CA constraint (Fails if entire table cannot be validated!) ALTER TABLE Emps_CA MODIFY CONSTRAINT asn_dates ENABLE NOVALIDATE –Enables Emps_CA constraint for newly set values; doesn't check current values

90 © Ellis Cohen Changing Check Constraints ALTER TABLE Emps MODIFY CONSTRAINT CollegeCheck CHECK( collegeName IN ('Amherst', 'Bates', 'Bowdoin', …, 'Parsons' ) ) Added In Oracle, you can’t even do this, you have to first drop CollegeCheck, then add an expanded CollegeCheck constraint

91 © Ellis Cohen Changing Constraints ALTER TABLE Emps ADD CONSTRAINT Emps_empno CHECK (empno > 1) –Adds Emps_empno constraint ALTER TABLE Emps DROP CONSTRAINT Emps_CA –Deletes Emps_CA constraint

92 © Ellis Cohen Constraint Metadata Information about constraints is maintained as part of metadata In Oracle, USER_CONSTRAINTS & USER_CON_COLUMNS contain constraint metadata