为什么我的SQL外键不起作用?

时间:2016-01-02 12:57:04

标签: sql sql-server

我应该找出这些SQL代码的外键但是有错误:

  

Msg 1767,Level 16,State 0,Line 18 Foreign key   ' FK__payments__custom__44FF419A'引用无效表'客户'。   消息1750,级别16,状态0,行18无法创建约束。看到   以前的错误。

这是我的代码:

Drop table Payments
CREATE TABLE payments (
customerNumber          Int             NOT NULL,
checkNumber             Varchar(50)     NOT NULL,
paymentDate             Datetime        NOT NULL,
amount                  Float           NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));

Drop table Customers

CREATE TABLE Customers (
customerNumber          Int             NOT NULL,
customerName            Varchar(50)     NOT NULL,
contactLastName         Varchar(50)     NOT NULL,
contactFirstName        Varchar(50)     NOT NULL,
phone                   Varchar (50)    NOT NULL,
addressLine1            Varchar (50)    NOT NULL,
addressLine2            Varchar (50)    NOT NULL,
city                    Varchar (50)    NOT NULL,
state                   Varchar (50)    NULL,
postalCode              Varchar (15)    NOT NULL,
country                 Varchar(50)     NOT NULL,
salesRepEmployeeNumber  Int             NOT NULL,
creditLimit             Float           NOT NULL,
PRIMARY KEY (customerNumber));
ALTER TABLE Payments  ADD FOREIGN KEY(customerNumber) REFERENCES Customer(customerNumber);

Drop table Offices
CREATE TABLE Offices (
officeCode              Varchar(10)     NOT NULL,
city                    Varchar(50)     NOT NULL,
phone                   Varchar(50)     NOT NULL,
addressLine1            Varchar(50)     NOT NULL,
addressLine2            Varchar(50)     NOT NULL,
state                   Varchar(50)     NULL,
country                 Varchar(50)     NOT NULL,
postalCode              Varchar(15)     NOT NULL,
territory               Varchar(10)     NOT NULL,
PRIMARY KEY (officeCode));


Drop table OrderDetails
CREATE TABLE OrderDetails (
orderNumber             Int             NOT NULL,
productCode             Varchar(15)     NOT NULL,
quantityOrdered         Int             NOT NULL,
priceEach               Float           NOT NULL,
orderLineNumber         SMALLINT        NOT NULL,
PRIMARY KEY (orderNumber, productCode));

Drop table ProductLines
CREATE TABLE ProductLines (
productLine             Varchar (50)    NOT NULL,
textDescription         TEXT            NOT NULL,
htmlDescription         TEXT            NOT NULL,
image                   Float           NOT NULL,
PRIMARY KEY (productLine));

Drop table Orders
CREATE TABLE Orders (
orderNumber      Int            NOT NULL,
orderDate        DateTime       NOT NULL,
requiredDate     DateTime       NOT NULL,
shippedDate      DateTime       NOT NULL,
status           Varchar(15)    Not null,
comments         TEXT           NOT NULL,
customerNumber   INT            NOT NULL,
Primary key(orderNumber));
ALTER TABLE OrdersDetails  ADD FOREIGN KEY(orderNumber) REFERENCES Orders(orderNumber);   

Drop table Employees
CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));

Drop table Products
CREATE TABLE Products ( 
productCode         Varchar(15)      Not Null,
productName         Varchar(70)      Not Null,
productLine         Varchar(50)      Not null,
productScale        Varchar(10)      Not null,
productVendor       Varchar(50)      Not null,
productDescription  TEXT             Not null,
quantityinStock     Int              Not null,
BuyPrice            Float            Not null,
MSRP                Float            Not null,
Primary key (productCode))

ALTER TABLE OrderDetails  ADD FOREIGN KEY(productCode) REFERENCES Product(productCode);

2 个答案:

答案 0 :(得分:3)

确实没有这样的表struct。您的表名为Customer,复数形式:

Customers

答案 1 :(得分:2)

表名sCustomerCustomers中存在OrdersDetails vs OrderDetails的多个拼写错误:

CREATE TABLE payments (
customerNumber          Int             NOT NULL,
checkNumber             Varchar(50)     NOT NULL,
paymentDate             Datetime        NOT NULL,
amount                  Float           NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));



CREATE TABLE Customers (
customerNumber          Int             NOT NULL,
customerName            Varchar(50)     NOT NULL,
contactLastName         Varchar(50)     NOT NULL,
contactFirstName        Varchar(50)     NOT NULL,
phone                   Varchar (50)    NOT NULL,
addressLine1            Varchar (50)    NOT NULL,
addressLine2            Varchar (50)    NOT NULL,
city                    Varchar (50)    NOT NULL,
state                   Varchar (50)    NULL,
postalCode              Varchar (15)    NOT NULL,
country                 Varchar(50)     NOT NULL,
salesRepEmployeeNumber  Int             NOT NULL,
creditLimit             Float           NOT NULL,
PRIMARY KEY (customerNumber));

ALTER TABLE Payments  ADD FOREIGN KEY(customerNumber) REFERENCES Customers(customerNumber);  -- here


CREATE TABLE Offices (
officeCode              Varchar(10)     NOT NULL,
city                    Varchar(50)     NOT NULL,
phone                   Varchar(50)     NOT NULL,
addressLine1            Varchar(50)     NOT NULL,
addressLine2            Varchar(50)     NOT NULL,
state                   Varchar(50)     NULL,
country                 Varchar(50)     NOT NULL,
postalCode              Varchar(15)     NOT NULL,
territory               Varchar(10)     NOT NULL,
PRIMARY KEY (officeCode));


CREATE TABLE OrderDetails (
orderNumber             Int             NOT NULL,
productCode             Varchar(15)     NOT NULL,
quantityOrdered         Int             NOT NULL,
priceEach               Float           NOT NULL,
orderLineNumber         SMALLINT        NOT NULL,
PRIMARY KEY (orderNumber, productCode));


CREATE TABLE ProductLines (
productLine             Varchar (50)    NOT NULL,
textDescription         TEXT            NOT NULL,
htmlDescription         TEXT            NOT NULL,
image                   Float           NOT NULL,
PRIMARY KEY (productLine));


CREATE TABLE Orders (
orderNumber      Int            NOT NULL,
orderDate        DateTime       NOT NULL,
requiredDate     DateTime       NOT NULL,
shippedDate      DateTime       NOT NULL,
status           Varchar(15)    Not null,
comments         TEXT           NOT NULL,
customerNumber   INT            NOT NULL,
Primary key(orderNumber));
ALTER TABLE OrderDetails  ADD FOREIGN KEY(orderNumber) REFERENCES Orders(orderNumber);   -- here


CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));


CREATE TABLE Products ( 
productCode         Varchar(15)      Not Null,
productName         Varchar(70)      Not Null,
productLine         Varchar(50)      Not null,
productScale        Varchar(10)      Not null,
productVendor       Varchar(50)      Not null,
productDescription  TEXT             Not null,
quantityinStock     Int              Not null,
BuyPrice            Float            Not null,
MSRP                Float            Not null,
Primary key (productCode))

ALTER TABLE OrderDetails  ADD FOREIGN KEY(productCode) REFERENCES Products(productCode);    

SqlFiddleDemo

我强烈建议您使用确切数据类型DECIMAL代替价格而不是近似数据类型FLOAT

并将已弃用的TEXT更改为NVARCHAR(MAX)