Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql MariaDB-在两个实体之间创建多对多关系表_Mysql_Sql_Foreign Keys_Mariadb - Fatal编程技术网

Mysql MariaDB-在两个实体之间创建多对多关系表

Mysql MariaDB-在两个实体之间创建多对多关系表,mysql,sql,foreign-keys,mariadb,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") 我用于创建表的

在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")
我用于创建表的代码:

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

我曾尝试在MariaDB的两个不同版本(10.0.38和5.5.60)中运行此功能,但我一直遇到相同的错误。

这是一个非常简短的示例:

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)
);

请记住,Table1和Table2主键必须是Table3外键的同一类型。

显示RTA_establecimiento表的
modalidad
的定义。搜索您的错误消息,您将看到通常的原因。您可能希望阅读此检查表以获得正确的外键:抱歉,缺少信息。事实上,这是两把钥匙的类型不同。谢谢你的帮助@Barmar不必在多:多表上处理FKs。如果有一个悬而未决的参考资料,那没什么大不了的。你能解释一下他做错了什么,需要改正吗?一个实际的例子并不能做到这一点。我认为类型不一样,但我不知道,因为他没有发布其他表。这可能是真的,但在他澄清之前,我们无法真正回答。非常感谢@Graiton。问题是关键类型。一个是bigint(20),另一个只是int。