- 1Introduction
- 2Database history
- 3Relational database characteristics
- 4Customer contact system
- 5Tables and the primary key
- 6Linking tables
- 7The one-to-many relationship
- 8The many-to-many relationship
- 9The one-to-one relationship
- 10Database normalization
- 11The first normal form (1NF)
- 12The second normal form (2NF)
- 13The third normal form (3NF)
- 14The final database design
- 15Another example: webshop database
- 16Conclusion
Database design tutorial
The one-to-many relationship
When one data item can be linked to 0, 1 or many other data items, you are dealing with a one-to-many relationship. You already saw one example of a one-to-many relationship. One customer can be linked to multiple contact moments. And vice versa each contact moment is linked to only 1 customer. The same applies to the relationship that exists between a woman and her children. A woman can have 0, 1 or many children and each child has one woman as its mother.
In the relational model a one-to-many relationship is modelled using two tables.
In the database we are designing for the customer contact system the Customers table plays the role of table A. The ContactMoments table corresponds to table B in the image. Note how every customer row in table A can have 0, 1 or multiple contact moment rows in table B.
The customer_id field is the primary key in table A. It uniquely identifies a customer row in that table. The customer ID is present in table B as well. In table B the customer_id is called a foreign key. A foreign key field is a field that points to the primary key field of another table. In the relational model data are linked using the foreign key. In our database design example, each contact moment is linked to a customer via the customer_id foreign key field.
Table Customers
customer_idis the primary key (pk) field in the Customers table.
ContactMoment table
enlarge
In the ContactMoment table, customer_id is a foreign key. It points to the customer_id field in the Customers table. The contact_moment_id is the primary key (pk) in the Customers table.