Innodb不接受外键

时间:2012-05-03 11:18:14

标签: mysql foreign-keys innodb

我们无法运行以下查询。我们有另一个名为“person”的表,它有一个主键person_id。如您所见,我们正在尝试将此列作为我们的(customers表)外键。 phpMyAdmin返回#1064语法错误。这里出了什么问题?

CREATE  TABLE IF NOT EXISTS `resort`.`customers` (
  `person_id` VARCHAR(45) NOT NULL ,
  `cid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  UNIQUE INDEX `person_id_UNIQUE` (`cid` ASC) ,
  PRIMARY KEY (`person_id`) ,
  UNIQUE INDEX `person_id_UNIQUE` (`person_id` ASC) ,
  CONSTRAINT `person_id`
    FOREIGN KEY ()
    REFERENCES `resort`.`person` ()
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB

顺便说一下,这个查询是由Oracle的MySQL Workbench生成的。

1 个答案:

答案 0 :(得分:3)

您需要在约束定义中包含列:

CONSTRAINT person_id
  FOREIGN KEY ()
  REFERENCES resort.person()
  ON DELETE CASCADE
  ON UPDATE CASCADE

应该是:

CONSTRAINT fk_person_id
  FOREIGN KEY (person_id)
  REFERENCES resort.person (id)
  ON DELETE CASCADE
  ON UPDATE CASCADE