Rdbms Unit 2
Rdbms Unit 2
Entity in DBMS
Entity:
An entity is referred to as an object or thing that exists in the real world. For
example, customer, car, pen, etc.
Entities are stored in the database, and they should be distinguishable, i.e., they
should be easily identifiable from the group.
For example, a group of pens that are from the same company cannot be
identified, so they are only objects, but pens with different colours become
unique and will be called an entity like a red pen, green pen, blue pen, black pen,
etc.In a group of pens, we can easily identify any pen because of its different
colours, so a pen of different colours is an entity.
For extracting data from the database, each data must be unique in its own way
so that it becomes easier to differentiate between them. Distinct and unique data
is known as an entity.
An entity has some attributes which depict the entity's characteristics. For
example, an entity "Student" has attributes such as "Student_roll_no",
"Student_name", "Student_subject", and "Student_marks".
ER (Entity Relationship)
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity
can be represented as rectangles.
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
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key 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
For example, A person's age changes over time and can be derived from
another attribute like Date of birth.
3. Relationship
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.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of
an entity on the right associates with the relationship then this is known as a one-
to-many relationship.
For example, Scientist can invent many inventions, but the invention is done by
the only specific scientist.
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.
DBMS
Cardinality
Cardinality means how the entities are arranged to each other or what is the
relationship structure between entities in a relationship set. In a Database
Management System, Cardinality represents a number that denotes how many
times an entity is participating with another entity in a relationship set. The
Cardinality of DBMS is a very important attribute in representing the structure of
a Database. In a table, the number of rows or tuples represents the Cardinality.
Cardinality Ratio
1. One to one
2. Many to one
3. One to many
4. Many to many
One to One
Another example is the relationship between the director of the school and the
school because one school can have a maximum of one director, and one
director can belong to only one school.
In many to one cardinality mapping, from set 1, there can be multiple sets that
can make relationships with a single entity of set 2. Or we can also describe it as
from set 2, and one entity can make a relationship with more than one entity of
set 1.
For example, there are multiple patients in a hospital who are served by a single
doctor, so the relationship between patients and doctors can be represented by
Many to one Cardinality.
One to Many Cardinalities:
In many, many cardinalities mapping, there can be one or more than one entity
that can associate with one or more than one entity of set 2. In the same way
from the end of set 2, one or more than one entity can make a relation with one
or more than one entity of set 1.
It is represented by M: N or N: M.
One to one cardinality, One to many cardinalities, and Many to one cardinality is
the subset of the many to many cardinalities.
For Example, in a college, multiple students can work on a single project, and a
single student can also work on multiple projects. So, the relationship between
the project and the student can be represented by many to many cardinalities.
Appropriate Mapping Cardinality
Evidently, the real-world context in which the relation set is modeled determines
the Appropriate Mapping Cardinality for a specific relation set.
○ There are certain keys in DBMS that are used for different purposes, from
which the most commonly known is the Primary Key.
○ Here, in this section, we will look at the Primary key - What it is, what is the
use of a primary key, and we will also implement some examples to
understand that how a primary key works.
○ A primary key of a relation is one of the possible candidate keys which the
database designer thinks it's primary. It may be selected for convenience,
performance and many other reasons. The choice of the possible primary
key from the candidate keys depend upon the following conditions.
Accessible: The primary key used should be accessible by anyone who wants
to use it. The user must easily insert, access or delete a tuple using it.
NON NULL Value: The primary key must have a non-null value for each tuple of
the relation, which is required for the identification of the tuple.
Time Variant: The values of the primary key must not change or become null
during the time of a relation.
Unique: The value of the primary key must not be duplicated in any of the tuples
of a relation.
Syntax for creating primary key constraint:
The primary key constraint can be defined at the column level or table
level.
At column level:
At table level:
1. Primary key(<column_name1>[,column_name>]....);
○ The data values for the primary key attribute should not be null.
○ Attributes which are part of a primary key are known as Prime attributes.
○ If the primary key is made of more than one attribute then those attributes
are irreducible.
○ We use the convention that the attribute that form primary key of relation is
underlined.
As defined above, a primary key is used to uniquely identify the rows of a table.
Thus, a row that needs to be uniquely identified, the key constraint is set as the
Primary key to that particular field. A primary key can never have a NULL value
because the use of the primary key is to identify a value uniquely, but if no value
will be there, how could it sustain. Thus, the field set with the primary key
constraint cannot be NULL. Also, it all depends on the user that the user can add
or delete the key if applied.
The database can be represented using the notations, and these notations can
be reduced to a collection of tables.
In the database, every entity set or relationship set can be represented in tabular
form.
The ER diagram is given below:
There are some points for converting the ER diagram to the table
In the STUDENT table, Age is the derived attribute. It can be calculated at any
point of time by calculating the difference between current date and Date of Birth.
Using these rules, you can convert the ER diagram to tables and columns and
assign the mapping between the tables. Table structure for the given ER diagram
is as below:
Figure: Table structure
Features of ER model
Disadvantages
Notation of ER diagram
Connectivity of a Relationship –
It means how many instances of one entity type are linked with how many entity
of another entity set. Different types of connectivity of a relationship are –
Type – 2