Entity-relationship model: an example with people and thermoses
Entities, attributes and many-to-one, one-to-many and many-to-many relationships explained with an everyday example from Uruguay: people and their mate thermoses.
Modeling means creating an abstract representation of reality, and in database design the most common tool is the entity-relationship (ER) model. Using a simple example, people and thermoses, this article explains what an entity is, what attributes are, and how the model changes with the rules of the reality being represented.
TABLA DE CONTENIDOS
Modeling means creating an abstract representation of reality. The representation itself (called a model) is usually shown as a diagram or drawing, which varies with the purpose of the modeling. For database design, the usual tool is the entity-relationship model (ER model). This article explains it with a simple example: people and their thermoses.
Entities, attributes and instances: the thermos
Suppose that, for some unknown reason, we need to build a system to manage information about every thermos in the world. First, we need to know enough about the properties of a thermos. Using a bit of common sense and general knowledge, we could assume that a thermos has a brand, a capacity and, if we assume it is cylindrical, measurements such as height and diameter. We could also say that a thermos has a color and a weight in kilograms. All of these elements (brand, capacity, height, diameter, color and weight) are attributes of the thermos. We can then assume that every thermos in the world shares this set of attributes. That is where the concept of the entity, or more technically the class, Thermos comes in.
Of course, my thermos is not the same as yours, nor is it the same as José’s. José lives in the USA and, to keep up the customs of Uruguay, where people carry a thermos of hot water to drink mate, he packed his beloved thermos in his suitcase. So we can say that your thermos, mine and José’s are all instances of the Thermos class, and each one will be stored as a separate record in the database.
Two entities and a relationship: people and thermoses
I don’t want to repeat myself, but let me add a new assumption to the example: besides storing data about every thermos, we also need to record information about the people who own them. To do that, we have to add a new entity, the Person class. This is where we have to work out which attributes of a person are necessary or important to store in our database (name, age, ID number, maybe blood type, and so on).
At this point we have two entities, or classes: the Thermos entity and the Person entity. Now, how do we record which thermoses each person owns? Two separate entities are not enough for that: we need to define a relationship between them.
A relationship links two entities, and there are many types of relationships, depending on the nature of that link.
Many-to-one: each person, only one thermos
Suppose there is a law in the world that says a person cannot own more than one thermos at a time. In that case, the Person-Thermos relationship would be a many-to-one relationship (N to 1), which can be read as: N people can each have at most one thermos. The best way to represent this is to define a thermos identifier as one more attribute of the Person entity. If we were also told that, in the reality being studied, every person must have a thermos, it would make sense to make the thermos identifier mandatory in the Person entity. That means the attribute cannot be left empty, or, put another way, there is no person without a thermos.
One-to-many: one person, several thermoses
That is not how things work in real life, though. There is usually no limit on how many thermoses a person can own, which gives us a one-to-many relationship (1 to N): one person can have N different thermoses, and each thermos belongs to only one person. In this case, it makes sense to add one more attribute to the Thermos entity: an identifier of the person who owns it. We could also add (if it makes sense for the reality being analyzed) the constraint that a thermos cannot exist without an owner, in which case the person identifier in the Thermos entity should be mandatory.
Many-to-many: shared thermoses
Now let’s take a less realistic case: imagine a world where each person can own more than one thermos, but each thermos can be shared by more than one person. It sounds like two people agreeing to buy a thermos together and each using it half the time. Here we would have a many-to-many Person-Thermos relationship (N to N), which roughly means: each person can have N thermoses, and each thermos can be assigned to (or shared by) N people. This type of relationship is usually modeled as a new entity that represents the relationship itself (let’s call it the Person-Thermos entity), with two attributes: a person identifier, which refers to one of the thermos owners, and a thermos identifier, which refers to one of the thermoses that this person “shares” or “owns.”
Same reality, different models
The reality analyzed in this example is always the same. What changes is the model used to represent it when storing its information. That depends on how we interpret the reality being modeled, how we understand the relationships between entities, and what requirements we are given. If there are so many possible variations of attributes and relationships to represent a simple reality with only two entities (people and thermoses are used here just to illustrate the ideas; it could be any other pair of entities), imagine how complex it is to model real-world scenarios, which usually have far more than two entities and one relationship.
With nothing more to write, I have to go. It’s time for mate, and I need to heat water for my thermos.
Pablo Carballo Soria
Computer Analyst / Software Developer