数据库设计:需要复合键+外键 [英] Database Design: need composite key + foreign key

查看:175
本文介绍了数据库设计:需要复合键+外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表与1-M关系:

I have 2 tables that have a 1-M relationship:

TABLE foo
 - foo_id (PK)
 - foobar_id
 - value

TABLE bar
 - bar_id (PK)
 - foo_id (FK to foo.foo_id)
 - foobar_id
 - value

我想确保 foo.foobar_id foo.value 是唯一的,因此希望使它们成为复合键。如果我这样做,那么我可以摆脱 foo.foo_id 。如果我这样做,我如何联系 foo bar

I want to make sure foo.foobar_id and foo.value are unique, and thus want to make them a composite key. If I do that, then I can probably get rid of foo.foo_id. And if I do that, how do I relate foo and bar?

我也想要 bar.foo_id bar.foobar_id 是唯一的,但会遇到问题如果我删除 foo.foo_id

I also want bar.foo_id and bar.foobar_id to be unique, but will run into problems if I delete foo.foo_id.

关于如何建模这个建议?

Suggestions on how to model this?

推荐答案

只需创建一个涵盖这两个字段的唯一索引。在这种情况下,只允许使用唯一的对。

Just create an unique index that covers both fields. In this case only unique pairs will be allowed.

这篇关于数据库设计:需要复合键+外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