密钥不在表中,但它是

时间:2016-06-22 10:20:09

标签: mysql postgresql

错误:在表“salesorderheader”上插入或更新违反外键约束“salesorderheader_customerid_fkey” 详细信息:表“customer”中不存在Key(customerid)=(29825)。 **********错误**********

但Customer.txt文件中存在29825。

以下是代码:

DROP TABLE IF EXISTS PersonAddress;
DROP TABLE IF EXISTS SalesOrderDetail;
DROP TABLE IF EXISTS Product;
DROP TABLE IF EXISTS SalesOrderHeader;
DROP TABLE IF EXISTS Customer;
DROP TABLE IF EXISTS Person;
DROP TABLE IF EXISTS ProductCategory;
DROP TABLE IF EXISTS Address;
DROP TABLE IF EXISTS CreditCard;

CREATE TABLE Person(
PersonID INT NOT NULL,
Title varchar(255),
FirstName VARCHAR(255) NOT NULL,
LastName VARCHAR(255) NOT NULL,
PRIMARY KEY (PersonID)
);



CREATE TABLE ProductCategory(
ProductCategoryID INT,
Name VARCHAR(255) NOT NULL,
PRIMARY KEY (ProductCategoryID)
);



CREATE TABLE Address(
AddressID INT NOT NULL,
AddressLine1 VARCHAR(255) NOT NULL,
AddressLine2 VARCHAR(255),
City VARCHAR(255) NOT NULL,
PostalCode VARCHAR(255) NOT NULL,
PRIMARY KEY (AddressID)
);



CREATE TABLE CreditCard(
CreditCardID INT NOT NULL,
CardType VARCHAR(255) NOT NULL,
CardNumber BIGINT NOT NULL,
ExpMonth INT NOT NULL,
ExpYear INT NOT NULL,
PRIMARY KEY (CreditCardID)
);



CREATE TABLE PersonAddress(
PersonID INT NOT NULL,
AddressID INT NOT NULL,
PRIMARY KEY (AddressID),
FOREIGN KEY (AddressID) REFERENCES Address(AddressID),
FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
);



CREATE TABLE Customer(
CustomerID INT NOT NULL,
PersonID INT,
PRIMARY KEY (CustomerID),
FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
);



CREATE TABLE SalesOrderHeader(
SalesOrderID INT NOT NULL,
OrderDate VARCHAR(255) NOT NULL,
DueDate VARCHAR(255) NOT NULL,
ShipDate VARCHAR(255) NOT NULL,
OnlineOrderFlag INT NOT NULL,
CustomerID INT NOT NULL,
CreditCardID INT,
SubTotal FLOAT NOT NULL,
TaxAmt FLOAT NOT NULL,
Freight FLOAT NOT NULL,
TotalDue FLOAT NOT NULL,
PRIMARY KEY (SalesOrderID),
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID),
FOREIGN KEY (CreditCardID) REFERENCES CreditCard(CreditCardID)
);



CREATE TABLE Product(
ProductID INT NOT NULL,
Name VARCHAR(255) NOT NULL,
SafetyStockLevel INT NOT NULL,
ReorderPoint INT NOT NULL,
StandardCost FLOAT NOT NULL,
ListPrice FLOAT NOT NULL,
ProductCategoryID INT,
PRIMARY KEY (ProductID),
FOREIGN KEY (ProductCategoryID) REFERENCES ProductCategory(ProductCategoryID)
);



CREATE TABLE SalesOrderDetail(
SalesOrderID INT NOT NULL,
SalesOrderDetailID INT NOT NULL,
OrderQty INT NOT NULL,
ProductID INT NOT NULL,
UnitPrice FLOAT NOT NULL,
UnitPriceDiscount FLOAT NOT NULL,
LineTotal FLOAT NOT NULL,
PRIMARY KEY (SalesOrderDetailID),
FOREIGN KEY (SalesOrderID) REFERENCES SalesOrderHeader(SalesOrderID),
FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);



COPY Person
FROM 'C:\Users\Public\Vaseis\Person.txt' DELIMITER ',' CSV HEADER;

COPY ProductCategory
FROM 'C:\Users\Public\Vaseis\ProductCategory.txt' DELIMITER ',' CSV HEADER;

COPY Address
FROM 'C:\Users\Public\Vaseis\Address.txt' DELIMITER ',' CSV HEADER;

COPY CreditCard
FROM 'C:\Users\Public\Vaseis\CreditCard.txt' DELIMITER ',' CSV HEADER;

COPY PersonAddress
FROM 'C:\Users\Public\Vaseis\PersonAddress.txt' DELIMITER ',' CSV HEADER;

COPY SalesOrderHeader
FROM 'C:\Users\Public\Vaseis\SalesOrderHeader.txt' DELIMITER ',' CSV HEADER;

COPY Product
FROM 'C:\Users\Public\Vaseis\Product.txt' DELIMITER ',' CSV HEADER;

COPY SalesOrderDetail
FROM 'C:\Users\Public\Vaseis\SalesOrderDetail.txt' DELIMITER ',' CSV HEADER;

29825 in the .txt

1 个答案:

答案 0 :(得分:1)

您没有填充Customer表:

COPY Customer
FROM 'C:\Users\Public\Vaseis\Customer.txt' DELIMITER ',' CSV HEADER;

COPY SalesOrderHeader
FROM 'C:\Users\Public\Vaseis\SalesOrderHeader.txt' DELIMITER ',' CSV HEADER;