§1 What is an Entity Relationship Diagram (ERD)? #
An Entity Relationship Diagram (ERD) is a flowchart that illustrates how “entities” such as people, objects, or concepts relate to each other within a system. In software engineering, ERDs are a fundamental tool used to model and design relational databases. They provide a visual blueprint of the database’s structure, showing its tables, the columns within those tables, and the relationships connecting them.
Example Bookstore ERD:
§2 Creating Entity Relationship Diagrams #
ERD’s can be created in a variety of ways, the diagrams on this page are created using mermaid.js, though several tools could be used:
§3 Core Components of an ERD #
Every ERD is built from three core components: entities, attributes, and relationships.
§3.1 Entities #
An entity represents a real-world object or concept that can be distinctly identified. In database terms, an entity corresponds to a table. For example, in a university database, Student, Course, and Professor would all be entities.
§3.2 Attributes #
An attribute is a property or characteristic of an entity. In a database, attributes are the columns of a table. For each attribute, we define a data type (e.g., integer, text, date).
Key attributes are special:
- Primary Key (PK): An attribute that uniquely identifies each record in an entity. A
StudentIDwould be a primary key for theStudententity. - Foreign Key (FK): An attribute in one entity that refers to the primary key of another entity. It’s used to link tables together.
Example: A Users Entity
Here’s a simple Student entity (or table) with a primary key and a few other attributes.
§3.3 Relationships #
A relationship defines how two or more entities are connected. For example, a Student “enrolls in” a Course. Relationships are crucial for showing how the database structure works as a whole. The “cardinality” of a relationship defines the number of instances of one entity that can be associated with instances of another.
§4 Cardinality: Defining Relationships #
Cardinality specifies the nature of the relationship between two entities. The most common types are one-to-one, one-to-many, and many-to-many.
§4.1 One-to-One (1:1) #
In a one-to-one relationship, one record in an entity is associated with exactly one record in another entity. For example, a User might have exactly one UserProfile.
The ||--|| notation between the two entities signifies that the relationship is “one and only one” on both sides.
§4.2 One-to-Many (1:N) #
In a one-to-many relationship, one record in an entity can be associated with multiple records in another entity. This is the most common relationship type. For example, one Author can write many Books.
The Author ||--|{ Book notation indicates that an Author has “one or many” Books, and a Book is written by “one and only one” Author.
||means “one and only one”.o{means “zero or many”.|{means “one or many”.
§4.3 Many-to-Many (N:M) #
In a many-to-many relationship, multiple records in one entity can be associated with multiple records in another. For example, a Student can enroll in many Courses, and a Course can have many Students.
This type of relationship cannot be implemented directly in a relational database. It requires a third “junction” or “linking” table that breaks the many-to-many relationship down into two one-to-many relationships.
§5 Complete Example: University Database #
Let’s combine these concepts into a more complete diagram for a university database, showing how departments, professors, courses, and students are all interconnected.
§6 More Resources #
If you would like to see more examples on ERD creation, here are some additional resources:
- https://www.visual-paradigm.com/guide/data-modeling/what-is-entity-relationship-diagram/;WWWSESSIONID=BF64E93D89F9AB510E6460C36ADCA348.www1
- https://www.lucidchart.com/pages/er-diagrams
- https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model
§7 Questions #
Always provide at least a brief explanations for your answers.
Please number each response to directly correspond with the question. Provide clear, concise answers that fully address each question without unnecessary elaboration or essay-style writing.
- What is the purpose of an Entity Relationship Diagram (ERD)?
- What are the three core components of an ERD?
- In the bookstore ERD, identify all foreign keys and explain which primary keys they reference.
§7.1 Diagram Creation #
- Create a simple ERD for a
CustomerandOrderrelationship, where a customer can have many orders, but each order belongs to only one customer. (you do not need to specify table columns, just the overall table relationships)
- A music streaming app needs to store playlists, songs, and artists. Each playlist can contain many songs, and each song can appear in multiple playlists. Additionally, each song is performed by one or more artists, and each artist can perform many songs. Design a normalized ERD for this scenario. (you do not need to specify table columns, just the overall table relationships)
- Create an ERD for a Movie information database inspired by imdb.com containing the following tables:
Movies,Shows,People,Studios,Genres,Ratings. Mention if your ERD is normalized or not. (you do not need to specify table columns, just the overall table relationships)
This will have several overlapping relationships, dont worry too much about the layout.