Database Normalization and ERD
Database Normalization and ERD
1. Data Redundancy: This occurs when the same piece of data is stored in multiple places.
Normalization reduces redundancy, which in turn minimizes the risk of data
inconsistency.
2. Data Integrity: It ensures that data is accurate and consistent throughout the database.
With a properly normalized database, you avoid problems like having different values for
the same piece of information in various places.
3. Functional Dependency: This is a relationship that describes how one attribute uniquely
determines another attribute. It plays a key role in the normalization process.
4. Normalization Forms: There are several stages, or "normal forms," of database
normalization, each addressing specific issues in database structure.
When to Denormalize
Sometimes, for performance reasons, databases are denormalized. This involves reintroducing
some redundancy to speed up read operations. However, this trade-off can lead to increased
complexity in maintaining data integrity. Therefore, normalization is a standard starting point in
database design, with denormalization used selectively in cases where performance gains are
critical.
The primary components of an ER Model include entities, relationships, and attributes. These
elements are usually represented in a diagram called an Entity Relationship Diagram (ERD).
1. Entity
An entity represents a real-world object, concept, or thing that has a distinct existence in the
system you are modeling. In a database, an entity typically corresponds to a table.
● Entity Types: These are the general categories or classes of objects. For example, in a
university database, "Student" and "Course" would be entity types.
● Entity Instance: A single, specific occurrence of an entity type. For instance, "John Doe"
is an instance of the "Student" entity.
2. Attributes
Attributes are represented as ovals in an ER diagram and connected to their respective entities.
3. Relationships
A relationship defines how two or more entities are associated with each other. In databases, this
corresponds to the foreign key relationship between tables.
Relationships are represented as diamonds in an ER diagram, with lines connecting the entities
involved.
4. Cardinality
Cardinality specifies the number of instances of one entity that can or must be associated with
each instance of another entity.
● Many-to-Many (M
): Many instances of one entity are related to many instances of another entity (e.g., a
student can enroll in multiple courses, and a course can have multiple students).
Cardinality is often depicted on the connecting lines between entities in the ER diagram.
5. Keys
In ER modeling, keys are attributes or sets of attributes that uniquely identify instances of an
entity.
Components of an ER Diagram
Example of an ER Diagram
Types of ER Models
Advantages of ER Modeling
Limitations of ER Modeling
● Complexity with Large Systems: ER diagrams can become large and complex when
modeling large systems with many entities and relationships.
● Limited in Capturing Data Constraints: While ER models are good for representing
relationships and structure, they do not fully capture all data constraints or business rules.
The Enhanced Entity Relationship (EER) model extends the traditional ER model by adding
concepts such as:
Entity Relationship Modeling is essential in the database design process, providing a structured
way to visualize and implement a logical database schema, ensuring data consistency, and laying
the groundwork for database systems.
RECURSIVE RELATIONSHIP
A recursive relationship (also called a unary relationship) occurs when an entity in a database
is related to itself. This means that instances of the same entity type can be related to other
instances of that same entity type.
● Same entity: Both ends of the relationship involve the same entity.
● Self-referencing: The relationship refers back to the same entity, allowing instances
within it to be associated with one another.
● Cardinality: Like any relationship, recursive relationships can have different
cardinalities, such as one-to-one, one-to-many, or many-to-many.
Consider an "Employee" entity in an organization where one employee can manage other
employees. In this case, the relationship "manages" is recursive because it relates employees to
other employees.
● Entity: Employee
● Relationship: Manages
● Description: An employee (manager) can manage other employees (subordinates).
● Cardinality: One-to-many (one employee manages many other employees).
In an ER diagram:
● The entity Employee will appear only once, but the manages relationship will loop back
to the same entity.
● One employee (manager) can be related to many subordinates (one-to-many
relationship).
Another example could be in a university database, where a "Course" can have a prerequisite
course.
● Entity: Course
● Relationship: Prerequisite
● Description: A course can be a prerequisite for another course.
● Cardinality: One-to-one (one course is a prerequisite for only one other course).
In an ER diagram:
● The entity Course will have a relationship named Prerequisite that connects to itself.
In a social network database, a person can be friends with multiple other people.
● Entity: Person
● Relationship: Is Friend Of
● Description: A person can have multiple friends, and those friends can also be friends
with each other.
● Cardinality: Many-to-many (a person can have many friends, and each friend can have
many other friends).
In an ER diagram:
● The entity Person will be connected by a friend relationship to other instances of the
Person entity.
1. One-to-One (1:1): Each entity instance is related to one other instance of the same entity.
For example, an employee supervises one other employee.
2. One-to-Many (1
): One instance of an entity is related to multiple instances of the same entity. For
example, a manager supervises many employees.
3. Many-to-Many (M
): Multiple instances of an entity are related to multiple other instances of the same entity.
For example, people are friends with multiple other people in a social network.
● A single entity rectangle connected by a relationship line that loops back to the entity
itself.
● Depending on the cardinality (one-to-one, one-to-many, or many-to-many), symbols like
crow's feet (for many) may be used to depict the cardinality constraints.
For example, in the Employee manages Employee relationship, the Employee entity would
have a relationship called manages, with one part of the relationship connecting back to the
entity itself, and the cardinality symbols (1
● One-to-Many Example: In an employee table, you would add a foreign key column
(e.g., ManagerID) that references the primary key (e.g., EmployeeID) of the same table.
This foreign key creates the self-referencing relationship where one employee can
manage other employees.
Table Structure:
scss
Copy code
Employee (EmployeeID, Name, ManagerID)
Table Structure:
scss
Copy code
Person (PersonID, Name)
Friends (PersonID1, PersonID2) -- Both columns are foreign keys
referencing PersonID in the Person table
In summary, recursive relationships are an essential concept in ER modeling when dealing with
entities that have a relationship with themselves. They are particularly useful for representing
hierarchical or network structures and allow for more sophisticated database designs.