Relational database

relationship

The relationship between the entity and the entity, reflected in the final design of the database up: The Relationship Between divided into three: one to one, one to many (many) and many to many.
All relationships refer to relationships between tables and tables.
1, one
-one: a record corresponding to a table only with the addition of a record in a table; and vice versa.

Student Table: name, sex, age, height, weight, place of birth, home address, emergency contacts

Table designed to form above, meet the requirements, which the name, sex, age, height, weight belong to frequently used data, but the place of birth, address, emergency contact does not belong to frequently used data, if each query is a query for all data, less frequently used data it will affect the efficiency, it does not actually.

Solution: The usual and unusual separate storage of information, divided into two tables
Common Information Table

Information Table is not commonly used: to ensure that no common information corresponding to the common information will be able to: find a uniqueness field (recording determination) having commonly connected to two tables, i.e., without increasing the common table id.

A record of a common table, only ever match a record in a table is not commonly used, in turn, a record is not commonly used table can only match a record: one to one relationship.

2, many
-to-many: there is a record in a table may correspond to a plurality of records of another table, but in turn, an additional record only a table corresponding to a record in the first table, the relationship It is one to many or many-to.

The relationship between mother and child: the mother, the child two entities

      妈妈表:ID(P)、名字、年龄、性别

       孩子表:ID(P)、名字、年龄、性别

Above relationship: a mother can be many records (may be one), but a child can only find a mother, it is a typical-many relationship found in the child table.

But above relates to the design table to solve the problem of entities, but does not solve the problem of the relationship, the child could not find her mother, and her mother could not find the child.

Solution: Add a field in a particular table, it is possible to find another record in a table: should add a field in the child table to the table my mother: as the records of the child table can only match to record a mom table.

      妈妈表:ID(P)、名字、年龄、性别

     孩子表:ID(P)、名字、年龄、性别、妈妈ID

3, many-to
-many: a table (A) can correspond to a record in another table (B) a plurality of records, a record notification B of the table can correspond to a plurality of records in the table A: pairs many relationships.

Teacher education, teachers and students

               老师表
      T_ID(P)        姓名       性别
       1                 A            男
        2                B            女

  学生表
       S_ID(P)     姓名        性别
        1                张三        男
        2                小芳        女

More than design: to achieve a design entity, but does not maintain the relationship of the entity.
A teacher taught more students; students are more than a teacher taught.

Solution: Add in the student table teacher field, add a field no matter where Zhang table, there will be a problem: the fields you want to save multiple and data, but also with other tables have fields relationship does not meet the design rules of Table, an increase of a new table : devoted to the defense relationship between the two tables.

  老师表
     T_ID(P)    姓名       性别
      1                A           男
      2                B           女

 学生表
   S_ID(P)     姓名        性别
    1                张三        男
    2                小芳        女

  中间关系表:老师与学生的关系
         ID                T_ID(老师)        S_ID(学生)
          1                   1                             1
          2                   1                             2
          3                   2                             1
          4

After the increase in the middle of the table, the middle table with the teacher table to form a one to many relationship; and the middle of the table is a multi-table, the only maintenance that can find a list of relationship; the same relationship, the students table and the middle table is one to many, It can be matched to-many relationship between the data relation table.

Students find a teacher: Find student id- "middle of the table to find a matching record (multiple) -" Teacher table match (a)
teacher looking for students: find teacher id- "middle of the table to find a matching record (multiple) -" Student table match (a).

Guess you like

Origin blog.csdn.net/weixin_44097082/article/details/94831635