Basic sql statement analysis table relationship

Example of a one-to-one relationship

Suppose there are two tables now, one is the wife table and the other is the husband table. There is obviously a one-to-one relationship between these two tables.

 There are two tables A and B, add primary keys respectively, and set a foreign key in one of the tables to associate with the primary key in the other table.

One-to-many relationship instance

In a one-to-many table relationship, the table on one side is called the master table, and the table on one side is called the slave table. Add a foreign key in the secondary table to associate the primary key in the main table, so that a straight-line one-to-many relationship can be achieved.

For example: department and employee (the department table is the main table, the employee table is the slave table, and the foreign key created in the employee table is associated with some tables)

Many-to-many relationship instance

If two tables A and B want to realize the many-to-many relationship, a table C is needed, and two foreign keys are set in C to associate the primary keys of tables A and B respectively. For example: table students and courses (student selection is a many-to-many relationship)

Because each piece of data in each table may correspond to multiple data, no matter which table the foreign key is placed in, it is not appropriate to use the third table.

Guess you like

Origin blog.csdn.net/weixin_53818758/article/details/126122246