由于外键的存在引发的一个mysql问题 Cannot change column 'id': used in a foreign key constraint
mysql 一个 in 39 Cannot ID Key
2023-09-27 14:24:31 时间
Duplicate entry '0' for key 'PRIMARY'
一查,发现表没有设置自增长。
尝试增加修改表,添加自增长。
ALTER TABLE sh_incentive_item MODIFY id SMALLINT UNSIGNED AUTO_INCREMENT;
报错
[SQL] ALTER TABLE sh_incentive_item MODIFY id SMALLINT UNSIGNED AUTO_INCREMENT; [Err] 1833 - Cannot change column 'id': used in a foreign key constraint 'FK_sh_incentive_item_id' of table 'storehelper.sh_incentive'
发现是因为外键的影响,不能随便的更改表结构。
要想更改表结构,首先要把基层的表修改了。
A表 作为B表的外键,A表不能随便修改。
B表 有A表的外键,必须先处理好B,然后A才能修改。
索性,我就把表中的外键全部去除。
原SQL
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `sh_incentive` -- ---------------------------- DROP TABLE IF EXISTS `sh_incentive`; CREATE TABLE `sh_incentive` ( `id` int(11) NOT NULL COMMENT '编号', `item_id` int(11) DEFAULT NULL COMMENT '名称', `agent_id` int(11) DEFAULT NULL COMMENT '关联代理商', `money` double(10,2) NOT NULL COMMENT '金额', `year` int(11) NOT NULL COMMENT '年份', `month` int(11) NOT NULL COMMENT '月份', `addtime` int(11) NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`), KEY `FK_sh_incentive_item_id` (`item_id`), CONSTRAINT `FK_sh_incentive_item_id` FOREIGN KEY (`item_id`) REFERENCES `sh_incentive_item` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='激励设定'; -- ---------------------------- -- Records of sh_incentive -- ---------------------------- -- ---------------------------- -- Table structure for `sh_incentive_item` -- ---------------------------- DROP TABLE IF EXISTS `sh_incentive_item`; CREATE TABLE `sh_incentive_item` ( `id` int(11) NOT NULL COMMENT '编号', `name` varchar(255) NOT NULL COMMENT '名称', `intro` varchar(255) NOT NULL COMMENT '说明', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统激励项'; -- ---------------------------- -- Records of sh_incentive_item -- ---------------------------- -- ---------------------------- -- Table structure for `sh_opener_bonus` -- ---------------------------- DROP TABLE IF EXISTS `sh_opener_bonus`; CREATE TABLE `sh_opener_bonus` ( `id` int(11) NOT NULL COMMENT '编号', `opener_id` int(11) DEFAULT NULL COMMENT '员工编号', `incentive_id` int(11) DEFAULT NULL COMMENT '激励条件id', `remark` varchar(255) DEFAULT NULL COMMENT '备注说明', `addtime` int(11) DEFAULT NULL COMMENT '记录时间', PRIMARY KEY (`id`), KEY `FK_sh_openernus_opener_id` (`opener_id`), KEY `FK_sh_openernus_incentive_id` (`incentive_id`), CONSTRAINT `FK_sh_openernus_incentive_id` FOREIGN KEY (`incentive_id`) REFERENCES `sh_incentive` (`id`), CONSTRAINT `FK_sh_openernus_opener_id` FOREIGN KEY (`opener_id`) REFERENCES `sh_opener` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='拓展员奖金'; -- ---------------------------- -- Records of sh_opener_bonus -- ---------------------------- -- ---------------------------- -- Table structure for `sh_opener_bonus_payment` -- ---------------------------- DROP TABLE IF EXISTS `sh_opener_bonus_payment`; CREATE TABLE `sh_opener_bonus_payment` ( `id` int(11) NOT NULL COMMENT '编号', `opener_id` int(11) NOT NULL COMMENT '拓展员', `agent_id` int(11) DEFAULT NULL COMMENT '代理商', `money` double(10,2) NOT NULL COMMENT '金额', `trode_number` varchar(255) NOT NULL COMMENT '转账流水号', `addtime` int(11) NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`), KEY `FK_sh_openerent_opener_id` (`opener_id`), CONSTRAINT `FK_sh_openerent_opener_id` FOREIGN KEY (`opener_id`) REFERENCES `sh_opener` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='拓展员奖金发放'; -- ---------------------------- -- Records of sh_opener_bonus_payment -- ----------------------------
把外键统统去掉
-- ---------------------------- -- Table structure for `sh_opener_bonus_payment` -- ---------------------------- DROP TABLE IF EXISTS `sh_opener_bonus_payment`; CREATE TABLE `sh_opener_bonus_payment` ( `id` int(11) NOT NULL COMMENT '编号', `opener_id` int(11) NOT NULL COMMENT '拓展员', `agent_id` int(11) DEFAULT NULL COMMENT '代理商', `money` double(10,2) NOT NULL COMMENT '金额', `trode_number` varchar(255) NOT NULL COMMENT '转账流水号', `addtime` int(11) NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='拓展员奖金发放'; -- ---------------------------- -- Table structure for `sh_opener_bonus` -- ---------------------------- DROP TABLE IF EXISTS `sh_opener_bonus`; CREATE TABLE `sh_opener_bonus` ( `id` int(11) NOT NULL COMMENT '编号', `opener_id` int(11) DEFAULT NULL COMMENT '员工编号', `incentive_id` int(11) DEFAULT NULL COMMENT '激励条件id', `remark` varchar(255) DEFAULT NULL COMMENT '备注说明', `addtime` int(11) DEFAULT NULL COMMENT '记录时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='拓展员奖金'; -- ---------------------------- -- Records of sh_opener_bonus -- ---------------------------- -- ---------------------------- -- Table structure for `sh_incentive` -- ---------------------------- DROP TABLE IF EXISTS `sh_incentive`; CREATE TABLE `sh_incentive` ( `id` int(11) NOT NULL COMMENT '编号', `item_id` int(11) DEFAULT NULL COMMENT '名称', `agent_id` int(11) DEFAULT NULL COMMENT '关联代理商', `money` double(10,2) NOT NULL COMMENT '金额', `year` int(11) NOT NULL COMMENT '年份', `month` int(11) NOT NULL COMMENT '月份', `addtime` int(11) NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='激励设定'; -- ---------------------------- -- Records of sh_incentive -- ---------------------------- -- ---------------------------- -- Table structure for `sh_incentive_item` -- ---------------------------- DROP TABLE IF EXISTS `sh_incentive_item`; CREATE TABLE `sh_incentive_item` ( `id` int(11) NOT NULL COMMENT '编号', `name` varchar(255) NOT NULL COMMENT '名称', `intro` varchar(255) NOT NULL COMMENT '说明', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统激励项'; -- ---------------------------- -- Records of sh_incentive_item -- ----------------------------
上面的顺序很重要哦。顺序有误,就不能执行成功!
处理好后,就可以添加自增长了。
相关文章
- 【阿里最新数据库面试题】MySQL主从一致性
- linux中备份mysql数据库的一个shell脚本
- 一个简单的HTML+PHP+MySQL的交互页面(输入框内容查询,HTML页面显示)
- mysql-存储过程案例-存储过程中创建表和修改表数据
- MYSQL 在当前时间加上或减去一个时间段
- MySQL把两个字段用逗号分隔合并成一个字段
- 推荐一个 MYSQL 的命令行的客户端 MYCLI
- mysql 在指定字段后新加一个字段
- MySQL 如何使用 PV 和 PVC?- 每天5分钟玩转 Docker 容器技术(154)
- MySQL学习之索引机制详解(B+树)
- Mysql:SQL Modes:最令我烦的sql-modes:一个标准它不香么?!
- mysql向某个字段前边追加一个字符串CONCAT命令
- mysql 插入一个字段 id自增并设置为主键
- 2022-09-02 mysql/stonedb-现有加载pack数据模块分析及需求推定
- 2023-02-01 mysql列存储引擎-用户自定义变量-代码现状分析
- mysql的DML语言(增删改)
- MySql错误处理(三)- 错误处理的例子
- [MySQL]-->询5几天之内解决在新年发行一个同事的生日过程
- 【MYSQL百炼成圣】炼体大成篇——数据库触发器
- mysql 检查一个字符串是不是身份证号
- mysql中char,varchar与text类型的区别和选用
- 从一个问题来加深对mysql可重复读的理解
- mysql下一个版本应该且实现并不复杂增加的常用功能
- MYSQL错误解决:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- MySQL 触发器