拥有唯一索引表,插入重复数据时处理方法

拥有唯一索引表,插入重复数据时处理方法。
3种方法:忽略不插入;先删除再插入

没有插入,有则更新指定字段。加入on duplicate key update后指定需要更新字段

INSERT INTO `test`.`t_port` (`port_id`, `port_name`, `port_describe`, `cost`, `on_off`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`)  VALUES ('7', '个人信用验证-A', '查询个人的征信记录', '20', '1', '1', '2018-09-28 22:40:17', '1', '2018-10-04 16:59:19', '', '0') on duplicate key update `port_id`=VALUES(`port_id`);

先删除再插入,使用REPLACE关键字即可

REPLACE INTO `test`.`t_port` (`port_id`, `port_name`, `port_describe`, `cost`, `on_off`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`) VALUES ('7777000', '个人信用验证-A', '查询个人的征信记录', '20', '1', '1', '2018-09-28 22:40:17', '1', '2018-10-04 16:59:19', '', '0');

忽略不插入,只需要在加入IGNORE关键字即可

INSERT IGNORE  INTO `test`.`t_port` (`port_id`, `port_name`, `port_describe`, `cost`, `on_off`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`) VALUES ('7', '个人信用验证-A', '查询个人的征信记录', '20', '1', '1', '2018-09-28 22:40:17', '1', '2018-10-04 16:59:19', '', '0');
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容