Extented Entity Relationship Model
Transcript of Extented Entity Relationship Model
Moussa Demba-Aljouf univer
Advanced Database
What you learn today, prepares you for tomorrow!
Moussa
�
�
�
�
�
�
�
�
Outline
rsity 1
Advanced Database-CSC426
Fall 2016
What you learn today, prepares you for tomorrow!
a Demba-Aljouf university
� Entity, Attribute, Relationship, Record
� 1-1, 1-n, n-m
� Primary key, Foreign key
� Subclass, Superclass and Inheritance
� Specialization and generalization
� Constraints and Characteristics
� Relationalschema
� Weakentity, partial key
utline
1
What you learn today, prepares you for tomorrow!
Moussa Demba-Aljouf university 2
Chap.1: Enhanced ER Model: Review
� The Entity-Relationship model models an enterprise as a collection of
entities and relationships:
� Entity: a “thing” in the real world that is distinguishable from all
other objects
Example: Student, Course, Professor, Account, Employee, Room,
Customer,…
� Described by a set of properties: attributes
Example: name, salary, grade, account_id, street,…
� Relationship: an association among several entities
,…: Takes, Teaches, DepositorExample
� ER Diagram Symbols and Notations:
Type of Relationships: Cardinality
� One-to-one (1-1):each entity is related to at most one entity.
With x,y=0, 1
E F R
(x,1) (y,1)
Moussa Demba-Aljouf university 3
An instanceofE is associated with at most oneinstanceofF, and aninstanceofF is
associated with at most oneinstanceofE.
� one-to-many:1-n or n-1
n: no limit
An instanceofE is associated with at most oneinstanceofF, and an instance ofF is
associated with any numberinstancesofE.
� many-to-many:n-m
n,m: no limit
E F R (x,1) (y,n)
E F R (x,n) (y,m)
1-n Relationship
Moussa Demba-Aljouf university 4
An instanceofE is associated with any numberinstancesofF, and an instance ofF
is associated with any numberinstancesofE.
Primary Key:is an attribute(or set of attributes) that is selected
to uniquely identify each instance of an entity.
the primary key. underlineewIn Design:
Example :Bank
1,n
PlaceOfBirth
1,1
n-m Relationship
Moussa Demba-Aljouf university 5
Primary Key in SQL:InPractice
Create table Customer(
Customer_idvarchar2(50),
Cnamevarchar2(20),
Street varchar2(15),
POB varchar2(15),
Primary key(Customer_id));
Foreign Key: a field that links one table to another table. A table can
have an unlimited number of foreign keys linking to other tables. We
use a sharp(#) for foreign keys.
Create table Customer(
Customer_idvarchar2(50),
Cnamevarchar2(20),
Street varchar2(15),
POB varchar2(15),
Primary key(Customer_id));
CREATE TABLE Account(
Account_Numbervarchar2(10),
Balance numeric(6,3),
Customer_idvarchar2(50),
Primary key(Account_Number),
FOREIGN KEY(Customer_id) references Customer)
Moussa Demba-Aljouf university 6
1,1
1,1
0,1
0,1
1,1 0,1
1,1 0,1
Mapping an ER Model to theRelational Schema
• One-to-one
With x=0,1
Relational Schema:
The FK must be in the relation with min=1.
Exercise
E(e1,e2)
F(f1, f2, e1#)
E F R (x,1) (1,1)
e1 e2 f1 f2
E
F
G
R1
R2
R3 R4
f1 f2
e1
e2
g1
g2
Moussa Demba-Aljouf university 7
1,1
1,1
0,1
1,n 0,1
1,1 1,1
1,1
• One-to-many
Relational Schema:
The FK must be in the relation with max=1.
Exercise
E(e1,e2)
F(f1, f2, e1#)
E F R (x,n) (1,1)
e1 e2 f1 f2
E
G
R1
R2
R3 R4
e1
e2
g1
g2
F
f1 f2
Moussa Demba-Aljouf university 8
• many-to-many
Relational Schema:
The FKs must be in a new relation.
Composite, derived and multivalued attributes
• address is a composite attribute.
• Years-of- services is a derivedattribute, it canbecalculated from Start-Date
andcurrent date.
• Skills is a multivalued attribute: an employee can have many skills.
E F R (x,n) (y,n)
E(e1,e2)
R(e1#,f1#,r1,r2)
F(f1,f2)
e1 e2 f1 f2 r1 r2
Employee
@
Box
street
city
eid Start date
Years of services skills
Moussa Demba-Aljouf university 9
In relational schema,
� Composite attributes are represented by components.
� Derived attributes are ignored
� Multi-valued attributes are represented by a separated table
Employee(eid, Box, street, city, start-date)
Emp-Skills(eid#, skills)
Reflexive relationships
Person(pid, husband#,…)
Here is another example:
Emp(eid, guarantor#,…)
wife
husband
0,1
0,1 or 0,n
Person Married-to pid
guarantor
beneficiary
n
1
Emp guarantor-of eid
Moussa Demba-Aljouf university 10
1 ,n
Course(cid,…)
Prerequisite(cdi1#, cid2#)
Weak entity � In a relational database, a weak entity is an entity that cannot be
uniquely identified by its attributes alone; � Therefore, it must use a foreign key in conjunction with its attributes to
create a primary key. � The foreign key is typically a primary key of an entity it is related to.
1 ,1 Building Room Has
box street
bid
rid area
cost
0,n
0,n
Course Prerequisite
s
cid
Moussa Demba-Aljouf university 11
� rid is a partial key of the weak entity Rooms.
� (rid+bid) is the primary key of Rooms
� bid is a foreign key in Rooms
in schema:
Building(bid, box, street)
Room(rid, bid#, area, cost)
Exercise
Give a relational schema that best describes the ER diagram:
1,1 0,n
1,1
0,n
1,1
1,n
1,n
1,n
1,n
0,nD
B
C
EF
R6
R3
R4
R5
R2
A R6
a1a2
b1
b2
c1
c2
d1
d2r21
r22
f1
f2
e1
e2
0,1
0,1
0,1 f3
d22d21
c3
Moussa Demba-Aljouf university 12
Subclass, Superclass and Inheritance
� As in C++, or other programming languages, attributes are inherited.
� If we declare A ISA B then every A entity is also considered to be a B
entity.
� And, every attribute of B is also considered to be an attribute of A.
� For example
Camel IS-A Animal Chicken IS-A Animal
Cow IS-A Animal Lion IS-A Animal
Lion
Cow Chicken
Animals
Camel
superclass
subclasses
Inheritance(IS A)
B
A
b1
b2
a1
C R
Moussa Demba-Aljouf university 13
� As well, a subclass inherits all the relationships in which the super class
participates.
Example 1:A student and a professor are both person.
Example 2:
Bottom-Up: generalization
Top-Down: specialization
Student pid
DOB name
level
Professor
salary
pid
DOB
name
Person
Student Professor
pid
DOB name
salary level
1,1 0,n lives Person
Student Professor
pid
DOB
salary
Building
level
Moussa Demba-Aljouf university 14
� EMPLOYEE may be further grouped into:
� SECRETARY, ENGINEER, TECHNICIAN, …
� Based on the EMPLOYEE’s Job
superclass
…subclasses
We read: SECRETARY IS-A EMPLOYEE
ENGINEER IS-A EMPLOYEE
TECHNICIAN IS-A EMPLOYEE
Employee
Secretary Engineer Technician
Inheritance(Is-A)
Car
VehiculeID Price
MaxSpeed LPlateNumbe
Nb_Of_Passengers
Truck
VehiculeID Price
Tonnage LPlateNumbe
Nb_Of_Axles
Ge
ne
raliza
tion
Sp
ecia
lizatio
n
Vehicle VehiculeID LPlateNumbe
Price
Car MaxSpeed
Nb_Of_Passengers
Tonnage
Nb_Of_Axles
Truck
Moussa Demba-Aljouf university 15
� MANAGER
� EMPLOYEEs who are managers
superclass
subclass
MANAGERIS-AEMPLOYEE
� SALARIED_EMPLOYEE, HOURLY_EMPLOYEE
� Based on the EMPLOYEE’s method of pay
SALARIED_EMPLOYEE IS-A EMPLOYEE
HOURLY_EMPLOYEE IS-A EMPLOYEE
� An Entity CANNOT exist in the database merely by being a member of a
subclass; it must also be a member of the superclass.
� Every Manager or Secretary or Salaried_employee is an employee.
� A member of the superclass can be OPTIONALLY included as a member of
any number of its subclasses
� An employee may not be a Manager.
� An employee may be a secretary and salaried_employee.
Employee
Salaried_Employee Hourly_Employee
Employee
Manager
IS-A
Moussa Demba-Aljouf university 16
1,1 0,n
0,n
1,n
0,n
1,n
1,1
0,n
0,n
1,n
0,n
1,n
1,1
0,n
We have also:
lives
takes
Course
Person
Student Professor
pid
DOB
salary level
Building
teaches
Generalization
lives1
takes
Course
Student Professor pid
DOB
salary
pid
Building
teaches
Specialization
lives2
DOB
level
Moussa Demba-Aljouf university 17
Constraints on Specialization and Generalization
Two other conditions apply to a specialization/generalization:
1. Disjointness Constraint: – Specifies that an entity can be a memberofat most one of the subclasses. – Specified by “d” in EER diagram – If not disjointed, overlap; that is the same entity may be a member of
morethan one subclass of the specialization – Specified by “o” in EER diagram
2. Completeness Constraint: – Total specialization: specifies that every entity in the superclass must be a
member of some subclass. Shown in EER diagrams by a double line – Partial specialization:allows an entity not to belong to any of the subclasses.
Shown in EER diagrams by a single line Note: Generalization usually is total because the superclass is derived from the subclasses. Example of disjointpartialSpecialization:Example of disjointtotal Specialization:
Student
Undergraduate
student
Graduate
student
d
Employee
Secretary Engineer Technician
d
Car
VehiculeID Price
MaxSpeed LPlateNumbe
Nb_Of_Passengers
Truck
VehiculeID Price
Tonnage LPlateNumbe
Nb_Of_Axles
Ge
ne
raliza
tion
Vehicle VehiculeID LPlateNumbe
Price
Car MaxSpeed
Nb_Of_Passengers
Tonnage
Nb_Of_Axles
Truck
d
Sp
ecia
lizatio
n
Moussa Demba-Aljouf university 18
Example of overlappingpartial Specialization:
Example of overlappingtotal Specialization: (in a university)
Person
Employee Student
O
Person
Teacher Student
O
Moussa Demba-Aljouf university 19
Translating in schema:
Fig.1
3 possibiliBes:
a) keep only the super-class
we get something like Fig.2.
and the schema is:
E1 (A1, A11, A2, A3, type) Fig.2
type is a discriminator to disFnguish which type each tuple is (E2, E3,…).
- many null columns + always applicable.
b) keep only the sub-classes
we get something like Fig.3.if the generalization is disjoint and complete
Fig.3
and the corresponding schema is:
E2 (A12, A11, A2)A1 is renamed A12 in E2 and A13 in E3.
E3 (A13, A11, A3)
E1
E2 E3
A1 A11
A2 A3
E1
A3
A11
A2
A1
E3
A11
A3
A13
E2
A11
A2
A12
Moussa Demba-Aljouf university 20
- This solution involves a significant redundancy of the data on the
super-class if there is not exclusion between the sub-classes.
- For an overlapping generalization, some values such as A11 would be
stored twice unnecessarily.
- Similarly, if the generalization were not complete (i.e., partial), that is
if some E1’s were neither E2 nor E3, then such E1’s could not be
represented by this method.
+ One relation per concrete class: the most natural way
+ Not require joins to retrieve information
c) keep all classes
we get something like Fig.4.
and the corresponding schema is:
E1 (A1, A11,type)
E2 (A1#,A2)
E3 (A1#, A3)
- many joins to retrieve information of a subclass
� Attribute type is here optional, but usingitcan minimize complex joins (improve
performance).
+ simple and allows different kind of queries.
Exercise:An engineer is an employee who works in a department, and he is a
person who lives in a building.
E1
E2 E3
A1 A11
A2 A3
Fig.4.
Moussa Demba-Aljouf university 21
1,n 1,n 1,1 1,n
0,n 0,n 0,n
1,1
1,1
0,n 0,n
1,1
Give the corresponding schema without keeping the entity Employee?
Exercise:Consideringa university database represented by the ERD:
Give a relational schema that best describes the ERD.
Student-
Boys
Student-
Girls
is-brother Course
offered
cid syllabus
Student enrolls
sid name
Course-
offering
Instructor teaches
time secno room iid name grade
Department
study-in has deptnam
1,1 0,n
1,n
1,1
Building
bid
pname Person
pid
lives
salary
Student grant Employee
Department
dname
works
Engineer type Secretary
age
Moussa Demba-Aljouf university 22
This ERD is designed using draw.io online tool.
Give a Schema that best describes the above ERD:
a. keeping all entities(generalization). b. without keeping the entity A1(specialization).
Moussa Demba-Aljouf university 23
Exercise:Draw an ER diagram according to the following specification and
transform it to a relational data model.
A hotel would like to make a registration of the expenses of its
guests. In the hotel the guests can stay in rooms for a specific period.
The main properties of the rooms are their size (floor space), the
number of beds in them and their daily cost. Guests can stay in one
or multiple rooms at the same time, while multiple guests can stay
together as well. Relevant information about room usage of guests
are the times of checking in and checking out. Of course there can be
rooms where no guest is staying. Guests can make reservations, the
related information are the planned dates of arrival and departure.
The guests can use various services (internet, meal, sauna …), related
information are the names of such services, their prices and their
description. Guests can use multiple services and there can be
services which have never been used by anyone.
We register the guest by their name (first name, last name) and
address (city, street, Box).
We want also to keep track of the dependents of each guest, if any.
We keep each dependent’s name, sex, birth date, and relationship to
the guest.
Moussa Demba-Aljouf university 24
ERD – Crow’s Foot Notation
Relationship Types:
� Relationship types are modeled by lines connectingthe entities
� Line is annotated with the name of the relationship which is a verb
� Cardinalities are represented graphically
ATTENTION: Cardinalities are written on the oppositeside of the relationship
(in contrast to “classic ER”)
Example:
� What happens to n-ary relationships or relationship attributes?
� Problem
Moussa Demba-Aljouf university 25
• N-ary relationship types are not supported bycrow’s foot notation, neither are
relationshipattributes
� Workaround solution:
• Intermediate entities must be used
• N-ary relationships are broken down in a series of binary
relationship types anchoring on the intermediate entity
Moussa Demba-Aljouf university 26
Is equivalent to
End.