zoukankan      html  css  js  c++  java
  • DB2基础学习2

    1、增删改查:select insert delete update merge

     Insert操作

    1. INSERT INTO USER (NAME,BIRTHDAY) VALUES ('张三','2000-1-1'); 
    2. INSERT INTO USER (NAME,BIRTHDAY) VALUES  
    3. ('张三','2000-1-1'),  
    4. ('李四','2000-1-1'),  
    5. ('王五','2000-1-1');
    6. MERGE INTO EMPLOYE AS EM  
    7. USING MANAGER AS MA  
    8. ON EM.EMPLOYEID=MA.MANAGERID  
    9. WHEN MATCHED AND EM.SALARY<MA.SALARY THEN UPDATE SET EM.SALARY=MA.SALARY  
    10. WHEN MATCHED AND EM.SALARY>MA.SALARY THEN DELETE  
    11. WHEN NOT MATCHED THEN INSERT VALUES (MA.MANAGERID,MA.NAME,MA.SALARY)  
    12. ELSE IGNORE;  

       Delete操作   

    DELETE FROM <TABLE_NAME> WHERE <CONDITION>; 

    1. DELETE FROM  
    2. (  
    3. SELECT * FROM <TABLE_NAME> WHERE <CONDITION>  
    4. ); 
    5. ALTER TABLE <TABLE_NAME> ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE  //对表的操作将不会记录日志,恢复的时候可能有问题,所以特别适合临时表

    2、表关联:inner/left/right/full join

    tb_a a join tb_b b on condition_1 where condition_2

     // condition_1条件,b表的过滤条件,以及a和b表的关联条件; condition_2条件是对a表的限制条件。

    3、索引index

    l  create unique index indname ON tabname(colname) ;

    l  alter table SMY.DEP_AR_SMY_DY add constraint SQL161117093932110                                       

           primary Key (ACG_DT, DEP_AR_ID, CSH_X_RMIT_IND_TP_ID, CCY_ID); 

    4、分区键partition key

    Partitioning Key (ACG_DT) Using Hashing       

    5、临时表session

    1)  declare global temporary table session.tb_name(uid integer)

    ON COMMIT DELETE ROWS;  

    2) declare global temporary table session.tb_name LIKE tb_name2

    including columns defaults

     with replace

     on commit preserve rows;

    3)  declare global temporary table session.tb_name as

    (select * from tb_name2 where condition)

    definition only

    with replace;

    在使用DB2的临时表时, 以下几点需要注意: 

    1) DB2的临时表需要用命令Declare Temporary Table来创建, 并且需要创建在用户临时表空间上
    2) DB2在数据库创建时, 缺省并不创建用户临时表空间, 如果需要使用临时表, 则需要用户在创建临时表之前创建用户临时表空间; 
    3) 临时表的模式为SESSION;  
    4) 缺省情况下, 在Commit命令执行时, 临时表中的所有记录将被删除; 这可以通过创建临时表时指定不同的参数来控制; 
    5) 运行ROLLBACK命令时, 用户临时表将被删除; 
    6) 在DB2版本8中, 可以对临时表纪录日志; 

    6、有用的SQL

    1. select (case when amo>=50000 then '5万及以上' else '5万及以下'),
    2. count(1) from tb_name
    3. group by (case when amo>=50000 then '5万及以上' else '5万及以下')

    排序函数 rownumber() 与  row_number() over

    l  在分页的场合下,我们首推row_number()over()函数

    l  row_number() over(partition by col1 order by col2)

    l  在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where group by  order by 的执行。

    l  rank() over()是跳跃排序,有两个第二名时接下来就是第四名.

    l  dense_rank() over()是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的 .

    聚合函数listagg()

    1. select uid, listagg(usr,',') within group(order by udt)
    2. from tb_name group by uid;

    7、runstats、reorg和reorgchk命令

    1)runstats

      runsats可以搜集表的信息,也可以搜集索引信息。作为runstats本身没有优化的功能,但是它更新了统计信息以后,可以让DB2优化器使用最新的统计信息来进行优化,这样优化的效果更好。
      runstats   on   table   <tbschema>.<tbname>   收集表   <tbname>   的统计信息。表名必须是用  <dbschema>   全限定的。

    2)reorg

     A、 reorg   table   <tbschema>.<tablename>   通过重构行来消除“碎片”数据并压缩信息,对表进行重组。表名必须是用   <dbschema>   全限定的。

    B、reorg还有一个功能就是可以将表中的数据按照某个索引关键字的顺序排列,从而可以减少某些查询I/O的数量。
     3)reorgchk

  • 相关阅读:
    LeetCode 1122. Relative Sort Array (数组的相对排序)
    LeetCode 46. Permutations (全排列)
    LeetCode 47. Permutations II (全排列 II)
    LeetCode 77. Combinations (组合)
    LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)
    LeetCode 922. Sort Array By Parity II (按奇偶排序数组 II)
    LeetCode 1219. Path with Maximum Gold (黄金矿工)
    LeetCode 1029. Two City Scheduling (两地调度)
    LeetCode 392. Is Subsequence (判断子序列)
    写程序判断系统是大端序还是小端序
  • 原文地址:https://www.cnblogs.com/llphhl/p/9558965.html
Copyright © 2011-2022 走看看