Professional Documents
Culture Documents
03 Relational Model and ER Diagrams
03 Relational Model and ER Diagrams
◆ Two sets A and B are equal iff (if and only if) they have the
same elements
◆ In other words, for every x: x is an element of A iff (if and
only if) x is an element of B
◆ In still different way: A and B are equal iff for every
possible x, the questions “is x in A” and “is x in B” give the
same answer
• Note there is no discussion or even a way of saying how many
times x appears in a set, really either none or at least once
◆ “More mathematically,”
A = B means ∀ x { x ∈ A ↔ x ∈ B }
◆ Therefore, as sets: {2, 5, 3, 7} = {2, 7, 5, 3, 5, 3, 3}
◆ This reiterates what we have said previously
◆ Let’s verify
• A: all lower case letters in English
• B: all positive integers less than 100
• S(A,B) satisfies the condition that any two tuples that are equal on
A must also be equal on B
S A B
a 2
a 2
b 3
c 4
d 3
S A B S A B
a 2 a 56
a 56 a 2
b 2 b 2
a 56
a 2
a 56
◆ Since the positions in the tuple (1st, 2nd, etc.) are labeled
with the column headings, the following two relations
are equal (are really one relation written in two different
ways)
S A B S B A
a 2 56 a
a 56 2 a
b 2 2 b
56 a
2 a
56 a
A 121 2367 2 1 90
B 101 3498 4 2 80
C 106 2987 2 3 70
4 70
◆ We are told that any two tuples that are equal on both FN
and LN are (completely) equal
• We have some tuples appearing multiple times: this is just for
clarifying that this permitted in the definition, we do not discuss
here why we would have the same tuple more than one time (we
will talk about this later)
◆ This is a property of every possible instance of Person
in our application—we are told that
◆ Then (FN, LN) is a superkey of Person, and in fact a key,
because neither FN nor LN by themselves are sufficient
(we are told that too)
© 2014 Zvi M. Kedem 13
Keys (and Superkeys)
8 128
9 139
7 147
◆ We are told that for any instance of Pay, any two tuples
that are equal on Grade are (completely) equal
• Of course, if each Grade appears in only one tuple, this is
automatically true
◆ Then, similarly to before, Grade is a key
◆ What about Salary, is this a key also?
◆ No, because we are not told (that is, we are not
guaranteed) that any two tuples that are equal on Salary
are equal on Grade in every instance of Pay
© 2014 Zvi M. Kedem 14
Keys (and Superkeys)
◆ To summarize
◆ We will know later what this means exactly, but I did not
want to leave you with not quite a useful definition
Employee
Likes Born
Animal Country
◆ 5 Employees: ◆ 4 Countries
• 1 is Alice has Erica • US
and Frank, born in US, • IN has 1150
likes Horse and Cat
• CN has 1330
• 2 is Bob has Bob and
Frank, born in US, • RU
likes Cat
• 4 is Carol ◆ 4 Animals
• 5 is David, born in IN • Horse in Asia
• 6 is Bob, born in CN, • Wolf in Asia
likes Yak
• Cat in Africa
• Yak in Asia
• Zebra in Africa
US
IN 1150
CN 1330
RU
Horse Asia
Wolf Asia
Cat Africa
Yak Asia
Zebra Africa
4 Carol
5 David
6 Bob Frank
◆ Replace (incorrect)
Employee ID# Name Child Child
4 Carol
5 David
6 Bob Frank
By (correct)
1 Alice 1 Erica
2 Bob 1 Frank
4 Carol 2 Bob
5 David 2 Frank
6 Bob 6 Frank
◆ Replace (incorrect)
Employee ID# Name Child Child
4 Carol
5 David
6 Bob Frank
By (correct)
1 Alice 1 Erica
2 Bob 1 Frank
4 Carol 2 Bob
5 David 2 Frank
6 Bob 6 Frank
1 Alice 1 Erica
2 Bob 1 Frank
4 Carol 2 Bob
5 David 2 Frank
6 Bob 6 Frank
◆ Note:
• Every row of Child has a single value of a primary key of
Employee, so every row of Child “maps” to a single row of
Employee
• Every row of Employee has zero or more rows of Child mapped
into it
In other words, no constraint
1 Alice 1 Erica
2 Bob 1 Frank
4 Carol 2 Bob
5 David 2 Frank
6 Bob 6 Frank
◆ Another option
◆ Note names do not have to be the same for the mapping
to take place
◆ But you need to specify which column is foreign key
referring to what
• Here: Parent in Child is foreign key referencing Employee
we have
1 Alice US US
2 Bob IN IN 1150
4 Carol CN 1330
5 David IN RU
6 Bob CN
1 Alice US US
2 Bob IN IN 1150
4 Carol CN 1330
5 David IN RU
6 Bob CN
1 Alice US US
2 Bob IN IN 1150
4 Carol CN 1330
5 David IN RU
6 Bob CN
1 Alice US US
2 Bob IN IN 1150
4 Carol CN 1330
5 David IN RU
6 Bob CN
2 Bob Cat
4 Carol
5 David
6 Bob Yak
Horse Asia 1
Wolf Asia
Cat Africa 1 2
Yak Asia 6
Zebra Africa
1 Horse
1 Cat
2 Cat
6 Yak
0 ..*
Person Likes Country Every Person likes 0 or more Countries
1 ..*
Person Likes Country Every Person likes 1 or more Countries
0 .. 1
Person Likes Country Every Person likes 0 or 1 Countries
1 .. 1
Person Likes Country Every Person likes 1 Country
0 ..*
Person Likes Country Person Likes Country
1 ..*
Person Likes Country Person Likes Country
0 .. 1
Person Likes Country Person Likes Country
1 .. 1
Person Likes Country Person Likes Country
0 ..*
Person Likes Country Person Likes Country
0 .. 1
Person Likes Country Person Likes Country
1 .. 1
Person Likes Country Person Likes Country
US PK CName
IN 1150 Population
CN 1330
RU
◆ 0..1
◆ 0..*
◆ 1..1
◆ 1..*
One Side
A
1 3 6 8 9
Many Side
© 2014 Zvi M. Kedem 56
Pattern Of Lines
Population
Child1 Employee1
Animal1 Likes1
Born
Country Cname Population Born ID# CName Country
US 1 US PK,FK1 ID#
PK CName
IN 1150 2 IN FK2 CName
Population
CN 1330 5 IN
RU 6 CN
6 Frank 6 Bob
General case
◆ We have a relationship R among entity sets E1, E2, …, En,
with properties P1, P2, …, Pm
◆ Each Ei is implemented as a table
◆ We can always implement R as a table with foreign key
constraints referencing E1, E2, …, En, and with R also
storing properties P1, P2, …, Pm
1 Erica 1 Alice
1 Frank 2 Bob
2 Bob 4 Carol
2 Frank 5 David
6 Frank 6 Bob
Country
PK CName
Population
Child Employee
PK ID# PK ID#
PK Child
Name
CName
Animal Likes
PK Species PK ID#
PK Species
Discovered
Country
CName
Population
Employee
Child
ID#
ID# Name
Child CName
Animal Likes
Species ID#
Discovered Species
FN LN
Person
Automobile Likes
Date
Title Author
Monitors
Name Grade Took Taught 0..1
MaxSize
Horse Prereq
3..50
First Second
Sec#
Section 1..1 1..*
Offered Course
Year
C# Title Description
Semester
Monitors
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
FK3 Monitor
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Horse
PK Name
SS#
FN
LN
DOB
Horse
PK Name
Horse
PK Name
Horse
PK Name
Horse
PK Name
Car
PK VIN
Color
Horse
PK Name
Car
PK VIN
Color
FK1 Model
FK1 Year
Horse
PK Name
Car
PK VIN
FK2 ID#
Color
FK1 Model
FK1 Year
Date
Horse
PK Name
Car
PK VIN
FK2 ID#
Color
FK1 Model Student
FK1 Year
Date PK,FK1 ID#
Horse
PK Name
Car
PK VIN
FK2 ID#
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Salary
Horse
PK Name
Car
PK VIN
FK2 ID#
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Salary
Horse
PK Name
Course
PK C#
Title
Description
Car
PK VIN
FK2 ID#
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Prereq
Salary
PK,FK2 First
PK,FK1 Second
Horse
PK Name
Course
PK C#
Title
Description
Book
Car
PK Author
PK VIN PK Title
FK2 ID#
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Prereq
Salary
PK,FK2 First
PK,FK1 Second
Horse
PK Name
Course
PK C#
Title
Description
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Prereq
Salary
PK,FK2 First
PK,FK1 Second
Horse
PK Name
Course
PK C#
Title
Description
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Prereq
Salary
PK,FK2 First
PK,FK1 Second
Horse
PK Name
Section Course
PK,FK1 C# PK C#
PK Year
PK Semester Title
PK Sec# Description
MaxSize
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
FK3 Monitor
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
FK3 Monitor
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Book Required
Car
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# PK,FK3 Title
Color
FK1 Model Student Professor
FK1 Year
Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary
PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
FK3 Monitor
Took
PK,FK1 ID# Section Course
PK,FK2 C#
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Book Required
Car *
PK Author PK,FK1 ID#
PK VIN PK Title PK,FK2 C#
PK,FK3 Author
FK2 ID# * PK,FK3 Title
Color
FK1 Model Student Professor *
FK1 Year
* Date PK,FK1 ID# PK,FK1 ID#
Taught Prereq
Salary *
* PK,FK2 ID# PK,FK2 First
PK,FK1 C# PK,FK1 Second
PK,FK1 Year
Horse PK,FK1 Semester
PK Name PK,FK1 Sec#
* *
FK3 Monitor
*
Took
* 1..*
PK,FK1 ID# Section Course
PK,FK2 C# 3..50
PK,FK2 Year PK,FK1 C# PK C#
PK,FK2 Semester PK Year
PK,FK2 Sec# PK Semester Title
PK Sec# Description
Grade
MaxSize
Grade Grade
Prereq Prereq
First Second First Second
Course Course1
Prereq
PK,FK2 First
PK,FK1 Second
Grade
Course1
Course PK C#
PK C# Title
Description
Title FK1 Prereq
Description Grade
5 1
7 2
5 1
7 2
◆ Example: Person
◆ Create a table for the entity without multivalued and
derived attributes, flattening composite attributes
The primary key of this table will consist of the attributes
serving as primary key of the entity
Example table: Person
◆ If there is a derived attribute, describe how it is computed,
but do not store it
◆ If there is a multivalued attribute, create a table for it
consisting of it and attributes of the primary key of the
entity; do not put it in the table for the entity
Example table: Child
The primary key of this table will consist of all its attributes
◆ Sets
◆ Relations and tables
◆ Relational schema
◆ Primary keys
◆ Implementing an ER diagram as a relational schema
(relational database)
◆ General implementation of strong entities
◆ Handling attributes of different types
◆ General implementation of relationships
◆ Possible special implementation of binary many-to-one
relationships
◆ Implementation of ISA
◆ Implementation of weak entities
◆ Foreign keys
◆ Primary key / foreign key constraints inducing many-to-
one relationships between tables
◆ Concept of referential integrity
◆ Crow’s feet notation: ends of lines
◆ Crow’s feet notation: pattern of lines