0% found this document useful (0 votes)
8 views31 pages

DBMS 2

Cuba

Uploaded by

ttap44832
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)
8 views31 pages

DBMS 2

Cuba

Uploaded by

ttap44832
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/ 31

ER diagram

 ER diagram or Entity Relationship diagram is a conceptual model that gives the


graphical representation of the logical structure of the database.
 It shows all the constraints and relationships that exist among the different components.
Components of ER diagram-
An ER diagram is mainly composed of following three components-
1. Entity Sets
2. Attributes
3. Relationship Set
Example-
Consider the following Student table-

Roll_no Name Age

1 Akshay 20

2 Rahul 19

3 Pooja 20

4 Aarti 19

This complete table is referred to as “Student Entity Set” and each row represents an
“entity”.

Representation as ER Diagram-
The above table may be represented as ER diagram as-

Here,
 Roll_no is a primary key that can identify each entity uniquely.
 Thus, by using student’s roll number, a student can be identified uniquely.

Prepared by SKS. CSE Page 1 of 31


ER Diagram Symbols-
An ER diagram is composed of several components and each component in ER diagram is
represented using a specific symbol.
ER diagram symbols are discussed below-
1. For Entity Sets-
An entity set is a set of same type of entities.
An entity refers to any object having-
 Either a physical existence such as a particular person, office, house or car.
 Or a conceptual existence such as a school or a company.

An entity set may be of the following two types-

1. Strong entity set


2. Weak entity set

1. Strong Entity Set-


 A strong entity set possess its own primary key.
 It is represented using a single rectangle.
2. Weak Entity Set-
 A weak entity set do not possess its own primary key.
 It is represented using a double rectangle.

Prepared by SKS. CSE Page 2 of 31


2. For Relationship Sets-
 Relationship defines an association among several entities.
 A relationship set is a set of same type of relationships.

A relationship set may be of the following two types-

1. Strong relationship set


2. Weak relationship set

1. Strong Relationship Set-


 A strong relationship exists between two strong entity sets.
 It is represented using a diamond symbol.

2. Weak Relationship Set-


 A weak or identifying relationship exists between the strong and weak entity set.
 It is represented using a double diamond symbol.

Prepared by SKS. CSE Page 3 of 31


3. For Attributes-
 Attributes are the properties which describes the entities of an entity set.
 There are several types of attributes.

4. For Participation Constraints-


Participation constraint defines the least number of relationship instances in which an
entity has to necessarily participate.
There are two types of participation constraints-

1. Partial participation
2. Total participation

1. Partial Participation-
Partial participation is represented using a single line between the entity set and
relationship set.
2. Total Participation-
Total participation is represented using a double line between the entity set and
relationship set.

Prepared by SKS. CSE Page 4 of 31


5. For Specialization and Generalization-
 Generalization is a process of forming a generalized super class by extracting the
common characteristics from two or more classes.
 Specialization is a reverse process of generalization where a super class is divided
into sub classes by assigning the specific characteristics of sub classes to them.

6. For Cardinality Constraints / Ratios-


Cardinality constraint defines the maximum number of relationship instances in which an
entity can participate.
There are 4 types of cardinality ratios-

1. Many-to-many cardinality (m:n)


2. Many-to-one cardinality (m:1)
3. One-to-many cardinality (1:n)
4. One-to-one cardinality (1:1)
Prepared by SKS. CSE Page 5 of 31
Entity Set in DBMS-

An entity set is a set of same type of entities.

An entity refers to any object having-


 Either a physical existence such as a particular person, office, house or car.
 Or a conceptual existence such as a school, a university, a company or a job.

In ER diagram,
 Attributes are associated with an entity set.
 Attributes describe the properties of entities in the entity set.
 Based on the values of certain attributes, an entity can be identified uniquely.

Prepared by SKS. CSE Page 6 of 31


Types of Entity Sets-
An entity set may be of the following two types-

1. Strong entity set


2. Weak entity set
1. Strong Entity Set-
 A strong entity set is an entity set that contains sufficient attributes to uniquely identify
all its entities.
 In other words, a primary key exists for a strong entity set.
 Primary key of a strong entity set is represented by underlining it.
Symbols Used-
 A single rectangle is used for representing a strong entity set.
 A diamond symbol is used for representing the relationship that exists between two
strong entity sets.
 A single line is used for representing the connection of the strong entity set with the
relationship set.
 A double line is used for representing the total participation of an entity set with the
