zoukankan      html  css  js  c++  java
  • oracle 更新删除

    创建表test1,test2。

    create  table  test1
    (
        FID  NUMBER(6) NOT NULL,
        FBillNo VARCHAR2(10),
        FDate  DATE  DEFAULT to_date(to_char(SYSDATE,'yyyy-mm-dd'),'yyyy-mm-dd')
    )
    create sequence seq_test1
           minvalue 1  --最小值
           nomaxvalue --最大值
           start with 1 --起始值
           increment by 1  --增长基数
           nocycle  --不循环,一直增加
           nocache ;
    
    create trigger tri_test1_ins 
           before insert on test1 for each row  when (new.FID is null)
        begin 
          select seq_test1.nextval into:new.FID from dual;
        end;
        
        insert  into  test1(FBillNo ,  FDate)  values ('FB001' ,to_date('2018-6-1','yyyy-mm-dd'));
    insert  into  test1(FBillNo ,  FDate)  values ('FB002' ,  to_date('2018-6-3','yyyy-mm-dd'));
    insert  into  test1(FBillNo ,  FDate)  values ('FB003' ,  to_date('2018-2-1','yyyy-mm-dd'));
    insert  into  test1(FBillNo ,  FDate)  values ('FB003' ,  to_date('2018-2-3','yyyy-mm-dd'));
    
    DROP TABLE test2
    create  table  test4
    (
        FID  NUMBER(6),
        FBillNo  VARCHAR2(10),
        FDate  DATE DEFAULT to_date(to_char(SYSDATE,'yyyy-mm-dd'),'yyyy-mm-dd')
    )
    create sequence seq_test4
           minvalue 1  --最小值
           nomaxvalue --最大值
           start with 1 --起始值
           increment by 1  --增长基数
           nocycle  --不循环,一直增加
           nocache ;
    
    create trigger tri_test4_ins 
           before insert on test1 for each row  when (new.FID is null)
        begin 
          select seq_test4.nextval into:new.FID from dual;
        end;
        
        insert  into  test4(FBillNo ,  FDate)  values ('FB2018001' ,  to_date('2018-6-1','yyyy-mm-dd'));
      insert  into  test4(FBillNo ,  FDate)  values ('FB2018002' ,  to_date('2018-6-3','yyyy-mm-dd'));
      insert  into  test4(FBillNo ,  FDate)  values ('FB003' ,  to_date('2018-2-1','yyyy-mm-dd'));
      insert  into  test4(FBillNo ,  FDate)  values ('FB004' ,  to_date('2018-2-3','yyyy-mm-dd'));
    
    update  test1  set  FBillNo='FB004'  where  FDate=to_date('2018-2-3','yyyy-mm-dd')
    
    
    update  test1 t1 set  t1.FBillNo=(SELECT t2.FBillNo
    FROM  t1
    left  join  test4  t2  on  FID=t2.FID 
    where  t2.FDate>=to_date('2018-6-1','yyyy-mm-dd'))
    
    SELECT * FROM
    test1
    
    truncate  table  test4
    SELECT * FROM
    test4

    2.delete和truncate区别

    区别1**:**DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。在删除过程中激活与表有关的删除触发器。

     

    TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。

    区别2**:**表和索引所占空间。当表被TRUNCATE 后,这个表和索引所占用的空间会恢复到初始大小,而DELETE操作不会减少表或索引所占用的空间。drop语句将表所占用的空间全释放掉。

    区别3**:**应用范围。TRUNCATE 只能对TABLE;DELETE可以是table和view。

    区别4**:**对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。

    区别5**:**DELETE自动编号不恢复到初始值。TRUNCATE自动编号恢复到初始值。

  • 相关阅读:
    摆花
    关于我的博客
    博客美化更新日志
    页面美化代码1.x
    本人已转至新博客!
    回归博客园
    退役快乐
    Luogu神贴合辑
    代码高亮预览
    NOIp2018普及组初赛解题报告
  • 原文地址:https://www.cnblogs.com/chenzhelove/p/13481804.html
Copyright © 2011-2022 走看看