RELATIONAL DATA STRUCTURES
Chapter 3 – part I
Relational Model Concepts
⚫ The concept of a Relation
◦ The strength of the relational approach to data management comes from the formal foundation provided by the theory of
relations
◦ A Relation is a mathematical concept
based on the ideas of sets
How is a Relational DB different from a File?
File Table Relational DB
File Table Relation
Record Row Tuple
Field Column Attribute
Records may be different types.
Field order matters Record order may
matter
No primary key or unique identifier field
Informal Definitions
⚫ A relation looks like a table of values.
◦ Tuple – one row of a relation
◦ Attribute – one column of a relation
◦ Relation – the whole table
◦ Domain of an Attribute – all the values that attribute can have
Informal Definitions
⚫ Key of a Relation:
◦ Each row has a value of a data item (or set of items) that uniquely identifies that row in the table
● Called the key
◦ Sometimes row-ids or sequential numbers are assigned as keys to identify the rows in a table
● Called artificial key or surrogate key
Informal Formal Translation
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Values Domain
Row Tuple
Table Definition Schema of a Relation
Populated Table State of the Relation
FORMAL TERMS
Domain
⚫ All the possible values an attribute can take
◦ Remember your mathematics?
⚫ A set of Atomic Values
⚫ Indivisible
⚫ Atomic Values have name, type, format
⚫ Name
⚫ “USA_phone_numbers”
⚫ Data type
⚫ the set of U.S. valid 10 digit phone numbers
⚫ Format
⚫ (ddd)ddd-dddd, where each d is a decimal digit.
Domain types
⚫ Simple Domain is a set of atomic values
⚫ Composite Domains
⚫ Cartesian product of simple domains
◦ Ex., Date = Month x Day x Year where Month = {1, 2, …, 12}
Day = {1, 2, … 31}
Year = { 1900, 1901, …}
E.g., 10 9 1999
Relational Database Schema
⚫ Informally, the table definition
⚫ COMPANY relational db schema
Relation Schema
⚫
Relation schema: R or R(A
1, A
2, …, A
n)
• Fixed set of attributes A
1, A
2, …, A
n• Each attribute A
jcorresponds to exactly one of the underlying domains D
j(j = 1, 2, …, n),
• Not necessarily distinct D
j= domain( A
j)
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION)
Note: EMPNO and MGR take values from the same domain, the set of valid employee numbers
⚫
n = The number of attributes in the relation schema (degree of the relation)
Degree 1 Unary
Degree 2 Binary Degree 3 Ternary Degree n n-ary
Tuple
⚫ An “ordered” set of attributes
⚫ Values derived from an appropriate domain
◦ Tuple in the EMPLOYEE relation
◦ <"John”, “X”,“Smith", “000-99-9999”>
⚫ A relation is an unordered set of such tuples
◦ Concrete realization, i.e. printed table does have an order.
◦ Abstract construct does not.
◦ Much easier to understand effects of operations if
no ordering is specified.
Example – A relation STUDENT
Same state, but with different tuple ordering
Relation Instance
⚫ An ordered list of attributes
◦ R(A
1, A
2, …, A
n)
⚫ With a set of values
◦ Each tuple consists of attribute-value pairs (a
j, v
j), where j = 1, 2, …, n
◦ v
jis a value from the unique domain D
jthat is associated with that attribute.
⚫ Mapped to a domain
◦ r =
{t1, t2, …, tm}◦ Each tuple t is a mapping from R → D
where, R = R(A1, A2, …, An) and
D = domain(A1) ∪ domain(A2) ∪ … ∪ domain(An)
Properties of Relations
⚫
Ordering of tuples
◦ Relation is a set
◦ Sets have no order
◦ Relation is not sensitive to ordering of tuples
⚫
Uniqueness of tuples
◦ No duplicate tuples in a relation!
⚫
Unknown values
◦ NULL value – used when value can’t be known or does not exist
⚫
Interpretation
◦ Relation is an assertion of facts
◦ Each tuple can be thought of as a fact about the world
● Or as a predicate in first order logic
⚫
Relations may not contain repeating groups.
◦ Such a relation is said to be normalized or in 1
stnormal form.
Normalization of Repeating Groups
Before : S# (PQ)*
P# QTY S1 P1 300
P2 200 P3 400 S2 P1 300 P2 400 S3 P2 200
3 “records”
After : S# P# QTY
S1 P1 300 S1 P2 200 S1 P3 400 S2 P1 300 S2 P2 400 S3 P2 200
6 “records”
SuperKey
⚫ A subset of the relation attributes where all tuple values must be
distinct.
1. SK is a set of attributes 2. t 1 and t 2 are tuples
3. Then, t 1 [SK] ≠ t 2 [SK]
SuperKey Example
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) What are the SuperKey(s) of this relation?
NAME
EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION EMPNO, NAME, DNO, JOB
EMPNO, NAME
EMPNO, NAME, SAL, COMMISSION
EMPNO
Key – formal definition
⚫ A SuperKey where no proper subset of the attributes is a SuperKey.
⚫ A SuperKey K such that removal of any attribute(s) from K results in a set of
attributes that is not a SuperKey …
⚫ i.e., does not possess the SuperKey
uniqueness property
Key Example
EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) What are the Key(s) of this relation?
NAME EMPNO
Why isn’t EMPNO, NAME a Key?
A Key is a minimal SuperKey.
Candidate and Primary Key
⚫ A Key is a minimal SuperKey.
⚫ A relation may have more than one Key, called Candidate Keys
⚫ A Primary Key is an arbitrarily chosen Candidate Key
◦ Generally the smallest of the candidate keys
(in terms of size)
CAR table
⚫ Two Candidate keys:
◦ {EngSerialNo} akin to a VIN
◦ {LicenseNo} contains State and Reg #
⚫ What would you choose for the Primary Key?
Summary
⚫ Any Key is a SuperKey (but not vice versa)
⚫ Any set of attributes that includes a key is a SuperKey
⚫ A minimal SuperKey is also a Key
⚫ Is every relation required to have a
Primary Key?
CHAPTER 9 OUTLINE
Relational Database Design Using ER-to-Relational Mapping
Mapping EER Model Constructs to Relations
Relational Database
Design by ER- and EER-to-Relational Mapping
⚫ Design a relational database schema
◦ Based on a conceptual schema design
⚫ Algorithm to convert the basic ER model constructs into relations
⚫ Additional steps for EER model
Relational Database Design Using
ER-to-Relational Mapping
E-R to Relational Mapping
1. Regular entity type
2. Weak entity type
3. Binary M:1 relationship
4. Binary 1:1 relationship
5. Binary M:N relationship
6. Multi-valued attributes
An ERD Example
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Regular entity?
Composite attribute?
Derived attribute?
Multi-value d attribute?
Weak entity?
M:1
relationship?
1:1
relationship?
M:N
ER-to-Relational Mapping Algorithm
⚫ Step 1: Mapping of Regular Entity Types
◦ For each regular entity type, create a relation R that includes all the simple attributes of E
◦ Called entity relations
• Each tuple represents an entity instance
Step 1: Regular Entities
⚫ Map each regular entity type into a relation.
◦ Each simple attribute of the entity type maps to an attribute of the relation.
◦ Each composite attribute will be broken into many simple attributes.
◦ We’ll treat Multi-valued attributes later.
◦ Derived attributes, by definition, can be derived, and therefore are not necessary to be represented.
◦ The primary key of the entity type maps to the primary
key of the relation.
An ERD Example – Step 1
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Primary keys are underlined.
Department (Dept#, Dname, City)
Employee (Emp#, First, MI, Last, Salary) Project (J#, Jname, City)
What happened to Name?
How about Phone, and Totqty?
ER-to-Relational Mapping Algorithm
⚫ Step 2: Mapping of Weak Entity Types
◦ For each weak entity type, create a relation R and include all simple attributes of the entity type as attributes of R
◦ Include primary key attribute of owner as
foreign key attributes of R
Step 2: Weak Entities
⚫ The identifying relationship of a weak entity is of course a Many-to-One relationship,
⚫ The weak entity type must “fully” participate in its identifying relationship.
⚫ Each weak entity type maps into a relation.
◦ Attributes are treated as usual.
◦ Introduce a foreign key that references the primary key of its identifying relation.
◦ The primary key is the primary key of its
identifying relation plus its own partial key .
An ERD Example – Step 2
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M M
1
M M
Dependent (Emp#, Dname#, Gender)
Foreign key Emp# References Employee
ER-to-Relational Mapping Algorithm
⚫ Step 3: Mapping of Binary 1:N Relationship Types
◦ For each regular binary 1: N relationship type
• Identify relation that represents participating entity type at N-side of relationship type
• Include primary key of other entity type as foreign key in S
• Include simple attributes of 1:N relationship type as
attributes of S
Step 3: M:1 Relationships
⚫ Foreign Key approach
⚫ Each regular Many-to-One relationship type,
◦ Add key attribute from “1” side as a foreign key to the relation on the
“many” side
⚫ Do not need to introduce a
separate relation.
An ERD Example – Step 3
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ_
MAN AGE PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Employee (Emp#, First, MI, Last, Salary, Dept#) Foreign key Dept# references Department Project (J#, Jname, City, Manager#)
Foreign key Manager# references Employee
Foreign keys don’t have to have the same name as their PK
ER-to-Relational Mapping Algorithm
⚫ Step 4: Mapping of Binary 1:1 Relationship Types
◦ For each binary 1:1 relationship type
• Identify relations that correspond to entity types participating in R
◦ Possible approaches:
• Foreign key approach
• Merged relationship approach
• Cross-reference or relationship relation
approach
Step 4: 1:1 Relationships
⚫ Foreign Key approach
◦ Similarly to Many-to-One relationship.
● Partial participation side is like Many side of relationship
● Key attribute from partial participation side is added as foreign key to full participation side.
⚫ Merged approach
◦ Combine into one relation / table
⚫ Relationship relation (cross reference)
⚫
Create a new table of foreign keys to relate the
two tables
An ERD Example – Step 4
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Department (Dept#, Dname, City, Manager#)
Foreign key Manager# references Employee
Foreign Key approach
An ERD Example – Step 4
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Department (Dept#, Dname, City, Mgr#, First, MI, Last, Salary)
All Manager information is now a part of Department
Merged approach
Is this a desirable solution?
An ERD Example – Step 4
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
DeptMgr (Dept#, Manager#)
New relation has a compound key that refers (FK) to both relations.
Cross Reference approach
ER-to-Relational Mapping Algorithm
⚫ Step 5: Mapping of Binary M:N Relationship Types
◦ For each binary M : N relationship type
• Create a new relation S
• Include primary key of participating entity types as foreign key attributes in S
• Include any simple attributes of M:N relationship
type
An ERD Example – Step 5
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Proj_Work (Emp#, J#)
Foreign key Emp# References Employee Foreign key J# References Project
Attributes of Proj_Work would
ER-to-Relational Mapping Algorithm
⚫ Step 6: Mapping of Multivalued Attributes
◦ For each multivalued attribute
• Create a new relation
• If the multivalued attribute is composite, include its simple components
• Create foreign key to relationship or entity it belongs to
• The primary key is the combination of all the attributes.
• Or some of the attributes, if compound.
An ERD Example – Step 6
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Emp_Phone (Phone, Emp#)
Foreign key Emp# References Employee
Step 6: Another example
Compound Multivalued Attribute
Customer (Cust#, name)
Credit_Cards (Cust#, Card#, Expiration)
Foreign key Cust# References Customer
CUSTOMER Cust#
NAME CREDIT_
CARDS CARD
# EXPIRA
TION
An ERD Example – Summary
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M N
1
M M
Employee (Emp#, First, MI, Last, Salary, Dept# (FK)) Project (J#, Jname, City, Manager# (FK))
Department (Dept#, Dname, City, Manager#(FK)) Dependent (Emp#(FK), Dname#, Gender)
Proj_Work (Emp# (FK), J# (FK)) Emp_Phone (Phone, Emp# (FK), )
An ERD Example – Summary
DEPARTME NT
EMPLOYEE WOR
K_IN MAN
AGE
1
DEPENDEN T EMP_
DEP
PROJECT PROJ
_MA NAG
E PROJ _WO RK DEPT
#
CITY DNA ME
EMP#
NAM E SALA
RY PHO
NE FIRST
MI LAST
DNA ME
GEN DER J#
JNAM E CITY
TOT QTY M
1
1
1
M M
1
M M
Employee (Emp#, First, MI, Last, Salary, Dept# (FK)) Project (J#, Jname, City, Manager# (FK))
Department (Dept#, Dname, City, Manager#(FK)) Dependent (Emp#(FK), Dname#, Gender)
Proj_Work (Emp# (FK), J# (FK)) Emp_Phone (Phone, Emp# (FK), )