Question on Microsoft Access - Cannot link primary and foreign key

时间:2019-03-17 22:23:36

标签: database ms-access

I have come across a question concerning Primary keys & Foreign keys in MS Access. I do have two tables, Customer and Sales.

Customer has three attributes:
CustomerID (short text which is my primary key),
FirstName (short text)
LastName (short text)

Sales has the following attributes:
SalesID (Number) is the Primary key
ProductID (number) should be a foreign key
CustomerID (short text) should be a foreign key
(...)

Now here is my problem: there can be duplicates of the CustomerID as they can buy several items. I cannot set the foreign key CustomerID in the Sales Table to the primary key CustomerID in the CustomerTable because there can be duplicates.

Do you have any ideas how to solve this problem?

1 个答案:

答案 0 :(得分:1)

一个相关的子(从属)表通常具有父主键的重复项。这是一对多或多对多关系的本质。依赖表中父键值的重复不是问题,数据库结构是问题。

如果每个销售都可以有多个项目,那么您需要另一个名为SaleDetails的表,其中包含以下字段:

SalesID_FK(数字-长)
ProductID_FK(数字-长)
数量(数量-长)

销售表将具有:

SaleID_PK(自动编号)
CustomerID_FK(数字-长)
SaleDate(日期/时间)

对客户的主键使用自动编号:

CustomerID_PK(自动编号)
CustomerAcct(短文本)