1 RelationalModel
1 RelationalModel
Chapter 3
sid name
name login
login age
age gpa
300 Smith smith@ee 18 3.4
sid
sid name login
name login age gpa
age gpa
300 Smith smith@ee 18 3.4
100 John john@ceng 19 1.7
200 Fatma fatma@ee 18 3.6
The DBMS optimizes several tasks for the use of PK (eg., while
referring to a tuple from another relation or while accessing the table
via an index, etc.)
CENG 351 File Structures and Data Management 20
Key Constraints: Example
• Student(sid: string, name: string, login:
string, age: integer, gpa:real)
• Based on the application requriements of a university DB:
– What can be the candidate keys for Student? sid or: login
– What can be the superkeys? sid,name
– Which one would you choose as sid,gpa PK?
sid,name,gpa
sid sid,name,gpa,login,age
Remark: ICs are based upon the semanticslogin,name
of the real-world enterprise that is being login,name,gpa
described in the database relations. sid,login
…
CENG 351 File Structures and Data Management 21
Primary and Candidate Keys in SQL
Possibly many candidate keys, one of which is chosen as
the primary key.
CREATE TABLE Student
(sid CHAR(20),
name CHAR(20),
login CHAR(10),
age INTEGER,
gpa REAL,
PRIMARY KEY (sid),
UNIQUE (login))
For domain, PK and unique constraints: If a DB modification
operation (add/del/update) violates them , operation is rejected
22
Primary and Candidate Keys in SQL
“For a given student and course, there is a single grade.”
Enrolled
Student
x ?
x
Request to
delete row
rejected 30
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET NULL: Set value of foreign key in
referencing row(s) in A to null
Enrolled Student
x
null
x
Row
deleted
31
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET DEFAULT: Set value of foreign key in
referencing row(s) in Enrolled to default value
(y) which must exist in Student
Default
Enrolled value
y Student
x
x
Row
deleted
32
Handling Foreign Key Violations (cont’d)
• Deletion from Student (cont’d):
– SET DEFAULT: Set value of foreign key in
referencing row(s) in Enrolled to default value
(y) which must exist in Student
Default
Enrolled value
y Student
y
Row
deleted
33
Handling Foreign Key Violations
(cont’d)
Enrolled
Student
x x
34
Referential Integrity in SQL/92