zoukankan      html  css  js  c++  java
  • Oracle Sql技巧 Upsert, Multitable Insert, Undrop

    近期参加OCP培训,讲师说的太快,之前一直是SQLSERVER,很多东西要恶补了。

    UpSert功能:

    MERGE <hint> INTO <table_name>
    USING <table_view_or_query>
    ON (<condition>)
    WHEN MATCHED THEN <update_clause>
    WHEN NOT MATCHED THEN <insert_clause>;

    multiTable Inserts功能:

    Multitable inserts allow a single INSERT INTO .. SELECT statement to conditionally, or non-conditionally, insert into multiple tables. This
    statement reduces table scans and PL/SQL code necessary for performing multiple conditional inserts compared to previous versions. It's
    main use is for the ETL process in data warehouses where it can be parallelized and/or convert non-relational data into a relational format:

    -- Unconditional insert into ALL tables

    INSERT ALL
    INTO sal_history VALUES(empid,hiredate,sal)
    INTO mgr_history VALUES(empid,mgr,sysdate) 
    SELECT employee_id EMPID, hire_date HIREDATE, salary SAL, manager_id MGR
    FROM employees WHERE employee_id > 200;

    -- Pivoting insert to split non-relational data

    INSERT ALL
    INTO Sales_info VALUES (employee_id,week_id,sales_MON)
    INTO Sales_info VALUES (employee_id,week_id,sales_TUE)
    INTO Sales_info VALUES (employee_id,week_id,sales_WED)
    INTO Sales_info VALUES (employee_id,week_id,sales_THUR)
    INTO Sales_info VALUES (employee_id,week_id, sales_FRI)
    SELECT EMPLOYEE_ID, week_id, sales_MON, sales_TUE,
    sales_WED, sales_THUR,sales_FRI 
    FROM Sales_source_data;

    -- Conditionally insert into ALL tables

    INSERT ALL
    WHEN SAL>10000 THEN
    INTO sal_history VALUES(EMPID,HIREDATE,SAL)
    WHEN MGR>200 THEN
    INTO mgr_history VALUES(EMPID,MGR,SYSDATE)
    SELECT employee_id EMPID, hire_date HIREDATE, salary SAL, manager_id MGR
    FROM employees WHERE employee_id > 200;

    -- Insert into the FIRST table with a matching condition

    INSERT FIRST
    WHEN SAL > 25000THEN
    INTO special_sal VALUES(DEPTID,SAL)
    WHEN HIREDATE like ('%00%') THEN
    INTO hiredate_history_00 VALUES(DEPTID,HIREDATE)
    WHEN HIREDATE like ('%99%') THEN 
    INTO hiredate_history_99 VALUES(DEPTID,HIREDATE)
    ELSE
    INTO hiredate_history VALUES(DEPTID, HIREDATE)
    SELECT department_id DEPTID, SUM(salary) SAL,
    MAX(hire_date) HIREDATE
    FROM employees GROUP BY department_id;

    The restrictions on multitable INSERTs are:

    Multitable inserts can only be performed on tables, not on views or materialized views.
    You cannot perform a multitable insert via a DB link.
    You cannot perform multitable inserts into nested tables.
    The sum of all the INTO columns cannot exceed 999.
    Sequences cannot be used in the subquery of the multitable insert statement.


    Undrop功能

    From Oracle 10g a table can be "undropped". Example:

    SQL> FLASHBACK TABLE emp TO BEFORE DROP;

    Flashback complete.

  • 相关阅读:
    向Oracle 数据表中插入一条带有日期类型的数据
    JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库
    球球大作战四亿人都在玩?玩家回归没有优越感,新玩家游戏被虐,游戏体验感极差!
    struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
    如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?
    idea中 在接口中如何直接跳转到该接口的是实现类中?
    使用IDEA 创建Servlet 的时候,找不到javax.servlet
    如何高效的遍历HashMap 以及对key 进行排序
    springboot 自动装配
    git 多账户添加ssh秘钥
  • 原文地址:https://www.cnblogs.com/iImax/p/2678871.html
Copyright © 2011-2022 走看看