Entity Relational

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ENTITY RELATIONAL (ER) MODEL

Entity relational (ER) model is a high-level conceptual data model diagram. It describes the structure of a
database with the help of a diagram, which is known as Entity Relationship Diagram (ER Diagram). An
ER model is a design or blueprint of a database that can later be implemented as a database. The main
components of E-R model are: entity set and relationship set.

An ER diagram shows the relationship among entity sets. ER diagram shows the complete logical structure of
a database. Let’s have a look at a simple ER diagram to understand this concept.

Components of E-R Diagram:-


1.) Entities
2.) Attributes
3.) Relationships

1.) Entity:- An entity is an object or component of data. An entity can be place, person, object, event or a
concept, which stores data in the database. It is represented by rectangle . Eg:- Person, Building, Product,
account etc.
STUDENT

Entity are of two Types:

 Strong entity:- An entity that can be uniquely identified by its own attributes and does not relies on
the relationship with other entity is called Strong entity. It is represented by single rectangle.
 Weak entity:- An entity that cannot be uniquely identified by its own attributes and relies on the
relationship with other entity is called weak entity. The weak entity is represented by a double
rectangle.
For example – a bank account cannot be uniquely identified without knowing the bank to which the
account belongs, so bank account is a weak entity and bank is a strong entity.

Entity Set:- An entity set is a group of similar kind of entities. It may contain entities with attribute sharing
similar values. Example:- Person (employee, doctor).
2.) Attributes:- An attribute describes the property of an entity. An attribute is represented as Oval in an ER
diagram.
NAME

There are 5 types of attributes:-

 Single value attribute:- An attribute consisting only one value for a given entity. For example,
Emp_Id (it cannot have more than one value for a given entity Employee). In ER diagram, this
attribute is represented by single oval.
Emp_ID

 Multi-valued attribute:- An attribute consisting more than one value for a given entity. For
example, Phone_No (can be more than one for a given student). In ER diagram, multi-valued attribute
is represented by double oval.

 Composite attribute:- An attribute composed of many other attribute is called as composite


attribute. For example, Address attribute of student Entity type consists of Street, City, State, and
Country. In ER diagram, composite attribute is represented by an oval comprising of ovals.

 Key attribute:- The attribute which uniquely identifies each entity in the entity set is called key
attribute. For example, Roll_No will be unique for each student. In ER diagram, key attribute is
represented by an oval with underlying lines.

 Derived attribute:- An attribute which can be derived from other attributes of the entity type is
known as derived attribute. e.g.; Age (can be derived from DOB). In ER diagram, derived attribute is
represented by dashed oval.

3.) Relationship:- A relationship is represented by diamond shape in ER diagram, it shows the relationship
among entities. There are four types of relationships:
1. One to One
2. One to Many
3. Many to One
4. Many to Many
1. One to One Relationship:- When a single instance(value) of an entity is associated with a single instance
of another entity then it is called one to one relationship. For example, a person has only one passport and a
passport is given to one person.

2. One to Many Relationship:- When a single instance of an entity is associated with more than one
instances of another entity then it is called one to many relationship. For example – a customer can place
many orders but a order cannot be placed by many customers.

3. Many to One Relationship:- When more than one instances of an entity is associated with a single
instance of another entity then it is called many to one relationship. For example – many students can study
in a single college but a student cannot study in many colleges at the same time.

4. Many to Many Relationship:- When more than one instances of an entity is associated with more than
one instances of another entity then it is called many to many relationship. For example, a student can be
assigned to many projects and a project can be assigned to many students.

Example of E-R Diagram


Keys in DBMS
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a relation(table).
They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a
combination of one or more columns in that table. It play an important role in the relational database.

Why do we need a Key?


In real world applications, number of tables required for storing the data is huge, and the different tables are
related to each other as well.
Also, tables store a lot of data in them. Tables generally extend to thousands of records stored in them,
unsorted and unorganised.
Now to fetch any particular record from such dataset, you will have to apply some conditions, but what if
there is duplicate data present and every time you try to fetch some data by applying certain condition, you
get the wrong data. How many trials before you get the right data?
To avoid all this, Keys are defined to easily identify any row of data in a table.

There are different types of Keys:-

 Super key:- Super Key is defined as a set of attributes within a table that can uniquely identify each
record within a table. Super Key is a superset of Candidate key.
student_id name phone age

