Chapter05 - Relational Database Cohnstraints - Notes - v2
Chapter05 - Relational Database Cohnstraints - Notes - v2
Navathe
CHAPTER 5
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
Primary Keys
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
CAR table with two candidate keys –
LicenseNumber chosen as Primary Key
• Entity Integrity:
Missing information Primary Keys cannot
can be represented contain NULL values
using NULLs Why: if primary key
A NULL indicates a has NULLs then will
missing or unknown not uniquely identify
value the tuple/entity
Copyright © 2017 Ramez Elmasri and Shamkant B. Navathe The Relational Model
Foreign Keys
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
Foreign Keys - Example
Department Employee
DID DName EID EName DID
13 Marketing 15 John Smith 13
14 Accounts 16 Mary Brown 14
15 Personnel 17 Mark Jones 13
18 Jane Smith NULL
Employee
{ID} is a Candidate Key for
ID Name Manager Employee, and {Manager} is a
E1496 John Smith E1499 Foreign Key, which refers to
E1497 Mary Brown E1498 the same relation - every
tuple’s Manager value is either
E1498 Mark Jones E1499
NULL or matches an ID value
E1499 Jane Smith NULL
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
Referential Integrity
A constraint involving two relations
The previous constraints involve a single relation.
Used to specify a relationship among tuples in
two relations:
The referencing relation and the referenced
relation.
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
RESTRICT
Department
• RESTRICT stops any
DID DName
action that violates
integrity 13 Marketing
14 Accounts
• You cannot update or
15 Personnel
delete Marketing or
Accounts
Employee
• You can change
Personnel as it is not EID EName DID
referenced
15 John Smith 13
16 Mary Brown 14
17 Mark Jones 13
ON DELETE RESTRICT means you can't 18 Jane Smith NULL
delete a given parent row if a child row
exists that references the value for that
parent row
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
CASCADE
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
Example: ON DELETE CASCADE
The
Copyright © 2017 Ramez Relational
Elmasri and Shamkant Model
B. Navathe
In-Class Exercise
(Taken from Exercise 5.15)
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course:
STUDENT(SSN, Name, Major, Bdate)
COURSE(Course#, Cname, Dept)
ENROLL(SSN, Course#, Quarter, Grade)
BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
TEXT(Book_ISBN, Book_Title, Publisher, Author)
Draw a relational schema diagram specifying the foreign keys for this
schema.