回答

收藏

循环外键约束失败mysql

技术问答 技术问答 214 人阅读 | 0 人回复 | 2023-09-12

如果我在InnoDB数据库设计在发动机上,有三个表不能删除,因为每个表都说外键约束失败-这是否意味着设计错误?
7 |& F4 t8 V# _6 P0 a7 O请参阅以下结构:+ F: w2 ]/ X8 c7 b( ^
CREATE TABLE IF NOT EXISTS `account` (  `account_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`account_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;CREATE TABLE IF NOT EXISTS `identity` (  `identity_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(10) unsigned DEFAULT NULL, `account_id` smallint(5) unsigned DEFAULT NULL, PRIMARY KEY (`identity_id`), KEY `fk_details1` (`user_id`),    KEY `fk_account1` (`account_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;CREATE TABLE IF NOT EXISTS `user` (  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `login` varchar(64) NOT NULL DEFAULT '', `password` varchar(32) NOT NULL DEFAULT '', `default_identity_id` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`user_id`), UNIQUE KEY `login_UNIQUE` (`login`), KEY `fk_identity1` (`default_identity_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;---- Constraints for table `identity`--ALTER TABLE `identity`  ADD CONSTRAINT `fk_details1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `fk_account1` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE;---- Constraints for table `user`--ALTER TABLE `user`  ADD CONSTRAINT `fk_identity1` FOREIGN KEY (`default_identity_id`) REFERENCES `identity` (`identity_id`) ON DELETE CASCADE ON UPDATE CASCADE;我怀疑问题出在default_identity_id …我应该把它作为标志移到身份表吗?
2 [. {0 G- g# |# e% s2 ?请指教!/ `. O. c- Z& O( T+ p! v4 Q
                                                                9 }: b8 m& @9 d; m/ |' [- [4 a# Y
    解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则