mysql 多对多关系

发布于 2024-09-16 07:57:35 字数 946 浏览 1 评论 0原文

一直在阅读教程 如何使用 PHP 处理多对多关系和MySQL

在这个问题中,我参考了“数据库模式”部分,其中规定了以下规则:

这个新表必须构造为 允许以下内容:

* 它必须有一个链接回表“A”的列。
* 它必须有一列链接回表“B”。
* 对于表“A”和表“B”中的行的任意组合,不得存在多于一行。
* 它必须有一个主键。

现在到目前为止一切都很清楚。

我遇到的唯一问题是第三条规则(“它必须允许任何组合都不能超过一行”)。
我也想应用这个,但它似乎不是这样工作的。

在我的 mysql (5.XX) 测试实例上,我能够添加反映相同关系的两行!

例如,如果我建立这种关系(通过添加一行):
A 到 B
它还允许我建立这种关系:
B 到 A

所以问题实际上是两个问题:

1)我如何执行第三条规则,该规则不允许执行上述操作?无论组合如何,都只有一个唯一关系。

2) 当我想要搜索“A”的所有关系时,SQL 查询会是什么样子

注意#1:基本上我的最终目标是创建一个“友谊”系统,据我了解,解决方案是一个多对多表。如果可能的话,提出其他建议。

注意#2:用户表与关系表位于不同的数据库(称为友谊)。因此我不能使用外键。

Been reading the tutorial How to handle a Many-to-Many relationship with PHP and MySQL .

In this question I refer to the "Database schema" section which states the following rules:

This new table must be constructed to
allow the following:

* It must have a column which links back to table 'A'.
* It must have a column which links back to table 'B'.
* It must allow no more than one row to exist for any combination of rows from table 'A' and table 'B'.
* It must have a primary key.

Now it's crystal clear so far.

The only problem I'm having is with the 3rd rule ("It must allow no more than one row to exist for any combination").
I want this to be applied as well, but it doesn't seem to work this way.

On my test instance of mysql (5.XX) I'm able to add two rows which reflect the same relationship!

For example, if I make this relation (by adding a row):
A to B
It also allows me to make this relation as well:
B to A

So the question is two questions actually:

1) How do I enfore the 3rd rule which will not allow to do the above? Have only one unique relation regardless of the combination.

2) When I'll want to search for all the relations of 'A', how would the SQL query look like?

Note #1: Basically my final goal is to create a "friendship" system, and as far as I understand the solution is a many-to-many table. Suggest otherwise if possible.

Note #2: The users table is on a different database from the relations (call it friendships) table. Therefore I cannot use foreign keys.

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

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

发布评论

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

评论(3

半世晨晓 2024-09-23 07:57:35

对于第一个问题:

  1. 对两者创建唯一约束
  2. 确保始终对列进行排序。所以如果你的桌子有
    ab 比确保
    a 小于或等于
    b

对于第二个问题:

SELECT
  *
FROM
  many_to_many_table
WHERE
  a = A or b = A

For the first question:

  1. Create a unique constraint on both
    columns
  2. Make sure you always sort the columns. So if your table has the
    colummns a and b than make sure
    that a is less than or equal to
    b

For the second question:

SELECT
  *
FROM
  many_to_many_table
WHERE
  a = A or b = A
打小就很酷 2024-09-23 07:57:35

听起来您想要一个复合主键。

CREATE TABLE relationship (
     A_id INTEGER UNSIGNED NOT NULL,
     B_id INTEGER UNSIGNED NOT NULL,
     PRIMARY KEY (A_id, B_id)
);

这就是设置表的方式,以便只能有一行将表 AB 定义为相关的。它之所以有效,是因为主键在表中必须是唯一的,因此数据库将只允许一行包含任何特定的值对。您可以创建不是主键的复合键,并且它们不必是唯一的(但您可以创建唯一的非主键,无论是否复合),但您的规范要求主键,所以这就是我的建议。

当然,您可以添加其他列来存储有关此特定关系的信息。

It sounds like you want a composite primary key.

CREATE TABLE relationship (
     A_id INTEGER UNSIGNED NOT NULL,
     B_id INTEGER UNSIGNED NOT NULL,
     PRIMARY KEY (A_id, B_id)
);

This is how you setup a table so that there can only ever be one row that defines tables A and B as related. It works because a primary key has to be unique in a table so therefore the database will allow only one row with any specific pair of values. You can create composite keys that aren't a primary key and they don't have to be unique (but you can create a unique non-primary key, composite or not), but your specification requested a primary key, so that's what I suggested.

You can, of course, add other columns to store information about this specific relationship.

月依秋水 2024-09-23 07:57:35

好吧,WoLpH 更快,我基本上同意(请注意,您必须同时在两列上创建一个单个约束!)。只是为了解释为什么您与您提到的规则发生冲突:通常,A 和 B 是不同的表。因此,n:m 关系的典型示例将允许条目 (1,0) 和 (0,1),因为它们引用不同的对。表 A=表 B 是一种不同的情况(您使用 A 和 B 作为用户,但在示例中它们是表)。

Ok WoLpH was faster, I basically agree (note that you have to create a single constraint on both columns at the same time!). And just to explain why you collide with the rules you mentioned: Typically, A and B are different tables. So the typical example for n:m relations would allow entries (1,0) and (0,1) because they'd be refering to different pairs. Having table A=table B is a different situation (you use A and B as users, but in the example they're tables).

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