SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)

发布时间:2020-06-23 18:59:05 作者:yuri_cto
来源:网络 阅读:2733

使用子查询处理数据

可以使用子查询中的数据操纵语言(DML)语句:

使用内嵌视图检索数据

从一张表向另一张表复制数据

基于另一张表的值更新表中数据

基于另一张表的值删除表中的行


使用子查询作为数据源检索数据

select department_name, city from departments

natural join (select l.location_id, l.city, l.country_id

from loc l

join countries c

on(l.country_id = c.country_id)

join regions using(region_id) where region_name = 'europe');


使用子查询作为目标插入数据

insert into (select l.location_id, l.city, l.country_id from locations l

join countries c

on(l.country_id = c.country_id)

join regions using(region_id)

where region_name = 'europe')

values (3300, 'Cardiff', 'UK');


在 DML  语句中使用WITH CHECK OPTION 

WITH CHECK OPTION 关键字,禁止子查询中行的更改。


显示的默认功能概述


使用显式的缺省值

INSERT 与 DEFAULT:


insert into deptm3 (department_id, department_name, manager_id) values (300, 'engineering', default);


UPDATE 与 DEFAULT:

update deptm3 set manager_id = default where department_id = 10;


从另一张表中复制行

insert into sales_reps(id, name, salary, commission_pct)

select employee_id, last_name, salary, commission_pct

from employees

where job_id like '%REP%';


使用以下类型完成多表插入:

– 无条件 INSERT

– 旋转 INSERT

– 有条件 INSERT ALL

– 有条件 INSERT FIRST


insert all

into target_a values(... , ... , ...)

into target_b values(... , ... , ...)

into target_c values(... , ... , ...)

select ...

from sourcetab

where ...;


多表查询示意图:

SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)

多表插入作用如下:

      – 单个 DML 语句与多表 INSERT…SELECT 语句

      – 单个 DML 语句与使用 IF...THEN 语法完成多表插入


多表INSERT 语句的类型

以下是不同类型的多表 INSERT 语句:


多表 INSERT 语法

insert [conditional_insert_clause]

[insert_into_clause values_clause] (subquery)


有条件 INSERT 子句:

[ALL|FIRST]

[WHEN condition THEN] [insert_into_clause values_clause]

[ELSE] [insert_into_clause values_clause]


无条件 INSERT ALL


insert all

into sal_history values(empid,hiredate,sal)

into mgr_history values(empid,mgr,sal)

select employee_id empid, hire_date hiredate,

salary sal, manager_id mgr

from employees

where employee_id > 200;


有条件INSERT ALL :示例

SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)

有条件INSERT ALL

insert all

when hiredate <  ' 01-JAN-95 ' then

into emp_history values(EMPID,HIREDATE,SAL)

when comm is not null then

into emp_sales values(EMPID,COMM,SAL)

select employee_id empid, hire_date hiredate,

salary sal, commission_pct comm

from employees

SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)

有条件INSERT FIRST

insert first

when salary < 5000 then

into sal_low values (employee_id, last_name, salary)

when salary between 5000 and 10000 then

into sal_mid values (employee_id, last_name, salary)

else

into sal_high values (employee_id, last_name, salary)

select employee_id, last_name, salary

from employees


旋转INSERT

将销售记录从非关系型数据库表中设置为关系格式

SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)


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;


限制条件


MERGE 语句

      – 避免单独更新

      – 提高了性能和易用性

      – 非常适用于数据仓库


MERGE  语句语法

使用MERGE语句,您可以根据条件插入,更新或删除表中的行

merge into table_name table_alias

using (table|view|sub_query) alias

on (join condition)

when matched then

update set

col1 = col1_val,

col2 = col2_val

when not matched then

insert (column_list)

values (column_values);


合并行:示例

插入或更新COPY_EMP3表中与EMPLOYEES相匹配的行。

merge into copy_emp3 c

using (select * from employees ) e

on (c.employee_id = e.employee_id)

when matched then

update set

c.first_name = e.first_name,

c.last_name = e.last_name,

...

delete where (e.commission_pct is not null)

when not matched then

insert values(e.employee_id, e.first_name, e.last_name,

e.email, e.phone_number, e.hire_date, e.job_id,

e.salary, e.commission_pct, e.manager_id,

e.department_id);


合并行 示例

truncate table copy_emp3;

select * from copy_emp3;


merge into copy_emp3 c

using (select * from employees ) e

on (c.employee_id = e.employee_id)

when matched then

update set

c.first_name = e.first_name,

c.last_name = e.last_name,

...

delete where (e.commission_pct is not null)

when not matched then

insert values(e.employee_id, e.first_name, ...


跟踪数据的变化

闪回版本查询示例

select salary from employees3 where employee_id = 107;

update employees3 set salary = salary * 1.30 where employee_id = 107;

commit;

select salary from employees3 versions between scn minvalue and maxvalue where employee_id = 107;


VERSIONS BETWEEN 子句

select versions_starttime "start_date",

versions_endtime "end_date",

salary

from employees

versions between scn minvalue

and maxvalue

where last_name = 'Lorentz';

推荐阅读:
  1. 《起 航 之SQL技能全掌握》下
  2. SQL Server数据库的T-SQL高级查询

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql 多表插入 基础之子

上一篇:java中JUnit的使用方法

下一篇:CentOS 5 CentOS 6 启动流程及关键步骤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》