replace的作用是插入数据之前检查是否重复,重复的时候删除以后再插入
#测试表,并且有2个唯一键(id和code)
CREATE TABLE `t1115` (
`id` bigint NOT NULL AUTO_INCREMENT,
`code` varchar(100) NOT NULL,
`name` varchar(100) DEFAULT NULL,
`age` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `t1115_unique` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
#查询数据
select * from t1115;
#插入5条数据
insert into t1115(id,code,name,age) values(1,11,"name1","age1");
insert into t1115(id,code,name,age) values(2,12,"name2","age2");
insert into t1115(id,code,name,age) values(3,13,"name3","age3");
insert into t1115(id,code,name,age) values(4,14,"name4","age4");
insert into t1115(id,code,name,age) values(5,15,"name5","age5");
#执行以后会删除多个唯一键的数据,慎用
replace into t1115(id,code,name,age) values(1,12,"name6","age6");
另外据说再主从环境,replace 主键会增加,从会被翻译成update(这里指的是只有主键一个唯一键),导致自增的主键不一致,这时候如果从升级成主节点,会出现ID重复而执行失败的异常。所以避免使用replace 或者避免使用自增主键。
标签:code,name,删除,t1115,age,replace,into,机制,id From: https://www.cnblogs.com/cxygg/p/18548131