Entity Relationship Diagrams


Entity Relationship Diagrams

Hope
Those diagrams that look like boxes, lines, and mysterious symbols are Entity Relationship Diagrams, or ERDs. An ERD is a visual way to express the logical design of a database: what kinds of things the database tracks, how those things relate, and often what information identifies them.

Anna
So it’s a way to make the design easier to understand before we build the database. Let’s use a concrete example: a database for people in a community that records which people own which pets, along with pet names, photos, addresses, and the animal type.

Hope
Those requirements tell us what information the database needs to support. A useful first step is to pick out the nouns, because nouns often point to the kinds of things we need to represent. We might start with terms like people, owners, pets, names, photos, animals, types, and addresses.

Anna
Then we refine that list. We use singular terms, so “People” becomes “Person.” We can remove “Owner,” because an owner is a person, and remove “Animal,” because the animals in these requirements are pets. And “Type” is vague, so we make it “Pet Type.”

Hope
That leaves six useful terms: Person, Pet, Name, Photo, Pet Type, and Address. Each describes a kind of thing the database may need to keep track of. But identifying the things isn’t enough; we also need to capture relationships between them.

Anna
For instance, a person owns a pet. If Tricia Anderson owns Cleo the cat and Buster the dog, the database needs to represent those particular connections—not just store a list of people and a separate list of pets.

Hope
We can sketch the relationships in ordinary language: a Person owns a Pet; a Person has a Name; a Pet has a Name; a Photo depicts a Pet; a Pet has a Pet Type; and a Pet resides at an Address. That sketch gives us a useful outline, but it still leaves an important question unanswered.

Anna
How many? The sketch says a person owns a pet, but not whether a pet can have one owner, several owners, or none. Cardinality is the number of instances of one kind of thing that can be associated with instances of another kind.

Hope
Exactly. For this example, we decide that each pet can be owned by zero or more people, and each person can own zero or more pets. Those are design decisions, and they need plausible justifications. The requirements may not settle every case automatically, so we have to think carefully about what the database should allow.

Anna
The same reasoning applies to the other relationships. Each person has one name, and a name can belong to zero or more people. Each pet has one name, and a name can belong to zero or more pets. That last choice matters: two pets may share a name, so the database shouldn’t mistakenly require every pet name to be unique.

Hope
Right. Each photo depicts one pet, while a pet can appear in zero or more photos. A pet has one pet type, and a pet type can apply to zero or more pets. And each pet resides at zero or one address, while an address can have zero or more pets living there.

Anna
Getting those choices wrong can make the finished system fail to meet its requirements. If we incorrectly decide that each name belongs to exactly one pet, for example, users may be blocked from entering two pets with the same name.

Hope
Now we can turn the sketch into an ERD. One option is Chen notation. In Chen notation, each kind of thing—such as Person, Pet, or Photo—is drawn as a box, called an entity. Relationships are shown as diamond shapes connected to the relevant entities by lines.

Anna
Chen notation also lets us show cardinality. In a mother-and-daughter example, reading from Daughter toward Mother, a daughter has a maximum of one mother. Reading from Mother toward Daughter, a mother has a maximum of N daughters, where N means any number, with no defined upper limit.

Hope
Chen notation can show minimums, too. A daughter has a minimum of one biological mother, so every daughter participates in that relationship. That universal participation is drawn with a double line. A mother may have zero daughters, so not every mother participates, and that non-universal participation is shown with a single line.

Anna
Applied to our database, the double lines show that every Person must have a Name, every Pet must have a Name, and every Pet must have a Pet Type. The maximums show that a person can own many pets and a pet can have many people as owners. The letters N and M both mean many; using different letters avoids suggesting the two quantities must be equal.

Hope
We can add attributes to make an ERD more informative. Attributes are characteristics or properties we want to record. For example, a Photo might have an Image Filename and a Date Uploaded. In Chen notation, attributes are ovals connected to their entity.

Anna
If Image Filename is unique for each photo, we underline it to mark it as a unique identifier. Sometimes identification takes a combination of attributes. If a Geographical Location is identified by Latitude and Longitude together, both attributes are underlined.

