对于这些约束,什么是合适的 table/database 设计?

What would be an appropriate table/database design for these constraints?

给定 4 table 秒,

table 'base'

table 'instance'

table 'modifier'

table 'base_has_modifier'

我试图建模的约束是,一个实例只能将修饰符绑定到它的基数。

目前我有这个,

table 'instance_has_base_modifier'

如您所见,这允许实例具有来自其他基础的修饰符。

现在的问题是,是否可以通过强制实例仅将修饰符标记到其基数的方式对其进行建模?如果是这样,如何?如果不是,为什么?

谢谢。

-- 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}

选项 1

如果基础修饰符适用于该基础的所有实例

-- 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 ;

选项 2

如果基础修饰符可能会或可能不会应用于该基础的实例。

-- 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}

注:

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.