SQL Relationships
What kinds of relationships can we represent in a database?
one-to-one, one-to-many, and many-to-many.
When do we use “One to One” relationships? Give an example from the real world.
In a one-to-one relationship, one record in a table is associated with one and only one record in another table. For example, in a school database, each student has only one student ID, and each student ID is assigned to only one person.
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?
The author has a column called customer_id in the customer table because each customer will have one id. Now this ID column can be used on the address table to connect each customer to one address.
Give an example from the real world when would need to use a “Many to One/One to Many” relationship.
An example of Many-to-One is: where one project can have more than one student working on it. A team of five students in a college in assigned a project that they need to complete in let us say one month. This states a relationship between two entities Student and Project
In a One-to-Many relationship, one record in a table can be associated with one or more records in another table. For example, each customer can have many sales orders .
Give an example from the real world when would need to use a “Many to Many” relationship.
Many-to-many relationship : When one row of table A can be linked to one or more rows of table B, and vice-versa. Real Life Example : A user can belong to multiple communities, and a community can have multiple users. A practical example to many-to-many is the courses taken in a college.
What does items_orders table do?
In some cases, you may need multiple instances on both sides of the relationship. For example, each order can contain multiple items. And each item can also be in multiple orders. The Items_Orders table has only one purpose, and that is to create a “Many to Many” relationship between the items and the orders.