Documente Academic
Documente Profesional
Documente Cultură
The Relational
Model & Relational
Database
Constraints
Chapter 3
Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified fro m Fundamentals of Database Syst ems (5 /6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.)
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Review
TerminologiesRelational Model
What is a tuple?
A tuple represents a row which contains data elements representing
facts that typically correspond to a real-world entity or relationship
What is an attribute?
A column header which gives some meaning of the data items in that
column
Review
Formal Definitions
iClicker: Q1
r(R) denotes
A. a database
B. a relation state
C. a relation schema
D. a tuple
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
iClicker: Q2
Review
Formal Definitions
Review
Definition Summary
Complete the following table
Relation
Attribute
Domain
Tuple
Schema
State
6
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
The Relational
Model & Relational
Database
Constraints
Chapter 2
Summary
Properties of Relations
1 Relation name is distinct from all other relation names in relational schema.
2 Each cell of relation contains exactly one atomic (single) value.
3 Each attribute has a distinct name.
4 Values of an attribute are all from the same domain.
5 Each tuple is distinct; there are no duplicate tuples.
6 Order of attributes has no significance.
7 Order of tuples has no significance, theoretically.
8
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Inherent constraints
Refers to the constraints that are inherent in the model
Examples:
Schema-based constraints
Can be directly expressed in schemas of the data model
Example:
Application-based constraints
Not directly expressed in schemas (enforced by application programs)
Example:
10
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Schema-based Constraints
Domain Constraints
Schema-based Constraints
Key Constraints
Superkey (SK)
Superkey is a subset of attributes that uniquely identifies the
tuples of a relation
No two distinct tuples in any valid relation state r(R) can have the same
value for SK
That is, for any distinct tuples t1 and t2 in r(R),
t1[SK] t2[SK]
where SK is superkey satisfying uniqueness constraint
12
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Schema-based Constraints
Key Constraints
A superkey can have redundant attributes
A more useful concept is that of a KEY (no redundancy)
Minimum
superkey
Key (K)
Removing any attribute A from K leaves a set of attributes K that
is not a superkey of R any more
Key satisfies two properties:
Two distinct tuples in any state of relation cannot have identical
values for (all) attributes in key uniqueness (i.e., it is a superkey)
Minimal superkey
Cannot remove any attributes and still have uniqueness constraint (in
the above condition) hold
13
Schema-based Constraints
Key Constraints
Example 1
Consider STUDENT relation
14
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Schema-based Constraints
Key Constraints
Candidate key
In general, a relation schema may have more than one key
Each of the keys is called a candidate key
Primary Key (PK)
If a relation has several candidate keys, one is chosen arbitrarily to be the
primary key
The primary key value is used to uniquely identify each tuple in a
relation
The primary key attributes are underlined
General rule: Choose a primary key with a single attribute or a small
number of attributes
Other candidate keys are designated as unique keys and are not underlined
15
Schema-based Constraints
Key Constraints
In Summary
Candidate key can be any key
Example: SSN, Student ID, Email, etc
.
.
.
16
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Schema-based Constraints
Key Constraints
Example 2
Consider the CAR relation schema:
CAR(State, Reg#, SerialNo, Make, Model, Year)
17
Schema-based Constraints
Key Constraints
Example 3
CAR table with two candidate keys
License_Number and Engine_serial_number
License_Number chosen as Primary Key (PK)
18
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
Schema-based Constraints
NOT NULL Constraint
Example
If car must have a valid, non-Null value for the
SerialNo attribute, then the SerialNo of Car is required
to be NOT NULL
19
Summary:
Primary Key (PK) can not be NULL
Why?
20
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
10
Summary:
Ex ample
Referential Integrity Constraint
22
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
11
2.
Value of FK in a tuple t1 of current state r(R1) either occurs as a value of PK for some
tuple t2 in current state r(R2) or is NULL.
In the former case, we have t1[FK]= t2[PK] and we say that the tuple t1 references
or refers to the tuple t2
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
12
Ex ample
25
Ex ample
Constraints for UNIVERSITY database
26
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
13
the max. no. of hours per employee for all projects he or she works on is
56 hrs per week
27
Insert
Delete
28
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
14
Ex ample
Populated Database State
29
Ex ample
Populated Database State
30
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
15
Key constraint:
if the value of a key attribute in the new tuple already exists in another
tuple in the relation
Referential integrity:
if a foreign key value in the new tuple references a primary key value that
does not exist in the referenced relation
Entity integrity:
if the primary key value is null in the new tuple
31
Ex ample 1
The Insert Operation
Figure 3.6
insert <John, H, Doe, Null, 1960-0405,55 Main Street, Miami, FL, 33165, M,
28000, Null, 4> into EMPLOYEE
Result
NOT OK
Violates the entity integrity constraint (Null for PK),
Will be rejected
32
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
16
Ex ample 2
The Insert Operation
Figure 3.6
insert <John, H, Doe, 999887777,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 4> into
EMPLOYEE
Result
NOT OK
Violates the key constraint because another tuple with the
same Ssn value already exists in the EMPLOYEE relation
Will be rejected
33
Ex ample 3
The Insert Operation
Figure 3.6
insert <John, H, Doe, 677678989,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 7> into
EMPLOYEE
Result
NOT OK
Violates the referential integrity constraint since Dno=7
does not exist
Will be rejected
34
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
17
Ex ample 4
The Insert Operation
Figure 3.6
insert <John, H, Doe, 677678989,
1960-04-05, 55 Main Street, Miami, FL,
33165, M, 28000, 987654321, 4> into
EMPLOYEE
Result
OK
Satisfies all constraints, it is acceptable
35
36
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
18
Ex ample 1
The Delete Operation
Figure 3.6
delete the WORKS_ON tuple with
ESSN=999887777 and PNO = 30
Result
OK
38
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
19
Ex ample 1
The Update Operation
Figure 3.6
Update the salary of the EMPLOYEE tuple with
Ssn = 999887777 to 28000
Result
OK
Practice Question(s)
Do exercise 3.11 on page 81 (6th Edition)
40
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.),
Database System Concepts (5/6) (Silberschatz et al.), Database Systems (9/10) (Coronel et al.)
20