出于某种原因,这个MySQL失败了:
CREATE SCHEMA IF NOT EXISTS `partB` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `partB`;
CREATE TABLE Employees ( ssn CHAR(11),
Name CHAR(30),
mlot INTEGER,
PRIMARY KEY(ssn))
ENGINE = InnoDB;
CREATE TABLE Dept_Mgr ( did INTEGER,
dname CHAR(20),
ssn CHAR(11) NOT NULL,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB;
它给出错误:
错误1005(HY000):无法创建表格
partb.dept_mgr
(错误:150)
导致这种情况的原因是什么?
答案 0 :(得分:2)
您必须在外表中为键指定列:
FOREIGN KEY (ssn) REFERENCES Employees (ssn) ...
答案 1 :(得分:1)
此命令:
SHOW ENGINE INNODB STATUS;
当您在创建外键时遇到问题,是您的朋友。输出(删节)
------------------------
LATEST FOREIGN KEY ERROR
------------------------
100225 2:51:42 Error in foreign key constraint of table test/dept_mgr:
FOREIGN KEY (ssn) REFERENCES Employees
ON DELETE NO ACTION)
ENGINE = InnoDB:
Syntax error close to:
ON DELETE NO ACTION)
ENGINE = InnoDB
如果您将陈述更改为:
CREATE TABLE Dept_Mgr (
did INTEGER,
dname CHAR(20),
ssn CHAR(11) NOT NULL,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees(ssn)
) engine = innodb;
它确实有用。
答案 2 :(得分:0)
您只有引用表的外键,而不是列。
尝试:引用Employee.ssn