sql - 我如何表示一个由 3 个其他实体组成的实体?
问题描述
我想在我的 ER 图中表示一个包由三 (3) 个不同的游戏组成。
如上图所示,礼包和游戏都是具有各自属性的产品。
这是他们各自的表格:
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)
我似乎找不到任何关于如何在模型中显示我的意思的例子,所以如果有人知道我会很感激。
解决方案
您可以使用 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)
)
推荐阅读
- python - 如何使用 Python 在屏幕上绘制一个空矩形
- java - /graphql POST 方法不允许在部署在 AWS lambda 上的 springboot graphql 应用程序中使用
- mongodb - 使用 --auth 和 --bind_ip 本地主机 my_IP_address 启动 Mongod,无法检索本地主机的内存
- sas - SAS : Proc format The SAS task name is [FORMAT (] Segmentation Violation Issue
- asp.net - 如何在实际服务器上(全局)部署 IIS webApp?
- javascript - 在 jQuery/JavaScript 中基于逗号分隔的数据属性值显示/隐藏 div
- dpdk - 有效 CPUSET 的 testPMD 错误(EAL:无效核心列表语法)
- javascript - JavaScript / 从 0 开始设置增量索引
- javascript - 无法使用 XMLHttpRequest 向 url 发送请求
- java - 为什么模块路径上的模块需要 --add-modules ?