ERDs explained: map entities and relationships to database tables
Learn how an entity-relationship diagram models entities, attributes, cardinality, and optionality, then translate the model into keys and constraints.
ERDs explained: map entities and relationships to database tables
An entity-relationship diagram (ERD) shows what a system stores and how the stored concepts connect. Teams use it to discuss rules before turning them into tables, keys, and constraints. An ERD alone does not enforce data integrity; the database schema must implement the rules.
Entities, attributes, and relationships
An entity is a kind of thing the system records, such as a customer, order, or product. Attributes are its properties, such as customer email or order date. A relationship describes a business connection between entities. Do not turn every noun into an entity: a shipping address may be an order attribute if it is managed with that order, but a shared address book may need its own entity.
Read cardinality and optionality
Cardinality describes how many instances on one side can relate to one instance on the other. Notation varies, so include a legend and, when possible, write minimum and maximum counts. 0..1 means optional and at most one; 1 means exactly one; 0..* means zero or many; 1..* means one or more. The minimum count expresses optional participation; the maximum expresses multiplicity.
In this teaching example, a customer can have zero or many orders, while each order belongs to exactly one customer: Customer 1 — 0..* Order. Orders and products form a many-to-many relationship. An OrderItem entity resolves it and can store attributes of that relationship, such as quantity or the price at purchase.
The diagram uses language-neutral codes: A=customer, B=order, C=order item, D=product. Numeric ranges label the relationship at each end.
Figure 1. The order-to-product many-to-many connection is represented through order items; each item refers to one order and one product.
Translate the model into tables
For a one-to-many relationship, the foreign key usually goes on the many side. A required order customer can be modeled with customer_id NOT NULL REFERENCES customers(id). The foreign key checks that the referenced customer exists; NOT NULL makes the relationship mandatory from the order’s side. A many-to-many relationship uses a junction table with foreign keys to both entities. A primary or unique key can prevent duplicate pairs, but decide whether one product may appear on multiple lines before selecting that key.
The following PostgreSQL syntax is illustrative and was not run:
Sources
[Oracle SQL Developer Data Modeler: entities and relationships](https://docs.oracle.com/cd/E29463_01/doc.33/e35120/data_modeling.htm)
[Microsoft SQL Server: relationships in database diagrams](https://learn.microsoft.com/en-us/ssms/visual-db-tools/design-database-diagrams-visual-database-tools)
[PostgreSQL 18: constraints](https://www.postgresql.org/docs/18/ddl-constraints.html)