我有以下数据库设计问题:
给出四种实体类型:this.a = _.uniqBy(a, 'id');
,Interface
,Device
,VLAN
。
有以下规则:
VNI
恰好有一个Interface
。因此,Device
可能有零到许多Device
。Interfaces
可以有零个到多个Interface
。 VLAN
可以有零个到多个VLAN
。 Interfaces
不能多次分配给同一VLAN
。Interface
可以有零个或一个VLAN
。因此,VNI
的个数为VNI
到零。到目前为止,这很容易,它可以像这样建模:
VLAN
Interface(id, device_id not null)
Device(id)
VLAN(id, vni_id nullable)
VNI(id)
但是有第4条规则:元组(InterfaceVLAN(interface_id not null, vlan_id not null) unique (interface_id, vlan_id)
,VNI
,Device
)必须是唯一的。
还必须能够将VLAN
分配给VLAN
,而不能将VNI
分配给VLAN
或Interface
。另外,Device
可以分配给VLAN
,而Interface
却没有VLAN
。
我一直坚持将其整合到模型中,并欢迎提出任何建议。
答案 0 :(得分:1)
从问题开始:
元组
(VNI, Device, VLAN)
必须是唯一的。
从您对詹姆斯·胡的评论:
每个VNI在每个设备上最多只能映射到一个VLAN。
这是两个不同的约束。第二个意味着(Device, VNI)
是唯一的,它意味着第一个,但并非相反。
-- Vni tag VNI exists.
--
vni_tag {VNI}
PK {VNI}
-- Virtual lan (vlan) VLN exists.
--
vlan {VLN}
PK {VLN}
-- Vlan VLN is mapped to vni tag VNI.
--
vln_vni {VLN, VNI}
PK {VLN}
SK {VLN, VNI}
FK1 {VLN} REFERENCES vlan {VLN}
FK2 {VNI} REFERENCES vni_tag {VNI}
-- Device DEV exists.
--
device {DEV}
PK {DEV}
-- Interface number IFC# of device DEV exists.
--
interface {DEV, IFC#}
PK {DEV, IFC#}
FK {DEV} REFERENCES device {DEV}
对于第二个约束,(DEV,VNI)唯一:
每个接口 可以连接到多个 vlan; 对于每个VLAN,一个以上接口 可以连接到该VLAN。
对于设备和vni标签的每种组合,设备和标签的组合最多可以出现一次。
-- Interface number IFC# of device DEV is connected to
-- vlan VLN, which is mapped to vni tag VNI.
--
ifc_vln_vni {DEV, IFC#, VLN, VNI}
PK {DEV, IFC#, VLN}
AK {DEV, VNI}
FK1 {DEV, IFC#} REFERENCES interface {DEV, IFC#}
FK2 {VLN, VNI} REFERENCES vln_vni {VLN, VNI}
在第一个约束的情况下,(DEV,VLN,VNI)唯一:
每个接口 可以连接到多个 vlan; 对于每个VLAN,一个以上接口 可以连接到该VLAN。
对于设备,vlan和vni标签的每种组合,该设备,vlan和标签的组合最多可以出现一次。
-- Interface number IFC# of device DEV is connected to
-- vlan VLN, which is mapped to vni tag VNI.
--
ifc_vln_vni {DEV, IFC#, VLN, VNI}
PK {DEV, IFC#, VLN}
AK {DEV, VLN, VNI}
FK1 {DEV, IFC#} REFERENCES interface {DEV, IFC#}
FK2 {VLN, VNI} REFERENCES vln_vni {VLN, VNI}
注意:
All attributes (columns) NOT NULL
PK = Primary Key
SK = Proper Superkey (Unique)
AK = Alternate Key (Unique)
FK = Foreign Key
Using suffix # to save on screen space.
OK for SQL Server and Oracle, for others use _NO.
For example, rename IFC# to IFC_NO.
答案 1 :(得分:0)
可能您可以尝试使用唯一约束。假设您有一个DEVICE
表,该表具有一个DEVICE_ID
列作为主键,而一个'VNI_ID`列作为外键,则只需在这两列上创建唯一约束。