4 DB Relmod
4 DB Relmod
4 DB Relmod
Database Systems
Werner Nutt
1
4. The Relational Data Model
4.1 Schemas
1. Schemas
2. Instances
3. Integrity Constraints
2
Different Schemas are Based
on Different Concepts
Conceptual
Conceptual Logical
Logical Physical
Physical
Schema
Schema Schema
Schema Schema
Schema
3
A Table
Table name
Column names
Product:
Name Price Category Manufacturer
4
Rows
Review of Mathematical Concepts (1)
• Sets: S, T, S1, ..., Sn, T1, ..., Tn, { }
Cardinality of a set S denoted as |S|
• Relation R over S, T:
subset of S × T, written R ⊆ S × T
We write (s,t)∈R or, equivalently, sRt
5
Review of Mathematical Concepts (2)
• Relation R over S1, ..., Sn:
subset R ⊆ S1 × ... × Sn
The number n is the arity of R
(R is binary if n=2 and ternary if n=3)
6
Review of Mathematical Concepts (3)
• Partial function f from S to T, written f: S ∼→ T
associates to some element s∈S
exactly one element of T, still denoted as f(s)
We write f(s) = ⊥ (read “bottom”)
if f does not associate any element of T to s
In other words:
every element of Si occurs in some tuple of R
7
Review of Mathematical Concepts (4)
• A relation R over S and T is functional in its
first argument if
sRt1 and sRt2 implies that t1= t2
for all s∈S, t1, t2 ∈T.
In other words, for every s∈S,
there is at most one t∈T related by R to s
8
How Many … ?
Consider sets
S, T with |S| = N and |T| = M
S1, ..., Sn with |Si| = Ni
9
How Many … ? (Cntd.)
10
Tables Look Like Relations …
A1 A2 A3 ... An
C Attributes {(a1, a2, a3, …, an),
a a1 a2 a3 an (b1, b2, a3, …, cn),
r
d b1 b2 a3 cn (a1, c3, b3, …, bn),
i Tuple
n a1 c3 b3 bn
...
a . (x1, v2, d3, …, wn)}
l .
i . Component
t
y x1 v2 d3 wn
Over which sets does
this relation range?
Arity
12
Types and Domains
Type: Class of atomic values, e.g.,
• integers, reals, strings
• integers between 15 and 80,
strings of (up to) 50 characters
4.2 Instances
1. Schemas
2. Instances
3. Integrity Constraints
14
Relation Schema and Instance
• A tuple of values (v1, ..., vn ) satisfies
the relation schema R(A1:D1, ..., An:Dn) if vi ∈ Di (i=1,…n)
• Important distinction:
– Relation Schema = stable over long periods of time
– Relation Instance = changes constantly, as data is
inserted/updated/deleted
15
Example
Domain declaration:
Name=String(30), DollarPrice=Decimal (10,2),
Relation schema:
Product(Prodname: Name, Price: DollarPrice,
Category: Name, Manufacturer: Name)
Instance:
Prodname Price Category Manufacturer
Important distinction:
– Database Schema = stable over long periods of time
– Database Instance = changes constantly
17
Updates
A database reflects the state of an aspect of the real world:
The world changes Î the database has to change
Updates to an instance:
1) adding a tuple
2) deleting a tuple
3) modifying an attribute of a tuple
Student
studno name hons tutor year thesis title
s1 jones ca bush 2 null
s2 brown cis kahn 2 null
s3 smith null goble 2 null
s4 bloggs ca goble 1 null
s5 jones cs zobel 1 null
s6 peters ca kahn 3 “A CS Survey”
19
Order and Duplication
In tables:
• Order of attributes is fixed
• Order of rows is fixed (i.e., tables with different order
of rows are different)
• Duplicate rows matter
In mathematical relations:
• Order of tuples and duplicate tuples do not matter
• Order of attributes is still fixed
Question:
Can we model relations so that we get rid of
attribute order?
20
Reminder: Relations as Subsets
of Cartesian Products
21
Alternative Definition:
Relations as Sets of Functions
• Fix the set A of attributes, e.g.
A = {Name, Price, Category, Manufacturer}
• A tuple is a function t: A → D
{Prodname
{Prodname gizmo,
gizmo,
• E.g. Price 19.99, This is the model
Price 19.99,
Category
Category gadgets,
gadgets, underlying SQL
Manufacturer
Manufacturer GizmoWorks}
GizmoWorks}
23
Two Definitions of Relations
24
Why Relations?
• Often a good match for the way we think about our data
25
4. The Relational Data Model
1. Schemas
2. Instances
3. Integrity Constraints
26
Integrity Constraints
Ideal: DB instance reflects the real world
In real life: This is not always the case
Goal: Find out, when DB is out of sync
Observation:
Not all mathematically possible instances make sense
Idea:
• Formulate conditions that hold for all plausible instances
• Check whether the condition holds after an update
27
Common Types of Integrity Constraints
• Functional Dependencies (FDs)
– “Employees in the same department have the same boss”
• Domain Constraints
– “No employee is younger than 15 or older than 80”
We read:
– “Dept functionally determines DeptHead”, or
– “Name, Dept, and DeptHead functionally depend on taxCode”
29
Functional Dependencies
DB relation R.
A functional dependency on R is an expression
A1,...,Am → B1, ...,Bn
where A1,...,Am and B1, ...,Bn are attributes of R.
t1[A1,...,Am] = t2[A1,...,Am]
implies
t1[B1, ...,Bn] = t2[B1, ...,Bn]
32
FDs: Example (Cntd.)
33
FDs, Superkeys and Keys
Person (SSN, Name, DOB)
SSN → Name, DOB
36
Foreign Keys
A set of attributes in a relation that exactly matches the
primary key in another relation
– the names of the attributes don’t have to be the same
but must be of the same domain
Notation:
FK1: Student (tutor, tutorroom) references Staff (lecturer, roomno)
FK2: Staff (appraiser, approom) references Staff (lecturer, roomno)
37
Satisfaction of Foreign Key Constraints
“FK: R(A) references S(B)”
To keep things
is satisfied by an instance of R and S if
simple, we assume
for every t1 in R there is a t2 in S such that that “lecturer” alone
t1[A] = t2[B], is the primary kay
provided t1[A] is not null of Staff
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
Foreign key constraints are also called
38
“referential integrity constraints.”
Updates May Violate Constraints …
Updates are
Questions:
• What can go wrong?
• How should the DBMS react?
39
Insertions (1)
If the following tuple is inserted into Student,
what should happen? Why?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
40
Insertions (2)
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
41
Insertions (3)
If the following tuple is inserted into Student,
what should happen? Why?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
42
Insertions (4)
If the following tuple is inserted into Student,
what should happen? Why?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
43
Deletions (1)
If the following tuple is deleted from Student,
is there a problem? And what should happen?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
44
Deletions (2)
And if this one is deleted from Staff ?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
45
Modifications (1)
What if we change in Student
(s1, jones, ca, bush, 2)
to
(s1, jones, ca, watson, 2) ?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
46
Modifications (2)
And what if we change in Student
(s2, brown, cis, kahn, 2)
to
(s1, jones, ca, bloggs, 2) ?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
47
Modifications (3)
And what if we change in Staff
(lindsey, 2.10, woods)
to
(lindsay, 2.10, woods) ?
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
48
Modifications (4)
Now, let’s change in Staff
(goble, 2.82, capon)
to
(gobel, 2.82, capon) …
Student Staff
studno name hons tutor year lecturer roomno appraiser
s1 jones ca bush 2 kahn IT206 watson
s2 brown cis kahn 2 bush 2.26 capon
s3 smith cs goble 2 goble 2.82 capon
s4 bloggs ca goble 1 zobel 2.34 watson
s5 jones cs zobel 1 watson IT212 barringer
s6 peters ca kahn 3 woods IT204 barringer
capon A14 watson
lindsey 2.10 woods
barringer 2.125 null
49
Summary: Reactions to Integrity Violations
If an update violates an IC, the DBMS can
50
Summary
• The relational model is based on concepts from set theory
(and logic)
• It formalises the concept of a table
• Distinguish:
– relation schema: relation name, attributes,
domains/types
– relation instance: relation over domains of attributes
• Two formalisations of tuples
– positional tuples vs. tuples as functions on attributes
• Integrity constraints: Domain cs, FDs, Keys, FKs
• Updates may violate ICs
⎯ and the DMBS has to react
51