Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Learning Goals
given an ER model of an application, design a minimum
Unit 3
Historical Perspective
Introduced by Edgar Codd (IBM) in 1970
Most widely used model today.
Unit 3
Historical Perspective
Competitor: object-oriented model
Unit 3
language
Lots of bells and whistles to do complicated things
Unit 3
Unit 3
relations
Unit 3
attribute/
column name/
field
tuple/
row/
record
sid
99111120
92001200
94001020
94001150
Unit 3
name
address
1234 W. 12th
G. Jones
Ave., Van.
2020 E. 18th St.,
G. Smith
Van
2020 E. 18th St.,
A. Smith
Van
Campeau
null
J.
domain
value
phone major
889-4444
CPSC
409-2222
MATH
222-2222
CPSC
null
null
column/
active
domain of
attribute
address
8
Example continued
Cardinality = #rows = #tuples =
#records =4
degree/arity = width = #columns = 5
all rows distinct
Order of rows isnt important
Order of fields logically speaking not
important, but may be important for
some query languages
9
Formal Structure
Unit 3
10 0
Formal Structure
Unit 3
11
A Mini-example
Consider relation Appointment(prof, dept).
profs
depts
12
sid
name
address
1234 W. 12th
99111120 G. Jones
Ave., Van.
2020 E. 18th St.,
92001200 G. Smith
Van
2020 E. 18th St.,
94001020 A. Smith
Van
94001150 S. Wang
null
phone major
889-4444
CPSC
409-2222
MATH
222-2222
CPSC
null
null
Unit 3
14
Unit 3
SQL-86
SQL-89 (minor revision)
SQL-92 (major revision, current standard)
SQL-99 (major extensions)
15
sid
name
address
phone major
1234 W. 12th
889-4444 CPSC
99111120 G. Jones
Ave., Van.
2020 E. 18th St.,
409-2222 MATH
92001200 G. Smith
Van
2020 E. 18th St.,
222-2222 CPSC
94001020 A. Smith
Van
To find the ids, names and phones of all CPSC students, we can write:
SELECT
FROM
WHERE
sid
name
phone
99111120 G. Jones
889-4444
94001020 A. Smith
222-2222
with SELECT *
Unit 3
16
Grade
sid
dept
CPSC
99111120 CPSC
course# mark
122
80
Unit 3
Unit 3
18
19
DROP TABLE
Student
Unit 3
REAL;
name = Smith):
DELETE
FROM
WHERE
Student
name = Smith
21
specified ICs
Unit 3
22
ICs
We will several different kinds of ICs for relational DBs,
including
Domain constraints: e.g., name must belong to char(40); salary
must be float.
Key constraints: same meaning as in ER model.
Functional Dependencies: generalization of key constraints.
Foreign Key constraints.
cannot be negative.
Unit 3
23
Unit 3
24
key
values for primary key must be unique
primary key attributes cannot be null
Unit 3
25 1a
keys.
One of them is declared to be the primary key.
All candidate keys, whenever not null, must be unique.
Primary key cannot ever be null.
Where do all these keys come from? IOW, how can we tell
which are the (candidate) keys of a table?
For now, just based on intuition and our understanding of an
application and of the meaning of attributes.
In reality, (some) design specs are translated into special integrity
constraints called functional dependencies, from which keys are
derived.
Unit 3
26
27 2
e.g.:
Unit 3
28
29
Some Examples
You cant rate a song that doesnt exist, i.e., is not
listed in the Songs table, say.
There cant be a grade for something that is not a course nor for
someone who isnt a registered bona fide student.
You cant borrow something from a library that is not a publication,
nor return it.
List your examples here.
Unit 3
30
name
G. Jones
J. Smith
G. Smith
address
.
.
.
Phone
major
sid
53666
53666
53650
53666
31 3
Unit 3
32
deleted?
Also delete all Grade tuples that refer to it?
Disallow deletion of this particular Student tuple?
Set sid in Grade tuples that refer to it, to a default sid.
Set sid in Grade tuples that refer to it, to null, (the
special value denoting `unknown or `inapplicable.)
o problem if sid is part of the primary key
33
Unit 3
ON DELETE CASCADE
ON UPDATE CASCADE
FOREIGN KEY (dept, course#)
REFERENCES Course
ON DELETE SET DEFAULT
ON UPDATE CASCADE );
34
35
36
Unit 3
Name
Phone
Address
John Doe
123-4567
Peter Bloe
765-4321
Jane Doe
234-5678
conceptual design.
Some constraints can be explicitly specified in the
conceptual model
Primary Key and foreign key ICs are shown on ER
diagrams.
Unit 3
38
tuples.
schema vs instance.
(attribute) domain constraints.
key constraints and foreign key constraints.
(candidate) keys, primary key.
creating tables in SQL/DDL, with both names and types as
well as key and foreign key constraints.
39
40
sin
name
Employee
Unit 3
salary
name
sin
salary
Employee
dname
did
Works_In
Unit 3
budget
Department
roles:
no
dept
title
Course
course
prereq
Prerequisite
course_no,
prereq_dept, prereq_no),
FOREIGN KEY (course_dept, course_no)
REFERENCES Course(dept, course#),
FOREIGN KEY (prereq_dept, prereq_no)
REFERENCES Course(dept, course#))
43
dname
salary
Employee
did
Manages
budget
Department
Each dept has at most one manager, according to the key constraint
on Manages.
How should we represent Manages?
Unit 3
44
Unit 3
45 4
46 4
47 4
did
dname
budget
sin
since
INTEGER,
CHAR(20),
REAL,
CHAR(11),
DATE,
PRIMARY KEY (did),
FOREIGN KEY (sin) REFERENCES Employee)
Unit 3
48 4
name
sin
salary
Employee
dname
did
Manages
budget
Department
50
Using the right method for Manages (add Manages relation in the Department table),
we can capture participation constraints by
ensuring that each did is associated with a sin that is not null
not allowing to delete a manager before he/she is replaced
Note: We cannot express this easily if the previous method was used for
Unit 3
Manages.
51
name
sin
salary
Employee
dname
did
Works-In
budget
Department
Unit 3
52
salary
Employee
allowance
Policy
pname
birthday
Dependent
(strong) entity.
Owner entity set and weak entity set participate in a one-to-many identifying
relationship set.
Weak entity set has total participation.
Unit 3
53
Weak entity set and its identifying relationship set are translated into a single table.
Primary key would consist of the owners primary key and week entitys partial
key
When the owner entity is deleted, all owned weak entities must also be
deleted.
birthday DATE,
allowance REAL,
sin
CHAR(11) NOT NULL,
PRIMARY KEY (sin, pname),
FOREIGN KEY (sin) REFERENCES Employees,
Unit 3
ON DELETE CASCADE,
ON UPDATE CASCADE)
Could ON
DELETE
NO ACTION
ever make
sense for this
case?
54 5
salary
Employee
hourly_rate
hours
contractid
Hourly_Emp
Contract_Emp
Unit 3
55
salary
Employee
hourly_rate
hours
contractid
Hourly_Emp
lots of
duplication
Not a good
answer!
Contract_Emp
56
salary
Employee
hourly_rate
hours
contractid
Hourly_Emp
Contract_Emp
Wastes a lot of
One table which has all attributes and a type field which can have values
57
sin
salary
Employee
hourly_rate
hours
contractid
Hourly_Emp
Contract_Emp
subclass attributes
Employee(sin, name, salary)
Hourly_Emp(sin, hourly_rate, hours)
Contract_Emp(sin, contractid)
Unit 3
58
salary
Employee
hourly_rate
hours
contractid
Hourly_Emp
Contract_Emp
If ISA-relation is partial, it
cannot be applied (may
loose entities)
Works poorly if the
superclass participates in
any relationship ( should
not be applied in that case)
If ISA-relation is not
disjoint, it duplicates info
59
Unit 3
60 6
Translating Aggregation
sin
name
Person
bname
Visits
type
Bar
relationship sets
Tables for our example :
Visits(sin, bname, frequent?)
Drinks(sin, bname, dname, date)
date
Drinks
Drink
dname
Special Case:
If Drinks is total on Visits and
Visits has no descriptive attributes
we could keep only the Drinks table
(discard Visits).
Unit 3
61
Views
A view is just a virtual table, often defined using a query: Normally, we only
store the views definition, rather than the rows in the view querys result.
title, mark) AS
62
Unit 3
63
View Updates
View updates must occur at the base tables.
Ambiguous
Difficult
Unit 3
64 7
TGDs.
Unit 3
65