我知道关于这个错误有很多类似的问题(外键在创建或更新表时失败)。我已经检查过:
- 两个表都是 InnoDb
- 所有字段都被索引(或PK,所以..索引),int和长度10
但它仍然不起作用......现在,我只希望有人看看我的 SQL 代码并告诉我是否有问题:(
-- ----------------------------
-- Table structure for `hotels`
-- ----------------------------
DROP TABLE IF EXISTS `hotels`;
CREATE TABLE `hotels` (
`id` int(10) NOT NULL AUTO_INCREMENT,
-- other fields
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `hotelsearch_query`
-- ----------------------------
DROP TABLE IF EXISTS `hotelsearch_query`;
CREATE TABLE `hotelsearch_query` (
`id` int(10) NOT NULL,
-- other fields
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `hotelsearch_result`
-- ----------------------------
DROP TABLE IF EXISTS `hotelsearch_result`;
CREATE TABLE `hotelsearch_result` (
`query_id` int(10) unsigned NOT NULL,
`hotel_id` int(10) unsigned NOT NULL,
-- other fields
PRIMARY KEY (`query_id`,`hotel_id`),
KEY `IDX_QUERY` (`query_id`),
KEY `IDX_HOTEL` (`hotel_id`),
FOREIGN KEY (`query_id`) REFERENCES `hotelsearch_query`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`hotel_id`) REFERENCES `hotels`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
当我删除关于外键的 2 行时,一切正常。当我尝试两者或一个一个时,我也有错误。
请帮我 !