使用两个以上的表创建外键
我有以下表格。
表系统
- sys_id
- name
Primary(sys_id)
表请求
- sys_id
- request_id
- subject
Primary(sys_id,request_id)
外键requests.sys_id --> system.sys_id
表字段
- sys_id
- field_id
- name
Primary(sys_id,field_id)
外键字段.sys_id --> system.sys_id
表 requests_props
- sys_id
- request_id
- field_id
- field_value
主要(sys_id,request_id,field_id)
外键 requests_props( sys_id, request_id ) --> requests(sys_id,request_id)
到目前为止,一切都很好。所以在这里我可以
- 创建一个具有特定 sys_id 的系统(在表系统中)
- 在该系统中创建一些字段(属性)(在表字段中)
- 向系统添加请求(在表请求中)
- 为每个请求设置字段值(属性)对应于该系统的field_ids。 (在表 requests_props 中)
现在我想创建一组草稿请求。 因此每个请求可以有多个草稿。 草稿只不过是临时请求。 因此,我决定采用以下架构
表草稿
- 草稿_id
- sys_id
- request_id
Primary(draft_id)
外键草稿(sys_id,request_id) --> requests(sys_id,request_id)
表草稿属性
- Draft_id
- field_id
- field_value
Primary(draft_id, field_id)
外键drafts_props(draft_id) -->草稿(draft_id)
外键????????? Drafts_props 和 fields 表之间 ???????????????
这里我想创建表 *drafts_props* 到 fields 之间的外键关系> 表,通过该表,我可以确保 *drafts_props* 具有相同的 *field_id*,这是 drafts 表和 *drafts_props* 表之间的 *draft_id's* 关联所允许的。 *sys_id* 与该草稿关联。
即,要找到草稿属性表中可以出现的有效字段 ID,我首先必须将草稿属性表连接到草稿表上的草稿 ID,并找到与该草稿关联的 sys_id,然后在字段表中找到与该 sys_id 关联的 field_ids。这将为我提供有效的 field_ids。
但据我所知,我不能使用3个表来创建外键约束。 外键 [drafts_props].[draft_id], [drafts].[sys_id] 引用 [fields].[sys_id], [fields].[field_id]
当然,我也可以在表 Drafts_props 中包含 sys_id 列,以便我可以创建这样的限制,但我不想创建这种冗余。 我也无法更改表系统、请求、requests_props 和字段。
提前致谢 !
I have following tables.
table system
- sys_id
- name
primary(sys_id)
table requests
- sys_id
- request_id
- subject
primary(sys_id,request_id)
foreign key requests.sys_id --> system.sys_id
table field
- sys_id
- field_id
- name
primary(sys_id,field_id)
foreign key field.sys_id --> system.sys_id
table requests_props
- sys_id
- request_id
- field_id
- field_value
primary(sys_id,request_id,field_id)
foreign key requests_props( sys_id, request_id ) --> requests(sys_id,request_id)
Until this point every thing is fine. So here I can
- create a system with a particular sys_id ( in table system)
- create some fields (properties) in this system ( in table fields )
- add request to the system ( in table requests )
- set the field values (properties) for each request corresponding to the field_ids of that system. (in table requests_props)
Now I want to create a set to Draft requests.
So each request can have multiple drafts for it.
Drafts are nothing but temporary requests.
So I have decided on following schema
table drafts
- draft_id
- sys_id
- request_id
primary(draft_id)
foreign key drafts(sys_id,request_id) --> requests(sys_id,request_id)
table drafts_props
- draft_id
- field_id
- field_value
primary(draft_id, field_id)
foreign key drafts_props(draft_id) --> drafts(draft_id)
foreign key ?????????? between drafts_props and fields table ??????????????
here I want to create the foreign key relation between the table *drafts_props* to fields table where by I can make sure that the *drafts_props* have the same *field_id* that are allowed by the *draft_id's* association between the drafts table and *drafts_props* table and the *sys_id* associated with that draft.
i.e. to find the valid field_ids that can come in drafts_props table, I first have to join the drafts_props table to drafts table on drafts_id and find the associated sys_id with that draft and then find the field_ids associated with that sys_id in fields table. This will give me the valid field_ids.
but as far as I know I cannot use 3 tables to create the foreign key constraint.
foreign key [drafts_props].[draft_id], [drafts].[sys_id] references [fields].[sys_id], [fields].[field_id]
of course I can include the column sys_id in table drafts_props also so that I can create such a constraint but I don't want to create this redundancy.
Also I cannot change the tables system, requests, requests_props and fields.
thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
drafts_props
中包含sys_id
的问题并不是它会占用物理空间(它会占用物理空间,但这不是问题)。问题是,您最终可能会得到一个drafts_props
行,该行引用具有一个sys_id
的drafts
,同时又具有不同的sys_id
本身。这应该合法吗?
drafts_props
中包含sys_id
即可。如果没有,您需要更改数据库模型,以便
sys_id
自然地向下传播标识关系并成为drafts_props
主键的一部分。像这样的事情:附加建议:
sys_id
放在第一位,具体取决于典型的查询模式)。sys_id
)。有些甚至可以压缩表数据。The problem of including
sys_id
indrafts_props
is not that it will take physical space (which it will, but this is not a problem). The problem is that you could end-up with adrafts_props
row that references thedrafts
with onesys_id
, while at the same time having a differentsys_id
itself.Should that be legal?
sys_id
indrafts_props
, outside its PK.If not, you'll need to change the database model so
sys_id
naturally propagates down identifying relationship and becomes part of thedrafts_props
primary key. Something like this:Additional suggestions:
sys_id
in the first place, depending on typical query patterns).sys_id
in your case). Some can even compress the table data.