L3 ERelational Model

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Entity-Relationship Model

Chapter-2

Database System Concept


by Silberschatz, Korth and Sudarshan
Entity Sets
• An entity is an object that exists and is distinguishable from other objects.
• Example: specific person, company, event, plant
• An entity set is a set of entities of the same type that share the same
properties.
• Example: set of all persons, companies, trees, holidays
• An entity is represented by a set of attributes; i.e., descriptive properties
possessed by all members of an entity set.
• Example: customer = (customer_ID, customer_name, customer_ street,
customer_ city)
course = (course_id, title, credits)
• A subset of the attributes form a primary key of the entity set; i.e. uniquely
identifying each member of the set.
Entity Sets customer and loan
customer_id customer_ customer_ customer_ loan_ amount
name street city number

11/4/2022 3
Attributes
 An entity is represented by a set of attributes, that is descriptive
properties possessed by all members of an entity set.
Example:
customer = (customer_id, customer_name, customer_street, customer_city )
loan = (loan_number, amount )
 Domain – the set of permitted values for each attribute
 Attribute types:
 Simple and composite attributes.
 Single-valued and multi-valued attributes
Example: multi valued attribute: phone_numbers
 Derived attributes
Can be computed from other attributes
Example: age, given date_of_birth

11/4/2022 4
Composite Attributes

11/4/2022 5
Relationship Sets
 A relationship is an association among several entities
Example:
Hayes depositor A-102
customer entity relationship set account entity
 A relationship set is a mathematical relation among n  2 entities, each
taken from entity sets
{(e1, e2, … en) | e1  E1, e2  E2, …, en  En}

where (e1, e2, …, en) is a relationship


 Example:
(Hayes, A-102)  depositor

11/4/2022 6
Relationship Set borrower
customer_id customer_ customer_ customer_ loan_ amount
name street city number

11/4/2022 7
Relationship Sets (Cont.)
 An attribute can also be property of a relationship set.
 For instance, the depositor relationship set between entity sets
customer and account may have the attribute access-date

11/4/2022 8
Degree of a Relationship Set

 Refers to number of entity sets that participate in a


relationship set.
 Relationship sets that involve two entity sets are binary (or
degree two). Generally, most relationship sets in a
database system are binary.

 Relationships between more than two entity sets are rare.


Most relationships are binary.

11/4/2022 9
Mapping Cardinality Constraints
 Number of entities to which another entity can be
associated via a relationship set.
 Most useful in describing binary relationship sets.
 For a binary relationship set the mapping cardinality must
be one of the following types:
 One to one
 One to many
 Many to one
 Many to many

11/4/2022 10
Mapping Cardinalities

One to one One to many


11/4/2022 11
Mapping Cardinalities

Many to one Many to many


11/4/2022 12
Keys
 A super key of an entity set is a set of one or more attributes
whose values uniquely determine each entity.
Example : Student (ID, Roll, name, age, address, date of birth)

 A candidate key of an entity set is a minimal super key


 Customer_id is candidate key of customer
 account_number is candidate key of account

 Although several candidate keys may exist, one of the candidate


keys is selected to be the primary key.

11/4/2022 13
Keys for Relationship Sets
• The combination of primary keys of the participating entity sets
form a super key of a relationship set.
Example:

• (customer_name, account_number) is the super key of depositor


• Must consider the mapping cardinality of the relationship set when
deciding what are the candidate keys

11/4/2022 14
Entity-Relationship Model
▪ An entity relationship diagram (ERD) is a representation
of data within a domain.
▪ It consists of entities as well as relationships between
entities.
▪ A graphical representation of entities and their
relationships to each other
▪ A logical data model of the real world.

11/4/2022 15
E-R Diagrams

Rectangles represent entity sets.


Diamonds represent relationship sets.
Lines link attributes to entity sets and entity sets to relationship sets.
Ellipses represent attributes
Double ellipses represent multivalued attributes.
Dashed ellipses denote derived attributes.
Underline indicates primary key attributes
11/4/2022 16
Customer Entity With Composite,
Customer ID Multivalued, and Derived
Name
Attributes
first name
middle name
last name
Composite
Address Attribute
City
Zip Code
State
Street
Street_name
Street_number
Apartment_number
Multivale
{ Phone_number } d
Date_of_birth
Attribute

Age
Derived
Attribute

11/4/2022 17
E-R Diagram With Composite, Multivalued, and Derived
Attributes

11/4/2022 18
Relationship Sets with Attributes

11/4/2022 19
Summary of Symbols Used in E-R Notation

11/4/2022 20
Roles
• The function that an entity plays in a relationship is called role.
• The labels “manager” and “worker” are called roles; they specify
how employee entities interact via the works_for relationship set.
• Roles are indicated in E-R diagrams by labeling the lines that
connect diamonds to rectangles.
• Role labels are optional, and are used to clarify semantics of the
relationship

11/4/2022 21
Cardinality Constraints
 We express cardinality constraints by drawing either a directed line
(→), signifying “one,” or an undirected line (—), signifying “many,”
between the relationship set and the entity set.
 One-to-one relationship:
 A customer is associated with at most one loan via the relationship
