SQL:用于继承的多个外键。

时间:2021-07-03 11:30:17

I have a few tables in a database and I have a quick question regarding foreign keys.

我在数据库中有一些表,我有一个关于外键的快速问题。

Tables

Company

公司

PK: CompanyID

Manufacturer

制造商

PK, FK: CompanyID

Make/Model

制作/模型

PK: MakeModelID
FK: ManufacturerID

Equipment

设备

PK: EquipmentID
FK: MakeModelID

Would it be proper to include a ManufacturerID column as a FK to Manufacturer in the Equipment table? And then would it be smart to create an Index on both ManufacturerID and MakeModelID in the Equipment table? I'm self taught and was just seeking some design input. Thanks.

是否应该在设备表中包括一个制造商作为FK的制造商?那么,在设备表上创建一个关于制造商和MakeModelID的索引是否明智?我是自学的,只是在寻找一些设计的输入。谢谢。

2 个解决方案

#1


2  

No, it would not be proper to include a FK to the manufacturer table in the equipment table.

不,在设备表中包含一个FK到制造商表是不合适的。

The equipment table already has a FK to the Make/Model table which has the FK to the Manufacturer table. If you created that FK, then technically a record in the equipment table could be linked to two different manufacturers.

设备表已经有一个FK到制造/模型表,它有FK到制造商表。如果您创建了FK,那么技术上,设备表中的记录可以链接到两个不同的制造商。

#2


0  

It's not necessary to include ManufacturerID in the Equipment table, since you already have MakeModelID, and the MakeModel table already has a ManufacturerID. So you could join through the MakeModel table to get the ManufacturerID.

没有必要在设备表中包含制造商id,因为您已经有了MakeModelID,并且MakeModel表已经有一个制造商id。因此,您可以通过MakeModel表来获得制造商id。

#1


2  

No, it would not be proper to include a FK to the manufacturer table in the equipment table.

不,在设备表中包含一个FK到制造商表是不合适的。

The equipment table already has a FK to the Make/Model table which has the FK to the Manufacturer table. If you created that FK, then technically a record in the equipment table could be linked to two different manufacturers.

设备表已经有一个FK到制造/模型表,它有FK到制造商表。如果您创建了FK,那么技术上,设备表中的记录可以链接到两个不同的制造商。

#2


0  

It's not necessary to include ManufacturerID in the Equipment table, since you already have MakeModelID, and the MakeModel table already has a ManufacturerID. So you could join through the MakeModel table to get the ManufacturerID.

没有必要在设备表中包含制造商id,因为您已经有了MakeModelID,并且MakeModel表已经有一个制造商id。因此,您可以通过MakeModel表来获得制造商id。