1

由于某种原因,这个 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(errno: 150)

这可能是什么原因造成的?

4

3 回答 3

2

您必须在外部表中为键指定列:

FOREIGN KEY (ssn) REFERENCES Employees (ssn) ...

于 2010-02-25T01:54:38.750 回答
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;

它确实有效。

于 2010-02-25T01:53:55.273 回答
0

您只有引用表的外键,而不是列。

尝试:引用Employee.ssn

于 2010-02-25T01:53:39.907 回答