Mysql

我怎樣才能讓這個程式碼工作?它在mysql中,我不斷收到錯誤1822’未能添加外鍵約束。缺少約束索引'

  • June 3, 2020
CREATE TABLE EMPLOYEE (
employeeNumber VARCHAR (15) NOT NULL, 
employeeName VARCHAR (20) NOT NULL, 
department VARCHAR (10) NOT NULL,
phone INT NOT NULL,
PRIMARY KEY (employeeName, department)

CREATE TABLE DEPARTMENTS( 
department VARCHAR (10) NOT NULL,
department Code VARCHAR(10) NOT NULL, 
department Name VARCHAR(30) NOT NULL, 
FOREIGN KEY (department)
REFERENCES EMPLOYEE (department)

CREATE TABLE EMAIL_INFO(
employee Name VARCHAR (20) NOT NULL,
email VARCHAR (30) NOT NULL, 
FOREIGN KEY (employeeName)
REFERENCES EMPLOYEE (employeeName)
);

您在 mysql 中的程式碼存在一些問題。

您不能使用帶有空格的列鬃毛,至少不能像您在表格部門中看到的那樣沒有反引號。

引用的列需要一個索引,您的表employee 有一個主鍵,但是因為它是一個雙列,所以您不能使用它。在員工和部門中,您的程式碼也缺少括號和分號

CREATE TABLE EMPLOYEE (
employeeNumber VARCHAR (15) NOT NULL, 
employeeName VARCHAR (20) NOT NULL, 
department VARCHAR (10) NOT NULL,
phone INT NOT NULL,
INDEX (employeeName),
INDEX (department),
PRIMARY KEY (employeeName, department)
);

CREATE TABLE DEPARTMENTS( 
department VARCHAR (10) NOT NULL,
`department Code` VARCHAR(10) NOT NULL, 
`department Name` VARCHAR(30) NOT NULL, 
FOREIGN KEY (department)
  REFERENCES EMPLOYEE (department));

CREATE TABLE EMAIL_INFO(
`employee Name` VARCHAR (20) NOT NULL,
 email VARCHAR (30) NOT NULL, 
FOREIGN KEY (`employee Name`)
  REFERENCES EMPLOYEE (employeeName)
);

引用自:https://dba.stackexchange.com/questions/268426