0
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
4

2 回答 2

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
  )
于 2013-11-10T18:55:08.493 回答
0

错误 150 处理外键错误。准备好在编写模式时经常遇到它们,因为它们很容易制作。

要询问引擎问题是什么,请运行以下命令:

SHOW ENGINE INNODB STATUS

这将显示最新的错误。

如果这没有帮助,只需遵循以下一般提示:

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

通常,列定义是让我绊倒的地方。因此,请确保父表包含BIGINT(20),并且没有UNSIGNED或不同的类型,例如INT(10).

于 2013-11-10T19:25:51.357 回答