Ch:2 Entity Relationship Model
Describes various entities and relationships
among these entities.
It represent overall logical structure of a database.
Database System Concepts - 5th Edition, Oct 5, 2006
6.1
Silberschatz, Korth and Sudarshan
Basic Concepts
A database can be modeled as:
a
collection of entities, entities.
relationships among
An entity is an object that exists and is distinguishable
from other objects.
Example: employee, customer, supplier etc.
An entity may be concrete (a person or a book, for
example) or abstract (like a holiday or a concept )
An entity has set or properties , and values for some set of properties may uniquely identify an entity. Example.
Database System Concepts - 5th Edition, Oct 5, 2006 6.2 Silberschatz, Korth and Sudarshan
An entity set is a set of entities of the same type
that share the same properties. Example: set of all customer, employees etc.
Database System Concepts - 5th Edition, Oct 5, 2006
6.3
Silberschatz, Korth and Sudarshan
The individual entities that constitute a set are
said to be the extension of the entity set.
Attributes:- An entity is represented by set of
attributes. It is descriptive property possessed by each member of the entity set.
Value:- Each entity has a value for each of its
attributes. Example.
Domain:- For each attribute , there is a set of
permitted values , called domain of that attribute.
Database System Concepts - 5th Edition, Oct 5, 2006
6.4
Silberschatz, Korth and Sudarshan
Types of attributes
Attribute types:
Simple
and composite attributes.
multi-valued
Single-valued and
attributes.
Derived
attributes
Database System Concepts - 5th Edition, Oct 5, 2006
6.5
Silberschatz, Korth and Sudarshan
Simple and composite attributes
Simple attribute:- They are not divided into
subparts. e.g. id
Composite attribute:- cab be divided into
subparts. E.g. name.
Composite attribute may appear as a hierarchy. In
composite attribute address, its component attribute street can be further divided into street no, street name etc. figure
Database System Concepts - 5th Edition, Oct 5, 2006
6.6
Silberschatz, Korth and Sudarshan
Single-valued and multi-valued attributes
Single-valued attribute:- Only one value. E.g.
loan_no of the loan entity has only one value.
multi-valued attributes: Phone_no attribute of
an employee entity.
Database System Concepts - 5th Edition, Oct 5, 2006
6.7
Silberschatz, Korth and Sudarshan
Derived attributes
The value of Age attribute of the customer entity
can be derived from attribute date-of-birth.
Date-of-birth :- base attribute / stored attribute
Database System Concepts - 5th Edition, Oct 5, 2006
6.8
Silberschatz, Korth and Sudarshan
An attribute takes null value when an entity does
not have value for it.
An unknown value may be either missing (value
exists but we do not have that information) or not known (we do not know whether or not the value actually exists).
Database System Concepts - 5th Edition, Oct 5, 2006
6.9
Silberschatz, Korth and Sudarshan
A relationship is an association among several
entities e.g. we can define a relationship that associates customer with loan. (depositor)
Relationship Sets:- it is a set of relationships of
the same type.
Examples: Two entity sets : customer and loan. We can
define the relationship set borrower to denote the association between customers and bank loans that the customer have.
Database System Concepts - 5th Edition, Oct 5, 2006
6.10
Silberschatz, Korth and Sudarshan
Two entity sets: loan and branch. Relationship set
loan-branch.
Participation:- Association between entity sets. i.e.
the entity sets E1,E2,,En participate in relationship set R.
Relationship Instance The function that an entity plays in a relationship is
called entitys role.
Database System Concepts - 5th Edition, Oct 5, 2006
6.11
Silberschatz, Korth and Sudarshan
Recursive relationship set:- e.g. one customer has
taken more than one loan.
Descriptive attribute:- a relationship may also
have attribute. E.g. depositor relationship set with entity sets customer and account. We can associate access-date attribute to that relationship to specify the most recent date on which a customer accessed an account.
Degree: no of entities participating in a
relationship set
Database System Concepts - 5th Edition, Oct 5, 2006 6.12 Silberschatz, Korth and Sudarshan
Constraints
Mapping cardinalities
Participation
Database System Concepts - 5th Edition, Oct 5, 2006
6.13
Silberschatz, Korth and Sudarshan
Mapping Cardinalities : express the number of
entities to which another entities can be associated via a relationship set.
Binary relationship Ternary relationship: Relationship sets may
involve more than two entity sets. The entity sets customer, loan, and branch may be linked by the ternary (degree three) relationship set .
Database System Concepts - 5th Edition, Oct 5, 2006
6.14
Silberschatz, Korth and Sudarshan
For a binary relationship set R between entity sets
A and B, the mapping cardinality must be one of the following:
One to One:- An entity in A is associated with at
most one entity in B, and entity in B is associated with at most one entity in A. Figure
One to Many: An entity in A is associated with
any number of entities in B. An entity in B however can be associated with at most one entity in A. Figure
Database System Concepts - 5th Edition, Oct 5, 2006 6.15 Silberschatz, Korth and Sudarshan
Many to One:- An entity in A is associated with
at most one entity in B. An entity in B, however can be associated with any number of entities in A. figure
Many to Many : An entity in A is associated with
any number of entities in B and an entity in B is associated with any number of entities in A. figure
Database System Concepts - 5th Edition, Oct 5, 2006
6.16
Silberschatz, Korth and Sudarshan
Participation Constraints
Total Participation: The participation of an entity
set E in relationship set R is said to be total if every entity in E participate in at least one relationship in R.
Partial Participation : If only some entities in E
participate in relationships in R, the participation of entity set E in relationship R is said to be partial.
Database System Concepts - 5th Edition, Oct 5, 2006
6.17
Silberschatz, Korth and Sudarshan
E.g. loan entity is related to at least one customer
through the borrower relationship. So the participation of loan in relationship set borrower is total.
Database System Concepts - 5th Edition, Oct 5, 2006
6.18
Silberschatz, Korth and Sudarshan
Keys
Super Key
Candidate Key Primary Key
Foreign Key
Database System Concepts - 5th Edition, Oct 5, 2006
6.19
Silberschatz, Korth and Sudarshan
Super Key:- A super key of an entity set is a set
of one or more attributes whose values uniquely determine each entity.
Candidate Key:- A candidate key of an entity set
is a minimal super key.
Primary Key:-:- it is use to denote a candidate key
that is chosen by the database designer as the principal means of identifying entities within an entity set.
Database System Concepts - 5th Edition, Oct 5, 2006
6.20
Silberschatz, Korth and Sudarshan
Foreign Key:-A foreign key is a field (or fields)
that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data.
Database System Concepts - 5th Edition, Oct 5, 2006
6.21
Silberschatz, Korth and Sudarshan
ERD
Entity Set :- Rectangles
Attribute:- Ellipse Relationship Set:- Diamond
Link : Line (links attributes to entity sets and
entity sets to relationship sets)
Multivalued Attribute : Double Ellipse
Derived Attribute: Dashed Ellipse
Weak Entity Set:- Double Rectangle
Database System Concepts - 5th Edition, Oct 5, 2006
6.22
Silberschatz, Korth and Sudarshan
E-R Diagrams (many-to-many relationship set)
Database System Concepts - 5th Edition, Oct 5, 2006
6.23
Silberschatz, Korth and Sudarshan
One-To-Many Relationship
Database System Concepts - 5th Edition, Oct 5, 2006
6.24
Silberschatz, Korth and Sudarshan
Many-To-One Relationships
Database System Concepts - 5th Edition, Oct 5, 2006
6.25
Silberschatz, Korth and Sudarshan
One-to-One Example
Database System Concepts - 5th Edition, Oct 5, 2006
6.26
Silberschatz, Korth and Sudarshan
Relationship Sets with Attributes
Database System Concepts - 5th Edition, Oct 5, 2006
6.27
Silberschatz, Korth and Sudarshan
E-R Diagram With Composite, Multivalued, and Derived Attributes
Database System Concepts - 5th Edition, Oct 5, 2006
6.28
Silberschatz, Korth and Sudarshan
We indicate roles in ERD by labeling the lines
that connect diamonds to rectangles.
Database System Concepts - 5th Edition, Oct 5, 2006
6.29
Silberschatz, Korth and Sudarshan
Roles
Database System Concepts - 5th Edition, Oct 5, 2006
6.30
Silberschatz, Korth and Sudarshan
E-R Diagram with a Ternary Relationship
Database System Concepts - 5th Edition, Oct 5, 2006
6.31
Silberschatz, Korth and Sudarshan
Participation of an Entity Set in a Relationship Set
Database System Concepts - 5th Edition, Oct 5, 2006
6.32
Silberschatz, Korth and Sudarshan
Weak Entity Sets
An entity set may not have sufficient attributes to
form a primary key. Such an entity set is termed a weak entity set.
An entity set that has a primary key is termed a
strong entity set.
Database System Concepts - 5th Edition, Oct 5, 2006
6.33
Silberschatz, Korth and Sudarshan
E.g. an entity set payment has three attributes :
Payment_no, Payment_date, Payment_amount.
Payment_no are sequential numbers, starting
from 1, generated separately for each loan.
Thus , although each payment entity is distinct ,
payment for different loans may share the same payment_no.
Weak entity set must be associated with another
entity set called the identifying or owner entity set.
Database System Concepts - 5th Edition, Oct 5, 2006 6.34 Silberschatz, Korth and Sudarshan
Weak entity is said to be existence dependent on
the identifying entity set.
The identifying entity set is said to be own the
weak entity set that it identifies.
Identifying Relationship
Loan identifying entity set for weak entity set
payment.
Discriminator / Partial Key:- the discriminator of
the weak entity set payment is the attribute payment_no.
Database System Concepts - 5th Edition, Oct 5, 2006
6.35
Silberschatz, Korth and Sudarshan
For each loan, a payment_no uniquely identifies
one single payment for that loan.
Primary key of weak entity set = Primary key of
identifying entity set + Discriminator of weak entity set - Example
Database System Concepts - 5th Edition, Oct 5, 2006
6.36
Silberschatz, Korth and Sudarshan
Weak Entity Sets
Database System Concepts - 5th Edition, Oct 5, 2006
6.37
Silberschatz, Korth and Sudarshan
Extended ER Features
Specialization
Generalization Attribute inheritance
Constraints on generalization
Aggregation
Database System Concepts - 5th Edition, Oct 5, 2006
6.38
Silberschatz, Korth and Sudarshan
Specialization
Consider an entity set person , with attributes
name, street, and city.
A person may be further classified as one of the
following:
Customer Employee
Each of these person type is described by a set of
attributes that includes all the attributes of entity set person + possibly additional attributes.
Database System Concepts - 5th Edition, Oct 5, 2006
6.39
Silberschatz, Korth and Sudarshan
E.g. the process of designating subgrouping
within an entity set is called specialization.
Up-to-Down Another example :- suppose bank wishes to
divide accounts into two categories , checking account and saving account.
Specialization of account :- checking account and
saving account.
Attributes of account entity are account no and
balance.
Database System Concepts - 5th Edition, Oct 5, 2006
6.40
Silberschatz, Korth and Sudarshan
Saving account- additional attribute-interest rate
Checking account- additional attribute-
overdraft_amt
ISA concept
Database System Concepts - 5th Edition, Oct 5, 2006
6.41
Silberschatz, Korth and Sudarshan
Specialization Example
Database System Concepts - 5th Edition, Oct 5, 2006
6.42
Silberschatz, Korth and Sudarshan
Generalization
Bottom up manner
Multiple entity sets are synthesized into higher
level entity set on the basis of common features.
Commonality is expressed by generalization. Person is higher level entity set. (super class) Customer- employee ---- lower level entity sets.
(sub classes )
Database System Concepts - 5th Edition, Oct 5, 2006
6.43
Silberschatz, Korth and Sudarshan
Generalization
Example
Account saving & checking For this , we have three tables:
Account with attributes account_no and balance
Saving_account with attribute account_no and
int_rate
Checking_account with attributes account_no and
overdraft_amount.
Database System Concepts - 5th Edition, Oct 5, 2006
6.44
Silberschatz, Korth and Sudarshan
Aggregation
It is an abstraction through which relationships
are treated as higher level entities.
Example :- ERD with redundant relationships and
ERD with aggregation
Database System Concepts - 5th Edition, Oct 5, 2006
6.45
Silberschatz, Korth and Sudarshan
Aggregation
Example
The table for relationship set manages between
the aggregation of works-on and the entity set manager includes a column for each attribute in the primary keys of the entity set manager and the relationship set works-on.
Also include a column for any descriptive
attribute
Database System Concepts - 5th Edition, Oct 5, 2006
6.46
Silberschatz, Korth and Sudarshan
Distinguished specialization
Synthesized - generalization If customer and employee neither have attributes
that person entity do not have nor participate in different relationships than those in which person entity participate , there would be no need to specialize the person entity set.
Generalization proceeds from the identification
that a no of entity sets share some common features.
Database System Concepts - 5th Edition, Oct 5, 2006 6.47 Silberschatz, Korth and Sudarshan
EER
Disjoint Overlapping Partial Total Examples & Symbols
Database System Concepts - 5th Edition, Oct 5, 2006
6.48
Silberschatz, Korth and Sudarshan
Attribute Inheritance
The attributes of higher level entity sets are said
to be inherited by lower level entity sets.
E.g. customer and employee inherit the attributes
of person.
A lower level entity set also inherits participation
in the relationship sets in which its higher level entity participates. Example
Single inheritance :- in a hierarchy, a given entity
set may be involved as a lower level entity set in only one ISA relationship.
Database System Concepts - 5th Edition, Oct 5, 2006 6.49 Silberschatz, Korth and Sudarshan
If a entity set is a lower level entity set in more
than one ISA relationship , then the entity set has multiple inheritance.
Database System Concepts - 5th Edition, Oct 5, 2006
6.50
Silberschatz, Korth and Sudarshan
Design Constraints on a Generalization
Constraint on which entities can be members of a
given lower-level entity set.
condition-defined:- e.g.
higher level entity set account having an attribute account_type. Only those entities that satisfy the condition account type=saving account OR account type=checking account are included.
user-defined:-
e.g. let us assume that , after 3 months of employment, bank employees are assigned to one of four work teams.
6.51 Silberschatz, Korth and Sudarshan
Database System Concepts - 5th Edition, Oct 5, 2006
Higher level entity set employee
Lower level entity sets: four work teams The user in-charge makes the team assignment on
individual basis.
Database System Concepts - 5th Edition, Oct 5, 2006
6.52
Silberschatz, Korth and Sudarshan
Constraint on whether or not entities may belong
to more than one lower-level entity set within a single generalization.
Disjoint:
an entity can either be saving account or checking account ,but can not be both. example : employee work team. Certain manager can participate in more than one work team. A given employee may therefore appear in more than one of the team entity sets that are lower level entity sets of employees.
Overlapping:
Database System Concepts - 5th Edition, Oct 5, 2006
6.53
Silberschatz, Korth and Sudarshan
Completeness constraint -- specifies whether or
not an entity in the higher-level entity set must belong to at least one of the lower-level entity sets within a generalization.
Total:
Each higher level entity must belong to lower level entity set.
some higher level entities may not belong to any lower level entity set.
Partial:-
Database System Concepts - 5th Edition, Oct 5, 2006
6.54
Silberschatz, Korth and Sudarshan
E-R Diagram for a Banking Enterprise
Database System Concepts - 5th Edition, Oct 5, 2006
6.55
Silberschatz, Korth and Sudarshan
Reduction of an E-R Schema to Tables
A database which conforms to an E-R diagram can
be represented by a collection of tables. For each entity set and relationship set there is a unique table which is assigned the name of the corresponding entity set or relationship set. Each table has a number of columns (generally corresponding to attributes), which have unique names. Converting an E-R diagram to a table format is the basis for deriving a relational database design from an E-R diagram.
Database System Concepts - 5th Edition, Oct 5, 2006
6.56
Silberschatz, Korth and Sudarshan
Tabular representation of strong entity set
Let E be a strong entity set with attributes a1, a2,
.,an.
We represent this entity by a table called E with n
distinct columns , each of which corresponds to one of the attributes of E.
Each row in this table corresponds to one entity
of the entity set E.
Database System Concepts - 5th Edition, Oct 5, 2006
6.57
Silberschatz, Korth and Sudarshan
Representing Entity Sets as Tables
A strong entity set reduces to a table with the same attributes.
Database System Concepts - 5th Edition, Oct 5, 2006
6.58
Silberschatz, Korth and Sudarshan
Four attributes: id, name, street, city
So table contains four columns. Another example:-the loan entity set with two
attributes loan_no and amount can be represented in table called loan with two columns.
We can add a new entity to the database by
inserting a row into table.
We can also delete/modify rows.
Database System Concepts - 5th Edition, Oct 5, 2006
6.59
Silberschatz, Korth and Sudarshan
Representing Weak Entity Sets
A weak entity set becomes a table that includes a column for
the primary key of the identifying strong entity set
Database System Concepts - 5th Edition, Oct 5, 2006
6.60
Silberschatz, Korth and Sudarshan
Weak entity set payment (with attributes
payment_no, Payment_date and payment_amount)
The primary key of the loan entity set , on which
payment depends, is loan no.
So we can represent payment by a table with four
columns: three attributes of payment entity set + Primary Key (loan no) of loan entity set.
Database System Concepts - 5th Edition, Oct 5, 2006
6.61
Silberschatz, Korth and Sudarshan
Representing Relationship Sets as Tables
A relationship set is represented as a table with columns for
the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. E.g.: table for relationship set borrower
Database System Concepts - 5th Edition, Oct 5, 2006
6.62
Silberschatz, Korth and Sudarshan
Separate table will be created only for many- to-many relationship set.
Database System Concepts - 5th Edition, Oct 5, 2006
6.63
Silberschatz, Korth and Sudarshan
Redundancy of the tables
The weak entity set payment is dependent on the
strong entity set loan via the relationship set loan-payment.
The primary key of the payment entity set is
payment_no + loan_no and the primary key of the loan entity set is loan_no.
Now the loan-payment table would have two
columns : Loan_no and Payment_no.
Database System Concepts - 5th Edition, Oct 5, 2006
6.64
Silberschatz, Korth and Sudarshan
The table for the payment entity set has four
columns.
Every (loan_no, payment_no) combination in
loan-payment would also be present in the payment table, and vice versa.
So the loan-payment table is redundant. In general, the table for the relationship set
linking a weak entity set to its corresponding strong entity set is redundant and does not need to be present in a tabular representation of ER diagram.
Database System Concepts - 5th Edition, Oct 5, 2006 6.65 Silberschatz, Korth and Sudarshan
Combination of Tables
Figure
Entities Account &Branch Relationship set Account-Brach
Participation of account entity set is total
Participation of branch entity set is partial Account can not exist without branch.
Database System Concepts - 5th Edition, Oct 5, 2006
6.66
Silberschatz, Korth and Sudarshan
Relationship set : account-branch many to one
So we can combine the table account-branch
with table account.
So we require only two tables now: Account : account_no, balance, branch_name Branch: branch_name, branch_city, assets
Database System Concepts - 5th Edition, Oct 5, 2006
6.67
Silberschatz, Korth and Sudarshan
Composite Attribute
We handle composite attribute by creating a
separate attribute for each of the component attribute.
We do not create a separate column for composite
attribute itself.
Example
Database System Concepts - 5th Edition, Oct 5, 2006
6.68
Silberschatz, Korth and Sudarshan
Multivalued Attribute
New tables are created for these attributes.
E.g. for the multivalued attribute , phone_no, we
create a table phone_no with columns ph_no referring to the phone number attribute of the employee entity set and employee_id, representing the primary key of the employee set employee.
Database System Concepts - 5th Edition, Oct 5, 2006
6.69
Silberschatz, Korth and Sudarshan