Relational Model�
Prof. S. Mehrotra
ICS Department
University of California at Irvine
Relational Model
2
2
Database Schema
3
3
Relation Example
4
4
Account
Account
(No order in attributes or tuples)
Relation:
account = { (150, 20, 11000), (160, 23, 2300), (180, 23, 32000) }
Relation schema: R=(attributes)
Account = (AccountId, CustomerId, Balance)
Constraints: no two accounts can have the same AccountID)
Attributes & Nulls
5
5
Customer(Id, Name, Addr)
A Relation is a Set
6
6
ID | Name | Age |
1 | Mark | 21 |
2 | Amelia | 20 |
3 | John | 20 |
ID | Name | Age |
1 | Mark | 21 |
2 | Amelia | 20 |
2 | Amelia | 20 |
Illegal
Legal
Constraints - Why?
Allow designers to specify the semantics of the data.
Enable DBMSs to check that new data satisfies the semantics.
Make application programmers’ lives easier.
Enable identification of redundancy in schemas (we will see this soon)
Help the DBMS in query processing (you need to take 222P for this one)
7
7
Constraints - Examples
8
8
Domain Constraints
9
9
Integer
String
String
violations
Key Constraints
10
10
Integer
String
String
23
violation since no two tuples should have the same value for Id, if Id is a primary key
Superkey Concept
A superkey is a set of attributes such that if no two tuples in the relation have “exactly” the same values for all of the attributes in the superkey.
Let R(A1, A2, …, An) be a relation. Let Attr = {A1, A2, .. An} be a relation. A subset of attributes K (i.e., K is a subset of Attr) is a superkey if and only if for any two distinct tuples t1 and t2 that can exist in the database, t1[K] ≠ t2[K]
11
11
SuperKey – Examples
12
12
Log(LogId, AccountId, Xact#, Time, Amount)
Illegal!
Candidate Key
13
13
LogID | AccountID | Xact# | Time | Amount |
111 | 111 | 2 | 1/12/02 | $100 |
122 | 222 | 2 | 12/28/01 | $20 |
123 | 222 | 5 | 12/11/23 | $25 |
Entity Identity Constraints:�Disallowing Null Values
14
14
Customer
Not what we want! (Solution: Disallow NULLs for this attribute)
Referential Integrity Constraints
15
15
S
R
K (primary key of R)
FK (Foreign key)
s
r
Examples of Referential Integrity�
16
16
Account
Customer
Account.CustomerId to Customer.Id
Student.dept to Dept.Name
(Every value of Student.Dept must also be a value of Dept.Name)
Student
Dept
Inclusion Dependencies�
17
17
S
Y1,…,Yn
R
X1,…,Xn
s
r
⊆
X1,...,Xn may not be a key of R
Inclusion Dependencies (cont.)�
18
18
S
Y1,…,Yn
R
X1,…,Xn
s
r
Example
19
19
DID | Name | MgrName |
1 | HR | Mark |
2 | IT | Maria |
EmpID | Name | Salary | DID |
1 | Maria | 80,000 | 2 |
2 | Mark | 90,000 | 1 |
3 | Maria | 95,000 | 2 |
Referential Integrity
Inclusion Dependencies
Mapping ER to Relational Model
Notes 04
21
(Strong) Entity Sets to Relations
Relation: Employee(ssno, salary, name)
Key: ssno
lastname, firstname)
Relation: Employee_Phone(ssno, tel)
Key: ssno, tel
Employee
ssno
name
first name
last name
salary
[ tel ]
Relation: WorksOn(ssno,proj#,startdate)
Key: ssno,proj#
IND (INclusion Dependencies):
workson[proj#] ⊆ project[proj#]
workson[ssno] ⊆ employee[ssno]
Notes 04
22
WorksOn
Relationship Sets to Relations
startdate
Employee
ssno
name
salary
Project
proj#
manager
Relation: WorksOn(ssno,proj#,startdate)
Key: ssno
IND (Inclusion Dependencies):
workson[proj#] ⊆ project[proj#]
workson[ssno] ⊆ employee[ssno]
Notes 04
23
Cardinality Constraints
An employee works on at most 1 project
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
For Many - 1 relationship: We can push the extra fields to relations corresponding to entity set on Many side as follows
Relation: Employee(ssno, name, salary, Proj#,startdate)
Key: ssno
Relation: Project(Proj#,mgr)
Key: Proj#
IND (Inclusion Dependencies): Employee[Proj#] ⊆ Project[Proj#]
Note that since employee entity set participation is partial, employees not connected to any projects will have NULL values for proj# and startdate fields.
Notes 04
24
Alternate Approach: Combining Relations
An employee works on at most 1 project
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
For Many - 1 relationship: We can also do the following
Relation: Employee(ssno,proj#,startdate, name, mgr, salary)
Key: ssno
What if the relationship was 1-1?
Notes 04
25
Yet Another Alternative: Combining Relations
An employee works on at most 1 project
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Relation: WorksOn(ssno, proj#,startdate)
Key: ssno,proj#
IND (Inclusion Dependencies):
workson[proj#] ⊆ project[proj#]
workson[ssno] ⊆ employee[ssno]
employee[ssno] ⊆ workson[ssno]
ICS122A/EECS116
Notes 04
26
Participation Constraints
An employee works on at least 1 project
participation!
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Relation: WorksOn(ssno,proj#,startdate)
Key: ssno
IND (Inclusion Dependencies):
workson[proj#] ⊆ project[proj#]
workson[ssno] ⊆ employee[ssno]
employee[ssno] ⊆ workson[ssno]
Notes 04
27
Multiple Constraints
An employee works on 1 and only 1 project
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Relation: WorksOn(ssno,proj#,startdate)
Key: ssno
IND (Inclusion Dependencies):
workson[proj#] ⊆ project[proj#]
workson[ssno] ⊆ employee[ssno]
employee[ssno] ⊆ workson[ssno]
project[proj#] ⊆ workson[proj#]
28
Multiple Constraints
An employee works on 1 and only 1 project
each project has an employee
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Relation: Employee(ssno,proj#,startdate, name, mgr, salary)
Key: ssno
Proj# fields must not be NULL
Notes 04
29
Combining Relations
An employee works on at most 1 project
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Multiway Relationships
Relation: WorksUsing(ssno,proj#,toolid, startdate)
Key: ssno,toolid, proj#
IND: worksusing[proj#] ⊆ project[proj#]
worksusing[ssno] ⊆ employee[ssno]
worksusing[toolid] ⊆ tools[toolid]
Notes 04
30
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Tool
toolid
spec
Multiway Relationships
Relation: WorksUsing(ssno,proj#,toolid, startdate)
Key: ssno,toolid
IND: worksusing[proj#] ⊆ project[proj#]
worksusing[ssno] ⊆ employee[ssno]
worksusing[toolid] ⊆ tools[toolid]
Notes 04
31
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Tool
toolid
spec
Multiway Relationships
Relation: WorksUsing(ssno,proj#,toolid, startdate)
Key: ssno,toolid
IND: worksusing[proj#] ⊆ project[proj#]
worksusing[ssno] ⊆ employee[ssno]
worksusing[toolid] ⊆ tools[toolid]
employee[ssno] ⊆ worksusing[ssno]
Notes 04
32
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Tool
toolid
spec
Multiway Relationships
Relation: WorksUsing(ssno,proj#,toolid, startdate)
Key: ssno,toolid OR ssno,proj#
IND: worksusing[proj#] ⊆ project[proj#]
worksusing[ssno] ⊆ employee[ssno]
worksusing[toolid] ⊆ tools[toolid]
employee[ssno] ⊆ worksusing[ssno]
Notes 04
33
WorksOn
startdate
Employee
ssno
name
salary
Project
proj#
manager
Tool
toolid
spec
Mapping 1:
Relations: (key: ssno for all relations)
staff(ssno, name, salary,position)
faculty(ssno, name, salary, rank)
studentassistant(ssno, name, salary, percentage_time)
Mapping 2:
Relations: (key: ssno for all relations)
Employee(ssno, name, salary)
staff(ssno,position)
faculty(ssno,, rank)
studentassistant(ssno, percentage_time)
Notes 04
34
Class Subclass Relationships
ISA
Employee
ssno
name
salary
Staff
position
Faculty
rank
Student assistant
percentage-time
Mapping 3 (ssno is key)
Relation: employee(ssno, name, salary, jobtype, position, rank, percentage_time)
Mapping 4: (ssno is key)
employee(ssno, name, salary IsStaff, position, IsFaculty, rank, IsStudentAssistant, percentage_time
)
Notes 04
35
Class Subclass Relationships
ISA
Employee
ssno
name
salary
Staff
position
Faculty
rank
Student assistant
percentage-time
which Mapping to use
36
| Total | partial |
disjoint | | |
overlapping | | |
cannot use Mapping 1
cannot use Mapping 3
Relations:
staff(ssno, name, salary,position)
faculty(ssno, name, salary, rank)
studentassistant(ssno, name, salary, percentage_time)
Key: ssno for all the relations
Requires a union to construct all employees
Notes 04
37
Disjoint & Total
ISA
Employee
ssno
name
salary
Staff
position
Faculty
rank
Student assistant
percentage-time
d
Relation: employee(ssno, name, salary, jobtype, position, rank, percentage_time)
Key: ssno
ICS122A/EECS116
Notes 04
38
Disjoint & Partial
ISA
Employee
ssno
name
salary
Staff
position
Faculty
rank
Student assistant
percentage-time
d
Relations:
employee(ssno, name, salary)
staff(ssno, position)
faculty(ssno, rank)
studentassistant(ssno, percentage_time)
Key: ssno for all relations
IND: staff[ssno] ⊆ employee[ssno]
faculty[ssno] ⊆ employee[ssno]
studentassistant[ssno] ⊆ employee[ssno]
Note: To model total constraint requires us to state that union of staff, faculty and student assistant covers all employees. Cannot be said using IND but can be specified as a general constraints.
ICS122A/EECS116
Notes 04
39
ISA
staff
faculty
student assistant
Overlapping
employee
ssno
salary
name
position
rank
Percentage
Time
Relation:
employee(ssno, name, salary, IsStaff, position, IsFaculty, rank, IsStudentAssistant, percentage_time
)
Key: ssno
ICS122A/EECS116
Notes 04
40
ISA
Overlapping: Alternative Design
Employee
ssno
name
salary
Staff
position
Faculty
rank
Student assistant
percentage-time
Weak Entity Sets
Relation:
account(acct#,customer, balance)
transaction(acct#,trans#, amount)
Key of Transaction: acct#, trans#
IND: transaction[acct#] ⊆ account[acct#]
No relation for the weak relationship set “Log.”
ICS122A/EECS116
Notes 04
41
log
Account
acct#
customer
balance
Transaction
trans#
amount
Weak Entity Sets (cont.)
Other relationships of the weak entity set (transaction) should use the complete key as its key
Relation:
ATM(id, address) Key: “id”
use(acct#,trans#, id, time) Key: acct#, trans#, id
IND: use[acct#,trans#] ⊆ transaction[acct#,trans#]
use[id] ⊆ ATM[id]
ICS122A/EECS116
Notes 04
42
use
time
log
Account
acct#
customer
balance
Transaction
trans#
amount
ATM
id
address
Aggregation
Relation:
using(id, pid, tid) Key: id, pid, tid
Q: What other relations are there in the overall schema?
A: employees, projects, tools and work (in addition to using)
ICS122A/EECS116
Notes 04
43
work
using
stime
Employees
id
Projects
pid
Tools
tid
Exercise (I) - Question
ICS122A/EECS116
Notes 04
44
Translate the above E/R diagram to relations, using the "E/R" approach to handling ISA hierarchies. (see next slide for answer)
Isa a partial and overlapping relationship
Exercise (I) - Answer
ICS122A/EECS116
Notes 04
45
c in this case. KEY: {a,c}
SQL
Preview to help you define schemas and insert/delete data
SQL as Data Definition Language
Domain Types
Create/Drop Table Command
Create table r (
A1 D1 [not null] [default V1]
A2 D2 [not null] [default V2]
…
An Dn [not null] [default Vn]
<integrity constraint 1>
<integrity constraint 2>
…
<integrity constraint k>
)
Drop table r
Example script - Create
CREATE TABLE designer (
id INTEGER,
name VARCHAR(1000),
PRIMARY KEY (id)
);
CREATE TABLE blueprint (
name VARCHAR(255),
designer INTEGER,
PRIMARY KEY (name),
FOREIGN KEY (designer) REFERENCES designer(id)
);
50
Example script - Insert
INSERT INTO designer (id, name) VALUES
(1, 'Art'),
(2, 'Taylor');
-- To see what we inserted:
SELECT * FROM designer;
INSERT INTO blueprint (name, designer) VALUES
('Donald Bren Hall', 1),
('Engineering Hall', 2);
SELECT * FROM blueprint;
51
More Inserts
INSERT INTO designer (id, name) VALUES (1, 'Cecelia');
SELECT * FROM designer;
INSERT INTO designer (id, name) VALUES (NULL, 'Cecelia');
SELECT * FROM designer;
INSERT INTO blueprint (name, designer) VALUES ('Steinhaus Hall', 50);
SELECT * FROM blueprint;
52
Example script - Constraints at work
-- Violates primary key constraint - id 1 already exists
INSERT INTO designer (id, name) VALUES (1, 'Cecelia');
SELECT * FROM designer;
-- Violates entity identity constraint - primary key cannot be NULL
INSERT INTO designer (id, name) VALUES (NULL, 'Cecelia');
SELECT * FROM designer;
-- Violates referential integrity - foreign key designer 50 does not exist yet
INSERT INTO blueprint (name, designer) VALUES ('Steinhaus Hall', 50);
SELECT * FROM blueprint;
53
Deletions
-- Fails since the default action on delete/update is reject (i.e., abort):
DELETE FROM designer WHERE id = 1;
SELECT * FROM designer;
SELECT * FROM blueprint;
Factoid about SQL: there is no way to delete only a single occurrence of a tuple that appears twice in a relation.
Data Modifications and Constraints
55
55
Example: Delete
56
56
Takes
CourseOfferings
Integrity Maintenance Policies
57
57
Takes
CourseOfferings
deleted
Example: Update Referenced Table
58
58
Takes
CourseOfferings
Example: Update Referencing Table
59
59
Takes
CourseOfferings
Recall the table and delete that failed…
CREATE TABLE designer (
id INTEGER,
name VARCHAR(1000),
PRIMARY KEY (id)
);
CREATE TABLE blueprint (
name VARCHAR(255),
designer INTEGER,
PRIMARY KEY (name),
FOREIGN KEY (designer) REFERENCES designer(id)
);
-- Fails since the default action on delete/update is reject (i.e., abort):
DELETE FROM designer WHERE id = 1;
SELECT * FROM designer;
SELECT * FROM blueprint;
Factoid about SQL: there is no way to delete only a single occurrence of a tuple that appears twice in a relation.
60
Example script - Change “on delete” action
-- To change the “on delete” action, we need to recreate the foreign key constraint
ALTER TABLE blueprint DROP CONSTRAINT blueprint_ibfk_1;
ALTER TABLE blueprint
ADD CONSTRAINT blueprint_ibfk_1
FOREIGN KEY (designer)
REFERENCES designer(id) ON DELETE CASCADE;
-- Now deletion succeeds and deletes a row in both tables
-- DELETE FROM designer WHERE id = 1
SELECT * FROM designer;
SELECT * FROM blueprint;
61
Example script - drop
-- While dropping tables, blueprint has to be dropped first due to foreign key constraint
-- DROP TABLE blueprint;
-- DROP TABLE designer;
62
Indexes
Suppose we have a relation
Person (name, social security number, age, city)
An index on “social security number” enables us to fetch a tuple
for a given ssn efficiently (not have to scan the whole relation).
The problem of deciding which indexes to put on the relations is
called : physical database design
Creating Indexes
CREATE INDEX ssnIndex ON Person(social-security-number)
Indexes can be created on more than one attribute:
CREATE INDEX doubleindex ON Person (name, social-security-number)