MySQL裡面的Replace 的用法和insert .. update 的用法
drop table if exists t;
create table t(id int not null auto_increment primary key, name varchar(100));
create unique index idx_t on t(name);
replace into t (name) values('first'); -- 數據的id=1
select * from t;
replace into t (name) values('first'); -- 數據的id變成了2
select * from t;
insert into t(name) values('second'); -- 數據的id為3
select * from t;
insert into t(name) values('second') on duplicate key update name='second2'; -- 依然為3,但內容變了
select * from t;
MySQL裡面的Replace 的用法和insert .. update 的用法