두 테이블 간의 다 대다 관계를 만들려고합니다. Mysql WorkBench에서 두 테이블 중 하나에 복합 기본 키가 있습니다 (2 개의 외국 키). 나는 SQL을 생성하기 위해 노력하고있어 때이 오류 받고 있어요 :외부 복합 키 Mysql 합성 키
ERROR: Error 1215: Cannot add foreign key constraint
SQL 코드 :
Cannot resolve column name close to:
)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
:이 메시지를 볼 수 SHOW ENGINE INNODB STATUS
에서
-- -----------------------------------------------------
-- Table `A_D_schema`.`Resources_has_OwnerGroups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `A_D_schema`.`Resources_has_OwnerGroups` (
`Resources_id` INT NOT NULL,
`OwnerGroups_id` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Instances_idInstances` INT NOT NULL,
`OwnerGroups_Instances_has_Customers_Customers_idCustomers` INT NOT NULL,
PRIMARY KEY (`Resources_id`, `OwnerGroups_id`, `OwnerGroups_Instances_has_Customers_Instances_idInstances`, `OwnerGroups_Instances_has_Customers_Customers_idCustomers`),
INDEX `fk_Resources_has_OwnerGroups_OwnerGroups1_idx` (`OwnerGroups_id` ASC, `OwnerGroups_Instances_has_Customers_Instances_idInstances` ASC, `OwnerGroups_Instances_has_Customers_Customers_idCustomers` ASC),
INDEX `fk_Resources_has_OwnerGroups_Resources1_idx` (`Resources_id` ASC),
CONSTRAINT `fk_Resources_has_OwnerGroups_Resources1`
FOREIGN KEY (`Resources_id`)
REFERENCES `A_D_schema`.`Resources` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Resources_has_OwnerGroups_OwnerGroups1`
FOREIGN KEY (`OwnerGroups_id` , `OwnerGroups_Instances_has_Customers_Instances_idInstances` , `OwnerGroups_Instances_has_Customers_Customers_idCustomers`)
REFERENCES `A_D_schema`.`OwnerGroups` (`id` , `Instances_has_Customers_Instances_idInstances` , `Instances_has_Customers_Customers_idCustomers`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SHOW CREATE TABLE Resources
및 SHOW CREATE TABLE OwnerGroups
:
CREATE TABLE `Resources` (
`idResources` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(45) DEFAULT NULL,
`role` int(11) DEFAULT NULL COMMENT 'role : 1 disptcher \n0 admin',
PRIMARY KEY (`idResources`),
UNIQUE KEY `idresources_UNIQUE` (`idResources`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `OwnerGroups` (
`idOwnerGroups` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`group` int(11) DEFAULT NULL,
PRIMARY KEY (`idOwnerGroups`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
확인은 .I이는 WB에서 오는 것으로 나타났습니다 당신의 발언 주셔서 감사합니다. id라는 ID를 id라는 이름으로 바꿨습니다. 하지만 여전히 스크립트에 있습니다. 왼쪽 패널에서 나는 중복 된 이름을 가진 테이블을 볼 수 있는데, 그 이름은 내가 삭제 한 이후로 ... 나는 Resources_has_OwnerGroups를 삭제했으며 여전히 스크립트에 존재한다. 은 WB이므로 도청 되었습니까? –
그래서 귀하의 의견을 정확하게 이해한다면 복합 키의 모범 사례는 무엇입니까? Resources_has_OwnerGroups 테이블을 예로 들어 보겠습니다. fk_Resources_id 및 fk_OwnerGroups_id로 구성된 하나의 새 키 Resources_has_OwnerGroups_id를 생성 하시겠습니까? –
'PRIMARY KEY (Resources_id, OwnerGroups_id)'가 가장 좋습니다. 더 많은 열을 포함하면 첫 번째 두 열과 관련하여 중복이 존재할 수 있습니다. 기본 키의 구성원은 FK 제약이 아닌 * 열 *입니다. –