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 -- ----------------------------
上面的順序很重要哦。順序有誤,就不能執行成功!
處理好后,就可以添加自增長了。