我在MariaDB 11.0.2中有以下两个表:
CREATE TABLE `Languages` (
`Name` char(49) DEFAULT NULL,
`ISO_639_1` char(2) NOT NULL,
`Language_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Main_Flag` varchar(20) DEFAULT NULL,
PRIMARY KEY (`Language_ID`),
UNIQUE KEY `Languages_UN` (`ISO_369_1`,`Main_Flag`)
) ENGINE=InnoDB AUTO_INCREMENT=136 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
字符串
和/或
CREATE TABLE `Tests` (
`Test_ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Test_Name` varchar(50) DEFAULT NULL,
`ISO_639_1` char(2) NOT NULL,
`Main_Flag` varchar(20) DEFAULT NULL,
PRIMARY KEY (`Test_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=136 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
型
但我不能将双列唯一键分配给它们的对应项:
MariaDB [my_db]> ALTER TABLE Tests ADD CONSTRAINT Test_Language_FK FOREIGN KEY (ISO_639_1, Main_Flag) REFERENCES Languages(ISO_639_1, Main_Flag);
ERROR 1005 (HY000): Can't create table `my_db`.`Tests` (errno: 150 "Foreign key constraint is incorrectly formed")
型
测试表的索引没有异常:
MariaDB [my_db]> show index from Tests;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Ignored |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Tests | 0 | PRIMARY | 1 | Test_ID | A | 37 | NULL | NULL | | BTREE | | | NO |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
1 row in set (0,001 sec)
型
是什么在阻碍我?数据类型相同,排序规则和字符集也匹配。
2条答案
按热度按时间oxosxuxt1#
这个错误有点神秘,你可以使用
字符串
在
LATEST FOREIGN KEY ERROR
部分,您将看到constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns
您需要并索引
型
看到这个working fiddle
u0njafvf2#
使用inno den的最好方法是运行最后一个命令,以便您尝试有关错误的更多信息。
在中,列组合的引用表中缺少唯一键或主键
| 姓名、名称|工作状态| Status |
| --|--| ------------ |
| ||===================================== 2023-07-29 20:36:04 0x 7 f8018926700 INNODB监视器输出|
fiddle