如何在 PostgreSQL 中建立这种关系?
您好。
如 ER 模型所示,我想在“Busses”和“Chauffeurs”之间创建一种关系,其中“Chauffeurs”中的每个实体必须在“Certified”中至少有一个关系,并且“Busses”中的每个实体必须在“已认证”中至少有一种关系。
虽然设计 ER 模型非常容易,但我似乎找不到在 PostgreSQL 中建立这种关系的方法。有人有一些想法吗?
谢谢
Hello.
As shown in the ER model, I want to create a relation between "Busses" and "Chauffeurs", where every entity in "Chauffeurs" must have at least one relation in "Certified", and every entity in "Busses" must have at least one relation in "Certified".
Though it was pretty easy to design the ER model, I can't seem to find a way of making this relation in PostgreSQL. Anybody got some ideas ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该解决方案应该与数据库无关。如果我理解正确的话,您可能希望您的认证表看起来像:
The solution should be database agnostic. If I understand you correctly, you probably want your certified table to look like:
我能够找到的唯一解决方案是在父表中使用单个强制字段来表示“至少一个”,然后将 2 个或更多关系存储在交集表中。
司机
认证的
公交车
要获取司机经过认证的所有公交车的列表,则
UNION(相对于 UNION ALL)负责使用
certified
中的值进行重复数据删除。The only solution I've been able to find is the notion of a single mandatory field in the parent table to represent the "at least one" and then storing the 2 or more relationships in the intersection table.
chauffeurs
certified
busses
To get a list of all busses where a chauffer is certified becomes
The UNION (vs UNION ALL) takes care of deduplication with the values in
certified
.