Entity-Relationship Diagrams

INF2603 - Databases I · Database Design

Entity-Relationship Diagrams

Entity-Relationship Diagrams (ERDs) are a vital tool in database design. They visually represent the data structure and the relationships between data entities. Understanding how to create and interpret ERDs is essential for effective database management.

Understanding Entities

An entity is a distinct object or concept in the real world that can be identified. Entities can be physical objects, such as a car or a student, or they can be concepts, such as a course or a project.

Remember: An entity is represented as a rectangle in an ERD.

Identifying Attributes

Attributes are the properties or characteristics of an entity. For example, a student entity may have attributes such as student ID, name, and date of birth. Each attribute can have a specific data type, such as integer, string, or date.

Tip: Use clear and meaningful names for attributes to improve understanding.

Defining Relationships

Relationships show how entities interact with each other. In ERDs, relationships are represented by diamonds connecting the entities. There are three main types of relationships:

  • One-to-One (1:1): Each instance of Entity A is related to one instance of Entity B and vice versa.
  • One-to-Many (1:N): Each instance of Entity A can be related to multiple instances of Entity B, but each instance of Entity B is related to only one instance of Entity A.
  • Many-to-Many (M:N): Each instance of Entity A can be related to multiple instances of Entity B and vice versa.

Watch out: Ensure you correctly identify the type of relationship to avoid errors in your diagram.

Creating an Entity-Relationship Diagram

To create an ERD, follow these steps:

  1. Identify the entities in your system.
  2. List the attributes for each entity.
  3. Determine the relationships between the entities.
  4. Draw the diagram using rectangles for entities, ovals for attributes, and diamonds for relationships.

Example: A Simple ERD

Let’s create an ERD for a university database that includes students and courses.

Step 1: Identify Entities

We have two entities: Student and Course.

Step 2: List Attributes

For the Student entity, we have:

  • Student ID (Primary Key)
  • Name
  • Date of Birth

For the Course entity, we have:

  • Course ID (Primary Key)
  • Course Name
  • Credits

Step 3: Determine Relationships

Each student can enroll in multiple courses, and each course can have multiple students. This is a many-to-many relationship.

Step 4: Draw the Diagram

The ERD for this scenario would look like this:

<Student>--->Enrolled In<---<Course>

The Enrolled In relationship connects the Student and Course entities.

Cardinality and Participation Constraints

Cardinality defines the number of instances of one entity that can or must be associated with each instance of another entity. Participation constraints define whether all or only some instances of an entity participate in a relationship.

  • Mandatory Participation: Every instance of an entity must participate in a relationship.
  • Optional Participation: Some instances of an entity may not participate in a relationship.

Example of Cardinality

In our university ERD, the cardinality for the relationship between Student and Course is:

  • Student: 0..N (A student can enroll in zero or many courses)
  • Course: 0..N (A course can have zero or many students)

Remember: Use the correct notation for cardinality, such as 0..N for zero or many.

Types of ER Diagrams

There are two main types of ER diagrams:

  • Conceptual ERD: This diagram shows the high-level relationships between entities without going into detail about attributes.
  • Logical ERD: This diagram includes detailed attributes and relationships, showing how the data will be structured in the database.

Normalization and ER Diagrams

Normalization is the process of organizing data to reduce redundancy. It is closely related to ER diagrams, as a well-designed ERD can help identify areas where normalization is needed. Normalization involves dividing a database into two or more tables and defining relationships between the tables.

Example of Normalization

Consider a scenario where you have a student table with the following attributes:

  • Student ID
  • Name
  • Course Name

This structure may lead to redundancy if multiple students are enrolled in the same course. By normalizing the database, you can create separate tables for Students and Courses, linking them with a third table for enrollments.

Conclusion

Entity-Relationship Diagrams are essential for visualizing the structure of a database. By understanding entities, attributes, relationships, cardinality, and normalization, you can design effective databases that meet the needs of users.

Remember: Practice creating ERDs for different scenarios to improve your skills.

Check your understanding

  • What is the purpose of an Entity-Relationship Diagram?
  • How do you identify entities and attributes in a database?
  • What are the different types of relationships in ERDs?
  • Explain the concept of cardinality in the context of ER diagrams.