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,因為id是主鍵,出現主鍵重復但使用了ignore則錯誤被忽略 replace into testtb(id,name,age)values(1,"aa",12); select * from testtb; //數據變為1,"aa",12 總結一下:如果要實現插入數據時檢查是否已經存在某個唯一鍵的數據,如果存在,則替換該記錄的其他字段,我們可以使用三種方法來實現插入數據時判斷是否存在對應鍵的記錄,分別是INSERT ... ON DUPLICATE KEY UPDATE、insert gnore into和replace into,其中INSERT ... ON DUPLICATE KEY UPDATE和replace into可以實現如果已經存在對應鍵的記錄時,替換該記錄的其他字段.