A database consists of several tables, such as: Customers, Products, Orders, Order Details, etc. Although the information is separate in each of the Tables, in practice there must be relationships between the tables. For example: An Order is made by a Customer and in this Order there may be several items, items that are recorded in the Order Details table. In addition, each Order has a unique number (Order Code), but the same Customer can make several orders and so on.
In a database, we need some way to represent these real-life relationships in terms of the tables and their attributes. This is possible with the use of "Relationships between tables", which can be of three types:
- One to One
- One for Various
- Various for Various
Type One-to-One Relationship:
This relationship exists when the fields that are related are both of the Primary Key type, in their respective tables. Each field has no repeated values. In practice there are few situations where we will use a relationship of this type. An example could be as follows: Imagine a school with a Student Master in the Students table, of which only a small part participates in the School Band. For reasons of database design, we can create a Second Table "Band Students", which relates to the Students table through a relationship of type One to One. Each student is only registered once in the Students Table and one only in the Band Students table. We could use the Student Enrollment Field as the Field that relates the two Tables.
Important: The field that lists two tables must be part of the structure of the two tables.
In the Band Students table, we could only place the student's Enrollment Number, as well as the information about the Instrument it plays, band time, etc. When it is necessary to search the information such as name, address, etc., these can be retrieved through the existing relationship between the two tables, thus avoiding that the same information (Name, Address, etc.) must be duplicated in the two tables , including increasing the likelihood of typing errors.
One-to-Many Relationship:
This is certainly the most common type of relationship between two tables. One of the tables (the one side of the relationship) has one field that is the Primary Key and the other table (the multiple side) relates through a field whose related values can be repeated several times.
Consider the example between the Customers and Orders table. Each Client is only registered once in the Customers table (so the Customer Code field in the Customers table is a primary key, indicating that two customers with the same code can not be registered), therefore the Customers table will be the side of the relationship. At the same time, each customer can make several requests, so the same Customer Code may appear several times in the Orders table: as many times as the customer requests. That is why we have a One to Many relationship between the Customers and Orders table, through the Customer Code field, indicating that the same Customer can make several (multiple) orders.
Multiple-to-Many type relationship:
This type of relationship would "happen" in a situation where on both sides of the relationship values could be repeated. Let's consider the case between Products and Orders. I may have Multiple Orders in which a particular product appears, in addition several Products may appear in the same Order. This is a situation where we have a Multiple-to-Many Type Relationship.
In practice it is not possible to implement such a relationship, due to a number of problems that would be introduced in the database model. For example, in the Orders table we would have to repeat the Order Number, Customer Name, Employee Name, Order Date, etc. for each Order item.
Here a good PDF for you to use some examples!