You are on page 1of 14

Database Systems

CS 204
Lecture 7
Relational Model II
(Concept of Keys)
C. J. Date

February 15, 2010

Kashif Zafar
Keys
 The concept of Keys is very important to the
relational model.

 We'll discuss the following types of keys.


 Candidate Keys
 Primary Keys and Alternative Keys
 Foreign Keys
Formal Definitions
 Candidate key: minimal number of attributes
with unique values.
 Null value: special value meaning value
unknown or inapplicable
 Primary key: a designated candidate key;
cannot contain null values
 Foreign key: column(s) whose values must
match the values in a candidate key of another
table
Candidate Keys
 Let R be a table. By definition, at any given
time, no two tuples in the value of R are
duplicates of one another.

 Let K be a set of attributes of R. Then K is a


candidate key for R if and only if it satisfies:
(1) Uniqueness: No legal value of R ever contains
two distinct tuples with the same value for K.
(2) Irreducibility (or minimality):
No proper subset of K has the uniqueness
property.
Candidate Key Examples
 For S {S#, SNAME, STATUS, CITY},
 S# and SNAME do not have duplicates, so
 Candidate KEY {S#}
 Candidate KEY {SNAME}

 For SP {S#, P#, QTY},


 {S#, P#} does not have duplicates, so
 Candidate KEY {S#, P#}
Primary and Alternative Keys
 Exactly one candidate key must be chosen as the
primary key, and the others are alternative keys.
 Primary key and alternative keys are both candidate
keys.

 For S {S#, SNAME, STATUS, CITY},


 if we choose KEY {S#} as the primary key,
 then KEY {SNAME} is an alternative key.

 For SP {S#, P#, QTY},


 We must choose KEY {S#, P#} as the primary key,
and there will be no alternative key.
Foreign Keys
 A foreign key is a set of attributes of one
table R2 whose values are required to match
values of some candidate key of some table
R1.

 {S#} is a foreign key of SP that references the


primary key {S#} of S.
 Any S# value of SP must exist in S.
 Similarly, {P#} is another foreign key of SP
that references the primary key {P#} of P.
Integrity Rules
 Entity integrity
 No two rows with the same primary key value
 No null values in a primary key
 Referential integrity
 Foreign keys must match candidate key of source
table
 Foreign keys in some cases can be null
 The database must not contain any unmatched
foreign key values.
 If B references A, A must exist.
Referenced Rows
 Referenced row
 Foreign keys reference rows in the associated
primary key table
 Each supply row references supplier and part rows.
 Actions on referenced rows
 Delete a referenced row
 Change the primary key of a referenced row
 Referential integrity should not be violated
Possible Actions
 Restrict: do not permit action on the
referenced row
 Cascade: perform action on related rows
 Nullify: only valid if foreign keys accept
null values
 Default: set foreign keys to a default value
Referential Actions on Foreign
Keys
 When we delete a tuple from a table, say S,
that is referenced by another table, say SP,
 if the primary key {S#} value of S exists in SP,
 there are several choices of referential
actions:
 ON DELETE CASCADE
 The corresponding tuples in SP will be deleted too.
 ON DELETE RESTRICT
 The deletion of the tuple from S is rejected.
Referential Actions (Cont.)
 When we update a tuple from a table, say S,
that is referenced by another table, say SP,
 There are similar choices of referential
actions:
 ON UPDATE CASCADE
 ON UPDATE RESTRICT

 There could be other choices besides these


three. , e.g.,
 ON UPDATE SET DEFAULT.
 ON UPDATE SET NULL.
Integrity (Fig. from C.J. Date)
S# SNAME STATUS CITY S# P# QTY
S SP
S1 Smith 20 London S1 P1 300
S2 Jones 10 Paris S1 P2 200
S3 Blake 30 Paris S1 P3 400
S4 Clark 20 London S1 P4 200
S5 Adams 30 Athens S1 P5 100
S1 P6 100
S2 P1 300
P# PNAME COLOR WEIGHT CITY S2 P2 400
P P1 Nut Red 12 London S3 P2 200
P2 Bolt Green 17 Paris S4 P2 200
P3 Screw Blue 17 Rome S4 P4 300
P4 Screw Red 14 London S4 P5 400
P5 Cam Blue 12 Paris
P6 Cog Red 19 London
SQL Syntax for Actions (Just a
flavor)
CREATE TABLE Enrollment
( OfferNo INTEGER NOT NULL,
StdSSN CHAR(11) NOT NULL,
EnrGrade DECIMAL(3,2),
CONSTRAINT PKEnrollment PRIMARY KEY(OfferNo,
StdSSN),
CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo)
REFERENCES Offering
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT FKStdSSN FOREIGN KEY (StdSSN) REFERENCES
Student
ON DELETE RESTRICT
ON UPDATE CASCADE )

You might also like