Hope
There’s also a special case called a weak entity: an entity whose identity depends partly on a related entity. Imagine an Order with several Order Lines. Order Number identifies an Order, while an Order Line is identified by Order Number together with its Line Sequence Number.

Anna
In Chen notation, the Order Line box and its relationship to Order get double borders to indicate that dependency. Line Sequence Number is underlined, but it cannot identify an Order Line by itself; it needs the parent Order’s identifier, Order Number, as well.

Hope
We could add attributes to every entity, but there’s a small modeling choice in our pet example. Since every Person and every Pet has exactly one Name, it may feel more natural to represent Name as an attribute of Person and Pet rather than as a separate entity.

Anna
And Chen is only one ERD notation. Another popular choice, especially outside educational settings, is crow’s foot notation. It doesn’t use diamonds for relationships: a line between two entities is enough, and the symbols at the line’s ends tell us the cardinalities.

Hope
At each end of a relationship line, crow’s foot notation uses two symbols. The first shows the minimum number and the second the maximum. A circle means zero, a vertical line means one, and the branching crow’s foot means many.

Anna
So the symbols at the Mother end can say that a mother has zero or more daughters: zero is the minimum and the crow’s foot marks the maximum. At the Daughter end, a line for the minimum and a line for the maximum say that each daughter has exactly one mother. Each relationship line has two symbols at each end.

Hope
When you classify relationships, focus first on the maximums. If both ends have a crow’s foot, it’s many-to-many: each pet can be owned by many people, and each person can own many pets. If just one end has a crow’s foot, it’s one-to-many—or, viewed from the other direction, many-to-one.

Anna
And if neither end has a crow’s foot and both maximums are one, it’s one-to-one. For example, each employee has one employee ID card, and each employee ID card belongs to one employee.

Hope
In the people-and-pets diagram, Person and Pet have a many-to-many relationship, with zero or more allowed on each side. Photo and Pet are one-to-many: each photo depicts one pet, but a pet may have zero or more photos. Pet Type and Pet are also one-to-many: a pet has one type, and a type can apply to zero or more pets.

Anna
Address and Pet are one-to-many as well. A pet may reside at zero or one address, while an address may have zero or more pets living there. The minimums matter too, because they distinguish a required connection from an optional one.

Hope
We can add attributes in crow’s foot notation, too. A useful diagram should at least show attributes that uniquely identify each entity, plus perhaps a few other significant attributes. It doesn’t have to show every column that the finished database will eventually contain.

Anna
You may see PK beside a primary key attribute—the attribute that uniquely identifies a record in its table. FK marks a foreign key attribute, which also appears as a primary key attribute on another entity. Those labels help show how the entities will connect in the database.

Hope
That’s the bridge from an ERD to a relational database design. Each entity can become a table, and each attribute can become a column. We define primary and foreign keys in line with the diagram, and each one-to-many or one-to-one relationship can be represented with a foreign key constraint.

Anna
But an ordinary foreign key constraint can’t directly express a many-to-many relationship like Person and Pet. The standard workaround is to introduce another entity between them, turning the original relationship into two one-to-many relationships.

Hope
For example, add a PET OWNERSHIP entity between PERSON and PET. If Jenny and Bill both own Rover, that’s two ownership instances. If Martin owns Skipper and Claude, those are two more instances. Each instance links one person to one pet.

Anna
PERSON to PET OWNERSHIP is one-to-many, and PET to PET OWNERSHIP is also one-to-many. PET OWNERSHIP becomes its own table, with foreign keys connecting it to PERSON and PET. Now both relationships can be handled with ordinary foreign key constraints.

Hope
So an ERD helps us move from requirements to a clear model: identify the entities, connect them with relationships, decide the cardinalities, and show important attributes and identifiers. Chen and crow’s foot notation present that information differently, but both help us reason about the design before translating it into tables and keys.

Ask anything
We've read this whole study set, so questions about it get the best answers.