Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的SQL外键不起作用?_Sql_Sql Server - Fatal编程技术网

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

为什么我的SQL外键不起作用?,sql,sql-server,Sql,Sql Server,我应该找出这些SQL代码的外键,但有一个错误: Msg 1767,16级,状态0,第18行外键 “FK___________44FF419A”引用了无效的表“Customer”。 Msg 1750,级别16,状态0,第18行无法创建约束。看见 以前的错误 这是我的代码: Drop table Payments CREATE TABLE payments ( customerNumber Int NOT NULL, checkNumber

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

Msg 1767,16级,状态0,第18行外键 “FK___________44FF419A”引用了无效的表“Customer”。 Msg 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);

表名
Customer
vs
Customers
OrdersDetails vs OrderDetails
中有多个拼写错误
s

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);    

我强烈建议对价格使用精确的数据类型
DECIMAL
,而不是近似的数据类型
FLOAT


并将不推荐使用的
TEXT
更改为
NVARCHAR(MAX)

在表名
Customer
vs
Customers
OrdersDetails vs OrderDetails
中使用
s
的多个打字错误:

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);    

我强烈建议对价格使用精确的数据类型
DECIMAL
,而不是近似的数据类型
FLOAT


并将不推荐使用的
文本更改为
NVARCHAR(MAX)

确实没有这样的表
Customer
。您的表格以复数形式命名为
客户

ALTER TABLE Payments 
ADD FOREIGN KEY (customerNumber) 
REFERENCES Customers (customerNumber);
-- Here! ----------^

确实没有这样的表
Customer
。您的表格以复数形式命名为
客户

ALTER TABLE Payments 
ADD FOREIGN KEY (customerNumber) 
REFERENCES Customers (customerNumber);
-- Here! ----------^

您没有OrderDetails,但您正在更改OrdersDetails您没有OrderDetails,但您正在更改OrdersDetails此代码运行良好。我可以知道如何从源系统加载数据吗?@JLLo您能详细说明一下吗?从源系统加载数据是什么意思?看起来你想问一个新问题。这段代码运行得很好。我可以知道如何从源系统加载数据吗?@JLLo您能详细说明一下吗?从源系统加载数据是什么意思?看起来你想问一个新问题