MS Access - 实施参照完整性 - 多表外键约束

时间:2014-09-04 23:59:12

标签: ms-access referential-integrity

我在MS Access中创建了四个表来描述个人消防设备的允许配置,以用作资产注册的一部分。详情如下:

TBL_1   :  FIRE_CLASSIFICATION
FIELDS  :   | PK | CLASS              |
---------------------------------------
ENTRIES :   | 1  | Fire Blanket       |
            | 2  | Fire Extinguisher  |
            | 3  | Fire Hose Reel     |

TBL_2   :  FIRE_TYPE
FIELDS  :   | PK | TYPE               | FK_CLASS           |
------------------------------------------------------------
ENTRIES :   | 1  | General            | Fire Blanket       |
            | 2  | Carbon Dioxide     | Fire Extinguisher  |
            | 3  | Foam               | Fire Extinguisher  |
            | 4  | Powder ABE         | Fire Extinguisher  |
            | 5  | Powder BE          | Fire Extinguisher  |
            | 6  | Vap. Liquid        | Fire Extinguisher  |
            | 7  | Water              | Fire Extinguisher  |
            | 8  | Wet Chemical       | Fire Extinguisher  |
            | 9  | General            | Fire Hose Reel     |

TBL_3   :  FIRE_SPECIFIC
FIELDS  :   | PK | SPECIFIC           | FK_CLASS           |
------------------------------------------------------------
ENTRIES :   | 1  | Dimensions         | Fire Blanket       |
            | 2  | Capacity (kg)      | Fire Extinguisher  |
            | 3  | Length (m)         | Fire Hose Reel     |

TBL_4   :  FIRE_OPTIONS
FIELDS  :   | PK | FK_CLASS           | FK_TYPE        | FK_SPECIFIC    | OPTION |    
----------------------------------------------------------------------------------
ENTRIES :   | 1  | Fire Extinguisher  | Carbon Dioxide | Capacity (kg)  | 2.0    |
            | 2  | Fire Extinguisher  | Carbon Dioxide | Capacity (kg)  | 3.5    |
            | 3  | Fire Extinguisher  | Carbon Dioxide | Capacity (kg)  | 5.0    |
            | 4  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 1.1    |
            | 5  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 2.1    |
            | 6  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 2.3    |
            | 7  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 2.7    |
            | 8  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 4.5    |
            | 9  | Fire Extinguisher  | Powder ABE     | Capacity (kg)  | 9.0    |
            | 10 | Fire Hose Reel     | General        | Length (m)     | 12.0   |
            ... CONTINUED

目前可以插入

等条目
            | 11 | Fire Hose Reel     | Powder ABE     | Dimensions     | 1200 x 1200 |     

进入FIRE_OPTIONS表。此行为是非常不受欢迎的,因为此条目不会 符合其他表格中规定的声明(允许)组合。

我想了解如何在FIRE_OPTIONS表上实现合适的约束,以便在指定的条目与其他三个表不一致时生成错误。

我尝试过使用"数据库工具" - "关系"然而,我没有能够实现参考完整性"在TBL_4与TBL_1,TBL_2和TBL_3之间同时进行。

我非常感谢对此问题的任何帮助。

**编辑

我能够通过修改主键定义来解决这个问题。详情如下:

TBL_1 : FIRE_CLASSIFICATION
        PK: PK (Auto Increment)

TBL_2 : FIRE_TYPE
        PK: PK (Auto Increment) & FK_CLASS

TBL_3 : FIRE_SPECIFIC
        PK: PK (Auto Increment) & FK_CLASS

TBL_4 : FIRE_OPTIONS
        PK: PK (Auto Increment)

然后我在"数据库工具" - "关系"中定义了以下关系。特征:

[ERI]    FIRE_CLASSIFICATION.PK (1)  ->  FIRE_TYPE.FK_CLASS (MANY)
[ERI]    FIRE_CLASSIFICATION.PK (1)  ->  FIRE_SPECIFIC.FK_CLASS (MANY)
[ERI] {  FIRE_CLASSIFICATION.PK (1)  ->  FIRE_OPTIONS.FK_CLASS (MANY)
         FIRE_TYPE.PK (1)            ->  FIRE_OPTIONS.FK_TYPE (MANY)
         FIRE_TYPE.FK_CLASS (1)      ->  FIRE_OPTIONS.FK_CLASS (MANY)
         FIRE_SPECIFIC.PK (1)        ->  FIRE_OPTIONS.FK_SPECIFIC (MANY)
         FIRE_SPECIFIC.FK_CLASS (1)  ->  FIRE_OPTIONS.FK_CLASS (MANY)    }

ERI : "Enforce Referential Integrity" Option Selected

1 个答案:

答案 0 :(得分:0)

目前,由于FIRE_CLASSIFICATION和FIRE_SPECIFIC表之间存在一对一的关系,因此可以将SPECIFIC列添加到FIRE_CLASSIFICATION表中。 如果事情变得更复杂,这可能不起作用。