borrower
 A loan is associated with at most one customer via borrower

11/4/2022 22
One-To-Many Relationship
• In the one-to-many relationship a loan is associated with at most
one customer via borrower, a customer is associated with several
(including 0) loans via borrower

11/4/2022 23
Many-To-One Relationships
• In a many-to-one relationship a loan is associated with several
(including 0) customers via borrower, a customer is associated with at
most one loan via borrower

11/4/2022 24
Many-To-Many Relationship
• A customer is associated with several (possibly 0) loans via
borrower
• A loan is associated with several (possibly 0) customers via
borrower

11/4/2022 25
Participation of an Entity Set in a Relationship Set
Total participation (indicated by double line): every entity in the
entity set participates in at least one relationship in the
relationship set
E.g. participation of loan in borrower is total; every loan
must have a customer
Partial participation: some entities may not participate in any
relationship in the relationship set
Example: participation of customer in borrower is partial

11/4/2022 26
Participation of an Entity Set in a Relationship Set

Every student must be a member of a team


The participation of student in LeaderOf is partial, because
only one student might be a team leader.

11/4/2022 27
Alternative Notation for Cardinality Limits
• Cardinality limits can also express participation
constraints

11/4/2022 28
E-R Diagram with a Ternary Relationship

11/4/2022 29
Weak Entity Sets
 An entity set that does not have a primary key is referred to as a
weak entity set.
 The existence of a weak entity set depends on the existence of a
identifying entity set
 weak entity set relate to the identifying entity set via a total, one-to-many
relationship
 Identifying relationship depicted using a double diamond

11/4/2022 30
Weak Entity Sets
 The discriminator (or partial key) of a weak entity set is the set of
attributes that distinguishes among all the entities of a weak entity
set.
 The primary key of a weak entity set is formed by the primary key
of the strong entity set on which the weak entity set is existence
dependent, plus the weak entity set’s discriminator.

11/4/2022 31
Weak Entity Sets
 We depict a weak entity set by double rectangles.
 We underline the discriminator of a weak entity set with a
dashed line.
 Primary key for payment – (loan_number, payment_number)

11/4/2022 32
Extended E-R Features: Specialization
 Top-down design process; we designate sub groupings within an entity
set that are distinctive from other entities in the set.

 Depicted by a triangle component labeled ISA (E.g. customer “is a”


person).

 Attribute inheritance – a lower-level entity set inherits all the attributes


and relationship participation of the higher-level entity set to which it is
linked.

11/4/2022 33
Specialization
• A group of entities is divided into sub-groups based on their
characteristics

11/4/2022 34
Extended ER Features: Generalization
• A bottom-up design process – combine a number of
entity sets that share the same features into a higher-
level entity set.
• Specialization and generalization are simple inversions of
each other; they are represented in an E-R diagram in
the same way.
• The terms specialization and generalization are used
interchangeably.
• The ISA relationship also referred to as superclass -
subclass relationship

11/4/2022 35
Generalization
• Process of generalizing entities. The generalized entities contain the
properties of all the generalized entities called generalization

11/4/2022 36
Specialization/Generalization Example

11/4/2022 37
Specialization/Generalization Example

11/4/2022 38
Design Constraints on a Specialization/Generalization
 Constraint on which entities can be members of a given lower-level
entity set.
 condition-defined
Example: all customers over 65 years are members of senior-
citizen entity set; senior-citizen ISA person.
 user-defined
 Constraint on whether or not entities may belong to more than one
lower-level entity set within a single generalization.
 Disjoint
an entity can belong to only one lower-level entity set
Noted in E-R diagram by writing disjoint next to the ISA triangle
 Overlapping
an entity can belong to more than one lower-level entity set

11/4/2022 39
Design Constraints on a Specialization/Generalization
(Cont.)
• 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 : an entity must belong to one of the lower-level entity sets
• partial: an entity need not belong to one of the lower-level entity sets

11/4/2022 40
Aggregation
Suppose we want to record managers for tasks
performed by an employee at a branch

11/4/2022 41
Aggregation (Cont.)
 Relationship sets works_on and manages represent overlapping
information
 Every manages relationship corresponds to a works_on relationship
 However, some works_on relationships may not correspond to any manages
relationships
So we can’t discard the works_on relationship
 Eliminate this redundancy via aggregation
 Treat relationship as an abstract entity
 Allows relationships between relationships
 Abstraction of relationship into new entity
 Without introducing redundancy
 An employee works on a particular job at a particular branch
 An employee, branch, job combination may have an associated manager

11/4/2022 42
E-R Diagram With Aggregation

11/4/2022 43
E-R Diagram With Aggregation
Defines a new relationship which associates some entity with some other existing
relationship called aggregation.

11/4/2022 44
Reduction to Relation Schemas
 An E-R diagram can be represented by a collection of schemas.
 Primary keys allow entity sets and relationship sets to be expressed
uniformly as relation schemas.
 For each entity set and relationship set there is a unique schema
that is assigned the name of the corresponding entity set or
relationship set.
 Each schema has a number of columns (generally corresponding to
attributes), which have unique names.

