以下sql命令中的错误,即错误1005:无法创建表(错误号:150)

时间:2013-11-10 18:40:09

标签: mysql sql sqlite

CREATE TABLE `db`.`employee_meeting` (
  `employee_id` BIGINT(20) NOT NULL,
  `meeting_id` BIGINT(20) NOT NULL,
  PRIMARY KEY (`employee_id`, `meeting_id`),
  INDEX `FK_meeting`(`meeting_id`),
  CONSTRAINT `FK_employee` FOREIGN KEY `FK_employee` (`employee_id`)
    REFERENCES `employee` (`employee_id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT,
  CONSTRAINT `FK_meeting` FOREIGN KEY `FK_meeting` (`meeting_id`)
    REFERENCES `meeting` (`meeting_id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT

2 个答案:

答案 0 :(得分:1)

可能是因为您的其他两个表尚不存在。以下工作正常:

CREATE TABLE employee ( 
  employee_id BIGINT(20) NOT NULL,
  PRIMARY KEY (employee_id)
);

CREATE TABLE meeting ( 
  meeting_id BIGINT(20) NOT NULL,
  PRIMARY KEY (meeting_id)
);

CREATE TABLE employee_meeting ( 
  employee_id BIGINT(20) NOT NULL, 
  meeting_id BIGINT(20) NOT NULL, 

  PRIMARY KEY (employee_id, meeting_id), 

  INDEX FK_meeting(meeting_id), 

  CONSTRAINT FK_employee 
    FOREIGN KEY FK_employee (employee_id) 
    REFERENCES employee (employee_id) 
    ON DELETE RESTRICT 
    ON UPDATE RESTRICT, 

  CONSTRAINT FK_meeting 
    FOREIGN KEY FK_meeting (meeting_id) 
    REFERENCES meeting (meeting_id) 
    ON DELETE RESTRICT 
    ON UPDATE RESTRICT
  )

答案 1 :(得分:0)

Error 150 deals with foreign key errors。准备好在编写模式时经常遇到它们,因为它们很容易制作。

要询问引擎问题所在,请运行以下命令:

SHOW ENGINE INNODB STATUS

这将显示最新的错误。

如果这没有用,请按照以下一般提示进行操作:

  • 确保外键引用的父表存在。
  • 确保在父表和子表中将列定义为完全相同,但子表可以为NULL,即使父表不是。
  • 确保您使用的是唯一的约束名称......虽然这实际上可能是错误的121.我也经常看到错误。

通常列定义是让我兴奋的原因。因此,请确保父表包含BIGINT(20),并且没有UNSIGNED或其他类型,例如INT(10)