Relational databases are complicated, and understanding the entire architecture of a database can be difficult when expressed solely in words. That's where Entity Relationship Diagrams (ERDs) come in. ERDs are a way of visualizing the relationships between different entities and their cardinality.
Cardinality is a crucial concept within entity relationship modeling which refers to the number of instances of an entity that can be associated with each instance of another entity. Defining cardinality helps people understand the nature of relationships.
The cardinality of relationships can be one-to-one, one-to-many, or many-to-many.
In addition to the above relationship types, each side of the relationship may be optional. Let's examine the case of an airline company which tracks Pilots and Completed Flights.
So in this case, the Pilot having Flights is optional.
Many-to-many relationships require special handling to ensure that the relationships and their data can be stored accurately in the database. An associative entity (also known as a joining table) handles this by converting the many-to-many relationship into two one-to-many relationships.
Let's imagine a university that wants to track students and courses. In this example, 1 student has many courses, and 1 course has many students, meaning it has a many-to-many relationship.
The bad way to architect the schema for this scenario is using a single table. This method results in lots of repeated data.
StudentCourses Table
| FirstName | LastName | CourseName | Instructor |
| Alice | Smith | Math | Prof. Smith |
| Alice | Smith | English | Prof. Johnson |
| Bob | Northwind | Math | Prof. Smith |
A better way to architect the schema for this scenario is to break it into 3 tables with 2 relationships. Now the data is properly normalized and we can track which students do which courses, and any extra details such as when the student starts the course.
Students Table
Contains the student data
| StudentID | Name | LastName |
| 1 | Alice | Smith |
| 2 | Bob | Northwind |
Courses Table
Contains the course data
| CourseID | CourseName | Instructor |
| 1 | Math | Prof. Smith |
| 2 | English | Prof. Johnson |
StudentCourses Table
Contains the data about which students are taking which courses
| StudentID | CourseID |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
Relationship 1 - Students and StudentCourses
Relationship 1 - Courses and StudentCourses
Note how wordy it was to illustrate the many-to-many relationship in the StudentCourses example. Now imagine that database expands to have 10 more tables. It would quickly become hard to keep track of everything. This problem is what ERDs solve.
An ERD helps quickly display all the relationships in a database at a glance. Let's see what it looks like for StudentCourses:
Figure: Student Courses ERD
In this example, Students, Courses and StudentCourses are represented via the rectangles. Meanwhile, their relationships are shown via the lines between the rectangles. You can see the cardinality indicated by what is called Crow's foot notation.
ERDs are a fantastic tool for visualizing a database at a glance. Through using this tool, developers can ensure they have a solid understanding of how data in the database is related and identify any problems quickly and easily.