11/4/2022 45
Representing Entity Sets as Schemas
 A strong entity set reduces to a schema with the same attributes.
 A weak entity set becomes a table that includes a column for the primary
key of the identifying strong entity set
loan(loan_number, amount)
payment = ( loan_number, payment_number, payment_date,
payment_amount )
Representing Relationship Sets as Schemas

 A many-to-many relationship set is represented as a schema with


attributes for the primary keys of the two participating entity sets,
and any descriptive attributes of the relationship set.
 Example: schema for relationship set borrower
borrower = (customer_id, loan_number )
Redundancy of Schemas
Many-to-one and one-to-many relationship sets that are total on the
many-side can be represented by adding an extra attribute to the
“many” side, containing the primary key of the “one” side
Example: Instead of creating a schema for relationship set
account_branch, add an attribute branch_name to the schema arising
from entity set account
Account=(account-number, balance, branch-name)

11/4/2022 48
Redundancy of Schemas (Cont.)
 For one-to-one relationship sets, either side can be
chosen to act as the “many” side

 That is, extra attribute can be added to either of the tables


corresponding to the two entity sets

 If participation is partial on the “many” side, replacing a


schema by an extra attribute in the schema
corresponding to the “many” side could result in null
values

11/4/2022 49
Binary Vs. Non-Binary Relationships
 Some relationships that appear to be non-binary may be better
represented using binary relationships
 E.g. A ternary relationship parents, relating a child to his/her father and
mother, is best replaced by two binary relationships, father and mother
Using two binary relationships allows partial information (e.g. only mother being
know)
 But there are some relationships that are naturally non-binary
Example: works_on

11/4/2022 50
Converting Non-Binary Relationships to Binary Form
• In general, any non-binary relationship can
be represented using binary relationships
by creating an artificial entity set.
• Replace R between entity sets A, B and C by
an entity set E, and three relationship sets:
For each relationship (ai , bi , ci) in R,
1. RA, relating E and A create
2. RB, relating E and B 1. a new entity ei in the entity set E
3. RC, relating E and C 2. add (ei , ai ) to RA
• Create a special identifying attribute for E 3. add (ei , bi ) to RB
4. add (ei , ci ) to RC
• Add any attributes of R to E

11/4/2022 51
Binary vs. Ternary Relationships
name
ssn lot pname age

Employees Covers Dependents

Bad design
Policies

policyid cost

name pname age


ssn lot

Dependents
Employees

Purchaser
Beneficiary

Better design
Policies

policyid cost
11/4/2022 52
Transforming E-R Diagrams into Relations

To transform the conceptual (ER) data model


into a set of normalized relations
• Steps
1. Represent entities
2. Represent relationships
3. Normalize the relations
4. Merge the relations

11/4/2022 53
Transforming E-R Diagrams into Relations
1. Represent entities

Customer(Customer_ID, name, Address, City_State_ZIP, Discount)

11/4/2022 54
E-R Diagrams

11/4/2022 55
Transforming E-R Diagrams into Relations
2. Represent relationships

Binary 1:N Relationships


• Add the primary key attribute of
the entity on the one side of the
relationship as a foreign key in
the relation on the many side

11/4/2022 56
Transforming E-R Diagrams into Relations
2. Represent relationships (cont.)

11/4/2022 57
Transforming E-R Diagrams into Relations
2. Represent relationships (cont.)

Binary 1:1 relationship


• Three possible options
a. Add the primary key of A as a foreign key of B
b. Add the primary key of B as a foreign key of A
c. Both

Binary and higher M:N relationships


• Create another relation and include primary keys of all
relations as primary key of new relation

11/4/2022 58
Transforming E-R Diagrams into Relations
since
Key Constraints name dname

• Each dept has at most ssn lot did budget


one manager, according
to the key constraint on
Manages. Employees Manages Departments

You can create a new table for the Or, no new table for relation but
relation Primary key foreign key relation
Only.
CREATE TABLE Manages(
CREATE TABLE Dept_Mgr(
ssn CHAR(11),
did INTEGER,
did INTEGER,
dname CHAR(20),
since DATE,
budget REAL,
PRIMARY KEY (did),
ssn CHAR(11),
FOREIGN KEY (ssn) REFERENCES
since DATE,
Employees,
PRIMARY KEY (did),
FOREIGN KEY (did) REFERENCES
FOREIGN KEY (ssn) REFERENCES
Departments)
Employees)
59
Transforming E-R Diagrams into Relations
Weak Entities
 Weak entity set and identifying relationship set are translated into a single table.
 When the owner entity is deleted, all owned weak entities must also be deleted.

name
cost pname age
ssn lot

Employees Policy Dependents

CREATE TABLE Dep_Policy (


pname CHAR(20),
age INTEGER,
cost REAL,
ssn CHAR(11) NOT NULL,
PRIMARY KEY (pname, ssn),
FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE
)
11/4/2022 60
Transforming E-R Diagrams into Relations
Translating hiearrarchies
• Two approaches
• Three tables: employee, contract_emps,Hourly_emps
• Two tables: Employees having Contract_emps attributes and Hourly_emps.
And vice versa

11/4/2022 61
Thank You

You might also like