0% found this document useful (0 votes)
4 views62 pages

Database System Concept: by Silberschatz, Korth and Sudarshan

Uploaded by

minhajshipon358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views62 pages

Database System Concept: by Silberschatz, Korth and Sudarshan

Uploaded by

minhajshipon358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Database System Concept

by Silberschatz, Korth and Sudarshan

Chapter 2 : Entity-Relationship Model


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

26-Feb-19 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
26-Feb-19 4
Example: age, given date_of_birth
Composite Attributes

26-Feb-19 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
26-Feb-19 6
Relationship Set borrower

26-Feb-19 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

26-Feb-19 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.

26-Feb-19 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

26-Feb-19 10
Mapping Cardinalities

One to one One to many


26-Feb-19 11
Mapping Cardinalities

Many to one Many to many


26-Feb-19 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.

26-Feb-19 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
26-Feb-19 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.

26-Feb-19 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
26-Feb-19 16
Customer Entity With Composite,
Customer ID Multivalued, and
Name Derived Attributes
first name
middle name
last name Composite
Attribute
Address
City
Zip Code
State
Street
Street_name
Street_number
Apartment_number
Multivaled
{ Phone_number } Attribute
Date_of_birth
Derived
Age Attribute 26-Feb-19 17
E-R Diagram With Composite, Multivalued, and Derived
Attributes

26-Feb-19 18
Relationship Sets with Attributes

26-Feb-19 19
Summary of Symbols Used in E-R Notation

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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.
26-Feb-19 27
Alternative Notation for Cardinality Limits
• Cardinality limits can also express participation
constraints

26-Feb-19 28
E-R Diagram with a Ternary Relationship

26-Feb-19 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

26-Feb-19 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.

26-Feb-19 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)

26-Feb-19 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.

26-Feb-19 33
Specialization
• A group of entities is divided into sub-groups based on their
characteristics

26-Feb-19 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 26-Feb-19 35
Generalization
• Process of generalizing entities. The generalized entities
contain the properties of all the generalized entities called
generalization

26-Feb-19 36
Specialization/Generalization
Example

26-Feb-19 37
Specialization/Generalization
Example

26-Feb-19 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
26-Feb-19 39
an entity can belong to more than one lower-level entity set
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

26-Feb-19 40
Aggregation
● Suppose
we want to record managers for tasks
performed by an employee at a branch

26-Feb-19 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
26-Feb-19 42
E-R Diagram With Aggregation

26-Feb-19 43
E-R Diagram With Aggregation
Defines a new relationship which associates some entity with some other existing
relationship called aggregation.

26-Feb-19 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.

26-Feb-19 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)

26-Feb-19 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

26-Feb-19 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

26-Feb-19 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:
1. RA, relating E and A
2.RB, relating E and B
3. RC, relating E and C
• Create a special identifying attribute for E
• Add any attributes of R to E
• For each relationship (ai , bi , ci) in R, create
1. a new entity ei in the entity set E 2. add (ei , ai ) to RA
3. add (ei , bi ) to RB 4. add (ei , ci ) to RC

26-Feb-19 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
26-Feb-19 52
Transforming E-R Diagrams into Relations

• To transform the conceptual data model


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

26-Feb-19 53
Transforming E-R Diagrams into Relations

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


26-Feb-19 54
Transforming E-R Diagrams into Relations
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

26-Feb-19 55
Transforming E-R Diagrams into Relations

26-Feb-19 56
Transforming E-R Diagrams into Relations

• 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

26-Feb-19 57
Transforming E-R Diagrams into Relations
Represent Relationships (continued)
• Binary and higher M:N relationships
• Create another relation and include primary keys of all relations as
primary key of new relation

26-Feb-19 58
Key Constraints
• Each dept has at most
one manager, according since
to the key constraint on name dname
Manages.
ssn lot did budget
CREATE TABLE Manages(
ssn CHAR(11),
did INTEGER, Employees Manages Departments
since DATE,
PRIMARY KEY (did), CREATE TABLE Dept_Mgr(
FOREIGN KEY (ssn) REFERENCES did INTEGER,
Employees, dname CHAR(20),
FOREIGN KEY (did) REFERENCES budget REAL,
Departments) ssn CHAR(11),
You can create single table for the since DATE,
relatin or no new table for relation but PRIMARY KEY (did),
Primary key foreign key relation FOREIGN KEY (ssn) REFERENCES
Only. Employees)

59
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
) 26-Feb-19 60
Translating hiearrarchies
• Two approaches
• Three tables:
employee,
contract_emps,Hou
rly_emps
• Two tables:
Employees having
Contract_emps
attributes and
Hourly_emps. And
vice versa

26-Feb-19 61
Thank You

You might also like