Unit-02 Notes - DBMS
Unit-02 Notes - DBMS
Introduction
ER Data Model is based on the real world objects and their relationship. In other words,
each and everything, either living or non-living things in this world forms the object in
database world. We identify all the required objects for our database requirement and give
the shape of database objects. Before putting them into database, it is very much essential to
understand the requirement properly and design them efficiently. It is like a foundation of
the building. For this purpose, we use ER diagrams where we plan the database pictorially.
ER diagram basically breaks requirement into entities, attributes and relationship. In this
unit we will discuss in detail about the E-R Data model and how to convert the E-R model
to relational tables.
Learning Objectives
After learning this unit you should be able to
• Define an entity and identify different types of entities
• Define an Attribute and A relationship
• Identify different types of Keys and Constraints
• Know the symbols used to represent the ER DiagramSymbols
• Understand the principles of Conversion of ER diagrams toRelations
Entity
In a database, we would be grouping only related data called attributes together and
storing them under one group name called Entity. In relational form an entity is
represented in a Table. This helps in identifying which data is stored where and under
what name. It reduces the time to search for a particular data in a whole database. Say, we
are creating a School database. Then what all things come into our mind? It is school,
students, teachers, subjects, class etc. If you observe what we have listed are items/names
which are part of school, but each of them having their own group of related information?
i.e.; Student has ID, name, address, DOB, class in which he is studying etc. for a Student.
Similarly, TEACHERS will have their ID, name, address subjects that they are teaching,
class which they are teaching etc. Hence each entity forms a group of related information
called attributes. In real world each and every object forms an entity. In short, all the
living and non-living things in the world form entity. One can consider all nouns as
entities, if that makes it simpler.
• Strong Entity: Entities having its own attribute(s) as primary key(s) are called
strong entity. For example, STUDENT has STUDENT_ID as primary key. Hence it is a
strong entity.
YCCE. Dept. of CSE
Page 2
• Weak Entity: Entities which cannot form their own attribute as primary key are
known as weak entities. These entities will derive their primary keys from the
combination of its attribute and primarykey from its mapping entity.
Consider CLASS and SECTION entity. The SECTION has SECTION _ID and NAME as
its attribute. But SECTION _ID alone cannot be a primary key, since it fails to tell for
which course it is related to. We will not be uniquely identifying the course section by
this attribute alone. But if this attribute along with CLASS_ID gives the meaning for each
section and we can uniquelyidentify the sections.
• Recursive Entity: If a relation exists between the same entities, then such entities
are called as recursive entity. For example, mapping between manager and employee is
Attribute
• The characteristics of an entity are called attributes. Ex. Roll no, name, marks,
etc are the attributes of an entity student.
• An attribute is a descriptive property of an entity
• An attribute can have single value or multiple value or range of values.
• In addition, each attribute can contain certain type of data like only numeric
value, or only alphabets, or combination of both, or date or negative or positive
values etc.
Depending on the values that an attribute can take, it is divided intodifferent type.
1. Simple Attribute
These kinds of attributes have values which cannot be divided further. For example,
STUDENT_ID attribute which cannot be divided further. Passport Number is unique value
and it cannot be divided.
2. Composite Attribute
This kind of attribute can be divided further to more than one simple attribute. For example,
address of a person. Here address can be further divided as Door#, street, city, state and pin
which are simple attributes.
3. Derived Attribute
Derived attributes are the one whose value can be obtained from other attributes of entities in
the database. For example, Age of a person can be obtained from date of birth and current date.
Average salary, annual salary, total marks of a student etc are few examples of derived
attribute.
4. Stored Attribute
The attribute which gives the value to get the derived attribute are called Stored Attribute. In
example above, age is derived using Dateof Birth. Hence Date of Birth is a stored attribute.
5. Single Valued Attribute
These attributes will have only one value. For example, EMPLOYEE_ID, passport#, driving
license#, SSN etc have only single value for a person.
YCCE. Dept. of CSE
Page 4
6. Multi-Valued Attribute
These attribute can have more than one value at any point of time. Manager can have more
than one employee working for him, a person can have more than one email address, and
more than one house etc is the examples.
7. Simple Single Valued Attribute
This is the combination of above four types of attributes. An attribute can have single value at
any point of time, which cannot be divided further. For example, EMPLOYEE_ID – it is
single value as well as it cannot be divided further.
8. Simple Multi-Valued Attribute
Phone number of a person, which is simple as well as he can have multiple phone numbers is
an example of this attribute.
9. Composite Single Valued Attribute
Date of Birth can be a composite single valued attribute. Any person can have only one
DOB and it can be further divided intodate, month and year attributes.
10. Composite Multi-Valued Attribute
Shop address which is located two different locations can be considered as example of this
attribute.
11. Descriptive Attribute
Attributes of the relationship is called descriptive attribute. For example, employee works for
department. Here ‘works for’ is the relation between employee and department entities. The
relation ‘works for’ can have attribute DATE_OF_JOIN which is a descriptive attribute.
Keys
• An important constraint on the entities of an entity type is the key or
uniqueness constraint on attributes.
• A key is an attribute (also known as column or field) or a combination of
(c)rimary Key - A Candidate Key that is used by the database designer for unique
identification of each row in a table is known as Primary Key. A Primary Key can consist of
one or more attributes of a table.
Example of Primary Key: Database designer can use one of the Candidate Key as a Primary
Key. In this case we have "Code” and "Name, Address” as Candidate Key, we will consider
"Code” Key as a Primary Key as the other key is the combination of more than one attributes.
(d) Foreign Key - A foreign key is an attribute or combination of attribute in one base table
that points to the candidate key (generally it is the primary key) of another table. The purpose
of the foreign key is to ensure referential integrity of the data i.e. only values that are
supposed to appear in the database are permitted.
Examples of Foreign Key –
Let consider we have another table i.e. Department Table with Attributes “Department_ID”,
“Department_Name”, “Manager_ID”, ”Location_ID” with Department_ID as an Primary
Key. Now the Department_ID attribute of Employee Table (dependent or child table) can be
defined as the Foreign Key as it can reference to the Department_ID attribute of the
Departments table (the referenced or parent table), a Foreign Key value must match an
existing value in the parent table or be NULL.
(e)Composite Key - If we use multiple attributes to create a Primary Key then that Primary
Key is called Composite Key (also called a Compound Key or Concatenated Key).
Examples of Composite Key: if we have used “Name, Address” asa Primary Key then it will
be our Composite Key.
(f) Alternate Key - Alternate Key can be any of the Candidate Keys except for the Primary
Key.
Examples of Alternate Key is “Name, Address” as it is the only other Candidate Key which
is not a Primary Key.
(g) Secondary Key - The attributes that are not even the Super Key but can be still
used for identification of records (not unique) are known as Secondary Key.
Examples of Secondary Key can be Name, Address, Salary, Department_ID etc. as they can
identify the records but they might not be unique.
Relationship
• There are several implicit relationships among the various entity types.
• In fact, whenever an attribute of one entity type refers to another entity type, some
relationship exists.
• For example, the attribute Manager of department refers to an employee who
manages the department.
• In the ER model, these references should not be represented as relationships or
YCCE. Dept. of CSE
Page 7
relation. There is a relation “borrower” in the entities customer and account which can be
shown as follows:
Degrees of Relationship
In a relationship two or more number of entities can participate. The number of entities who
are part of a particular relationship is called degrees of relationship. If only two entities
participate in the mapping, then degree of relation is 2 or binary. If three entities are involved,
then degree of relation is 3 or ternary. If more than 3 entities are involved then the degree of
relation is called n-degree orn-nary.
Cardinality of Relationship
How many number of instances of one entity is mapped to how many number of instances of
another entity is known as cardinality of a relationship. In a ‘studies’ relationship above, what
we observe is only one Student X is studying in on Class Y. i.e.; single instance of entity
student mapped to a single instance of entity Class. This means the cardinality between
Student and Class is 1:1.
Constraints
This represents how an entity is involved in the relation. That means, if all the entity values
are participating in any relation, then it is called total participation. If only few values of an
entity are partof relation, then it is a partial participation.
For example, ‘Employee Works for a Department’. Here all the employees work for one or
the other department. No employees are left without department. Hence all the employees
are participating in this relation. Thus, participation of employee is total participation.
But individual Department will not have all the employees. Each department will have only
few groups of employees working. Hence partial participation of department is seen here.
ER Diagram Symbols
Since ER diagram is the pictorial representation of real worldobjects, it involves various
symbols and notation to draw the diagrams. Let us see one by one below.
Entity: Rectangles are used to represent the entity in the diagram.Name of the Entity is
written inside the rectangle.
Multi-value attributes are those which can take up more than one values are represented by
double oval shapes.
Derived attributes are those which can be derived from other attributes are represented by
oval shapes with dashed lines.
A composite attribute is an attribute that has two or more simple attributes as its components.
Each simple attribute is represented by an oval shape, but these attributes will be connected to
its parent attribute called composite attribute forming a tree structure.
The below mentioned figure, gives the representation of composite attribute, here address of
a student.
Relationship: A diamond shape is used to show the relationship between the entities. A
mapping with weak entity is shown using double diamond. Relationship name will be written
inside them.
Participation Constraints: Total participation constraints are shown by double lines and
partial participations are shown as singleline.
Let us create a simple ER diagram for a STUDENT database. What is the requirement of this
database?
‘Student attends class. Each class is divided into one or more sections. Each class will have
its own specified subjects. Students have to attend all the subjects of the class that he attends’.
Now let us identify what are the entities? STUDENT, CLASS, SECTION, SUBJECT are
the entities. Attributes of these entities are not specified here. But we know what could be
the entities of each of the entities. We can list them as below at this point of time.
ADDRESS
DOB
AGE
CLASS_ID
SECTION_ID
What are the relationships we have? ‘Attends’, ‘has section’, ‘have subjects’ and ‘studies
subjects’ are the relations here. With this knowledge of requirement, we can draw the ER
diagram as below.
Observe the diagram carefully. Did we miss or drew it correctly? Are we missing anything
on the diagram? Is it inferring correct requirement? What are our observations?
• Age attribute can be derived from DOB. Hence we have to draw dashed oval.
• Address is a composite attribute. We have to draw its sub attributes too. So that
Fig: Example ER Diagram with Strong Entities, Weak Entities, andSimple attribute,
Composite Attribute, Keys and Relationships etc.
Is the above diagram is a complete diagram? No, we have not added the cardinality and
participation in the diagram.
Participation Constraints
• All the Students attend any one of the class, but class can have only certain
Once designing ER diagram is complete, we need to put it into logical structure. But how it
can be done? Let us discuss this in thelast section.
Generalization
• The refinement from an initial entity set into successive levels of entity sub groupings
represents a top-down design process in which distinctions are made explicit.
The design process may also proceed in a bottom-up manner, in which multiple entity sets
are synthesized into a higher-level entity set on the basis of common features. The database
designer may have first identified a customer entity set with the attributes name, street, city,
and customer-id, and an employee entity set with the attributes name, street, city, employee-
id, and salary. There are similarities between the customer entity set and the employee
entity set in the sense that they have several attributes in common. This commonality can
be expressed by generalization, which is a containment relationship that exists between a
higher-level entity set and one or more lower-level entity sets. In our example, person is the
higher- level entity set and customer and employee are lower-level entity sets.
• Higher- and lower-level entity sets also may be designated by the terms super class and
subclass, respectively. The person entity set is the super class of the customer and employee
subclasses.
• For all practical purposes, generalization is a simple inversion of specialization. We
will apply both processes, in combination, in the course of designing the E-R schema for
an enterprise. In terms of the E-R diagram itself, we do not distinguish between
specialization and generalization. New levels of entity representation will be distinguished
(specialization) or synthesized (generalization) as the design schema comes to express fully
the database application and the user requirements of the database.
Specialization
• An entity set may include sub groupings of entities that are distinct in some way from
other entities in the set.
• For instance, a subset of entities within an entity set may have attributes that are not
shared by all the entities in the entity set. The E-R model provides a means for representing
these distinctive entity groupings.
• 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 types is described by a set of attributes that includes all the
attributes of entity set person plus possibly additional attributes.
• For example, customer entities may be described further by the attribute customer-id,
whereas employee entities may be described further by the attributes employee-id and salary.
• The process of designating sub groupings within an entity set is called specialization.
• The specialization of person allows us to distinguish among persons according to
whether they are employees or customers.
• As another example, suppose the bank wishes to divide accounts into two categories,
checking account and savings account. Savings accounts need a minimum balance, but the
YCCE. Dept. of CSE
Page 16
bank may set interest rates differently for different customers, offering better rates to favored
customers.
• Checking accounts have a fixed interest rate, but offer an overdraft facility; the overdraft
amount on a checking account must be recorded.
• The bank could then create two specializations of account, namely savings-account and
checking-account.
• As we saw earlier, account entities are described by the attributes account-number and
balance.
• The entity set savings-account would have all the attributes of account and an additional
attribute interest-rate.
• The entity set checking-account would have all the attributes of account, and an
additional attribute overdraft-amount.
• We can apply specialization repeatedly to refine a design scheme. For instance, bank
employees may be further classified as one of the category such as Officer, Teller, Secretary,
etc.
• Each of these employee types is described by a set of attributes that includes all the
attributes of entity set employee plus additional attributes. For example, officer entities may
be described further by the attribute office-number, teller entities by the attributes station-
number and hours-per-week, and secretary entities by the attribute hours-per-week. Further,
secretary entities may participate in a relationship secretary-for, which identifies which
employees areassisted by a secretary.
• An entity set may be specialized by more than one distinguishing feature. In our
example, the distinguishing feature among employee entities is the job the employee
performs. Another, coexistent, specialization could be based on whether the person is a
temporary (limited-term) employee or a permanent employee, resulting in the entity sets
temporary- employee and permanent-employee. When more than one specialization is
formed on an entity set, a particular entity may belong to multiple specializations. For
instance, a given employee may be a temporary employee who is a secretary.
In terms of an E-R diagram, specialization is depicted by a triangle component labeled ISA.
The label ISA stands for "is a” and represents, for example, that a customer "is a” person.
The ISA relationship may also be referred to as a super class- subclass relationship. Higher-
and lower-level entity sets are depicted as regular entity sets—that are, as rectangles
containing the name of the entity set.
Aggregation
• One limitation of the E-R model is that it cannot express relationships among
relationships.
• To illustrate the need for such a construct, consider the ternary relationship works-on,
which we saw earlier, between a employee, branch and job.
• Now, suppose we want to record managers for tasks performed by an employee at a
• It appears that the relationship sets works-on and manages can be combined into one
single relationship set.
• Nevertheless, we should not combine them into a single relationship, since some
employee, branch, job combinations many not have a manager.
• There is redundant information in the resultant figure, however, since every employee,
branch, job combination in manages is alsoin works-on.
• If the manager were a value rather than a manager entity, we could instead make
manager a multi valued attribute of the relationship works-on.
• But doing so makes it more difficult (logically as well as in execution cost) to find, for
example, employee-branch-job triples for which a manager is responsible. Since the manager
is a managerentity, this alternative is ruled out in any case.
• The best way to model a situation such as the one just described isto use aggregation.
• Aggregation is an abstraction through which relationships are treated as higher-level
2. Concert all single valued attributes of an entity is converted to a column of the table
All the attributes, whose value at any instance of time is unique, are considered as columns of
that table. In the STUDENT Entity, STUDENT_ID, STUDENT_NAME form the columns of
STUDENT table. Similarly, LECTURER_ID, LECTURER_NAME form the columns of
LECTURER table. And so on.
3. Convert the Key attributes in the ER diagram to the Primary keys of the table. In diagram
above, STUDENT_ID, LECTURER_ID, COURSE_ID and SUB_ID are the key attributes of the
entities. Hence we consider them as the primary keys of respective table.
4. Declare the foreign key column, if applicable.
In the diagram, attribute COURSE_ID in the STUDENT entity is from COURSE entity.
Hence add COURSE_ID in the STUDENT table and assign it foreign key constraint.
COURSE_ID and SUBJECT_ID in LECTURER table forms the foreign key column. Hence
by declaring the foreign key constraints, mapping between the tables are established.
6. Any composite attributes are merged into same table as different columns.
In the diagram above, Student Address is a composite attribute. It has Door#, Street, City,
State and Pin. These attributes are merged into STUDENT table as individual columns.
7. One can ignore derived attribute, since it can be calculated at any time.
In the STUDENT table, Age can be derived at any point of time by calculating the difference
between Date of Birth and current date. Hence we need not create a column for this attribute.
It reduces the duplicity in the database.
These are the very basic rules of converting ER diagram into tables and columns, and
assigning the mapping between the tables. Table structure at this stage would be as below:
YCCE. Dept. of CSE
Page 20
Let us see some of the special cases.
Converting Weak Entity
Weak entity is also represented as table. All the attributes of the weak entity forms the
column of the table. But the key attribute represented in the diagram cannot form the primary
key of this table. We have to add a foreign key column, which would be the primary key
column of its strong entity. This foreign key column along with its key attribute column
forms the primary key of the table.
In our example above, SUBJECTS is the weak entity. Hence, we create a table for it. Its
attributes SUBJECT_ID and SUBJECT_NAME forms the column of this table. Although
SUBJECT_ID is represented as key attribute in the diagram, it cannot be considered as
primary key. In order to add primary key to the column, we have to find the foreign key first.
COURSE is the strong entity related to SUBJECT. Hence the primary key COURSE_ID of
COURSE is added to SUBJECT table as foreign key. Now we can create a composite
primary key out of COURSE_ID and SUBJECT_ID.
Representing 1: N relationship
Consider SUBJECT and LECTURER relation, where each Lecturer teaches multiple subjects.
This is a 1: N relation. In this case, primary key of LECTURER table is added to the
SUBJECT table. i.e.; the primary key at 1 cardinality entity is added as foreign key to N
cardinality entity
Representing M: N relationship
Consider the example, multiple students enrolled for multiple courses, which is M: N
relation. In this case, we create STUDENT and COURSE tables for the entities. Create one
more table for the relation ‘Enrolment’ and name it as STUD_COURSE. Add the primary
keys of COURSE and STUDENT into it, which forms the composite primary key of the new
table.
YCCE. Dept. of CSE
Page 22
That is, in this case both the participating entities are converted into tables, and a new table is
created for the relation between them. Primary keys of entity tables are added into new table
to form the composite primary key. We can add any additional columns, if present as
attribute of the relation in ER diagram.