relationship set.
 Total participation may or may not exist in the relationship.

Prepared by SKS. CSE Page 7 of 31


Example-
Consider the following ER diagram-

In this ER diagram,
 Two strong entity sets “Student” and “Course” are related to each other.
 Student ID and Student name are the attributes of entity set “Student”.
 Student ID is the primary key using which any student can be identified uniquely.
 Course ID and Course name are the attributes of entity set “Course”.
 Course ID is the primary key using which any course can be identified uniquely.
 Double line between Student and relationship set signifies total participation.
 It suggests that each student must be enrolled in at least one course.
 Single line between Course and relationship set signifies partial participation.
 It suggests that there might exist some courses for which no enrollments are made.
2. Weak Entity Set-
 A weak entity set is an entity set that does not contain sufficient attributes to uniquely
identify its entities.
 In other words, a primary key does not exist for a weak entity set.
 However, it contains a partial key called as a discriminator.
 Discriminator can identify a group of entities from the entity set.
 Discriminator is represented by underlining with a dashed line.

NOTE-
 The combination of discriminator and primary key of the strong entity set makes it
possible to uniquely identify all entities of the weak entity set.
 Thus, this combination serves as a primary key for the weak entity set.
 Clearly, this primary key is not formed by the weak entity set completely.

Prepared by SKS. CSE Page 8 of 31


Symbols Used-
 A double rectangle is used for representing a weak entity set.
 A double diamond symbol is used for representing the relationship that exists between
the strong and weak entity sets and this relationship is known as identifying
relationship.
 A double line is used for representing the connection of the weak entity set with the
relationship set.
 Total participation always exists in the identifying relationship.
Example-
Consider the following ER diagram-

In this ER diagram,
 One strong entity set “Building” and one weak entity set “Apartment” are related to each
other.
 Strong entity set “Building” has building number as its primary key.
 Door number is the discriminator of the weak entity set “Apartment”.
 This is because door number alone can not identify an apartment uniquely as there may
be several other buildings having the same door number.
 Double line between Apartment and relationship set signifies total participation.
 It suggests that each apartment must be present in at least one building.
 Single line between Building and relationship set signifies partial participation.
 It suggests that there might exist some buildings which has no apartment.
To uniquely identify any apartment,
 First, building number is required to identify the particular building.
 Secondly, door number of the apartment is required to uniquely identify the apartment.
Thus,
Primary key of Apartment
= Primary key of Building + Its own discriminator
= Building number + Door number
Prepared by SKS. CSE Page 9 of 31
Differences between Strong entity set and Weak entity set-

Strong entity set Weak entity set

A single rectangle is used for the A double rectangle is used for the
representation of a strong entity set. representation of a weak entity set.

It contains sufficient attributes to form its It does not contain sufficient attributes to
primary key. form its primary key.

A double diamond symbol is used for the


A diamond symbol is used for the
representation of the identifying
representation of the relationship that
relationship that exists between the strong
exists between the two strong entity sets.
and weak entity set.

A single line is used for the representation A double line is used for the representation
of the connection between the strong entity of the connection between the weak entity
set and the relationship. set and the relationship set.

Total participation may or may not exist in Total participation always exists in the
the relationship. identifying relationship.

Important Note-
In ER diagram, weak entity set is always present in total participation with the identifying
relationship set.
So, we always have the picture like shown here-

Prepared by SKS. CSE Page 10 of 31


Relationship in DBMS-

A relationship is defined as an association among several entities.

Example-
‘Enrolled in’ is a relationship that exists between entities Student and Course.

Relationship Set-
A relationship set is a set of relationships of same type.
Example-
Set representation of above ER diagram is-

Degree of a Relationship Set-


The number of entity sets that participate in a relationship set is termed as the degree of
that relationship set. Thus,

Degree of a relationship set = Number of entity sets participating in a relationship


set

Prepared by SKS. CSE Page 11 of 31


Types of Relationship Sets-
On the basis of degree of a relationship set, a relationship set can be classified into the
following types-

1. Unary relationship set


2. Binary relationship set
3. Ternary relationship set
4. N-ary relationship set

1. Unary Relationship Set-


Unary relationship set is a relationship set where only one entity set participates in a
relationship set.
Example-
One person is married to only one person

Prepared by SKS. CSE Page 12 of 31


2. Binary Relationship Set-
Binary relationship set is a relationship set where two entity sets participate in a
relationship set.
Example-
Student is enrolled in a Course

3. Ternary Relationship Set-