1 John 9876723452 17

2 Tom 9991165674 19

3 Akon 7898756543 18

4 Akon 8987867898 19

5 Walter 9990080080 17

Example:- In the table defined above super key would include student_id, (student_id, name), phone etc.

The first one is pretty simple as student_id is unique for every row of data, hence it can be used to identity
each row uniquely.
Next comes, (student_id, name), now name of two students can be same, but their student_id can't be same
hence this combination can also be a key.
Similarly, phone number for every student will be unique, hence again, phone can also be a key.
So they all are super keys.
 Candidate Key:- Candidate keys are defined as the minimal set of fields which can uniquely identify
each record in a table. It is an attribute or a set of attributes that can act as a Primary Key for a table to
uniquely identify each record in that table. There can be more than one candidate key.
In our example, student_id and phone both are candidate keys for table Student.
 A candiate key can never be NULL or empty. And its value should be unique.
 There can be more than one candidate keys for a table.
 A candidate key can be a combination of more than one columns(attributes).

 Primary Key:- Primary key is a candidate key that is most appropriate to become the main key for
any table. It is a key that can uniquely identify each record in a table.

For the table Student we can make the student_id column as the primary key.
 Composite Key:- Key that consists of two or more attributes that uniquely identify any record in a
table is called Composite key. But the attributes which together form the Composite key are not a key
independently or individually.

In the above picture we have a Score table which stores the marks scored by a student in a particular subject.
In this table student_id and subject_id together will form the primary key, hence it is a composite key.
 Secondary or Alternative key:- The candidate key which are not selected as primary key are known
as secondary keys or alternative keys. In our example, student_id and phone both are candidate keys
for table Student. If we take student_id as a primary key then phone is a secondary key.

 Foreign key:- Foreign keys are the column of the table which is used to point to the primary key of
another table.

Example:- In a company, every employee works in a specific department, and employee and
department are two different entities. So we can't store the information of the department in the
employee table. That's why we link these two tables through the primary key of one table. We add the
primary key of the DEPARTMENT table, Department_Id as a new attribute in the EMPLOYEE table.
Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.

Extended ER Features

 Super Class and Sub Class


 Generalization
 Specialization
 Aggregation
1.) Super class and Sub class

Super Class: A super class is an object whose instances store attributes that are common to one or more
subclasses of the object. A super class is a generic entity type that has a relationship with one or many
classes.

Sub Class:- A sub class is an object class whose instances inherit some common attributes form a subclass
and then add other attributes that are unique to an instance of the subclass. A subgroup of entity type that has
attributes distinct from those in other subgroups.

2.) Generalization:- Generalization is a bottom-up approach in which two lower level entities combine to
form a higher level entity. In generalization, the higher level entity can also combine with other lower level
entities to make further higher level entity.
It's more like Superclass and Subclass system, but the only difference is the approach, which is bottom-up.
Hence, entities are combined to form a more generalised entity, in other words, sub-classes are combined to
form a super-class.
For Example, STUDENT and FACULTY can be generalized to a higher level entity called PERSON as
shown in Figure 1. In this case, common attributes like P_NAME, P_ADD become part of higher entity
(PERSON) and specialized attributes like S_FEE become part of specialized entity (STUDENT).

3.) Specialization:- Specialization is opposite to Generalization. It is a top-down approach in which one


higher level entity can be broken down into two lower level entity. In specialization, a higher level entity may
not have any lower-level entity sets, it's possible.

For Example, EMPLOYEE entity in an Employee management system can be specialized into
DEVELOPER, TESTER etc. as shown in Figure 2. In this case, common attributes like E_NAME, E_SAL
etc. become part of higher entity (EMPLOYEE) and specialized attributes like TES_TYPE become part of
specialized entity (TESTER).
4.) Aggregation:- In aggregation, the relation between two entities is treated as a single entity. In
aggregation, relationship with its corresponding entities is aggregated into a higher level entity.

An ER diagram is not capable of representing relationship between an entity and a relationship which may be
required in some scenarios. In those cases, a relationship with its corresponding entities is aggregated into a
higher level entity.

For example: Center entity offers the Course entity act as a single entity in the relationship which is in a
relationship with another entity visitor. In the real world, if a visitor visits a coaching center then he will
never enquiry about the Course only or just about the Center instead he will ask the enquiry about both.

You might also like