create table testtb( id int not null primary key, name varchar(50), age int ); insert into testtb(id,name,age)values(1,"bb",13); select * from testtb; insert ignore into testtb(id,name,age)values(1,"aa",13); select * from testtb;//仍是1,“bb”,13,因?yàn)閕d是主鍵,出現(xiàn)主鍵重復(fù)但使用了ignore則錯(cuò)誤被忽略 replace into testtb(id,name,age)values(1,"aa",12); select * from testtb; //數(shù)據(jù)變?yōu)?,"aa",12 總結(jié)一下:如果要實(shí)現(xiàn)插入數(shù)據(jù)時(shí)檢查是否已經(jīng)存在某個(gè)唯一鍵的數(shù)據(jù),如果存在,則替換該記錄的其他字段,我們可以使用三種方法來(lái)實(shí)現(xiàn)插入數(shù)據(jù)時(shí)判斷是否存在對(duì)應(yīng)鍵的記錄,分別是INSERT ... ON DUPLICATE KEY UPDATE、insert gnore into和replace into,其中INSERT ... ON DUPLICATE KEY UPDATE和replace into可以實(shí)現(xiàn)如果已經(jīng)存在對(duì)應(yīng)鍵的記錄時(shí),替換該記錄的其他字段.