Database design
From conceptual to logical data model, one relationship at a time
The three rules that cover most projects, with the example of a student carpooling app.
Updated on
What we transform
The conceptual data model (MCD in the Merise method) describes what the project handles: entities (User, Ride, Vehicle) linked by relationships (Books, Uses), with their cardinalities. The logical data model (MLD) describes the tables you are going to create. Going from one to the other follows fixed rules: you do not choose, you apply.
For Covoit'Campus, a carpooling app between students, the starting model is:
- User (id_user, name, email, campus)
- Ride (id_ride, origin, destination, date_time)
- Vehicle (id_vehicle, seats)
- User (0,n) — Books — (1,n) Ride
- Ride (1,1) — Uses — (0,n) Vehicle
To read a cardinality, start from the entity: "a ride uses at least 1 and at most 1 vehicle", "a vehicle is used by 0 to n rides". Only the maximum matters for the logical model.
Rule 1: each entity becomes a table
The entity's identifier becomes the primary key, its properties become columns.
Nothing else changes at this step. Relationships do not show up yet.
Rule 2: a maximum cardinality of 1 gives a foreign key
A ride uses a single vehicle: the cardinality on the Ride side is (1,1). The vehicle's identifier moves into the table on the side where the maximum is 1, as a foreign key. The Uses relationship disappears: the foreign key is enough to represent it.
The minimum tells whether the foreign key is required: (1,1) gives a NOT NULL column, (0,1) a column that can stay empty. If the relationship has properties, they follow the foreign key into the same table.
Rule 3: two maximum cardinalities of n give a table
A user books several rides, a ride is booked by several users. Neither table can hold the other's key: the relationship becomes a table, whose primary key combines both identifiers.
The relationship's properties become columns of this table: a booking date, a number of seats, a status.
Cases that come up often
- Relationship between three or more entities: it becomes a table, as in rule 3, with a primary key made of the identifiers involved.
- (0,1) — (1,1) relationship: the foreign key goes on the (1,1) side, where it is required.
- Self-relationship (a user refers another user): the same rules, with two columns pointing to the same table and names that say their role (id_referrer, id_referee).
Check the result
Every table has a primary key. Every foreign key points to a table that exists. No column repeats information already held elsewhere. If a column depends on a column other than the key, for example a campus name repeated on every user, it is a sign that a Campus entity is missing.
For Covoit'Campus, the full logical model fits in four tables:
- app_user (id_user, name, email, campus)
- vehicle (id_vehicle, seats)
- ride (id_ride, origin, destination, date_time, #id_vehicle)
- booking (#id_user, #id_ride)