MySQL 对两个使用相同外键 ID 的唯一键进行约束

发布于 2025-01-04 23:37:20 字数 1263 浏览 0 评论 0 原文

我想创建一个 MySQL 表来保存用户之间的关系数据。用户A与B之间以及用户B与A之间的关系可以不同。

示例:

  1. 鲍勃(与)爱丽丝的关系:0.9 - 鲍勃喜欢爱丽丝的东西。
  2. 爱丽丝(与)鲍勃的关系:0.5 - 爱丽丝发现鲍勃的东西平庸。

我的问题:

我已经在引用用户表中的 user_ids 的两个外键上实现了两个约束作为唯一键。我可以这样做吗?它们是否被视为两个单独的唯一密钥?

如何实现一个约束,该约束仅允许每个 user_id 每个(从)UserA(到)UserB 关系和(从)UserB(到)UserA 关系出现一次?我以正确的方式处理这件事吗?

SQL:

CREATE TABLE relationships (
  relationship_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
  from_user_id MEDIUMINT UNSIGNED NOT NULL,
  to_user_id MEDIUMINT UNSIGNED NOT NULL,
  relationship_level DECIMAL(1,1) NOT NULL,
  PRIMARY KEY (relationship_id),

  FOREIGN KEY (from_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  FOREIGN KEY (to_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  CONSTRAINT from_to_relationship UNIQUE KEY (from_user_id, to_user_id),
  CONSTRAINT to_from_relationship UNIQUE KEY (to_user_id, from_user_id),

  INDEX relationship_from_to (relationship_id, from_user_id, to_user_id, relationship_level),
  INDEX relationship_to_from (relationship_id, to_user_id, from_user_id, relationship_level)

) ENGINE=INNODB;

我希望有人能提供帮助。

I want to create a MySQL table to hold relationship data between users. The relationship between user A to B and user B to A can be different.

Example:

  1. Relationship of (from) Bob (to) Alice: 0.9 - Bob loves Alice's stuff.
  2. Relationship of (from) Alice (to) Bob: 0.5 - Alice finds Bob's stuff mediocre.

My question:

I have implemented two CONSTRAINTS as UNIQUE KEYs on two FOREIGN KEYS that reference user_ids in a users table. Can I do this? Are they treated as two separate UNIQUE KEYs?

How can I implement a CONSTRAINT that will only allow one occurrence of each (from) UserA (to) UserB relationship and (from) UserB (to) UserA relationship per user_id? Am I going about it the right way?

The SQL:

CREATE TABLE relationships (
  relationship_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
  from_user_id MEDIUMINT UNSIGNED NOT NULL,
  to_user_id MEDIUMINT UNSIGNED NOT NULL,
  relationship_level DECIMAL(1,1) NOT NULL,
  PRIMARY KEY (relationship_id),

  FOREIGN KEY (from_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  FOREIGN KEY (to_user_id) REFERENCES users (user_id)
    ON DELETE CASCADE ON UPDATE NO ACTION,

  CONSTRAINT from_to_relationship UNIQUE KEY (from_user_id, to_user_id),
  CONSTRAINT to_from_relationship UNIQUE KEY (to_user_id, from_user_id),

  INDEX relationship_from_to (relationship_id, from_user_id, to_user_id, relationship_level),
  INDEX relationship_to_from (relationship_id, to_user_id, from_user_id, relationship_level)

) ENGINE=INNODB;

I hope someone can assist.

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

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

发布评论

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

评论(1

热血少△年 2025-01-11 23:37:20

仅保留这些UNIQUE 约束之一 - 无需同时拥有两者。当一行失败 UNIQUE KEY (from_user_id, to_user_id) 时,它也会失败 UNIQUE KEY (to_user_id, from_user_id),反之亦然,因此它们在逻辑上是等效的。即使只有一个 UNIQUE 约束,当尝试表示 Alice 和 Bob 之间的关系时,最多可以有一个 {Alice, Bob} 行,and 最多可以有一个 {鲍勃,爱丽丝}行。

至于性能(即双向遍历关系),您可能需要考虑索引 {from_user_id, to_user_id} (用于“向前”遍历)和/或 {to_user_id, from_user_id}< /code> (用于“向后”遍历)。您甚至可以放弃代理主键 (relationship_id) 并采用自然 PK,从而降低对索引的需求(辅助索引对于聚集表来说非常昂贵,请参阅 了解 InnoDB 聚集索引,“缺点”部分聚类”)。

在我看来,您的表格应该如下所示:

CREATE TABLE relationships (
    from_user_id MEDIUMINT UNSIGNED NOT NULL,
    to_user_id MEDIUMINT UNSIGNED NOT NULL,
    relationship_level DECIMAL(1,1) NOT NULL,
    PRIMARY KEY (from_user_id, to_user_id), -- InnoDB is clustered, so naturally "covers" relationship_level as well.
    FOREIGN KEY (from_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
    FOREIGN KEY (to_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
    INDEX relationship_to_from (to_user_id, from_user_id, relationship_level) -- Including relationship_level may or may not be a good idea.
) ENGINE=INNODB;

注意:是否在 INDEX 中包含 relationship_level 取决于您是否想要 仅索引扫描 也以“向后”方向进行。 “前进”方向自然被PK覆盖(因为InnoDB是

Keep only one of these UNIQUE constraints - there is no need to have both. When a row fails UNIQUE KEY (from_user_id, to_user_id) it also fails UNIQUE KEY (to_user_id, from_user_id) and vice versa, so they are logically equivalent. Even with only one UNIQUE constraint, when trying to represent a relationship between Alice and Bob, you can have at most one {Alice, Bob} row, and at most one {Bob, Alice} row.

As for performance (i.e. traversing the relationship in both directions), you might want to consider indexing {from_user_id, to_user_id} (for "forward" traversal) and/or {to_user_id, from_user_id} (for "backward" traversal). You might even ditch the surrogate primary key (relationship_id) and go for the natural PK, thus lowering the need for indexing (secondary indexes are expensive for clustered tables, see Understanding InnoDB clustered indexes, section "Disadvantages of clustering").

In my opinion, your table should look like this:

CREATE TABLE relationships (
    from_user_id MEDIUMINT UNSIGNED NOT NULL,
    to_user_id MEDIUMINT UNSIGNED NOT NULL,
    relationship_level DECIMAL(1,1) NOT NULL,
    PRIMARY KEY (from_user_id, to_user_id), -- InnoDB is clustered, so naturally "covers" relationship_level as well.
    FOREIGN KEY (from_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
    FOREIGN KEY (to_user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE NO ACTION,
    INDEX relationship_to_from (to_user_id, from_user_id, relationship_level) -- Including relationship_level may or may not be a good idea.
) ENGINE=INNODB;

NOTE: Whether you include relationship_level in INDEX or not depends on whether you want index-only scan in "backward" direction as well. The "forward" direction is naturally covered by the PK (since InnoDB is clustered).

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