表中不存在键列。的SQL

时间:2018-11-01 09:46:37

标签: sql ddl

因此,我尝试查找其他问题并进行了尝试,但是我无法弄清为什么我的SQL无法正常工作。它一直说DepartmentID表不存在,我已将其声明为Department下的主键。

CREATE TABLE EMPLOYEE(
SSN INT NOT NULL,
WorkID INT NOT NULL,
BirthDate DATE NOT NULL,
Name VARCHAR(20) NOT NULL,
UNIQUE(WorkID),
PRIMARY KEY(SSN),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
);

CREATE TABLE DEPARTMENT(
DepartmentID INT NOT NULL,
DepartmentName VARCHAR (20) NOT NULL,
PRIMARY KEY(DepartmentID),
UNIQUE(DepartmentName)
);

CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Isle VARCHAR(5) NOT NULL,
Company VARCHAR(20) NOT NULL,
PRIMARY KEY(ProductID),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
);


CREATE TABLE DEVICE(
DeviceID INT NOT NULL,
DateReceived DATE NOT NULL,
PRIMARY KEY(DeviceID)
);

CREATE TABLE SALES(
SalesID INT NOT NULL,
Profit INT NOT NULL,
Revenue INT NOT NULL,
PRIMARY KEY(SalesID)
);

CREATE TABLE AmountSold(
FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
);

2 个答案:

答案 0 :(得分:3)

问题是您正在创建外键,但是列DepartmentID  在您引用的那个表上不存在,第二个是  您的订单不正确。

CREATE TABLE DEPARTMENT(
DepartmentID INT NOT NULL,
DepartmentName VARCHAR (20) NOT NULL,
PRIMARY KEY(DepartmentID),
UNIQUE(DepartmentName)
);

CREATE TABLE DEVICE(
DeviceID INT NOT NULL,
DateReceived DATE NOT NULL,
PRIMARY KEY(DeviceID)
);

CREATE TABLE EMPLOYEE(
SSN INT NOT NULL,
WorkID INT NOT NULL,
BirthDate DATE NOT NULL,
DepartmentID INT NOT NULL,
DeviceID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
UNIQUE(WorkID),
PRIMARY KEY(SSN),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
);



CREATE TABLE PRODUCT(
ProductID INT NOT NULL,
Name VARCHAR(20) NOT NULL,
DepartmentID INT NOT NULL,
Isle VARCHAR(5) NOT NULL,
Company VARCHAR(20) NOT NULL,
PRIMARY KEY(ProductID),
FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
);




CREATE TABLE SALES(
SalesID INT NOT NULL,
Profit INT NOT NULL,
Revenue INT NOT NULL,
PRIMARY KEY(SalesID)
);

CREATE TABLE AmountSold(
FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
);

答案 1 :(得分:1)

这里的问题是您没有在DepartmentId表中声明Employee列。还要先执行父表,然后再执行引用的

 CREATE TABLE DEPARTMENT(
 DepartmentID INT NOT NULL,
 DepartmentName VARCHAR (20) NOT NULL,
 PRIMARY KEY(DepartmentID)
 );

 CREATE TABLE DEVICE(
 DeviceID INT NOT NULL,
 DateReceived DATE NOT NULL,
 PRIMARY KEY(DeviceID)
 );

 CREATE TABLE EMPLOYEE(
 SSN INT NOT NULL,
 WorkID INT NOT NULL,
 BirthDate DATE NOT NULL,
 Name VARCHAR(20) NOT NULL,
 DepartmentID INT,
 DeviceID INT ,
 UNIQUE(WorkID),
 PRIMARY KEY(SSN),
 FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID),
 FOREIGN KEY(DeviceID) REFERENCES DEVICE(DeviceID)
 );

 CREATE TABLE PRODUCT(
 ProductID INT NOT NULL,
 Name VARCHAR(20) NOT NULL,
 Isle VARCHAR(5) NOT NULL,
 Company VARCHAR(20) NOT NULL,
 DepartmentID INT,
 PRIMARY KEY(ProductID),
 FOREIGN KEY(DepartmentID) REFERENCES DEPARTMENT(DepartmentID)
 );

 CREATE TABLE SALES(
 SalesID INT NOT NULL,
 Profit INT NOT NULL,
 Revenue INT NOT NULL,
 PRIMARY KEY(SalesID)
 );

 CREATE TABLE AmountSold(
 SalesId INT NOT NULL,
 ProductId INT NOT NULL,
 FOREIGN KEY (SalesID) REFERENCES SALES(SalesID),
 FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID)
 );