ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 如果不限制条件,NULL值会造成表更新过程中数据的丢失!

如果不限制条件,NULL值会造成表更新过程中数据的丢失!

原创 Linux操作系统 作者:thompsun 时间:2010-05-23 23:51:21 0 删除 编辑

示例如下:

SQL> create table mytest1(id number,name varchar2(20));

表已创建。

SQL> insert into mytest1 select 1,'aaa' from dual;

已创建 1 行。

SQL> insert into mytest1 select 2,'bbb' from dual;

已创建 1 行。

SQL> insert into mytest1 select null,'ccc' from dual;

已创建 1 行。

SQL> commit;

提交完成。

SQL> create table mytest2(id number,name varchar2(20));

表已创建。

SQL> insert into mytest2 select 1,'aa' from dual;

已创建 1 行。

SQL> insert into mytest2 select 2,'bb' from dual;

已创建 1 行。

SQL> insert into mytest2 select null,'cc' from dual;

已创建 1 行。

SQL> commit;

提交完成。

SQL> select * from mytest1;

        ID NAME
---------- --------------------
         1 aaa
         2 bbb
           ccc

SQL> select * from mytest2;

        ID NAME
---------- --------------------
         1 aa
         2 bb
           cc

SQL> update mytest1 a set a.name=(select b.name from mytest2 b where b.id=a.id);

已更新3行。

SQL> select * from mytest1;

        ID NAME
---------- --------------------
         1 aa
         2 bb


//这里将ID号为null的值丢失了.

SQL> rollback;

回退已完成。

//以下为三种相同结果的更新语句.

SQL> update mytest1 a set a.name=(select b.name from mytest2 b where a.id=b.id)
  2  where exists(select 1 from mytest2 c where a.id=c.id);

已更新2行。

SQL> select * from mytest1;

        ID NAME
---------- --------------------
         1 aa
         2 bb
           ccc

SQL> rollback;

回退已完成。

SQL> update mytest1 a set a.name=(select b.name from mytest2 b where b.id=a.id)
  2  where a.id=(select c.id from mytest2 c where c.id=a.id);

已更新2行。

SQL> select * from mytest1;

        ID NAME
---------- --------------------
         1 aa
         2 bb
           ccc

SQL> rollback;

回退已完成。

SQL> update mytest1 a set a.name=(select b.name from mytest2 b where b.id=a.id)
  2  where a.id is not null;

已更新2行。

SQL> select * from mytest1;

        ID NAME
---------- --------------------
         1 aa
         2 bb
           ccc

SQL> rollback;

回退已完成。

//如果要更新的数据存在空值,须要在更新过程中加以限制,否则会丢失数据.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11134237/viewspace-663498/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: oracle与防火墙
请登录后发表评论 登录
全部评论

注册时间:2009-01-11

  • 博文量
    96
  • 访问量
    276880