简体   繁体   English

引用表错误中没有主键或候选键

[英]There are no primary or candidate keys in the referenced table error

I have a problem with combining foreign keys to different tables. 将外键组合到不同的表时遇到问题。 For example I made a table Customers and a Table invoices. 例如,我制作了一张表客户和一张表发票。 I want a foreign key from Customers to invoices so I can get the name and everything of the Customer: 我希望从客户到发票使用外键,以便获得客户的名称和所有内容:

Create table code of Customers : 创建Customers表代码:

Create Table Customers
(
    customerID int IDENTITY(100,1) NOT NULL, 
    customer_email varchar(30) NOT NULL,
    username varchar(255) NOT NULL,
    password varchar(50) NOT NULL,
    firstname varchar(255) NOT NULL,
    lastname varchar(255) NOT NULL,
    insertion varchar(10)   NULL,
    phonenumber int NULL,
    streetname varchar(20) NOT NULL,
    number  int NOT NULL,
    zipcode varchar(10) NOT NULL,
    city    varchar(255) NOT NULL,

    Constraint pk_Customers 
       PRIMARY KEY (customerID, customer_email, username)
)

Create table code of Invoices : 创建Invoices表代码:

Create Table Invoices 
(
    invoiceID int IDENTITY(1000,1) NOT NULL,
    customer_email varchar(30) NOT NULL,
    customerID int NOT NULL,
    creationdate datetime NOT NULL DEFAULT GETDATE(),
    totalAmount decimal(5,2) NOT NULL,

    Constraint pk_Invoices 
        PRIMARY KEY (invoiceID, customer_email,creationdate)
) 

The foreign key code that I want to use: 我要使用的外键代码:

ALTER Table Invoices
ADD Constraint fk_Customers_Invoices 
FOREIGN KEY (customerID) REFERENCES Customers (customerID)
   ON UPDATE CASCADE 
   ON DELETE NO ACTION

It throws the following error: 它引发以下错误:

There are no primary or candidate keys in the referenced table 'Customers' that match the referencing column list in the foreign key 'fk_Customers_Invoices'. 在参考表“客户”中没有与外键“ fk_Customers_Invoices”中的参考列列表匹配的主键或候选键。

How can I add my foreign key? 如何添加外键?

I think it's because your primary key is a composite key: 我认为这是因为您的主键是组合键:

customerID, customer_email, username

This suggests that it is only the combination of these three fields that will uniquely identify a customer, and the foreign key would need to reference all three fields. 这表明只有这三个字段的组合才能唯一标识客户,外键将需要引用所有三个字段。

If customerID is unique, then it should be the primary key for the table and your foreign keys would be able to use it as a reference. 如果customerID是唯一的,则它应该是表的主键,并且您的外键可以将其用作参考。

For what purpose are the other fields included in the primary key? 出于什么目的,主键中还包含其他字段?

Since your Customers table defines this primary key: 由于您的Customers表定义了此主键:

Constraint pk_Customers 
   PRIMARY KEY (customerID, customer_email, username)

any table that wants to reference Customers must provide all three columns of that primary key. 任何要引用Customers表都必须提供该主键的所有三列 That's the way FK constraints work. 这就是FK约束的工作方式。

So from your Invoices tables, you must provide all 3 columns that make up the primary key of Customers - not just one. 因此,在“ Invoices表中,您必须提供构成“ Customers ”主键的所有3列 ,而不仅仅是其中的一列。 You can never refernce only part of a primary key - it's the whole key or nothing.... 永远不能只引用主键的一部分-它是整个键或什么都没有。

ALTER Table Invoices
ADD Constraint fk_Customers_Invoices 
FOREIGN KEY (customerID) REFERENCES Customers (customerID)
   ON UPDATE CASCADE 
   ON DELETE NO ACTION

You can either : 可以

  • change the PK for Customers to be just CustomerID which would make a whole lot more sense since it's an identity column 更改PK Customers是公正CustomerID这将使整个很多更有意义,因为它是一个标识

  • or you could add the other two columns in the Customers PK to yoru table Invoices 或者您可以将“ Customers PK”中的其他两列添加到yoru表“ Invoices

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM