unit 2 dbms
unit 2 dbms
Components of ER Diagram
ER Model consists of Entities, Attributes, and Relationships among Entities in a Database
System.
What is Entity?
An Entity may be an object with a physical existence – a particular person, car, house,
or employee – or it may be an object with a conceptual existence – a company, a job, or a
university course.
What is Entity Set?
An Entity is an object of Entity Type and a set of all entities is called an entity set.
For Example, E1 is an entity having Entity Type Student and the set of all students is called
Entity Set. In ER diagram, Entity Type is represented as:
1. Types of Entity
1. Strong Entity
A Strong Entity is a type of entity that has a key Attribute. Strong Entity does not
depend on other Entity in the Schema. It has a primary key, that helps in identifying it
uniquely, and it is represented by a rectangle. These are called Strong Entity Types.
2. Weak Entity
An Entity type has a key attribute that uniquely identifies each entity in the entity set.
But some entity type exists for which key attributes can’t be defined. These are called Weak
Entity types.
For Example, A company may store the information of dependents (Parents, Children,
Spouse) of an Employee. But the dependents can’t exist without the employee. So Dependent
will be a Weak Entity Type and Employee will be Identifying Entity type for Dependent,
which means it is Strong Entity Type.
A weak entity type is represented by a Double Rectangle. The participation of weak
entity types is always total. The relationship between the weak entity types and its identifying
strong entity type is called identifying relationship and it is represented by a double diamond.
Types of Attributes
1. Key Attribute
The attribute which uniquely identifies each entity in the entity set is called the key
attribute. For example, Roll_No will be unique for each student. In ER diagram, the key
attribute is represented by an oval with underlying lines.
2. Composite Attribute
An attribute consisting of more than one value for a given entity. For example,
Phone_No (can be more than one for a given student). In ER diagram, a multivalued attribute
is represented by a double oval.
4. Derived Attribute
An attribute that can be derived from other attributes of the entity type is known as a
derived attribute. e.g. Age (can be derived from DOB). In ER diagram, the derived attribute
is represented by a dashed oval.
The Complete Entity Type Student with its Attributes can be represented as:
3. Relationship Type and Relationship Set
A Relationship Type represents the association between entity types. For example,
‘Enrolled in’ is a relationship type that exists between entity type Student and Course. In ER
diagram, the relationship type is represented by a diamond and connecting the entities with
lines.
A set of relationships of the same type is known as a relationship set. The following
relationship set depicts S1 as enrolled in C2, S2 as enrolled in C1, and S3 as registered in C3.
Relationship set
What is Cardinality?
The number of times an entity of an entity set participates in a relationship set is
known as cardinality. Cardinality can be of different types:
1. One-to-One: When each entity in each entity set can take part only once in the
relationship, the cardinality is one-to-one. Let us assume that a male can marry one female
and a female can marry one male. So the relationship will be one-to-one.
the total number of tables that can be used in this is 2.
3. Many-to-One: When entities in one entity set can take part only once in the relationship
set and entities in other entity sets can take part more than once in the relationship set,
cardinality is many to one. Let us assume that a student can take only one course, but one
course can be taken by many students. So the cardinality will be n to 1. It means that for one
course there can be n students but for one student, there will be only one course.
The total number of tables that can be used in this is 3.
Using Sets, it can be represented as:
each student is taking only 1 course but 1 course has been taken by many students.
4. Many-to-Many: When entities in all entity sets can take part more than once in the
relationship cardinality is many to many. Let us assume that a student can take more than one
course and one course can be taken by many students. So the relationship will be many to
many.
the total number of tables that can be used in this is 3.
Defining Entities
A thing in the real world with an independent existence. It may be an object with physical
existence (ex: house, person) or with a conceptual existence (ex: course, job). They are
represented by rectangle.
3. Many to One(N:1)
Many-to-one relationship in entity relationship diagram means that multiple entities
are related to only single entity in another table. For example, many developers works on
single project.
4. Many to Many relationship(M: N)
Many to many relationship means that multiple entities in one table is associated with
multiple entities in another table. For example, multiple customers have multiple accounts.
Bank and branch has One to Many relationship (a bank has multiple branches).
Branch and loan has also One to Many relationship(a branch can provide many loans).
Branch and employee has One to Many relationship(one branch has many employees).
Branch and account has One to Many relationship(one branch has many accounts).
Branch and customer has Many to Many relationship(multiple branches have multiple
customers).
Customer and account has Many to Many relationship(multiple customers have multiple
accounts).
Customer and loan has Many to Many relationships(multiple customers have multiple loans)
Identify Primary Keys
Primary keys are the unique identifier for each record in database table. It is denoted
by an underline under the attribute name.
Final ER Diagram
The below diagram is our final entity relationship diagram for bank with all entities,
their attributes and the relationship between them with the PRIMARY KEY and Cardinality
ratio.
Benefits of an ER Diagram:
E.F. Codd introduced the Relational Model to organize data as relations or tables. After
creating the conceptual design of a database using an ER diagram, this design must be
transformed into a relational model which can then be implemented using relational database
systems like Oracle SQL or MySQL.
Student Table
Key Terms
• Attribute: Attributes are the properties that define an entity. e.g. ROLL_NO,
NAME, ADDRESS.
• Relation Schema: A relation schema defines the structure of the relation and
represents the name of the relation with its attributes. e.g., STUDENT
(ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the relation schema for
STUDENT. If a schema has more than 1 relation it is called Relational Schema.
• Tuple: Each row in the relation is known as a tuple. The above relation contains
4 tuples one of which is shown as:
It may be worth noting that, unlike the Primary Key of any given relation, Foreign Key can
be NULL as well as may contain duplicate tuples i.e. it need not follow uniqueness
constraint. For Example, STUD_NO in the STUDENT_COURSE relation is not unique. It
has been repeated for the first and third tuples. However, the STUD_NO in STUDENT
relation is a primary key and it needs to be always unique, and it cannot be null.
Composite Key
Sometimes, a table might not have a single column/attribute that uniquely identifies
all the records of a table. To uniquely identify rows of a table, a combination of two or more
columns/attributes can be used. It still can give duplicate values in rare cases. So, we need
to find the optimal set of attributes that can uniquely identify rows in a table.
• It acts as a primary key if there is no primary key in a table
• Two or more attributes are used together to make a composite key.
• Different combinations of attributes may give different accuracy in terms of
identifying the rows uniquely.
Relational Model Notation
• Relation schema R of degree n is denoted by by R(A1, A2, …,An).
• Uppercase letters Q, R, S denote relation names.
• Lowercase letters q, r, s denote relation states.
• Letters t, u, v denotes tuples.
• In general, the name of a relation schema such as STUDENT also indicates the
current set of tuples in that relation.
• An attribute A can be qualified with the relation name R to which it belongs by
using the dot notation R.A for example, STUDENT.Name or STUDENT.Age.
• An n-tuple t in a relation r(R) is represented as t=<v1, v2,…,vn> where vi is the
value corresponding to the attribute Ai. The value vi for attribute Ai in tuple t can
be accessed using t[Ai] or t.Ai.
BRANCH_CODE of STUDENT can only take the values which are present in
BRANCH_CODE of BRANCH which is called referential integrity constraint. The relation
which is referencing another relation is called REFERENCING RELATION (STUDENT in
this case) and the relation to which other relations refer is called REFERENCED RELATION
(BRANCH in this case).
7. Anomalies in the Relational Model
An anomaly is an irregularity or something which deviates from the expected or
normal state. When designing databases, we identify three types of
anomalies: Insert, Update, and Delete.
On Delete Cascade
It would delete the tuples from REFERENCING RELATION if the value used by
REFERENCING ATTRIBUTE is deleted from REFERENCED RELATION. e.g.; if we
delete a row from BRANCH with BRANCH_CODE ‘CS’, the rows in STUDENT relation
with BRANCH_CODE CS (ROLL_NO 1 and 2 in this case) will be deleted.
On Update Cascade
Super Keys
Any set of attributes that allows us to identify unique rows (tuples) in a given
relationship is known as super keys. Out of these super keys, we can always choose a proper
subset among these that can be used as a primary key. Such keys are known as Candidate
keys. If there is a combination of two or more attributes that are being used as the primary
key then we call it a Composite key.
Case 1: Binary Relationship with 1:1 cardinality with total participation of an entity
A person has 0 or 1 passport number and Passport is always owned by 1 person. So, it is
1:1 cardinality with full participation constraint from Passport.
First Convert each entity and relationship to tables. Person table corresponds to
Person Entity with key as Per-Id. Similarly, Passport table corresponds to Passport Entity
with key as Pass-No. Has Table represents relationship between Person and Passport
(Which person has which passport). So, it will take attribute Per-Id from Person and Pass-
No from Passport.
From Table 1, each Per-Id and Pass-No has only one entry in Has Table. So, we can merge
all three tables into 1 with attributes shown in Table 2. Each Per-Id will be unique and not
null. So, it will be the key. Pass-No can’t be key because for some person, it can be NULL.
Case 2: Binary Relationship with 1:1 cardinality and partial participation of both
entities
A male marries 0 or 1 female and vice versa as well. So, it is 1:1 cardinality with partial
participation constraint from both. First Convert each entity and relationship to
tables. Male table corresponds to Male Entity with key as M-Id. Similarly Female table
corresponds to Female Entity with key as F-Id. Marry Table represents relationship
between Male and Female (Which Male marries which female). So, it will take attribute
M-Id from Male and F-Id from Female.
As we can see from Table 3, some males and some females do not marry. If we merge 3
tables into 1, for some M-Id, F-Id will be NULL. So, there is no attribute which is always
not NULL. So, we can’t merge all three tables into 1. We can convert into 2 tables. In table
4, M-Id who are married will have F-Id associated. For others, it will be NULL. Table 5 will
have information of all females. Primary Keys have been underlined.
Case 3: Binary Relationship with n: 1 cardinality
In this scenario, every student can enroll only in one elective course but for an elective
course there can be more than one student. First Convert each entity and relationship to
tables. Student table corresponds to Student Entity with key as S-Id. Similarly
Elective_Course table corresponds to Elective_Course Entity with key as E-Id. Enrolls
Table represents relationship between Student and Elective_Course (Which student
enrolls in which course). So it will take attribute S-Id from Student and E-Id from
Elective_Course.
As we can see from Table 6, S-Id is not repeating in Enrolls Table. So, it can be considered
as a key of Enrolls table. Both Student and Enrolls Table’s key is same. We can merge it as
a single table. The resultant tables are shown in Table 7 and Table 8. Primary Keys have
been underlined.
Case 4: Binary Relationship with m: n cardinality
In this scenario, every student can enroll in more than 1 compulsory course and for a
compulsory course there can be more than 1 student. First Convert each entity and
relationship to tables. Student table corresponds to Student Entity with key as S-Id.
Similarly Compulsory_Courses table corresponds to Compulsory Courses Entity with
key as C-Id. Enrolls Table represents relationship between Student and
Compulsory_Courses (Which student enrolls in which course). So it will take attribute S-
Id from Person and C-Id from Compulsory_Courses.
As we can see from Table 9, S-Id and C-Id both are repeating in Enrolls Table. But its
combination is unique; so it can be considered as a key of Enrolls table. All tables’ keys
are different, these can’t be merged. Primary Keys of all tables have been underlined.
In this scenario, an employee can have many dependents and one dependent can
depend on one employee. A dependent does not have any existence without an employee
(e.g; you as a child can be dependent of your father in his company). So it will be a weak
entity and its participation will always be total. Weak Entity does not have key of its own.
So its key will be combination of key of its identifying entity (E-Id of Employee in this case)
and its partial key (D-Name).
First Convert each entity and relationship to tables. Employee table corresponds to
Employee Entity with key as E-Id. Similarly Dependents table corresponds to Dependent
Entity with key as D-Name and E-Id. Has Table represents relationship between
Employee and Dependents (Which employee has which dependents). So it will take
attribute E-Id from Employee and D-Name from Dependents.
As we can see from Table 10, E-Id, D-Name is key for Has as well as Dependents Table.
So we can merge these two into 1. So the resultant tables are shown in Tables 11 and 12.
Primary Keys of all tables have been underlined.