一对多 MySQL

发布于 2024-12-17 05:26:40 字数 374 浏览 2 评论 0原文

可能的重复:
MySQL 关系

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

两个表,userlocation。每个用户可以有多个位置,但每个位置只能有一个用户

我该如何配置这个?如果有帮助的话,我正在使用 HeidiSQL,尽管我也可以输入代码。

Possible Duplicate:
MySQL Relationships

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2024-12-24 05:26:40

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

区别在于控制关系的逻辑,即您编写的代码中的逻辑。
通过让表共享相同的主键 (PK) 来维护 1-1 关系。
辅助表声明 PK 作为指向其他表 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 -> Manymany <- 1 由链接字段的位置决定。

通常每个表都有一个唯一的id,链接字段称为tablename_id
包含链接字段的表是关系的many 端,另一个表位于1 端。

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

Table user
id: primary key
name......
.....

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

通过将链接字段放置在 location 表中,您可以强制执行某个位置只能有 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.

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

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

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
.......

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.

她如夕阳 2024-12-24 05:26:40

这里有一个例子几乎正是您所需要的 innodb 中的外键

CREATE TABLE parent (
  id INT NOT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE child (
  id INT,
  parent_id INT,
  INDEX par_ind (parent_id),
  FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE
) ENGINE=INNODB;

在您的示例中,用户与父级相同(一个用户有多个位置,一个父级有多个子级),并且位置与子级相同(一个位置有一个用户,一个子级有一个父级) )

There is an example here that is almost exactly what you need foreign keys in innodb

CREATE TABLE parent (
  id INT NOT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE child (
  id INT,
  parent_id INT,
  INDEX par_ind (parent_id),
  FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE
) ENGINE=INNODB;

In your example user is the same as parent (a user has many locations, a parent has many childs) and location is the same as child (a location has one user, a child has one parent)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文