Given 4 tables,
table 'base'
table 'instance'
table 'modifier'
table 'base_has_modifier'
The constraint I am trying to model is, an instance can only have modifiers tied to its base.
Currently I have this,
table '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? 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.