与具有约束

时间:2017-08-18 16:55:44

标签: sql database database-design foreign-keys

我有这个数据库设计

Installations
_______________
id

Groups (relation 1:n with installations)
_______________________________________
id
installation_id 

Services (relation 1:n with installations)
________
id
installation_id

Buttons (relation 1:n with services)
___________________________________
id
service_id

buttons_enabled
_______________
group_id
button_id
service_id

实际上我已经简化了,因为实际上服务和安装之间存在M:M关系。

我想要的是限制buttons_enabled只使用与该组安装相关的服务按钮。

所以例如

安装
Inst1
INST2


G1 Inst1
G2 Inst11

服务
S1 Inst1
S2 Inst1
S3 Inst2

按钮
B1 S1
B2 S1
B3 S2
B4 S3

Buttons_enabled
G1 B1
G1 B4

最后一行在物理上是正确的但不合逻辑因为一组安装与另一安装的一项服务有关(B4与与 Inst2 相关的Service3有关。但G1与 INST1 )。

如何避免它?

由于

0 个答案:

没有答案