DBUNIT2
DBUNIT2
E-R Model:
R
VA
A
N
A
PP
A
TT
U
IM
XM
LA
Below picture clearly depicts about different notations we need while using ER
diagram in DBMS. They are –
R
VA
A
N
A
PP
A
TT
Diamond – It represents relationship between entity and attribute.
Line – It links attribute(s) to entity set(s) and entity set(s) to relationship
set(s).
Doubles Ellipses – It represents multivalued attributes.
Dashed Ellipses – It denotes derived attributes.
Double lines – It indicates total participation of an entity in a relationship
set.
Double Rectangle – It represents weak entity set.
Double Diamonds – It represents weak relationships.
Multiple ellipses connected to single ellipse using lines – It represents
composite attribute
Ellipse with line inside it – It represents single values attributes
Component of ER Diagram
R
VA
A
N
1. Entity:
A
PP
A
An entity may be any object, class, person or place. In the ER diagram, an entity
TT
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak
entity doesn't contain any key attribute of its own. The weak entity is represented
by a double rectangle.
2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to
represent an attribute.
For example, id, age, contact number, name, etc. can be attributes of a student.
R
a. Key Attribute
VA
A
N
The key attribute is used to represent the main characteristics of an entity. It
A
PP
represents a primary key. The key attribute is represented by an ellipse with the
text underlined.
A
TT
U
IM
XM
LA
b. Composite Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
R
For example, A person's age changes over time and can be derived from
VA
another attribute like Date of birth. A
N
A
PP
A
TT
U
IM
XM
LA
1. Primary key
o It is the first key used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys, as we saw in the PERSON
table. The key which is most suitable from those lists becomes a primary
key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for
each employee. In the EMPLOYEE table, we can even select
License_Number and Passport_Number as primary keys since they are
also unique.
o For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify
a tuple.
o Except for the primary key, the remaining attributes are considered a
candidate key. The candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The
rest of the attributes, like SSN, Passport_Number, License_Number, etc., are
considered a candidate key.
3. Super Key
R
VA
Super key is an attribute set that can uniquely identify a tuple. A super key is a
superset of a candidate key.
A
N
A
EMPLOYEE_NAME), the name of two employees can be the same, but their
A
EMPLYEE_ID can't be the same. Hence, this combination can also be a key.
TT
U
etc.
XM
4. Foreign key
LA
o Foreign keys are the column of the table used to point to the primary key of
another table.
o Every employee works in a specific department in a company, and
employee and department are two different entities. So we can't store the
department's information in the employee table. That's why we link these
two tables through the primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id, as a
new attribute in the EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the
tables are related.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely
identify each tuple in a relation. These attributes or combinations of the attributes
are called the candidate keys. One key is chosen as the primary key from these
candidate keys, and the remaining candidate key, if it exists, is termed the
alternate key. In other words, the total number of the alternate keys is the total
number of candidate keys minus the primary key. The alternate key may or may
not exist. If there is only one candidate key in a relation, it does not have an
alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No,
that act as candidate keys. In this relation, Employee_Id is chosen as the primary
key, so the other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
R
VA
Whenever a primary key consists of more than one attribute, it is known as a
A
composite key. This key is also known as Concatenated Key.
N
A
as a composite key since the primary key comprises more than one attribute.
IM
7. Artificial key
XM
LA
The key created using arbitrarily assigned data are known as artificial keys.
These keys are created when a primary key is large and complex and has no
relationship with many other relations. The data values of the artificial keys are
usually numbered in a serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and
Proj_ID, is large in employee relations. So it would be better to add a new virtual
attribute to identify each tuple in the relation uniquely.
3. Relationship
A relationship is used to describe the relation between entities. Diamond or
rhombus is used to represent the relationship.
Types of relationship are as follows:
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one
female.
R
b. One-to-many relationship
VA
A
N
When only one instance of the entity on the left, and more than one instance of
A
PP
an entity on the right associates with the relationship then this is known as a one-
to-many relationship.
A
TT
For example, Scientist can invent many inventions, but the invention is done by
U
IM
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of
an entity on the right associates with the relationship then it is known as a many-
to-one relationship.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship then it is known
as a many-to-many relationship.
For example, Employee can assign by many projects and project can have many
employees.
R
VA
1. Entity : An entity is a thing in a real-world with independent existence.
An entity can exist independently and is distinguishable from other objects. It
A
N
can be identified uniquely.
A
Tangible Entity : Entities that exist in the real world physically. Example:
A
TT
Example :
A table named student in a university database.
A table named employee in a company database.
Note :
The category of a particular entity in the relation in RDBMS is called the
entity type.
It is represented by the name of the table and its schema.
3. Entity Set : An entity set is a collection or set of all entities of a particular
entity type at any point in time. The type of all the entities should be the same.
Example :
The collection of all the students from the student table at a particular
instant of time is an example of an entity set.
The collection of all the employees from the employee table at a particular
instant of time is an example of an entity set.
Note :
Entity sets need not be disjoint. For example, the entity set of Article Writer
(all content creators for GeeksforGeeks) and the entity set of Article
Reader (all students who read the article of GeeksforGeeks) may have
members in common.
The collection of all the entities in the relation of RDBMS is called an entity
set.
Relation With Table :
Consider a table student as follows :
Table Name : Student
R
Student_I Student_Nam Student_Ag Student_Gende
D e e r
VA
A
N
A
1 Avi 19 M
PP
A
2 Ayush 23 M
TT
U
IM
3 Nikhil 21 M
XM
4 Riya 16 F
LA
Entity Type : Each entity belongs to the student type. Hence, the type of
entity here is a student.
Entity Set : The complete data set of all entities is called entity set. For the
above table, the records with student id 1, 2, 3, 4 are the entity set.
Difference Table :
Entity Entity Type Entity Set
R
There are numbers (represented by M and N) written above the lines which
VA
connect relationships and entities. These are called cardinality ratios. These
A
represent the maximum number of entities that can be associated with each
N
other through relationship, R. Types of Cardinality
A
PP
A
TT
U
IM
XM
LA
:T here can be 4
types of cardinality –
1. One-to-one (1:1) – When one entity in each entity set takes part at most
once in the relationship, the cardinality is one-to-one.
2. One-to-many (1: N) – If entities in the first entity set take part in the
relationship set at most once and entities in the second entity set take part
many times (at least twice), the cardinality is said to be one-to-many.
3. Many-to-one (N:1) – If entities in the first entity set take part in the
relationship set many times (at least twice), while entities in the second
entity set take part at most once, the cardinality is said to be many-to-one.
4. Many-to-many (N: N) – The cardinality is said to be many to many if
entities in both the entity sets take part many times (at least twice) in the
relationship set.
Participation Constraints : Participation Constraints tell us that the
participation in a relationship can either be total or partial.
R
When each entity in an entity set participates in a relation, it is called Total
VA
Participation. However, when all entities in the given entity set do not
A
participate in a relation, it is called Partial Participation. Structural
N
Constraints : Structural Constraints are also called Structural properties of a
A
constraints refer to the fact that such limitations must be imposed on the data,
TT
Degree of Relationship
In DBMS, a degree of relationship represents the number of entity types that
are associated with a relationship. For example, we have two entities, one is a
student and the other is a bag and they are connected with the primary key
and foreign key. So, here we can see that the degree of relationship is 2 as 2
entities are associating in a relationship.
Types of degree
Now, based on the number of linked entity types, we have 4 types of degrees
of relationships.
1. Unary
2. Binary
3. Ternary
4. N-ary
Let’s discuss them one by one with the help of examples.
Unary (Degree 1)
In this type of relationship, both the associating entity types are the same. So,
we can say that unary relationships exist when both entity types are the same
and we call them the degree of relationship is 1. In other words, in a relation
only one entity set is participating then such type of relationship is known as a
R
VA
unary relationship.
Example: In a particular class, we have many students, there are monitors
A
too. So, here class monitors are also students. Thus, we can say that only
N
A
Binary (Degree 2)
In a Binary relationship, there are two types of entity associates. So, we can
say that a Binary relationship exists when there are two types of entity and we
call them a degree of relationship is 2. Or in other words, in a relation when
two entity sets are participating then such type of relationship is known as a
binary relationship. This is the most used relationship and one can easily be
converted into a relational table.
Example: We have two entity types ‘Student’ and ‘ID’ where each ‘Student’
has his ‘ID’. So, here two entity types are associating we can say it is a binary
relationship. Also, one ‘Father’ can have many ‘daughters’ but each ‘daughter’
should belong to only one ‘father. We can say that it is a one-to-many binary
relationship.
Ternary (Degree 3)
In the Ternary relationship, there are three types of entity associates. So, we
can say that a Ternary relationship exists when there are three types of entity
and we call them a degree of relationship is 3. Since the number of entities
increases due to this, it becomes very complex to turn E-R into a relational
table. Now let’s understand with the examples.
Example: We have three entity types ‘Teacher’, ‘Course’, and ‘Class’. The
relationship between these entities is defined as the teacher teaching a
particular course, also the teacher teaches a particular class. So, here three
R
entity types are associating we can say it is a ternary relationship.
VA
A
N
A
PP
A
TT
U
IM
XM
LA
N-ary (n Degree)
In the N-ary relationship, there are n types of entity that associates. So, we
can say that an N-ary relationship exists when there are n types of entities.
There is one limitation of the N-ary relationship, as there are many entities so
it is very hard to convert into an entity, rational table. So, this is very
uncommon, unlike binary which is very much popular.
Example: We have 5 entities Teacher, Class, Location, Salary, Course. So,
here five entity types are associating we can say an n-ary relationship is 5.
What is Cardinality?
In Mathematics, cardinality means the number of members in a set. But in DBMS,
R
the definition of cardinality is different. The uniqueness of data present in a
VA
column is called cardinality.
A column with a high cardinality means that a significant percentage of the values
A
N
are unique. Low cardinality refers to a column with a large number of repeated
A
values.
PP
entities inside a table. It has a huge impact on the query execution plan, that is,
IM
the number of steps a user takes to search for a particular value/object inside the
XM
database.
Cardinality is also critical in optimizing database performance. The database
LA
optimizer can select the most efficient query execution plan by precisely
assessing the cardinality of distinct characteristics or columns in a table. A
greater cardinality indicates more distinct values, leading to improved selectivity
and faster query execution. A reduced cardinality, on the other hand, may
suggest a limited number of distinct values, allowing the database to use
specialized optimization techniques such as index selection and join algorithms.
What is Mapping Cardinality?
Mapping cardinality refers to the relationship between two entities in a database.
It specifies how many instances of one entity can be linked to instances of
another entity. It aids in understanding the nature and limits of relationships,
allowing for appropriate database design and ensuring data integrity throughout
data manipulation procedures.
R
VA
A
N
A
PP
A
TT
U
IM
Here, this relationship is one-to-one relationship because one team is led by one
team leader.
2. One-to-Many Cardinality (1)
An entity set A is associated with any number of entities in B (including zero), and
each entity in B is associated with just one entity in A.
Let's have a look at the example for more clarity.
R
VA
A
N
A
Here, this is one-to-many relationship because one team has many team
PP
members.
3. Many-to-One Cardinality (m:1)
A
TT
An entity set in A can only be associated with one entity in B. In contrast, an entity
U
R
VA
A
N
A
PP
A
TT
U
R
entity. Like strong entity, weak entity does not have any primary key, It has
VA
partial discriminator key. Weak entity is represented by double rectangle. The
A
relation between one strong and one weak entity is represented by double
N
diamond.
A
PP
A
TT
U
IM
XM
LA
R
VA
A
N
A
PP
A
TT
U
IM
XM
LA
LA
XM
IM
U
TT
A
PP
A
N
A
VA
R