03 Relational Model
03 Relational Model
SCS 15-415
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
Faloutsos 15-415
Faloutsos
SCS 15-415
Faloutsos 15-415
Faloutsos 15-415
Faloutsos 15-415
Faloutsos
SCS 15-415
Cardinality = 3, arity = 5 ,
all rows distinct
Q: do values in a column need to be
distinct?
Faloutsos 15-415
Faloutsos 15-415
Faloutsos
SCS 15-415
SQL Overview
CREATE TABLE <name> ( <field>
<domain>, )
INSERT INTO <name> (<field
names>)
VALUES (<field values>)
DELETE FROM <name>
WHERE <condition>
Faloutsos 15-415
10
SQL Overview
UPDATE <name>
SET <field name> =
<value>
WHERE <condition>
SELECT <fields>
FROM <name>
WHERE <condition>
Faloutsos 15-415
11
12
Faloutsos
SCS 15-415
Faloutsos 15-415
13
Faloutsos 15-415
14
Faloutsos 15-415
15
Faloutsos
SCS 15-415
Faloutsos 15-415
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
17
Faloutsos 15-415
Keys
Keys help associate tuples in different
relations
Keys are one form of integrity constraint
(IC)
Enrolled
Faloutsos 15-415
Students
18
Faloutsos
SCS 15-415
Keys
Keys help associate tuples in different
relations
Keys are one form of integrity constraint
(IC)
Enrolled
FOREIGN Key
Students
PRIMARY Key
Faloutsos 15-415
19
Primary Keys
A set of fields is a superkey if:
No two distinct tuples can have same
values in all key fields
A set of fields is a key for a relation if :
minimal superkey
Faloutsos 15-415
20
Primary Keys
what if >1 key for a relation?
Faloutsos 15-415
21
Faloutsos
SCS 15-415
Primary Keys
what if >1 key for a relation?
one of the keys is chosen (by DBA) to be
the primary key. Other keys are
called candidate keys..
Q: example?
Faloutsos 15-415
22
Primary Keys
E.g.
sid is a key for Students.
What about name?
The set {sid, gpa} is a superkey.
Faloutsos 15-415
23
Faloutsos 15-415
24
Faloutsos
SCS 15-415
25
Faloutsos 15-415
26
Faloutsos 15-415
27
Faloutsos
SCS 15-415
Foreign Keys
Enrolled
Students
28
Faloutsos 15-415
29
Faloutsos 15-415
Enrolled
Faloutsos 15-415
Students
30
10
Faloutsos
SCS 15-415
Enrolled
Students
31
Faloutsos 15-415
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
32
Faloutsos 15-415
Enrolled
Faloutsos 15-415
Students
33
11
Faloutsos
SCS 15-415
34
Faloutsos 15-415
Enrolled
Students
Faloutsos 15-415
35
36
12
Faloutsos
SCS 15-415
Faloutsos 15-415
37
Faloutsos 15-415
38
Faloutsos 15-415
39
13
Faloutsos
SCS 15-415
Faloutsos 15-415
40
Faloutsos 15-415
41
Faloutsos 15-415
42
14
Faloutsos
SCS 15-415
Faloutsos 15-415
43
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
Faloutsos 15-415
44
ER to tables outline:
strong entities
weak entities
(binary) relationships
1-to-1, 1-to-many, etc
total/partial participation
ternary relationships
ISA-hierarchies
aggregation
Faloutsos 15-415
45
15
Faloutsos
SCS 15-415
name
lot
Employees
46
Faloutsos 15-415
name
lot
Employees
Faloutsos 15-415
dname
lot
Employees
Faloutsos 15-415
did
Works_In
budget
Departments
48
16
Faloutsos
SCS 15-415
dname
lot
Employees
budget
did
Works_In
Departments
49
Faloutsos 15-415
key of many-to-many
relationships:
Keys from participating
entity sets (as foreign
keys).
50
Faloutsos 15-415
since
name
ssn
dname
lot
Employees
Faloutsos 15-415
did
Manages
budget
Departments
51
17
Faloutsos
SCS 15-415
1-to-1
Many-to-1
1-to Many
Many-to-Many
52
Faloutsos 15-415
Faloutsos 15-415
name
ssn
dname
Employees
Faloutsos 15-415
did
lot
Manages
budget
Departments
54
18
Faloutsos
SCS 15-415
name
ssn
dname
Employees
budget
did
lot
Manages
Departments
CREATE TABLE
Departments(
did
INTEGER),
dname
CHAR(20),
budget REAL,
PRIMARY KEY (did),
)
Two-table-solution
55
Faloutsos 15-415
name
ssn
dname
Employees
budget
did
lot
Manages
Departments
Single-table-solution
56
Faloutsos 15-415
name
ssn
dname
did
lot
Employees
Manages
budget
Departments
CREATE
ssn
did
since
TABLE Manages(
CREATE TABLE Dept_Mgr(
CHAR(11),
ssn
CHAR(11),
INTEGER,
did
INTEGER,
Vs. since DATE,
DATE,
dname CHAR(20),
budget REAL,
PRIMARY KEY (did),
PRIMARY KEY (did),
FOREIGN KEY (ssn)
FOREIGN KEY (ssn)
REFERENCES Employees,
REFERENCES Employees)
FOREIGN KEY (did)
REFERENCES Departments)
Faloutsos 15-415
57
19
Faloutsos
SCS 15-415
58
Faloutsos 15-415
Drill:
What if the toy department has no
manager (yet) ?
59
ER to tables outline:
strong entities
weak entities
(binary) relationships
1-to-1, 1-to-many, etc
total/partial participation
ternary relationships
ISA-hierarchies
aggregation
Faloutsos 15-415
60
20
Faloutsos
SCS 15-415
name
ssn
Employees
dname
did
lot
Manages
budget
Departments
Works_In
Faloutsos 15-415
since
61
62
63
21
Faloutsos
SCS 15-415
Faloutsos 15-415
ER to tables outline:
strong entities
weak entities
(binary) relationships
1-to-1, 1-to-many, etc
total/partial participation
ternary relationships
ISA-hierarchies
aggregation
65
Faloutsos 15-415
lot
Employees
Faloutsos 15-415
cost
Policy
dname
age
Dependents
66
22
Faloutsos
SCS 15-415
name
ssn
lot
Employees
cost
Policy
dname
age
Dependents
Faloutsos 15-415
67
68
69
23
Faloutsos
SCS 15-415
ER to tables outline:
strong entities
weak entities
(binary) relationships
1-to-1, 1-to-many, etc
total/partial participation
ternary relationships
ISA-hierarchies
aggregation
70
Faloutsos 15-415
name
ssn
lot
Employees
hours_worked
ISA
Hourly_Emps
contractid
Contract_Emps
71
Faloutsos 15-415
Drill:
What would you do?
name
ssn
lot
Employees
hourly_wages
hours_worked
ISA
Hourly_Emps
Faloutsos 15-415
contractid
Contract_Emps
72
24
Faloutsos
SCS 15-415
H_EMP(ssn,
h_wg, h_wk)
CONTR(ssn, cid)
73
Faloutsos 15-415
74
ER to tables outline:
strong entities
weak entities
(binary) relationships
1-to-1, 1-to-many, etc
total/partial participation
ternary relationships
ISA-hierarchies
aggregation
Faloutsos 15-415
75
25
Faloutsos
SCS 15-415
Faloutsos 15-415
76
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
Faloutsos 15-415
77
Views
Virtual tables
CREATE VIEW YoungActiveStudents
(name,grade)
AS SELECT S.name, E.grade
FROM Students S, Enrolled E
WHERE S.sid=E.sid and S.age<21
DROP VIEW
Faloutsos 15-415
78
26
Faloutsos
SCS 15-415
Faloutsos 15-415
79
Outline
Introduction
Integrity constraints (IC)
Enforcing IC
Querying Relational Data
ER to tables
Intro to Views
Destroying/altering tables
Faloutsos 15-415
80
Table changes
DROP TABLE
ALTER TABLE, e.g.
ALTER TABLE students
ADD COLUMN maiden-name CHAR(10)
Faloutsos 15-415
81
27
Faloutsos
SCS 15-415
Faloutsos 15-415
82
83
84
28