<legend id='e6rpW'><style id='e6rpW'><dir id='e6rpW'><q id='e6rpW'></q></dir></style></legend>
    • <bdo id='e6rpW'></bdo><ul id='e6rpW'></ul>

      <small id='e6rpW'></small><noframes id='e6rpW'>

      <i id='e6rpW'><tr id='e6rpW'><dt id='e6rpW'><q id='e6rpW'><span id='e6rpW'><b id='e6rpW'><form id='e6rpW'><ins id='e6rpW'></ins><ul id='e6rpW'></ul><sub id='e6rpW'></sub></form><legend id='e6rpW'></legend><bdo id='e6rpW'><pre id='e6rpW'><center id='e6rpW'></center></pre></bdo></b><th id='e6rpW'></th></span></q></dt></tr></i><div id='e6rpW'><tfoot id='e6rpW'></tfoot><dl id='e6rpW'><fieldset id='e6rpW'></fieldset></dl></div>
      <tfoot id='e6rpW'></tfoot>

        一对多 MySQL

        时间:2023-11-28

          <small id='ZHF7d'></small><noframes id='ZHF7d'>

              • <bdo id='ZHF7d'></bdo><ul id='ZHF7d'></ul>
              • <tfoot id='ZHF7d'></tfoot>
              • <i id='ZHF7d'><tr id='ZHF7d'><dt id='ZHF7d'><q id='ZHF7d'><span id='ZHF7d'><b id='ZHF7d'><form id='ZHF7d'><ins id='ZHF7d'></ins><ul id='ZHF7d'></ul><sub id='ZHF7d'></sub></form><legend id='ZHF7d'></legend><bdo id='ZHF7d'><pre id='ZHF7d'><center id='ZHF7d'></center></pre></bdo></b><th id='ZHF7d'></th></span></q></dt></tr></i><div id='ZHF7d'><tfoot id='ZHF7d'></tfoot><dl id='ZHF7d'><fieldset id='ZHF7d'></fieldset></dl></div>
                <legend id='ZHF7d'><style id='ZHF7d'><dir id='ZHF7d'><q id='ZHF7d'></q></dir></style></legend>

                  <tbody id='ZHF7d'></tbody>
                • 本文介绍了一对多 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  MySQL 关系

                  我正在尝试使用外键在 MySQL 中创建一对多关系.

                  I am trying to create a one to many relationship in MySQL with foreign keys.

                  两个表,userlocation.每个user可以有多个location,但每个location只能有一个user.

                  Two tables, user and location. Each user can have many locations, but each location can have only one user.

                  我该如何配置?如果有帮助,我正在使用 HeidiSQL,不过我也可以输入代码.

                  How do I configure this? I am using HeidiSQL if that helps, though I can input code as well.

                  推荐答案

                  MySQL 不知道,也不需要知道关系是 1-1 还是 1-many.
                  没有 SQL 支持多对多关系,都需要一个中间表,将多对多关系拆分为 2 个单独的 1 对多.

                  MySQL does not know, nor does it need to know if a relationship is 1-1, or 1-many.
                  No SQL supports many-many relationships, all require a intermediate table which splits a many-many relationship into 2 separate 1-many.

                  区别在于控制关系的逻辑,这在您编写的代码中.
                  通过让表共享相同的主键 (PK) 来维护 1-1 关系.
                  辅助表将 PK 声明为指向其他表 PK 的外键.

                  The difference is in the logic that controls the relationships, which is in the code that you write.
                  A 1-1 relationship is maintained by having the tables share the same primary key (PK).
                  With the secondary table declaring that PK as a foreign key pointing to the other tables PK.

                  Table chinese_mother (
                  id integer primary key,
                  name....
                     
                  
                  Table chinese_child (
                  id integer primary key,
                  name ....
                  ....,
                  foreign key (id) references chinese_mother.id
                  

                  关系的方向1 ->many vs many <- 1 由链接字段的位置决定.

                  The direction of the relationship 1 -> many vs many <- 1 is determined by the location of the link field.

                  通常每个表都有一个唯一的id,链接字段称为tablename_id.
                  包含链接字段的表是关系的many 侧,另一个表在1 侧.

                  Usually every table has a unique id and the link field is called tablename_id.
                  The table that has the link field in it is the many side of the relationship, the other table is on the 1 side.

                  每个用户可以有多个位置,但每个位置只能有一个用户.

                  Each user can have many locations, but each location can have only one user.

                  Table user
                  id: primary key
                  name......
                  .....
                  
                  Table location
                  id: primary key
                  user_id foreign key references (user.id)
                  x
                  y
                  .......
                  

                  通过将链接字段放在 location 表中,您可以强制要求一个位置只能有 1 个用户.但是,一个用户可以拥有多个位置.

                  By placing the link field in the location table, you force things so that a location can only have 1 user. However a user can have many locations.

                  这篇关于一对多 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我应该为 MySQL 数据库使用 MyISAM 还是 InnoDB 表? 下一篇:varchar 值如何存储在 SQL Server 数据库中?

                  相关文章

                  <small id='vtQ3A'></small><noframes id='vtQ3A'>

                  • <bdo id='vtQ3A'></bdo><ul id='vtQ3A'></ul>
                • <legend id='vtQ3A'><style id='vtQ3A'><dir id='vtQ3A'><q id='vtQ3A'></q></dir></style></legend>
                  <i id='vtQ3A'><tr id='vtQ3A'><dt id='vtQ3A'><q id='vtQ3A'><span id='vtQ3A'><b id='vtQ3A'><form id='vtQ3A'><ins id='vtQ3A'></ins><ul id='vtQ3A'></ul><sub id='vtQ3A'></sub></form><legend id='vtQ3A'></legend><bdo id='vtQ3A'><pre id='vtQ3A'><center id='vtQ3A'></center></pre></bdo></b><th id='vtQ3A'></th></span></q></dt></tr></i><div id='vtQ3A'><tfoot id='vtQ3A'></tfoot><dl id='vtQ3A'><fieldset id='vtQ3A'></fieldset></dl></div>

                • <tfoot id='vtQ3A'></tfoot>