SQL Relationships
- What kinds of relationships can we represent in a database?
- One to One
- One to Many / Many to One
- Many to Many
- Self-Referencing
-
When do we use “One to One” relationships?
It’s not used very frequently because it can only represent very simple and straight forward relations that can usually be conflated within a single record of a table. A particular attribute value in such a One to One relationship can only be connected to a single entry, e.g. the address that belongs to a customer. -
In the article in the “One to One”-section, the author of the article has a column called customer_id in the customers table. Why? How is it used in order to connect customers to addresses?
It serves as a Foreign Key through which records in one table can be uniquely identified within another table. -
Give an example from the real world where we would need to use a “Many to One/One to Many” relationship
If we want to store family relationships in a database each person can have multiple children or siblings. Conversely, multiple children will have exactly one mother. -
Give an example from the real world where we would need to use a “Many to Many” relationship
In a database that tracks orders and products each order may be associated with multiple products and any given product can be part of different orders. -
What does items_orders table do?
In this case it creates a relationship (Many to Many) between the entries of two distinct tables