mysql,sqlserver,oracle各自的存在更新不存在添加写法

发布时间 2023-10-23 12:48:05作者: sunny123456

mysql,sqlserver,oracle各自的存在更新不存在添加写法

在向表中插入数据的时候,经常遇到这样的情况:

  1. 首先判断数据是否存在;
  2. 如果不存在,则插入:
  3. 如果存在,则更新。

SQL server

脚本先查询,没有数据再进行数据插入,有数据就走更新

  1. if not exists (select 1 from t where id = 1)
  2. insert into t(id, update_time) values(1, getdate())
  3. else
  4. update t set update_time = getdate() where id = 1
  5. 或者
  6. if exists (select 1 from t where id = 1)
  7. insert into t(id, update_time) values(1, getdate())
  8. else
  9. update t set update_time = getdate() where id = 1

mysql

replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中。

  1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。
  2. 否则,直接插入新数据。
    Ps:要注意的是:插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据。
  1. ###插入或替换 -- 没有就插入,有就先删除再插入
  2. REPLACE INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99);
  3. 若id=1的记录不存在,REPLACE语句将插入新记录,否则,当前id=1的记录将被删除,然后再插入新记录。
  4. ###插入或更新 -- 如果没有数据就行新增,有数据就更新处理
  5. INSERT INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99) ON DUPLICATE KEY UPDATE name='小明', gender='F', score=99;
  6. ###插入或忽略 -- 如果已有id为1的数据本次数据就不会再插入,会忽略本次sql操作
  7. INSERT IGNORE INTO students (id, class_id, name, gender, score) VALUES (1, 1, '小明', 'F', 99);

oracle

脚本先查询,如果有数据表先进行删除操作,然后在进行新增表操作。
同样的,如果是数据,会先查询数据,如果有就进行删除,删除后再进行插入,如果没有就直接进行插入

  1. declare num number;
  2. begin
  3. select count(1) into num from user_tables where table_name='ACCOUNT';
  4. if num > 0 then
  5. dbms_output.put_line('存在!');
  6. execute immediate 'drop table ACCOUNT ';
  7. end if;
  8. execute immediate 'create table Account
  9. (
  10. AccountID nvarchar2(50) primary key,
  11. AccountName nvarchar2(50)
  12. )';
  13. dbms_output.put_line('成功创建表!');
  14. end;

1:隐式游标法 SQL%NOTFOUND SQL%FOUND
SQL%NOTFOUND 是SQL中的一个隐式游标,在增删查改的时候自动打开,如果有至少有一条记录受影响,都会返回false,这就就巧妙的构思出了第一种解决方案:

  1. begin
  2. update account set AccountName = '修改-a' where AccountID = '5';
  3. IF SQL%NOTFOUND THEN
  4. insert into account(AccountID,AccountName) values('5','添加-b');
  5. END IF;
  6. end;
  7. 先根据唯一ID到数据表中修改一条记录,如果这条记录在表中存在,则修改,并且SQL%NOTFOUND返回false。如果修改的记录不存在,SQL%NOTFOUND返回true,并且执行插入语句。

2:异常法 DUP_VAL_ON_INDEX
当Oracle语句执行时,发生了异常exception进行处理,需要唯一索引id,重复插入出现异常

  1. begin
  2. insert into account(AccountID,AccountName) values('6','添加-b');
  3. exception
  4. when DUP_VAL_ON_INDEX then
  5. begin
  6. update account set AccountName = '修改-b' where AccountID = '6';
  7. end;
  8. end;

3:虚拟表法 dual:
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录。

  1. declare t_count number;
  2. begin
  3. select count(*) into t_count from dual where exists(select 1 from account where AccountID='11');
  4. if t_count< 1 then
  5. dbms_output.put_line('添加');
  6. insert into account(AccountID,AccountName) values('11','添加-11');
  7. else
  8. dbms_output.put_line('修改');
  9. update account set AccountName = '修改-11' where AccountID = '11';
  10. end if;
  11. end;

4:no_data_found法
先查找要插入的记录是否存在,存在则修改,不存在则插入。具体的实现如下:

  1. declare t_cols number;
  2. begin
  3. select AccountName into t_cols from account where AccountID = '8';
  4. exception
  5. when no_data_found then begin
  6. --dbms_output.put_line('添加');
  7. insert into account(AccountID,AccountName) values('8','添加-8');
  8. end;
  9. when others then
  10. begin
  11. --dbms_output.put_line('修改');
  12. update account set AccountName = '修改-8' where AccountID = '8';
  13. end;
  14. end;

5:merge法
先来看一下merge的语法,

  1. MERGE INTO table_name alias1
  2. USING (table|view|sub_query) alias2
  3. ON (join condition)
  4. WHEN MATCHED THEN
  5. UPDATE table_name SET col1 = col_val1
  6. WHEN NOT MATCHED THEN
  7. INSERT (column_list) VALUES (column_values);
  8. 模仿
  9. merge into Account t1
  10. using (select '3' AccountID,'肖文博' AccountName from dual) t2
  11. on (t1.AccountID = t2.AccountID)
  12. when matched then
  13. update set t1.AccountName = t2.AccountName
  14. when not matched then
  15. insert values (t2.AccountID, t2.AccountName);
  16. commit;
原文链接:https://blog.csdn.net/wwh1st/article/details/129851614