Table of contents
What things you will learn from this blog 🔍
ER model is the hot topic from which a lot of questions comes in interviews , so if you are looking for simple and concise introudction about ER model in database management system then you are at the right place . Because in this blog post we will discuss about the following questions regarding ER model
- What is ER model and why we use it ?
- What is the difference between attributes and entities in ER model ?
- What are the different types of attributes In ER model of dbms ?
- Types of relationship in ER model ?
So without any further due , let's jump straight into the content
What is ER model and why we need it in DBMS ?
ER model which stands for entity relationship model is used to logically represent the entire database by defining what would be the entities in database , what would be the attributes of those entities and and how the entities will be related to other entities in the database .
What is the difference between attributes and entities in ER model?
As you can see in the ER model above ( related to pharmacy store information ) all the blue rectangles are the entities whereas all the light purple ellipse shapes are attributes of those entities. But what is the definition of attribute and entities becuase you can't write or tell someone about attributes and entities like this, so here is what you can explain about attributes and entities .
Entity is anything about which the data can be stored . For Example : Student is an entity because we can easily store the information about student , like ( Name , class , roll number ).
Attribute is simply the characteristic of entity , for example : If student is entity then his/her name , class , age and id can be called as the attribute of student entity.
Types of Attributes in ER model
There are basically 10 different attribute in ER model , but we will make couple of 2 attributes and will discuss the difference between them
Simple Attributes Vs Composite Attributes
Single valued Attributes Vs Multivalued Attributes
- Derived Attributes Vs Complex Attributes
Derived attributes are always represented by dashed or dotted elliptical shapes.
- Key Attributes
Types of Relationships in ER model
There are basically 4 different types of relationships that exist in ER model and those 4 types are :
- One to One relationship
- One to Many relationship
- Many to One relationship
- Many to Many relationship
So now let's discuss each type with some amazing visuals to enhance your learning.
One to One relationship
One to one is that type of relationship in ER model where every instance of a entity is uniquely related to every instance of another entity.
The actual representation of one to one relationship in ER model is represented as
In order to better understand one to one relationship let us take a look at visual above .So in the visual as you can see that all the instances of Student entity are uniquely related to all the instances of course entity .Here for the simplicity purpose the attributes of both student and course are not show in the ER diagram , instead they are shown in the relational model ( table ).
In the relational model , as you can see in the middle we have one attribute from both the relational mode of student and course , but the question is why those only and why not other ? . So the answer to this query is that we always take primary keys in order to form relationship between entities in case of one to one relationship , because for a particular entity the primary key values are always unique whereas there can be the case where 2 sutdents have same name but are enrolled in maths course , so in that case the relationship will no longer be called as one to one , instead it would become many to one relationship.
Can we reduce the tables in one to one realationship🤷🏽
This is also one of the common questions which is asked , one to one relationship topic , so if in some case you also encountered this question in an interview then you have to say yes , and the reason for the yes is : If you will take a closer look at the Studies relational model ( table ) then there you will notice that we are using foreign keys ( Foreign key is the key which take reference from primary key ) , thus instead of making a seperate relational mode for studies we can merge ( Studies table with either Student table or Course table ).
One additional thing , which you must keep in mind is that , if asked about primary key in the studies relational model , then out of ( St-id / Cr-id ) , any column can be defined as primary key because both attributes are helping us to uniquely identify row in the database.
One to Many relationship
One to many relationship is that type of relationship where an instance of an entity is related to multiple instances of another entity.
The actual representation of one to Many relationship in ER model is
Just like one to one relationship , for this type of relationship we will discuss 2 questions :
- What would be the primary key in relational model of relation between the customer and product ?
- How we would reduce the tables in one to many relationship ?
Before answering the above mentioned questions , let me first describe what's going on in the visual representation of one to many relationship , so basically we have 2 entities customer having attributes ( Customer id , name and city ) and product having attributes ( Product id , item and cost ) . In addition to it the relation between both of these entities also have one attribute knwon as date .
Now basically when we will convert the ER diagrams into relational model , then we will observe that Customer id and product id are the primary keys , but in the relational model of order out of 2 foriegn keys ( Customer id and product id ) the primary key would be Product id only , and not the customer id . But now the question arrises how we will get to know about which is the primary key out of 2 entities?
What would be the primary key in the relational model of relation between entities?
In order to find the primary key , first of all understand the ER diagram and the intuition behind one to many relationship , basically in one to many relationship it is obvious that a single customer can have multiple orders , but for every order there will be a unique customer only , thus the primary key would be Product id attribute and not the customer id , because using the product key we will be able to uniquely identify any row in the database which would not be possible using the customer id , as for same id there can be multiple order id's .
Can we reduce table in one to many relationship ?
The answer to this questions is yes , because in the relational model of relation between the entities the product id is the primary key , thus if we there is presence of same primary key in multiple relational model then we can combine them to make a single relational model .
Many to One relationship
Many to one is that type of relationship in ER model where , multiple instance of entity are uniquely related to only one instance of an entity.
As you can see in the visual diagram above the multiple instance of the student entity that are related to only one instance of the product entity . Apart from it talking about the primary key in the relational model of ( works on ) , the student id will be the primary key , because all the attribute values of student id are unique .
Reducing the number of tables in the Many to one relationship
Many to Many relationship
Many to many relationship is that type of relationship , where multiple instances of an entity are related to multiple instances of another entity.
In many to many relationship , when we will convert ER model into relational model then we will realise that there will be no primary key in the relational model of relationship between student and course . The reason behind it is that , multiple instances of one entity are related to multiple instances of another entity , thus there is no attribute in the relational model of relationship between student and course having unique attribute value .
Now since there is no primary key in the relational model of relation between student and course , thus we will not be able to reduce tables in case of Many to Many relationship .