Three relationships between relational database tables and tables, three types of databases
1. One-to-one relationship: Definition: There are two tables. a row in the first table is only related to a row in the second table, and a row in the second table, it is also related to only one row in the first table. We call these two tables a one-to-one relationship. For example, the first table:
ID |
Name |
Nationality |
Contribution |
1001 |
Wang Damai |
China |
Never imagined |
1002 |
Jizemingbu |
Japan |
Action Art Film |
1003 |
Blade queen |
Unknown |
Family planning |
The second table:
ID |
Date of birth |
Date of death |
1001 |
1988 |
NULL |
1002 |
1984 |
NULL |
1003 |
Unknown |
3XXX |
Each row in the first table corresponds to only one row in the second table. Tip: one-to-one relationships are rare. However, this type is still required in some cases. Case 1: A table contains too many data columns. Case 2: Separate data into different tables and divide the data into different security levels. Case 3: extract common data columns to form a table 2, one-to-many relationship: Definition: there are multiple tables, the rows in the first table can be associated with one or more rows in the second table, but the rows in the second table can only be associated with the rows in the first table. For example, the first table:
ID |
Name |
Nationality |
|
1001 |
Wang Damai |
China |
|
1002 |
Jizemingbu |
Japan |
|
1003 |
Blade queen |
Unknown |
|
The second table:
ID |
Program ID |
1001 |
10001 |
1001 |
10002 |
1003 |
10003 |
1003 |
10004 |
The third table:
Program ID |
Program name |
10001 |
Never imagined |
10002 |
Report to the boss |
10003 |
Interstellar 2 |
10004 |
League of legends |
The row in the first table can correspond to multiple rows in the second table; the row in the second table corresponds to the row in the third table. Tip: One-to-multiple relationships are the most common link types. 3. Multi-to-Multi-Relationship: Define: There are two tables. The row of the first table can be associated with one or more rows in the second table. At the same time, a row in the second table can be associated with one or more rows in the first table. For example, the first table:
ID |
Name |
Nationality |
|
1001 |
Wang Damai |
China |
|
1002 |
Jizemingbu |
Japan |
|
1003 |
Blade queen |
Unknown |
|
The second table:
ID |
Program ID |
1001 |
10001 |
1001 |
10002 |
1003 |
10003 |
1003 |
10004 |
The third table:
Program ID |
Program name |
10001 |
Never imagined |
10002 |
Report to the boss |
10003 |
Interstellar 2 |
10004 |
League of legends |
Between the first and third tables, one person can participate in multiple programs, or multiple people can participate in one program. For example, Mr. Wang participated in "never imagined" and "Report to the boss". Wang Damai and Ji zemingbu are in "never imagined. This is a many-to-many relationship.