DBMS Unit-2
DBMS Unit-2
2.1 Introduction:
Peter Chen first proposed modelling databases using a graphical technique that
humans can relate to easily. Humans can easily perceive entities and their characteristics
in the real world and represent any relationship with one another. Entity–Relationship
(ER) model gives the conceptual model of the world to be represented in the database.
The main motivation for defining the ER model is to provide a high level model for
conceptual database design, which acts as an intermediate stage prior to mapping the
enterprise being modelled onto a conceptual level.
The ER model achieves a high degree of data independence which means that the
database designer do not have to worry about the physical structure of the database. A
database schema in ER model can be pictorially represented by Entity–Relationship
diagram.
1
– Lines represent linking of attributes to entity sets and of entity sets to
relationship sets.
Example of ER diagram
In the ER diagram the two entities are STUDENT and CLASS. Two simple attributes
which are associated with the STUDENT are Roll number and the name. The attributes
associated with the entity CLASS are Subject Name and Hall Number. The relationship
between the two entities STUDENT and CLASS is Attends.
Strong Entity
Strong entity is one whose existence does not depend on other entity.
Example
Consider the example, student takes course. Here student is a strong entity.
2
In this example, course is considered as weak entity because, if there are no students to
take a particular course, then that course cannot be offered. The COURSE entity depends
on the STUDENT entity.
Weak Entity
Weak entity is one whose existence depends on other entity.
Example
Consider the example, customer borrows loan. Here loan is a weak entity. For every
loan, there should be at least one customer. Here the entity loan depends on the entity
customer hence loan is a weak entity.
3
Single Value Attribute
Single value attribute means, there is only one value associated with that attribute.
Example
The examples of single value attribute are age of a person, Roll number of the student,
Registration number of a car, etc.
Multivalued Attribute
In the case of multivalue attribute, more than one value will be associated with that
attribute. For example, a person can have more than one phone number, email_address,
etc.
Examples of Multivalued Attribute
Derived Attribute
Derived attributes are the attributes that do not exist in the physical database, but their
values are derived from other attributes present in the database. For example,
average_salary in a department should not be saved directly in the database, instead it
can be derived. For another example, age can be derived from data_of_birth.
Example of Derived Attribute
4
Example
In application forms, there is one column called phone no. if a person do not have phone
then a null value is entered in that column.
Simple attribute − Simple attributes are atomic values, which cannot be divided
further. For example, a student's phone number is an atomic value of 10 digits
Composite attribute − Composite attributes are made of more than one simple ttribute.
For example, a student's complete name may have first_name and last_name.
Consider the attribute “address” which can be further subdivided into Street name, City,
and State.
Relationship degree refers to the number of associated entities. The relationship degree
can be broadly classified into unary, binary, and ternary relationship.
2.5.1 Unary Relationship
The unary relationship is otherwise known as recursive relationship. In the unary
relationship the number of associated entity is one. An entity related to itself is known as
recursive relationship.
5
2.5.2 Binary Relationship
In a binary relationship, two entities are involved. Consider the example; each staff will
be assigned to a particular department. Here the two entities are STAFF and
DEPARTMENT.
6
The relationship that associates one entity to more than one entity is called one-to-many
relationship. Example of one-to-many relationship is Country having states. For one
country there can be more than one state hence it is an example of one-to-many
relationship. Another example of one-to-many relationship is parent–child relationship.
For one parent there can be more than one child. Hence it is an example of one-to-many
relationship.
2.6.2 One-to-One Relationship Type
One-to-one relationship is a special case of one-to-many relationship. True one-to-one
relationship is rare. The relationship between the President and the country is an
example of one-to-one relationship. For a particular country there will be only one
President. In general, a country will not have more than one President hence the
relationship between the country and the President is an example of one-to-one
relationship. Another example of one-to-one relationship is House to Location. A house is
obviously in only one location.
2.6.3 Many-to-Many Relationship Type
The relationship between EMPLOYEE entity and PROJECT entity is an example of many-
to-many relationship. Many employees will be working in many projects hence the
relationship between employee and project is many-to- many relationship.
2.6.4 Many-to-One Relationship Type
The relationship between EMPLOYEE and DEPARTMENT is an example of many-to-one
relationship. There may be many EMPLOYEES working in one DEPARTMENT. Hence
relationship between EMPLOYEE and DEPARTMENT is many-to-one relationship. The four
relationship types are summarized and shown in Table 2.1.
7
Regular entities are entities that have an independent existence and generally represent
real-world objects such as persons and products. Regular entities are represented by
rectangles with a single line.
2.7.2 Mapping Regular Entities
– Each regular entity type in an ER diagram is transformed into a relation. The name
given to the relation is generally the same as the entity type.
– Each simple attribute of the entity type becomes an attribute of the relation.
– The identifier of the entity type becomes the primary key of the corresponding relation.
Example 1
Mapping regular entity type tennis player
Here,
– Entity name = Name of the relation or table.
In our example, the entity name is PLAYER which is the name of the table
– Attributes of ER diagram=Column name of the table.
In our example the Name, Nation, Position, and Number of Grand slams won which
forms the column of the table.
2.7.3 Converting Composite Attribute in an ER Diagram to Tables
When a regular entity type has a composite attribute, only the simple component
attributes of the composite attribute are included in the relation.
Example
In this example the composite attribute is the Customer address, which consists of
Street, City, State, and Zip.
8
When the regular entity type contains a multi-valued attribute, two new relations are
created.
The first relation contains all of the attributes of the entity type except the multi-valued
attribute.
The second relation contains two attributes that form the primary key of the second
relation. The first of these attributes is the primary key from the first relation, which
becomes a foreign key in the second relation. The second is the multi-valued attribute.
9
2.7.5 Converting “Weak Entities” in ER Diagram to Tables
Weak entity type does not have an independent existence and it exists only through an
identifying relationship with another entity type called the owner.
For each weak entity type, create a new relation and include all of the simple attributes
as attributes of the relation. Then include the primary key of the identifying relation as a
foreign key attribute to this new relation.
The primary key of the new relation is the combination of the primary key of the
identifying and the partial identifier of the weak entity type. In this example DEPENDENT
is weak entity.
10
Here we have two entities CUSTOMER and ORDER. The relationship between CUSTOMER
and ORDER is one-to-many. For two entities CUSTOMER and ORDER, two tables namely
CUSTOMER and ORDER are created as shown later. The primary key CUSTOMER ID in
the CUSTOMER relation becomes the foreign key in the ORDER relation.
CUSTOMER
11
The first step is to create three relations, one for each of the two participating entity
types and the third for the associative entity. The relation formed from the associative
entity is associative relation.
12
2.7.8 Converting Unary Relationship to Tables
Unary relationships are also called recursive relationships. The two most important cases
of unary relationship are one-to-many and many-to-many.
One-to-many Unary Relationship
Each employee has exactly one manager. A given employee may manage zero to many
employees. The foreign key in the relation is named Manager-ID. This attribute has the
same domain as the primary key Employee-ID.
13
The primary key attributes – Patient ID, Physician ID, and Treatment Code – become
foreign keys in PATIENT TREATMENT. These attributes are components of the primary
key of PATIENT TREATMENT.
14