Database one-to-many, many-to-many table design

To do a project, the database design must be indispensable! In the learning stage, it is basically a single table. However, in the actual development process, one-to-many and many-to-many tables are everywhere! Simply organize, one-to-many, many-to-many table how to design and organize ideas:

       There are three correspondences between database entities: one-to-one, one-to-many, and many-to-many.

       An example of a one-to-one relationship:

    • One student corresponds to one student file material, or each person has a unique ID number.

       Example of a one-to-many relationship:

    • A student belongs to only one class, but a class has multiple students.

       Example of many-to-many relationship:

    • A student can choose multiple courses, and a course can have multiple students.

1. One-to-many relationship processing:

       Learn one-to-many with student and class questions:

       Design database table: just  add an ID of the class number to the student table  ;

Note: When you are a beginner in the table in the database, you should add primary and foreign key constraints to avoid data confusion when deleting data!

 

2. Many-to-many relationship processing:

    Learn how to deal with many-to-many problems through student selection:

 

       In many-to-many, adding a field to a table will not work, so when dealing with many-to-many table problems, it is necessary to consider establishing a relationship table

example:

 Student table:      Class table:    Relation table:

 

Note: So for many-to-many tables, the relationship between the two tables is established through the relationship table! After establishing the primary foreign key in the many-to-many table, delete the content of the constraint table first and then delete the content of the main table

Guess you like

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