0

我们无法运行下面的查询。我们有另一个名为“person”的表,它有一个主键 person_id。如您所见,我们试图将此列作为我们(客户表的)外键。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 生成的。

4

1 回答 1

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
于 2012-05-03T11:21:05.653 回答