DBMS Module 2
DBMS Module 2
-Jyoti Bansode
SAKEC
Entity-Relationship Model
� A data model is a collection of conceptual tools for
describing data, data relationships,data semantics and
consistency constraints.
� Entity-Relationship Model: The entity-relationship
(E-R) data model uses a collection of basic objects,
called entities, and relationships among these objects.
� An entity is a “thing” or “object” in the real world that
is distinguishable from other objects.(person, car,
student, employee)or it may be an object with a
conceptual existence ( company, course,project).
Entities,Attributes and
Relationships
� Entities are described in a database by a set of attributes. e.g.
Roll_No,Name,Branch,Marks are the attributes of Student
entity
� A relationship is an association among several entities.
� E.g. Faculty works_in department, so works_in is the
relationship between Faculty and department
� The set of all entities of the same type and the set of all
relationships of the same type are termed an Entity set and
Relationship set, respectively.
� The overall logical structure (schema) of a database can be
expressed graphically by an Entity-Relationship (E-R)
diagram.
Key Attributes
� An entity type usually has one or more attributes whose
values are distinct for each individual entity in the entity
set. Such an attribute is called a key attribute, and its
values can be used to identify each entity uniquely.
C2 JAVA 10 C2 S1 IV 2nd
C2 S2 IV 2nd
E-R Diagram
� Employee is the strong entity
with Employee_id as primary
key (which is represented by
solid underlined) and
Dependent is the weak entity.
� The discriminator(Name) of a
weak entity is underlined with
a dashed, rather than a solid,
line.
� The relationship set
connecting the weak entity set
to the identifying strong
entity set is depicted by a
double diamond.
E-R Diagram
E-R Diagram
� As you can see in the diagram,
Customer is the Strong Entity
with (cust_id as a primary key)
as it does not depend on any other
Entity.
� Loan is the Weak Entity as it
depends on Customer. If
Customer is not there Loan will
not exists.
� In Loan Entity Loan_name is
discriminator which is
represented by dashed(_ _ _ _)
underline.
Attributes
� Attributes: Attributes are descriptive properties
possessed by each member of an entity set.
� E.g. Entity Student can have attributes such as (Rno,
Name,Phone_No, Address, Percentage,DOB)
� Student((Rno,Name,Phone_No,Address,Percentage,DOB)
� Entity Department can have attributes as(Dno, Dname,
HOD)
� Depaertment(Dno, Dname, HOD)
�Multivalued Attributes:
These attributes can take more than one
value for a given entity from an entity set.
�They are represented by Double Oval.
�In the given diagram Mob_no and
Email_id are Multivalued Attributes as a
person can have more than one phone
numbers and email_ids
Types Of Attributes
� Derived Attributes:
Derived attributes are those attributes
which can be derived from other
attribute(s).
� They are represented as dashed oval.
�In the diagram Age is the Derived
Attribute as it is derived from
DOB(Date Of Birth) attribute.
�Key Attributes: Key attributes
are those attributes which can identify
an entity uniquely in an entity set.
�It is represented by Underlining the
attribute.
�In the diagram Roll_No is the Key
Attribute as it Uniquely Identifies the
particular entity.
Types Of Attributes
E-R Diagram with different
Attributes
Relationship Set
� A relationship is an association among several entities.
For example, a member relationship associates an
instructor with her department.
� The set of all relationships of the same type are termed
as relationship set
� Relationship sets are represented by a diamond
connecting a pair of related entity sets. The name of the
relationship is placed inside the diamond.
member
Relationship Set
� In this diagram there are
two Entities Student
(stu_id, Stu_name,
Stu_Addr) and College
(Col_ID,Col_Name)
and Relationship
between these two
entities is named as
StudyIn
Identifying Relationship Set
� The relationship associating
the weak entity set with the
identifying entity set is
called the identifying
relationship.
⮚ It is shown by Double
Diamond
⮚ Double line indicates total
participation of weak entity
⮚ Every section must be
related via sec_course to
some course
Identifying Relationship Set
� This is another notation of E-R
diagram. In this there are two
Entities
Customer(Cust_Id,Cust_name,C
ust_add) and
Loan(loan_name,loan,date)
� As the Loan is the weak entity
,the relationship Borrows is
shown in double diamond as it
is Identifying Relationship
� Double lines indicate total
participation of an entity in a
relationship set.
� Total Participation means every
loan should be related via
Borrows to Customer
Participation Constraint
�
Participation Constraint
is applied on the entity
participating in the
relationship set.
� Total Participation:
� Each entity in the entity
set must participate in
the relationship. If each
student must enroll in a
course, the participation
of student will be total.
Total participation is
shown by double line in
ER diagram.
� Partial Participation:
� The entity in the entity set may
or may NOT participate in the
relationship.
� If some courses are not
enrolled by any of the student,
the participation of course will
be partial.
� The diagram depicts the
‘Enrolled in’ relationship set
with Student Entity set having
total participation and Course
Entity set having partial
participation.
Relationship Types
� There are different types of Relationships like:
� Binary Relationship
� Ternary Relationship
� N-Ary Relationship
� Recursive Relationship
Relationship Types
� Binary Relationship:
� When there is a relationship
between two different entities,
it is known as a binary
relationship.
� Ternary Relationship:
� When there is a relationship
between three different
entities, it is known as a
ternary relationship.
Relationship Types
� N-Ary Relationship: When any
relationship consists of more than
two Entities then it is called N-ary
relationship.
� One-To-Many
� Many-To-One
� Many-To-Many
Mapping Cardinality
� One-To-One:An entity in A is
associated with at most one entity
in B, and an entity in B is
associated with at most one entity
in A.
� E.g. Student can enrol at most one
course and course can have at most
one student.
� E.g. We draw a directed line from
the relationship set advisor to both
entity sets instructor and student.
This indicates that an instructor
may advise at most one student,
and a student may have at most
one advisor.
Mapping Cardinality
� One-To-Many:An entity in
A is associated with any
number (zero or more)of
entities in B. An entity in B,
however, can be associated
with at most one entity in A.
� E.g. An instructor may advise
many students, but a student
may have at most one advisor.
� e.g. A student can enrol for
many courses
Mapping Cardinality
� 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 (zero or more) of
entities in A.
� This indicates that an Customer
may borrows at most one loan,
but a loan may be borrowed by
many Customers
� This is another notation which
shows student can take at most
one course and one course can be
taken by many students.
Mapping Cardinality
� Many-To-Many: An entity in
A is associated with any number
(zero or more)of entities in B, and
an entity in B is associated with
any number (zero or more) of
entities in A.
� An instructor may advise many
students, and a student may have
many advisors.
� This is another notation to show
mapping cardinality. Which
shows one student can take many
courses and one course can be
taken by many students.
E-R Diagram of Banking System
Enhanced/Extended Entity-
Relationship(EER)
� Although the basic E-R concepts can model most database
features, some aspects of a database may be more appropriately
expressed by certain extensions to the basic E-R model.
� Features of EER Model:
1. EER creates a design more accurate to database schemas.
2. It reflects the data properties and constraints more precisely.
3. It includes all modeling concepts of the ER model.
4. Diagrammatic technique helps for displaying the EER schema.
5. It includes the concept of specialization and generalization.
6. It is used to represent a collection of objects that is union of
objects of different of different entity types.
Extended Entity-Relationship(EER)
� It is a diagrammatic technique for displaying the
following concepts:
� Sub Class and Super Class
� Union or Category
� Aggregation
Sub Class and Super Class
⮚ Sub Class and Super Class:
� Sub class and Super class relationship leads the concept
of Inheritance.
� The relationship between sub class and super class is
denoted with d symbol.
Domain:
create table student(roll_no numeric(2),name varchar(10), marks
numeric(3));
2. Student(Roll_no,Reg_no,Name,Ph_no,Marks,Dno)
Dno will be the foreign key which will take values from Dno of
Department relation.
Supplier
Sno Sname Location Ph_no Shipment
S1 ABC Delhi 56677885 Sno Pno Qty
S2 XYZ Mumbai 12324566 S1 P1 10
S3 PQR Delhi 7890067 S1 P2 5
S2 P2 15
Product
Pno Pname Price
P1 Keyboard 3000
P2 Mouse 1000
Example1
1. Constraints that are inherent in the data model. We call these inherent
model-based constraints or implicit constraints.
e.g. The constraint that a relation cannot have duplicate tuples is an inherent
constraint
e.g.
Create table student (roll_no int(5) primary key,reg_no int(10), Name
varchar(40), ph_no int(10), marks int(3) ,dno varchar(5));
Schema-based constraints or Explicit
constraints.
A relation can have only ONE primary key. This primary key can
consist of single or multiple attributes(fields).
Primary Key Constraints
The relation containing the foreign key is called the child relation
and the relation containing the primary key is called the
referenced or parent relation.
Referential Integrity Constraint
(Foreign Key)
Referential Integrity Constraint:Using Create command
1. create table department (dno varchar(5) primary key,
dname varchar(30) unique, HOD varchar(35) not null);
2. Create table student (roll_no int(5) primary key,reg_no int(10), Name
varchar(40) not null , ph_no int(10) unique, marks int(3) ,dno varchar(5)
references department(dno), unique (reg_no);
3. Create table student (roll_no int(5) primary key,reg_no int(10), Name
varchar(40) not null, ph_no int(10) not null, marks int(3) ,dno
varchar(5),constraint uk unique (reg_no), foreign key(dno) references
department(dno);
10 IT XYZ
ER-to-Relational Mapping Algorithm
Slide 7- 107
■ Mapping EER Model Constructs to
Relations
■ Step 8: Options for Mapping Specialization
or Generalization.
■ Step 9: Mapping of Union Types
(Categories).
The ER conceptual schema diagram for the COMPANY
database. Figure1
Slide 7- 109
Result of mapping the COMPANY ER schema into a
relational schema.
Slide 7- 110
ER-to-Relational Mapping Algorithm
Slide 7- 111
According to step1 there will be three strong entities
which will be get converted as following relations:
Slide 7- 113
■ Example: Create the relation
DEPENDENT in this step to correspond
to the weak entity type DEPENDENT.
■ Include the primary key SSN of the
EMPLOYEE relation as a foreign key attribute
of DEPENDENT (renamed to ESSN).
■ The primary key of the DEPENDENT relation
is the combination {ESSN,
DEPENDENT_NAME} because
DEPENDENT_NAME is the partial key of
DEPENDENT.
According to step 2 there will be one more relation as
Dependent:
4. Dependent
(Essn,Dependent_name,Sex,Bdate,Relation):
Where (Essn , Dependent_Name) is composite primary
key and Essn is Foreign key which will refer to Ssn of
Emp
ER-to-Relational Mapping
Algorithm (contd.)
■ Step 3: Mapping of Binary 1:1 Relation Types
■ For each binary 1:1 relationship type R in the ER
schema, identify the relations S and T that
correspond to the entity types participating in R.
■ There are three possible approaches:
1. Foreign Key approach: Choose one of the relations-
say S-and include a foreign key in S the primary key of
T. It is better to choose an entity type with total
participation in R in the role of S.
■ Example: 1:1 relation MANAGES is mapped by
choosing the participating entity type DEPARTMENT
to serve in the role of S, because its participation in
the MANAGES relationship type is total.
.
Slide 7- 116
2.Merged relation option: An alternate mapping of a
1:1 relationship type is possible by merging the two
entity types and the relationship into a single relation.
This may be appropriate when both participations are
total.
3.Cross-reference or relationship relation option:
The third alternative is to set up a third relation R for
the purpose of cross-referencing the primary keys of
the two relations S and T representing the entity types
Manages is the 1:1 relationship. According to step 3
Mgr_Ssn will be added as Foreign key and Mgr_start_date
will be added as an attribute. So Department will have
following attributes:
Department
(Dname,Dnumber,Mgr_Ssn,Mgr_start_date) : Where
Dno as Primary key and Mgr_Ssn will be foreign key
which will refer to Ssn of Employee relation.
ER-to-Relational Mapping
Algorithm (contd.)
■ Step 4: Mapping of Binary 1:N Relationship
Types.
■ For each regular binary 1:N relationship
type R, identify the relation S that represent
the participating entity type at the N-side of
the relationship type.
■ Include as foreign key in S the primary key
of the relation T that represents the other
entity type participating in R.
■ Include any simple attributes of the 1:N
relation type as attributes of S.
Slide 7- 119
■ Example: 1:N relationship types
WORKS_FOR, CONTROLS, and
SUPERVISION in the figure.
■ For WORKS_FOR we include the primary
key DNUMBER of the DEPARTMENT
relation as foreign key in the EMPLOYEE
relation and call it DNO.
There are three 1:N relationship types WORKS_FOR,
CONTROLS, and SUPERVISION in the figure. According to
step 4 the relations will be as follows after adding
foreign keys in Employee and Project relations:
Employee (Fname,Mint,Lname,Ssn,Bdate,Address,Sex,
Salary,Super_Ssn,Dno) : Where Super_Ssn will be foreign
key(for Supervision relation) which will refer to Ssn of
same relation and Dno will be the foreign key(for
Works_For relationship) which will refer to Dnumber of
Department
Project (Pname,Pnumber,Location,Dnum) : Where Dnum
will be foreign key(for Controls relationship) which will refer
to Dnumber of Department.
ER-to-Relational Mapping Algorithm
(contd.)
■ Step 5: Mapping of Binary M:N
Relationship Types.
■ For each regular binary M:N relationship type
R, create a new relation S to represent R.
■ Include as foreign key attributes in S the primary
keys of the relations that represent the
participating entity types; their combination will
form the primary key of S.
■ Also include any simple attributes of the M:N
relationship type (or simple components of
composite attributes) as attributes of S.
Slide 7- 122
■ Example: The M:N relationship type WORKS_ON from
the ER diagram is mapped by creating a relation
WORKS_ON in the relational database schema.
■ The primary keys of the PROJECT and
EMPLOYEE relations are included as foreign keys
in WORKS_ON and renamed PNO and ESSN,
respectively.
■ Attribute HOURS in WORKS_ON represents the
HOURS attribute of the relation type. The primary
key of the WORKS_ON relation is the combination
of the foreign key attributes {ESSN, PNO}.
There is one M:N relationship types WORKS_ON in the
figure. According to step 5 there will be separate
relation of WORKS_ON as follows:
6.Dept_Locations(Dnumber,Dlocation):Where
(Dnumber,Dlocation) will be composite primary key.
Dnumber will be foreign key which will refer to
Dnumber of Department relation.
So finally there will be total 6 relations for E-R
diagram of Figure 1.
1.Employee (Fname,Mint,Lname,Ssn,Bdate,Address,Sex,
Salary,Super_Ssn,Dno)
2. Department(Dname,Dnumber,Mgr_Ssn,Mgr_start_date)
3.Project (Pname,Pnumber,Location,Dnum)
4. Dependent
(Essn,Dependent_name,Sex,Bdate,Relation)
5.Works_On(Essn,Pno,Hours)
6.Dept_Locations(Dnumber,Dlocation)
ER-to-Relational Mapping Algorithm (contd.)
■ Step 7: Mapping of N-ary Relationship
Types.
■ For each n-ary relationship type R, where
n>2, create a new relationship S to
represent R.
■ Include as foreign key attributes in S the
primary keys of the relations that represent
the participating entity types.
■ Also include any simple attributes of the n-
ary relationship type (or simple components
of composite attributes) as attributes of S.
Slide 7- 129
■ Example: The relationship type SUPPLY
in the ER on the next slide.
■ This can be mapped to the relation SUPPLY
shown in the relational schema, whose primary
key is the combination of the three foreign keys
{SNAME, PARTNO, PROJNAME}
FIGURE 4.11
Ternary relationship types. (a) The SUPPLY relationship.
Slide 7- 131
FIGURE 7.3
Mapping the n-ary relationship type SUPPLY from
Figure 4.11a.
Slide 7- 132
Summary of Mapping constructs
and constraints
Table 7.1 Correspondence between ER and Relational Models
ER Model Relational Model
Entity type “Entity” relation
1:1 or 1:N relationship type Foreign key (or “relationship” relation)
M:N relationship type “Relationship” relation and two foreign keys
n-ary relationship type “Relationship” relation and n foreign keys
Simple attribute Attribute
Composite attribute Set of simple component attributes
Multivalued attribute Relation and foreign key
Value set Domain
Key attribute Primary (or secondary) key Slide 7- 133
Mapping EER Model Constructs
to Relations
■ Step8: Options for Mapping Specialization or Generalization.
■ Convert each specialization with m
subclasses {S1, S2,….,Sm} and
generalized superclass C, where the
attributes of C are {k,a1,…an} and k
is the (primary) key, into relational
schemas using one of the four
following options:
■Option 8A: Multiple relations- Superclass and subclasses
■Option 8B: Multiple relations-Subclass relations only
Slide 7- 134
Mapping EER Model Constructs
to Relations
■ Option 8A: Multiple relations- Superclass and subclasses
■ Create a relation L for C with attributes Attrs(L) = {k,a1,…an}
and PK(L) = k. Create a relation Li for each subclass Si, 1 < i <
m, with the attributesAttrs(Li) = {k} U {attributes of Si} and
PK(Li)=k. This option works for any specialization (total or
partial, disjoint of over-lapping).
■ Option 8B: Multiple relations-Subclass relations only
■ Create a relation Li for each subclass Si, 1 < i < m, with the
attributes Attr(Li) = {attributes of Si} U {k,a1…,an} and PK(Li) =
k. This option only works for a specialization whose subclasses
are total (every entity in the superclass must belong to (at least)
one of the subclasses).
Slide 7- 135
EER diagram notation for an attribute-defined
specialization on JobType.(Example of option 8A)
Slide 7- 136
Options for mapping specialization or generalization.
(a) Mapping the EER schema using option 8A.
Slide 7- 137
Generalization. (b) Generalizing CAR and TRUCK into the
superclass VEHICLE. (Example Of option 8B)
Slide 7- 138
Options for mapping specialization or generalization.
(b) Mapping the EER schema using option 8B.
Slide 7- 139
Mapping EER Model Constructs to
Relations (contd.)
■ Option 8C: Single relation with one type attribute
■ Create a single relation L with attributes Attrs(L) =
{k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t}
and PK(L) = k. The attribute t is called a type (or
discriminating) attribute that indicates the subclass to
which each tuple belongs
■ Option 8D: Single relation with multiple type
attributes
■ Create a single relation schema L with attributes Attrs(L) =
{k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t1,
t2,…,tm} and PK(L) = k. Each ti, 1 < I < m, is a Boolean type
attribute indicating whether a tuple belongs to the subclass
Si.
Slide 7- 140
EER diagram notation for an attribute-defined specialization
on JobType. (Example of option 8C)
Slide 7- 141
Options for mapping specialization or generalization.
(c) Mapping the EER schema using option 8C.
Slide 7- 142
EER diagram notation for an overlapping (non-disjoint)
specialization. (Example of option 8D)
Slide 7- 143
Options for mapping specialization or generalization. (d)
Mapping using option 8D with Boolean type fields Mflag
and Pflag.
Slide 7- 144
Summary of options 8A,8B,8C,8D
Slide 7- 145
Mapping EER Model Constructs to Relations
(contd.)
Slide 7- 146
A specialization lattice with multiple inheritance for a
UNIVERSITY database.
Slide 7- 147
Mapping the EER specialization lattice in previous slide
figure using multiple options.
Slide 7- 148
Mapping EER Model Constructs to Relations
(contd.)
■ Step 9: Mapping of Union Types (Categories).
■ For mapping a category whose defining superclass
have different keys, it is customary to specify a new
key attribute, called a surrogate key, when creating a
relation to correspond to the category.
■ In the example below we can create a relation
OWNER to correspond to the OWNER category and
include any attributes of the category in this relation.
The primary key of the OWNER relation is the
surrogate key, which we called OwnerId.
Slide 7- 149
Two categories (union types): OWNER and
REGISTERED_VEHICLE.
Slide 7- 150
Mapping the EER categories (union types) in previous
slide figure to relations.
Slide 7- 151
Chapter Summary
Slide 7- 152
Chapter Summary