Ternary relationship set is a relationship set where three entity sets participate in a
relationship set.
Example-

4. N-ary Relationship Set-


N-ary relationship set is a relationship set where ‘n’ entity sets participate in a relationship
set.
Cardinality Constraint-

Cardinality constraint defines the maximum number of relationship instances in


which an entity can participate.

Prepared by SKS. CSE Page 13 of 31


Types of Cardinality Ratios-
There are 4 types of cardinality ratios-

1. Many-to-Many cardinality (m:n)


2. Many-to-One cardinality (m:1)
3. One-to-Many cardinality (1:n)
4. One-to-One cardinality (1:1 )

1. Many-to-Many Cardinality-
By this cardinality constraint,
 An entity in set A can be associated with any number (zero or more) of entities in set B.
 An entity in set B can be associated with any number (zero or more) of entities in set A.

Symbol Used-

Example-
Consider the following ER diagram-

Here,
 One student can enroll in any number (zero or more) of courses.
 One course can be enrolled by any number (zero or more) of students.

Prepared by SKS. CSE Page 14 of 31


2. Many-to-One Cardinality-
By this cardinality constraint,
 An entity in set A can be associated with at most one entity in set B.
 An entity in set B can be associated with any number (zero or more) of entities in set A.
Symbol Used-

Example-
Consider the following ER diagram-

Here,
 One student can enroll in at most one course.
 One course can be enrolled by any number (zero or more) of students.

3. One-to-Many Cardinality-
By this cardinality constraint,
 An entity in set A can be associated with any number (zero or more) of entities in set B.
 An entity in set B can be associated with at most one entity in set A.

Prepared by SKS. CSE Page 15 of 31


Symbol Used-

Example-
Consider the following ER diagram-

Here,
 One student can enroll in any number (zero or more) of courses.
 One course can be enrolled by at most one student.

4. One-to-One Cardinality-
By this cardinality constraint,
 An entity in set A can be associated with at most one entity in set B.
 An entity in set B can be associated with at most one entity in set A.
Symbol Used-

Prepared by SKS. CSE Page 16 of 31


Example-
Consider the following ER diagram-

Here,
 One student can enroll in at most one course.
 One course can be enrolled by at most one student.

Participation Constraints-

Participation constraints define the least number of relationship instances in which


an entity must compulsorily participate.

Types of Participation Constraints-


There are two types of participation constraints-

1. Total participation
2. Partial participation

1. Total Participation-
 It specifies that each entity in the entity set must compulsorily participate in at least one
relationship instance in that relationship set.
 That is why, it is also called as mandatory participation.
 Total participation is represented using a double line between the entity set and
relationship set.
Symbol Used-

Prepared by SKS. CSE Page 17 of 31


Example-

Here,
 Double line between the entity set “Student” and relationship set “Enrolled in” signifies
total participation.
 It specifies that each student must be enrolled in at least one course.

2. Partial Participation-
 It specifies that each entity in the entity set may or may not participate in the
relationship instance in that relationship set.
 That is why, it is also called as optional participation.
 Partial participation is represented using a single line between the entity set and
relationship set.
Symbol Used-

Example-

Here,
 Single line between the entity set “Course” and relationship set “Enrolled in” signifies
partial participation.
 It specifies that there might exist some courses for which no enrollments are made.
Relationship between Cardinality and Participation Constraints-
Minimum cardinality tells whether the participation is partial or total.
 If minimum cardinality = 0, then it signifies partial participation.
 If minimum cardinality = 1, then it signifies total participation.
Maximum cardinality tells the maximum number of entities that participates in a
relationship set.
Prepared by SKS. CSE Page 18 of 31
Attributes in ER Diagram-
 Attributes are the descriptive properties which are owned by each entity of an Entity Set.
 There exist a specific domain or set of values for each attribute from where the attribute
can take its values.
Types of Attributes-
In ER diagram, attributes associated with an entity set may be of the following types-

1. Simple attributes
2. Composite attributes
3. Single valued attributes
4. Multi valued attributes
5. Derived attributes
6. Key attributes
1. Simple Attributes-
Simple attributes are those attributes which can not be divided further.
Example-

Here, all the attributes are simple attributes as they can not be divided further.

Prepared by SKS. CSE Page 19 of 31


2. Composite Attributes-
Composite attributes are those attributes which are composed of many other simple
attributes.
Example-

Here, the attributes “Name” and “Address” are composite attributes as they are composed
of many other simple attributes.
3. Single Valued Attributes-
Single valued attributes are those attributes which can take only one value for a given
entity from an entity set.
Example-

