Download presentation
Presentation is loading. Please wait.
Published bySibyl Carroll Modified over 9 years ago
1
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 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 http://www.openlineconsult.com/db
2
2 © Ellis Cohen 2001-2008 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
3 © Ellis Cohen 2001-2008 Relational State Constraints
4
4 © Ellis Cohen 2001-2008 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
5 © Ellis Cohen 2001-2008 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
6 © Ellis Cohen 2001-2008 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
7 © Ellis Cohen 2001-2008 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
8 © Ellis Cohen 2001-2008 Check Constraints
9
9 © Ellis Cohen 2001-2008 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
10 © Ellis Cohen 2001-2008 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
11 © Ellis Cohen 2001-2008 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
12 © Ellis Cohen 2001-2008 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
13 © Ellis Cohen 2001-2008 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
14 © Ellis Cohen 2001-2008 Upgrading Attributes to Classes Employee College collegeName … empno ename Employee empno ename collegeName It can be useful to upgrade attributes to classes. WHY?
15
15 © Ellis Cohen 2001-2008 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
16 © Ellis Cohen 2001-2008 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
17 © Ellis Cohen 2001-2008 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
18 © Ellis Cohen 2001-2008 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
19 © Ellis Cohen 2001-2008 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
20 © Ellis Cohen 2001-2008 Not Null Constraints
21
21 © Ellis Cohen 2001-2008 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
22 © Ellis Cohen 2001-2008 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
23 © Ellis Cohen 2001-2008 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
24 © Ellis Cohen 2001-2008 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
25 © Ellis Cohen 2001-2008 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
26 © Ellis Cohen 2001-2008 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
27 © Ellis Cohen 2001-2008 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
28 © Ellis Cohen 2001-2008 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
29 © Ellis Cohen 2001-2008 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
30 © Ellis Cohen 2001-2008 Simple Uniqueness Constraints
31
31 © Ellis Cohen 2001-2008 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
32 © Ellis Cohen 2001-2008 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
33 © Ellis Cohen 2001-2008 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
34 © Ellis Cohen 2001-2008 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
35 © Ellis Cohen 2001-2008 Composite Primary Keys
36
36 © Ellis Cohen 2001-2008 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
37 © Ellis Cohen 2001-2008 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
38 © Ellis Cohen 2001-2008 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
39 © Ellis Cohen 2001-2008 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
40 © Ellis Cohen 2001-2008 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
41 © Ellis Cohen 2001-2008 Composite Uniqueness Constraints
42
42 © Ellis Cohen 2001-2008 Composite Unique Constraint Example divnodeptnodname 110SALES 130ACCOUNTING 150SUPPORT 210SALES 242ACCOUNTING 250PARTYING 350MARKETING composite primary key non-null Combination of divno & dname is unique
43
43 © Ellis Cohen 2001-2008 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
44 © Ellis Cohen 2001-2008 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
45 © Ellis Cohen 2001-2008 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
46 © Ellis Cohen 2001-2008 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
47 © Ellis Cohen 2001-2008 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
48 © Ellis Cohen 2001-2008 Simple Foreign Key Constraints
49
49 © Ellis Cohen 2001-2008 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
50 © Ellis Cohen 2001-2008 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
51 © Ellis Cohen 2001-2008 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
52 © Ellis Cohen 2001-2008 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
53 © Ellis Cohen 2001-2008 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
54 © Ellis Cohen 2001-2008 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
55 © Ellis Cohen 2001-2008 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
56 © Ellis Cohen 2001-2008 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
57 © Ellis Cohen 2001-2008 Deletion Integrity Rules Note: These reflect other business rules (resulting in pre- and post-conditions) They are NOT state constraints
58
58 © Ellis Cohen 2001-2008 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
59 © Ellis Cohen 2001-2008 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
60 © Ellis Cohen 2001-2008 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
61 © Ellis Cohen 2001-2008 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
62 © Ellis Cohen 2001-2008 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
63 © Ellis Cohen 2001-2008 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
64 © Ellis Cohen 2001-2008 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
65 © Ellis Cohen 2001-2008 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
66 © Ellis Cohen 2001-2008 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
67 © Ellis Cohen 2001-2008 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
68 © Ellis Cohen 2001-2008 Composite Foreign Key Constraints
69
69 © Ellis Cohen 2001-2008 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
70 © Ellis Cohen 2001-2008 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
71 © Ellis Cohen 2001-2008 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
72 © Ellis Cohen 2001-2008 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
73 © Ellis Cohen 2001-2008 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
74 © Ellis Cohen 2001-2008 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
75 © Ellis Cohen 2001-2008 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
76 © Ellis Cohen 2001-2008 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
77 © Ellis Cohen 2001-2008 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
78 © Ellis Cohen 2001-2008 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
79 © Ellis Cohen 2001-2008 Foreign Keys for Unique Attributes
80
80 © Ellis Cohen 2001-2008 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
81 © Ellis Cohen 2001-2008 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
82 © Ellis Cohen 2001-2008 Foreign Keys + Unique Emps empno ename addr dname Depts deptno dname ! Emps empno ename addr dname Depts deptno dname !
83
83 © Ellis Cohen 2001-2008 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
84 © Ellis Cohen 2001-2008 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
85 © Ellis Cohen 2001-2008 Foreign Keys + Unique Emps empno ename addr divno dname Depts divno deptno dname Emps empno ename addr divno dname Depts divno deptno dname
86
86 © Ellis Cohen 2001-2008 Defining and Changing Constraints
87
87 © Ellis Cohen 2001-2008 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
88 © Ellis Cohen 2001-2008 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
89 © Ellis Cohen 2001-2008 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
90 © Ellis Cohen 2001-2008 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
91 © Ellis Cohen 2001-2008 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
92 © Ellis Cohen 2001-2008 Constraint Metadata Information about constraints is maintained as part of metadata In Oracle, USER_CONSTRAINTS & USER_CON_COLUMNS contain constraint metadata
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.