UNIT 02 Rdbms
UNIT 02 Rdbms
DATABASE DESIGNS
The following diagram showcases two entities - Student and Course, and their
relationship. The relationship described between student and course is many-to-
many, as a course can be opted by several students, and a student can opt for more
than one course. Student entity possesses attributes - Stu_Id, Stu_Name &
Stu_Age. The course entity has attributes such as Cou_ID & Cou_Name.
• ER Diagram helps you conceptualize the database and lets you know
which fields need to be embedded for a particular entity
• Lines: It links attributes to entity types and entity types with other
relationship types
Components of ER Diagram
• Entities
• Weak Entity
• Attributes
• Key Attribute
• Composite Attribute
• Multivalued Attribute
• Derived Attribute
• Relationships
• One-to-One Relationships
• One-to-Many Relationships
• Many-to-One Relationships
• Many-to-Many Relationships
Entities
For example, in a student study course, both the student and the course are
entities.
Weak Entity
An entity that makes reliance over another entity is called a weak entity
In the example below, school is a strong entity because it has a primary key
attribute - school number. Unlike school, the classroom is a weak entity because
it does not have any primary key and the room number here acts only as a
discriminator.
Attribute
For example: For a student entity, the roll number can uniquely identify a student
from a set of students.
Composite Attribute
Multivalued Attribute
Some attributes can possess over one value, those attributes are called
multivalued attributes.
Derived Attribute
An attribute that can be derived from other attributes of the entity is known as a
derived attribute.
In the ER diagram, the dashed oval represents the derived attribute.
Relationship
In the example below, both the student and the course are entities, and study is
the relationship between them.
One-to-One Relationship
One-to-Many Relationship
When a single element of an entity is associated with more than one element of
another entity, it is called a one-to-many relationship
For example, a customer can place many orders, but an order cannot be placed by
many customers.
Many-to-One Relationship
When more than one element of an entity is related to a single element of another
entity, then it is called a many-to-one relationship.
For example, students have to opt for a single course, but a course can have many
students.
Many-to-Many Relationship
When more than one element of an entity is associated with more than one
element of another entity, this is called a many-to-many relationship.
For example, you can assign an employee to many projects and a project can have
many employees.
• First, identify all the Entities. Embed all the entities in a rectangle and
label them properly.
• Make sure your ER Diagram supports all the data provided to design the
database.
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.
In this case, 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.
Every student in the Student Entity set participates in a relationship but there
exists a course C4 that is not taking part in the relationship.
In the above example, there is a "Student" table that contains data such as
"Student_id", "Name", "Course", "Session", "Fee", and "Department". As you
can see, some data is repeated in the table, which causes redundancy.
We will understand these anomalies with the help of the following student table:
1. Insertion Anomaly:
Insertion anomaly arises when you are trying to insert some data into the database,
but you are not able to insert it.
Example: If you want to add the details of the student in the above table, then
you must know the details of the department; otherwise, you will not be able to
add the details because student details are dependent on department details.
2. Deletion Anomaly:
Deletion anomaly arises when you delete some data from the database, but some
unrelated data is also deleted; that is, there will be a loss of data due to deletion
anomaly.
Example: If we want to delete the student detail, which has student_id 2, we will
also lose the unrelated data, i.e., department_id 102, from the above table.
3. Updating Anomaly:
An update anomaly arises when you update some data in the database, but the
data is partially updated, which causes data inconsistency.
Extended ER features
The extended Entity Relationship (ER) models are three types as given below −
• Aggregation
• Specialization
• Generalization
Specialization
Example
If the sub-class information is given for the given entity set then, ISA relationship
type will be used to represent the connectivity between the subclass and
superclass as shown below −
Example
Aggregation
It is an abstraction in which relationship sets are treated as higher level entity sets
and can participate in relationships. Aggregation allows us to indicate that a
relationship set participates in another relationship set.
Normalization in DBMS
Normalization in DBMS is a technique using which you can organize the data in
the database tables so that:
It is a multi-step process that puts data into tabular form, removes duplicate data,
and set up the relationship between tables.
First Normal Form is defined in the definition of relations (tables) itself. This rule
defines that all the attributes in a relation must have atomic domains. The values
in an atomic domain are indivisible units.
Before we learn about the second normal form, we need to understand the
following −
If we follow second normal form, then every non-prime attribute should be fully
functionally dependent on prime key attribute. That is, if X → A holds, then there
should not be any proper subset Y of X, for which Y → A also holds true.
We see here in Student_Project relation that the prime key attributes are Stu_ID
and Proj_ID. According to the rule, non-key attributes, i.e. Stu_Name and
Proj_Name must be dependent upon both and not on any of the prime key
attribute individually. But we find that Stu_Name can be identified by Stu_ID
and Proj_Name can be identified by Proj_ID independently. This is called partial
dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no
partial dependency.
1. It satisfies the First Normal Form and the Second Normal form.
2. And, it doesn't have Transitive Dependency.
To bring this relation into third normal form, we break the relation into two
relations as follows −
• Boyce and Codd Normal Form is a higher version of the Third Normal
Form.
• This form deals with a certain type of anomaly that is not handled by 3NF.
• A 3NF table that does not have multiple overlapping candidate keys is
said to be in BCNF.
• For a table to be in BCNF, the following conditions must be satisfied:
o R must be in the 3rd Normal Form
o and, for each functional dependency ( X → Y ), X should be a Super
Key.
In the above image, Stu_ID is the super-key in the relation Student_Detail and
Zip is the super-key in the relation ZipCodes. So,
and
Zip → City
• The fifth normal form is also called the PJNF - Project-Join Normal
Form
• It is the most advanced level of Database Normalization.
• Using Fifth Normal Form you can fix Join dependency and reduce data
redundancy.
• It also helps in fixing Update anomalies in DBMS design.