[英]What would be an appropriate table/database design for these constraints?
Given 4 tables,给定4张桌子,
table 'base'表“基地”
table 'instance'表“实例”
table 'modifier'表“修饰符”
table 'base_has_modifier'表'base_has_modifier'
The constraint I am trying to model is, an instance can only have modifiers tied to its base.我试图对 model 的约束是,实例只能将修饰符绑定到其基础。
Currently I have this,目前我有这个,
table 'instance_has_base_modifier'表“instance_has_base_modifier”
As you can see, this allows instance to have modifiers from other bases.如您所见,这允许实例具有来自其他基础的修饰符。
Now the question, is it possible to model this in a way that enforces an instance to only have modifiers tagged to its base?现在的问题是,model 是否可以通过强制实例仅将修饰符标记到其基础的方式来实现? If so, how?如果是这样,怎么做? If not, why?如果不是,为什么?
Thank you.谢谢你。
-- Base BAS exists.
--
base {BAS}
PK {BAS}
-- Base BAS has instance number INS#, of that base.
--
instance_ {BAS, INS#}
PK {BAS, INS#}
FK1 {BAS} REFERENCES base {BAS}
-- Modifier MOD exists.
--
modifier {MOD}
PK {MOD}
-- Modifier MOD applies to base BAS
--
base_mod {BAS, MOD}
PK {BAS, MOD}
FK1 {BAS} REFERENCES base {BAS}
FK2 {MOD} REFERENCES modifier {MOD}
If a base modifier applies to all instances of that base.如果基础修饰符适用于该基础的所有实例。
-- Modifier MOD applies to instance number INS# of base BAS.
--
CREATE VIEW inst_mod
AS
SELECT i.BAS
, i.INS#
, b.MOD
FROM instance_ AS i
JOIN base_mod AS b ON b.BAS = i.BAS ;
If a base modifier may, or may not, apply to instances of that base.如果一个基础修饰符可能会或可能不会应用于该基础的实例。
-- Modifier MOD applies to instance number INS# of base BAS.
--
inst_mod {BAS, INS#, MOD}
PK {BAS, INS#, MOD}
FK1 {BAS, INS#} REFERENCES instance_ {BAS, INS#}
FK2 {BAS, MOD} REFERENCES base_mod {BAS, MOD}
Note:笔记:
All attributes (columns) NOT NULL
PK = Primary Key
FK = Foreign Key
Using suffix # to save on screen space.
OK for SQL Server and Oracle, for others use _NO.
For example, rename INS# to INS_NO.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.