Duplicate entry '' 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=;

-- ----------------------------
-- Table structure for `sh_incentive`
-- ----------------------------
DROP TABLE IF EXISTS `sh_incentive`;
CREATE TABLE `sh_incentive` (
`id` int() NOT NULL COMMENT '编号',
`item_id` int() DEFAULT NULL COMMENT '名称',
`agent_id` int() DEFAULT NULL COMMENT '关联代理商',
`money` double(,) NOT NULL COMMENT '金额',
`year` int() NOT NULL COMMENT '年份',
`month` int() NOT NULL COMMENT '月份',
`addtime` int() 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() NOT NULL COMMENT '编号',
`name` varchar() NOT NULL COMMENT '名称',
`intro` varchar() 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() NOT NULL COMMENT '编号',
`opener_id` int() DEFAULT NULL COMMENT '员工编号',
`incentive_id` int() DEFAULT NULL COMMENT '激励条件id',
`remark` varchar() DEFAULT NULL COMMENT '备注说明',
`addtime` int() 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() NOT NULL COMMENT '编号',
`opener_id` int() NOT NULL COMMENT '拓展员',
`agent_id` int() DEFAULT NULL COMMENT '代理商',
`money` double(,) NOT NULL COMMENT '金额',
`trode_number` varchar() NOT NULL COMMENT '转账流水号',
`addtime` int() 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() NOT NULL COMMENT '编号',
`opener_id` int() NOT NULL COMMENT '拓展员',
`agent_id` int() DEFAULT NULL COMMENT '代理商',
`money` double(,) NOT NULL COMMENT '金额',
`trode_number` varchar() NOT NULL COMMENT '转账流水号',
`addtime` int() 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() NOT NULL COMMENT '编号',
`opener_id` int() DEFAULT NULL COMMENT '员工编号',
`incentive_id` int() DEFAULT NULL COMMENT '激励条件id',
`remark` varchar() DEFAULT NULL COMMENT '备注说明',
`addtime` int() 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() NOT NULL COMMENT '编号',
`item_id` int() DEFAULT NULL COMMENT '名称',
`agent_id` int() DEFAULT NULL COMMENT '关联代理商',
`money` double(,) NOT NULL COMMENT '金额',
`year` int() NOT NULL COMMENT '年份',
`month` int() NOT NULL COMMENT '月份',
`addtime` int() 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() NOT NULL COMMENT '编号',
`name` varchar() NOT NULL COMMENT '名称',
`intro` varchar() NOT NULL COMMENT '说明',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统激励项'; -- ----------------------------
-- Records of sh_incentive_item
-- ----------------------------

上面的顺序很重要哦。顺序有误,就不能执行成功!

处理好后,就可以添加自增长了。

由于外键的存在引发的一个mysql问题 Cannot change column 'id': used in a foreign key constraint的更多相关文章

  1. MySQL设置外键报错 #1452 - Cannot add or update a child row: a foreign key constraint fails 解决方法

    MySQL数据库,当我尝试在A表中设置B表的主键为外键时,报出错误:#1452 - Cannot add or update a child row: a foreign key constraint ...

  2. 删除带外键的表【foreign key constraint fails】报错

    title: 删除带外键的表[foreign key constraint fails]报错 date: 2018-08-02 21:59:06 tags: 数据库 --- 遥想当时正在学hibern ...

  3. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  4. mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决

    ERROR 1215 (HY000): Cannot add foreign key constraint 最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下: 本来是要建两张表: 1 2 ...

  5. MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint

    1.数据类型      2.数据表的引擎 数据表 mysql> show tables; +------------------+ | Tables_in_market | +--------- ...

  6. mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

    mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...

  7. Hibernate 注解多对一 要求在多那边产生一个外键而不会另外产生一个表

    在使用hibernate注解的时候,我们映射一对多时,有时候莫名其妙的产生了两张表,其中一张表是A_B,这并不符合数据库中多的一方放置一个外键的原则,那么如何控制只产生一个表呢,请看下面的例子: 多的 ...

  8. hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

    一篇文章里边有多张图片,典型的单向一对多关系 多方 当程序运行到这一句的时候必然报错 但是参考书也是这样写的 其中em是 EntityManager em = JPA.createEntityMana ...

  9. SQL 语句外键 a foreign key constraint fails

    queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;"); queryRunner.update(sql, pid); queryRunn ...

随机推荐

  1. hdu 2196 computer

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. scala 学习: 逆变和协变

    scala 逆变和协变的概念网上有很多解释, 总结一句话就是 参数是逆变的或者不变的,返回值是协变的或者不变的. 但是为什么是这样的? 协变: 当s 是A的子类, 那么func(s) 是func(A) ...

  3. linux运维笔记——常用命令详解diff

    1.diff 你可以把diff看成是linux上的文件比对工具 例子文件内容: [root@localhost disks]# cat test1.txt a b c d [root@localhos ...

  4. wxPython--Python GUI编程参考链接

    原文链接http://www.cnblogs.com/coderzh/archive/2008/11/23/1339310.html

  5. @gettrcname.sql

    http://www.eygle.com/archives/2007/05/script_gettrcname.html 最近有很多朋友问起<深入浅出Oracle>一书中的一个脚本gett ...

  6. iOS多线程编程之NSOperation和NSOperationQueue的使用(转自容芳志专栏)

    转自由http://blog.csdn.net/totogo2010/ 使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBl ...

  7. EXCEL某列长度超过255个字符导入SQL SERVER的处理方法

    问题描述: [Excel 源 [1]] 错误: 输出“Excel 源输出”(9) 上的 输出列“Description 3”(546) 出错.返回的列状态是:“文本被截断,或者一个或多个字符在目标代码 ...

  8. ASP.NET简单验证码

    今天写了一个特别简单的验证码实现.现将代码贴出. protected void Page_Load(object sender, EventArgs e) { CreateCheckCodeImage ...

  9. Python成长笔记 - 基础篇 (四)函数

    1.面向对象:类(class) 2.面向过程:过程(def) 3.函数式编程:函数(def)----python   1.函数:http://egon09.blog.51cto.com/9161406 ...

  10. iOS UIWebView中javascript与Objective-C交互、获取摄像头

    UIWebView是iOS开发中常用的一个视图控件,多数情况下,它被用来显示HTML格式的内容. 支持的文档格式 除了HTML以外,UIWebView还支持iWork, Office等文档格式: Ex ...