意外的外键

Foreign Key Unexpected

我正在使用 MySQL workbench 并且我正在尝试创建一个 table 由使用 SQL 查询的外键组成。我对外国部分有疑问。

create table employee_position
(
ePID int primary key,
ePName varchar(45) not null,
eID int foreign key references employee_profile(eID)
)
create table employee_position
(
  ePID int primary key,
  ePName varchar(45) not null,
  eID int,
  foreign key (eID) references employee_profile(eID)
)

SQLFiddle demo

你的语法有误。尝试:

create table employee_position
(
ePID int primary key,
ePName varchar(45) not null,
eID int,
foreign key (keyname) references employee_profile(eID)
)

有关详细信息,请参阅 mysql documentation

在 MySQL 中检查 FOREIGN KEYS

试试这个:

CREATE TABLE employee_position
(
   ePID INT NOT NULL AUTO_INCREMENT,
   ePName VARCHAR(45) NOT NULL,
   eID INT NOT NULL,
   PRIMARY KEY (ePID),
   KEY CustomerID (eID),
   CONSTRAINT FK_employee_position_EP 
           FOREIGN KEY (eID) 
           REFERENCES employee_profile (eID) ON DELETE CASCADE ON UPDATE CASCADE
);