Gui*_*ido 2 mysql sql foreign-keys mariadb
我在 MariaDB 中创建多对多关系表时遇到问题。我尝试过使用工作台、手动创建脚本和“Show Create Table xxxxxxx;” 从另一个已经创建的n到n个表但结果总是相同的,出现以下错误:
Error Code: 1005. Can't create table `asi_234_api_establecimientos`.`oe_modalidad` (errno: 150 "Foreign key constraint is incorrectly formed")
Run Code Online (Sandbox Code Playgroud)
我用来创建表的代码:
CREATE TABLE `oe_modalidad` (
`oferta_establecimiento_id` bigint(20) NOT NULL,
`modalidad_id` bigint(20) NOT NULL,
KEY `fk_oe_modalidades_oferta_establecimiento1_idx` (`oferta_establecimiento_id`),
KEY `fk_oe_modalidad_modalidad1_idx` (`modalidad_id`),
CONSTRAINT `fk_oe_modalidad_modalidad1` FOREIGN KEY (`modalidad_id`) REFERENCES `modalidad` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_oe_modalidades_oferta_establecimiento1` FOREIGN KEY (`oferta_establecimiento_id`) REFERENCES `oferta_establecimiento` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB
Run Code Online (Sandbox Code Playgroud)
我尝试在两个不同版本的 MariaDB(10.0.38 和 5.5.60)中运行它,但我不断收到相同的错误。
小智 5
这是一个非常简短的例子:
create table Table1(
id int auto_increment key,
name varchar(50) not null
);
create table Table2(
id int auto_increment key,
name varchar(50) not null
);
create table Table3(
idTable1 int not null,
idTable2 int not null,
primary key(idTable1, idTable2),
CONSTRAINT fk_table3_table1 foreign key (idTable1) references Table1 (id),
CONSTRAINT fk_table3_table2 foreign key (idTable2) references Table2 (id)
);
Run Code Online (Sandbox Code Playgroud)
请记住,Table1 和 Table2 主键必须与 Table3 外键类型相同。
| 归档时间: |
|
| 查看次数: |
2683 次 |
| 最近记录: |