简体   繁体   English

MySQL的:使用两个外键到同一个表

[英]Mysql: using two foreign keys to the same table

I'm using MySQL workbench to design a database. 我正在使用MySQL工作台来设计数据库。 Server is mysql 5.5.6 服务器是mysql 5.5.6

I've defined a few foreign keys linking the "candidates" table to the "countries" table. 我定义了一些外键,将“候选人”表链接到“国家”表。 I get this error: 我收到此错误:

Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)

The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. 事情是:我引用了countrys表两次:一次是“国籍”列,一次是用户地址的原籍国。 Is that allowed? 可以吗 Is this the right way to do it? 这是正确的方法吗?

Here is the generated code that seems to trigger the issue. 这是似乎触发问题的生成的代码。

CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nom` VARCHAR(40) NULL,
  `prenom` VARCHAR(40) NULL,
  `qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
  `sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
  `date_de_naissance` DATE NULL,
  `Nationalite` INT NOT NULL,
  `selor_bilinguisme` TINYINT(1) NULL,
  `rue` VARCHAR(60) NULL,
  `numero` VARCHAR(10) NULL,
  `pays` INT NOT NULL,
  `region` INT NOT NULL,
  `localité` VARCHAR(40) NULL,
  `code_postal` VARCHAR(10) NULL,
  `email` VARCHAR(241) NULL,
  `tel_domicile` VARCHAR(30) NULL,
  `tel_bureau` VARCHAR(30) NULL,
  `tel_mobile` VARCHAR(30) NULL,
  `tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
  PRIMARY KEY (`id`),
  INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
  INDEX `fk_candidats_régions1_idx` (`region` ASC),
  CONSTRAINT `fk_candidats_pays`
    FOREIGN KEY (`Nationalite` , `pays`)
    REFERENCES `customer`.`pays` (`id` , `id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_candidats_régions1`
    FOREIGN KEY (`region`)
    REFERENCES `customer`.`régions` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

The "pays" table ("countries" in French) “付款”表(法语中的“国家”)

CREATE TABLE IF NOT EXISTS `customer`.`pays` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nom_fr` VARCHAR(45) NULL,
  `nom_nl` VARCHAR(45) NULL,
  `nationalite_fr` VARCHAR(45) NULL,
  `nationalite_nl` VARCHAR(45) NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB

Your schema generator doesn't work correctly. 您的架构生成器无法正常工作。

It should generate: 它应该生成:

CONSTRAINT `fk_candidats_pays`
    FOREIGN KEY (`pays`)
    REFERENCES `customer`.`pays` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
    FOREIGN KEY (`Nationalite`)
    REFERENCES `customer`.`pays` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,

To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship. 另一个问题是:第一次看到这种类型的引用似乎很奇怪,但这很正常,我认为没有其他方法可以构建这种类型的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM