In this section, we’ll look at the steps required to manually
translate an ER model into database tables. We’ll then perform these
steps using the music
database as
an example. In Using Tools for Database Design,” we’ll see how
we can automate this process with the MySQL Workbench tool.
When converting an ER model to a database schema, we work through each entity and then through each relationship according to the following rules to end up with a set of database tables.
For each strong entity, create a table comprising its attributes and designate the primary key. The parts of any composite attributes are also included here.
For each weak entity, create a table comprising its attributes and including the primary key of its owning entity. The primary key of the owning entity is known as a foreign key here, because it’s a key not of this table, but of another table. The primary key of the table for the weak entity is the combination of the foreign key and the partial key of the weak entity. If the relationship with the owning entity has any attributes, add them to this table.
For each multivalued attribute of an entity, create a table comprising the entity’s primary key and the attribute.
For each one-to-one relationship between two entities, include the primary key of one entity as a foreign key in the table belonging to the other. If one entity participates totally in the relationship, place the foreign key in its table. If both participate totally in the relationship, consider merging them into a single table.
For each nonidentifying one-to-many relationship between two entities, include the primary key of the entity on the “1” side as a foreign key in the table for the entity on the “N” side. Add any attributes of the relationship in the table alongside the foreign key. Note that identifying one-to-many relationships (between a weak entity and its owning entity) are captured as part of the entity-mapping stage.
For each many-to-many relationship between two entities, create a new table containing the primary key of each entity as the primary key, and add any attributes of the relationship. This step helps to identify intermediate entities.
For each relationship involving more than two entities, create a table with the primary keys of all the participating entities, and add any attributes of the relationship.
Following the mapping rules as just described, we first map entities to database tables:
For the strong entity
Artist
, we create the tableartist
comprising the attributesartist_id
andartist_name
, and designateartist_id
as the primary key.For the weak entity
Album
, we create the tablealbum
comprising the attributesalbum_id
andalbum_name
, and include the primary keyartist_id
of the owningArtist
entity as a foreign key. The primary key of thealbum
table is the combination {artist_id
,album_id
}.For the weak entity
Track
, we create the tabletrack
comprising the attributestrack_id
,track_name
, andtime
, and include the primary key {artist_id
,album_id
} of the owningAlbum
entity as a foreign key. The primary key of thetrack
table is the combination {artist_id
,album_id
,track_id
}.For the weak entity
Played
, we create the tableplayed
comprising the attributeplayed
, and include the primary key {artist_id
,album_id
,track_id}
of the owningTrack
entity as a foreign key. The primary key of theplayed
table is the combination {artist_id
,album_id
,track_id
,played
}.There are no multivalued attributes in our design, nor are there any nonweak relationships between our entities, so our mapping is complete here.
You don’t have to use consistent names across all tables; for
example, you could have a column musician
in the album
table that contains the artist ID
that you call artist_id
in the
artist
table. Obviously, it’s
much better to use a consistent naming convention to avoid
confusion. Some designers put fk_
in front of columns that contain foreign keys; for example, in the
album
table, we could store the
artist ID in the fk_artist_id
column. We don’t use this convention in this book.
Get Learning MySQL now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.