table-to-table relationship

---Restore content begins---

Introduction:

Disadvantages of storing all data in one table

1: The organizational structure of the table is complex and unclear

2: Waste of space

3: Extremely poor scalability

1. Find the relationship routine between tables and tables:

Example: emp employee table dep department table

step:

  part1:

    1. First stand at the angle of the left table emp

    2. Find out whether multiple records in the left table emp correspond to a record in the right table dep

    3. The meaning of translation 2:

        Multiple records in the left table emp == "" multiple employees

        A record in the right table dep == "" a department

 

         Final translation: Can multiple employees belong to the same department?

         If so, you need to go through the process of part2

  part2:

    1. First stand at the angle of dep on the right table

    2. Find out whether multiple records in the right table dep can correspond to a record in the left table emp

    3. The meaning of translation 2:

        Multiple records in the right table dep == "" multiple employees

        A record in the left table emp==""a department

 

         Final translation: Can multiple departments contain the same employee

         If not, it can be determined that the relationship between emp and dep is only one-way many-to-one

         How to achieve?

            Add a dep_id field to the emp table, which points to the id field of the dep table

 With picture:

 

Use foreign key to achieve many-to-one

Constraint 1: When creating a table, first create the associated table dep (that is, one of many-to-one), and then create the associated table emp (many-to-one-many), otherwise an error will be reported

Constraint 2: When inserting records, the closed table dep must be inserted before the associated table emp can be inserted, otherwise an error will be reported

3: Constraint 3: Both updates and deletions need to take into account the relationship between the association and the associated relationship

1: Delete the associated table emp1 first, and then delete the associated table dep1, otherwise an error will be reported

2: Rebuild: new features, synchronous update, synchronous deletion use on update cascade on delete cascade

Create first:

Query data:

Synchronous deletion: delete from table name where + condition

Synchronous update: update table name set change content where + condition

Clear the table: delete from tb1

q emphasizes; the above command can indeed delete all records in the table, but it will not reset the id to 0,

All this command is not used to empty the table at all, delete is used to delete some eligible records in the table

delete from tb1 where id >10;

If you want to empty the table, use truncate tb1;

The effect is to reset the entire table.

 

 As shown in the figure: After deleting the data with id>3 or more, adding new data is based on the original id that was not deleted. Therefore, delete should not be used at this time.

Two: many-to-many

There is a bidirectional many-to-one relationship between the two tables, which is called many-to-many

How to achieve

Create a third table, which has a field fk, the id of the left table, and a field fk, the id of the right table

For example: two tables, one for books and one for authors:

 

First create these two tables:

Now create the 3rd table

Inquire:

 

 one-on-one

A record in the left table only corresponds to a record in the right table, and vice versa

With picture:

Zhang Tidan, Li Cannon, Yang Li, Zhao Bullet, Liu Erya, Wang San Cannon, Alex Liang Shudong

 

 create table

Insert data:

Check data:

 

---End of recovery content---

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326491370&siteId=291194637