简体   繁体   English

对于这些约束,合适的表/数据库设计是什么?

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

Given 4 tables,给定4张桌子,

table 'base'表“基地”

  • has an id column (primary key)有一个 id 列(主键)

table 'instance'表“实例”

  • has an id column (primary key)有一个 id 列(主键)
  • has a base_id column (foreign key to table 'base')有一个 base_id 列(表'base'的外键)

table 'modifier'表“修饰符”

  • has an id column (primary key)有一个 id 列(主键)
  • for simplicity sake, assume 2 column defining min and max.为简单起见,假设 2 列定义最小值和最大值。

table 'base_has_modifier'表'base_has_modifier'

  • has a base_id column (foreign key to table 'base')有一个 base_id 列(表'base'的外键)
  • has a modifier_id column (foreign key to table 'modifier')有一个 modifier_id 列(表“修饰符”的外键)

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”

  • has instance_id (from 'instance')有 instance_id(来自“实例”)
  • has base_id (from 'base_has_modifier')有base_id(来自'base_has_modifier')
  • has modifier_id (from 'base_has_modifier')有modifier_id(来自'base_has_modifier')
  • for simplicity sake, assume 1 column containing value between min and max, as defined under table 'modifier'为简单起见,假设 1 列包含最小值和最大值之间的值,如表“修饰符”下定义

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}

Option 1选项1

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 ;

Option 2选项 2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM