有則更新無則插入(mySql,oracle)

來源:互聯網
上載者:User

標籤:str   into   orcale   uniq   河北   div   mysq   rom   traints   

有則更新無則插入(mySql,oracle)

2018-02-01

 

1 Orcale
create table table1(id varchar2(100) primary key,name varchar2(1000),address varchar2(1000));-- 執行兩次,會報  [Err] ORA-00001: unique constraint (PBOC.SYS_C0014610) violatedinsert into table1(id,name,address)values(‘01001‘,‘影子‘,‘河北‘) ;-- 查看constraintSELECT UC.OWNER,       UC.CONSTRAINT_NAME,       UC.CONSTRAINT_TYPE,       UC.TABLE_NAME,       UCC.COLUMN_NAME,       UC.SEARCH_CONDITION,       UC.R_CONSTRAINT_NAMEFROM USER_CONSTRAINTS UC  INNER JOIN USER_CONS_COLUMNS UCC    ON (UC.CONSTRAINT_NAME = UCC.CONSTRAINT_NAME) and UC.TABLE_NAME=‘TABLE1‘;-- merge有則更新,無責插入merge into table1 t1  using (select ‘01001‘ id,‘影子2‘ name,‘遼寧‘ address from dual) t2  on (t1.id = t2.id)  when matched then       update set t1.name = t2.name, t1.address = t2.address  when not matched then       insert values (t2.id, t2.name,t2.address);  select * from table1;drop table table1;

 

2 Sql
create table table1(id varchar(100) primary key,name varchar(1000),address varchar(1000));-- 執行兩次,會報  [Err] 1062 - Duplicate entry ‘01001‘ for key ‘PRIMARY‘insert into table1(id,name,address)values(‘01001‘,‘yingzi‘,‘hebei‘) ;-- 無責插入(返回:受影響的行: 1),有則更改(返回:受影響的行: 2)INSERT INTO  table1(id,name,address) VALUES (‘01001‘,‘yingzi2‘,‘hunan‘)  ON DUPLICATE KEY UPDATE name=‘yingzi2‘,address=‘hunan‘;select * from table1;drop table table1;

 

有則更新無則插入(mySql,oracle)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.