Here, all the attributes are single valued attributes as they can take only one specific value
for each entity.
4. Multi Valued Attributes-
Multi valued attributes are those attributes which can take more than one value for a
given entity from an entity set.
Example-

Here, the attributes “Mob_no” and “Email_id” are multi valued attributes as they can take
more than one values for a given entity.

Prepared by SKS. CSE Page 20 of 31


5. Derived Attributes-
Derived attributes are those attributes which can be derived from other attribute(s).
Example-

Here, the attribute “Age” is a derived attribute as it can be derived from the attribute
“DOB”.
6. Key Attributes-
Key attributes are those attributes which can identify an entity uniquely in an entity set.
Example-

Here, the attribute “Roll_no” is a key attribute as it can identify any student uniquely.
Converting ER Diagrams to Tables-
After designing an ER Diagram,
 ER diagram is converted into the tables in relational model.
 This is because relational models can be easily implemented by RDBMS like MySQL ,
Oracle etc.

Following rules are used for converting an ER diagram into the tables-
Rule-01: For Strong Entity Set With Only Simple Attributes-
A strong entity set with only simple attributes will require only one table in relational
model.
 Attributes of the table will be the attributes of the entity set.
 The primary key of the table will be the key attribute of the entity set.

Prepared by SKS. CSE Page 21 of 31


Example-

Roll_no Name Sex

Schema : Student ( Roll_no , Name , Sex )

Rule-02: For Strong Entity Set With Composite Attributes-


 A strong entity set with any number of composite attributes will require only one table in
relational model.
 While conversion, simple attributes of the composite attributes are taken into account
and not the composite attribute itself.
Example-

Prepared by SKS. CSE Page 22 of 31


Roll_no First_name Last_name House_no Street City

Schema : Student ( Roll_no , First_name , Last_name , House_no , Street , City )

Rule-03: For Strong Entity Set With Multi Valued Attributes-


A strong entity set with any number of multi valued attributes will require two tables in
relational model.
 One table will contain all the simple attributes with the primary key.
 Other table will contain the primary key and all the multi valued attributes.
Example-

Roll_no City

Roll_no Mobile_no

Prepared by SKS. CSE Page 23 of 31


Rule-04: Translating Relationship Set into a Table-
A relationship set will require one table in the relational model.
Attributes of the table are-
 Primary key attributes of the participating entity sets
 Its own descriptive attributes if any.
Set of non-descriptive attributes will be the primary key.
Example-

Emp_no Dept_id since

Schema : Works in ( Emp_no , Dept_id , since )

NOTE-
If we consider the overall ER diagram, three tables will be required in relational model-
 One table for the entity set “Employee”
 One table for the entity set “Department”
 One table for the relationship set “Works in”

Prepared by SKS. CSE Page 24 of 31


Rule-05: For Binary Relationships With Cardinality Ratios-
The following four cases are possible-

Case-01: Binary relationship with cardinality ratio m:n


Case-02: Binary relationship with cardinality ratio 1:n
Case-03: Binary relationship with cardinality ratio m:1
Case-04: Binary relationship with cardinality ratio 1:1

Case-01: For Binary Relationship With Cardinality Ratio m:n

Here, three tables will be required-


1. A ( a1 , a2 )
2. R ( a1 , b1 )
3. B ( b1 , b2 )

Case-02: For Binary Relationship With Cardinality Ratio 1:n

Here, two tables will be required-


1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )

NOTE- Here, combined table will be drawn for the entity set B and relationship set R.

Prepared by SKS. CSE Page 25 of 31


Case-03: For Binary Relationship With Cardinality Ratio m:1

Here, two tables will be required-


1. AR ( a1 , a2 , b1 )
2. B ( b1 , b2 )

NOTE- Here, combined table will be drawn for the entity set A and relationship set R.
Case-04: For Binary Relationship With Cardinality Ratio 1:1

Here, two tables will be required. Either combine ‘R’ with ‘A’ or ‘B’
Way-01:
1. AR ( a1 , a2 , b1 )
2. B ( b1 , b2 )

Way-02:
1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )

Prepared by SKS. CSE Page 26 of 31


Thumb Rules to Remember

While determining the minimum number of tables required for binary relationships
with given cardinality ratios, following thumb rules must be kept in mind-
 For binary relationship with cardinality ration m : n , separate and individual
