首页 > 解决方案 > 我如何表示一个由 3 个其他实体组成的实体?

问题描述

我想在我的 ER 图中表示一个包由三 (3) 个不同的游戏组成。

我目前的 ER 模型

如上图所示,礼包和游戏都是具有各自属性的产品。

这是他们各自的表格:

packs(id, game1_id, game2_id, game3_id) where id is the primary key (also a foreign key of products)

games(id, studio_id) where id is the primary key (also a foreign key of products)

我似乎找不到任何关于如何在模型中显示我的意思的例子,所以如果有人知道我会很感激。

标签: sqlentity-relationship

解决方案


您可以使用 CHECK 约束来确保所有 3 个游戏都不同。

create table packs(
  id       int not null primary key, 
  game1_id int not null, 
  game2_id int not null, 
  game3_id int not null,
  constraint c12 check (game1_id <> game2_id), 
  constraint c23 check (game2_id <> game3_id), 
  constraint c31 check (game3_id <> game1_id),
  -- FKs 
  constraint fk1 foreign key (game1_id) references games(id),
  constraint fk2 foreign key (game2_id) references games(id),
  constraint fk3 foreign key (game3_id) references games(id)
)

推荐阅读