MySQL更新或者插入SQL語句

2021-10-08 06:45:18 字數 513 閱讀 8556

對於下面這張表:

create table `aaa` (

`a` int(11) not null,

`b` int(11) default null,

`c` int(11) default null,

primary key (`a`)

) engine=innodb default charset=utf8;

執行兩次以下sql語句:

insert into `aaa` (a,b,c) values (1,2,3)

on duplicate key update c=5;

效果:

第一次執行(受影響行數1):執行一條正常的插入語句;

第二次執行(受影響行數2):執行一條插入語句,因為主鍵唯一插入失敗,執行修改語句令c=5。

官方介紹:

SQL更新部分字段或者插入新資料

sql更新部分字段或者插入新資料 因為業務的原因,一張表的資料需要整合兩張外來鍵關聯不強的表資料,這裡涉及到更新部分字段或者插入新資料。今天簡單的做個筆記,後續有需要再補充完整 測試表結構 create table test id int 10 not null,name varchar 255 c...

Mysql 增加主鍵或者修改主鍵的sql語句操作

alter table table1 add transactor varchar 10 not null alter table table1 add id int unsigned not null auto increment primary key alter table 表名稱 chang...

ORACLE 資料插入或者更新

在寫入資料的時候有時候需要根據資料庫中是否含有該條資料來判斷資料是插入還是更新,以下為oracle插入更新語法 單條資料錄入 select sys seq.nextval as id from dual merge into sys token a using select as id,as use...