tables will be drawn for each entity set and relationship.
 For binary relationship with cardinality ratio either m : 1 or 1 : n , always
remember “many side will consume the relationship” i.e. a combined table will be
drawn for many side entity set and relationship set.
 For binary relationship with cardinality ratio 1 : 1 , two tables will be required. You
can combine the relationship set with any one of the entity sets.

Rule-06: For Binary Relationship With Both Cardinality Constraints and Participation
Constraints-

 Cardinality constraints will be implemented as discussed in Rule-05.


 Because of the total participation constraint, foreign key acquires NOT NULL constraint
i.e. now foreign key can not be null.

Case-01: For Binary Relationship With Cardinality Constraint and Total Participation
Constraint From One Side-

Because cardinality ratio = 1 : n , so we will combine the entity set B and relationship set
R.
Then, two tables will be required-
1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )
Because of total participation, foreign key a1 has acquired NOT NULL constraint, so it can’t
be null now.

Prepared by SKS. CSE Page 27 of 31


Case-02: For Binary Relationship With Cardinality Constraint and Total Participation
Constraint From Both Sides-
If there is a key constraint from both the sides of an entity set with total participation, then
that binary relationship is represented using only single table.

Here, Only one table is required.


 ARB ( a1 , a2 , b1 , b2 )

Rule-07: For Binary Relationship With Weak Entity Set-


Weak entity set always appears in association with identifying relationship with total
participation constraint.

Here, two tables will be required-


1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )

ER Diagrams to Tables-
Before you go through this article, make sure that you have gone through the previous
article on ER Diagrams to Tables.
After designing an ER Diagram,
 ER diagram is converted into the tables in relational model.
 This is because relational models can be easily implemented by RDBMS like MySQL ,
Oracle etc.
 The rules used for converting an ER diagram into the tables are already discussed.

In this article, we will discuss practice problems based on converting ER Diagrams to


Tables.
Prepared by SKS. CSE Page 28 of 31
PRAACTICE PR ROBLEMS
S BASED ON
O CONV
VERTING ER
E DIAGR
RAM TO TABLES-
T
Pro
oblem-01::
Fin
nd the min
nimum num
mber of ta
ables requiired for the following
g ER diagrram in rela
ational
mod
del-

Solu
ution-
App
plying the rules, min
nimum 3 tables
t will be required-
 MR1 (M1 , M2
M M , M3 , P1)
 P (P1 , P2)
 NR
R2 (P1 , N1
N , N2)

Pro
oblem-02::
Fin
nd the min
nimum num
mber of ta
ables requiired to rep
present the
e given ER
R diagram in
rela
ational mod
del-

Solu
ution-
App
plying the rules, min
nimum 4 tables
t will be required-
 AR
R1R2 (a1 , a2 , b1 , c1)
 B (b1 , b2)
 C (c1 , c2)
 R3
3 (b1 , c1)

Prepared by SKS. CS
SE Page 29 of
o 31
Probblem-03:
Findd the miniimum num
mber of tab
bles requirred to reprresent the given ER diagram in
rela
ational moddel-

Solu
ution-
App
plying the rules, min
nimum 5 tables
t will be required-
 BR
R1R4R5 (b b1 , b2 , a1
a , c1 , d1
1)
 A (a1 , a2)
 R2
2 (a1 , c1)
 CR
R3 (c1 , c22 , d1)
 D (d1 , d2)

Pro
oblem-04::
Findd the miniimum num
mber of tab
bles requirred to reprresent the given ER diagram in
rela
ational moddel-

Sollution-
Appplying the rules, min
nimum 3 tables
t will be required-
 E1 (a1 , a2)
 E2
2R1R2 (b1 1 , b2 , a1 , c1 , b3)
 E3
3 (c1 , c2)
Prepared by SKS. CS
SE Page 30 of
o 31
Probblem-05:
Fin
nd the min
nimum num
mber of ta
ables requiired to rep
present the
e given ER
R diagram in
rela
ational mod
del-

Sollution-
App
plying the rules thatt we have learnt, miinimum 6 tables willl be requirred-
 Ac
ccount (Acc_no , Bala
ance , b_nname)
 Brranch (b_n
name , b_ccity , Assetts)
 Lo
oan (L_no , Amt , b_
_name)
 Boorrower (C
C_name , L_no)
L
 Cuustomer (C
C_name , C_street , C_city)
 Depositor (C
C_name , Ac_no)
A

Prepared by SKS. CS
SE Page 31 of
o 31

You might also like