SQL 基础之子查询、多表插入、merge 语句、跟踪一段时间数据变化(二十)
2017-03-27 00:25
417 查看
使用子查询处理数据
可以使用子查询中的数据操纵语言(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 关键字,禁止子查询中行的更改。
显示的默认功能概述
使用 DEFAULT 关键字设置字段默认值。
允许用户控制什么时候使用默认值应用到数据
可以在INSERT和UPDATE语句中显式使用缺省值
使用显式的缺省值
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 语句的子查询:
insert into sales_reps(id, name, salary, commission_pct)
select employee_id, last_name, salary, commission_pct
from employees
where job_id like '%REP%';
不使用 VALUES 子句
INSERT 子句与子查询的列数、类型相匹配
使用以下类型完成多表插入:
– 无条件 INSERT
– 旋转 INSERT
– 有条件 INSERT ALL
– 有条件 INSERT FIRST
insert all
into target_a values(... , ... , ...)
into target_b values(... , ... , ...)
into target_c values(... , ... , ...)
select ...
from sourcetab
where ...;
多表查询示意图:
多表插入作用如下:
使用INSERT…SELECT语句插入行到多个表中,作为一个单一的DML语句。
数据仓库系统中使用的多表INSERT语句将一个或多个操作的源数据写入到一组目标表中。
它们提供显着的性能改善:
– 单个 DML 语句与多表 INSERT…SELECT 语句
– 单个 DML 语句与使用 IF...THEN 语法完成多表插入
多表INSERT 语句的类型
以下是不同类型的多表 INSERT 语句:
无条件 INSERT
旋转 INSERT
有条件 INSERT ALL
有条件 INSERT FIRST
多表 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 :示例
有条件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
有条件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
将销售记录从非关系型数据库表中设置为关系格式
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;
限制条件
只能对表执行多表插入语句,不能对视图或物化视图执行;
不能对远端表执行多表插入语句;
不能使用表集合表达式;
不能超过999个目标列;
在RAC环境中或目标表是索引组织表或目标表上有BITMAP索引时,多表插入语句不能并行执行;
多表插入语句不支持执行计划稳定性;
多表插入语句中的子查询不能使用序列。
MERGE 语句
提供根据条件进行更新、插入、删除数据的功能
如果数据存在执行UPDATE,如果不存在则INSERT:
– 避免单独更新
– 提高了性能和易用性
– 非常适用于数据仓库
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';
可以使用子查询中的数据操纵语言(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 关键字,禁止子查询中行的更改。
显示的默认功能概述
使用 DEFAULT 关键字设置字段默认值。
允许用户控制什么时候使用默认值应用到数据
可以在INSERT和UPDATE语句中显式使用缺省值
使用显式的缺省值
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 语句的子查询:
insert into sales_reps(id, name, salary, commission_pct)
select employee_id, last_name, salary, commission_pct
from employees
where job_id like '%REP%';
不使用 VALUES 子句
INSERT 子句与子查询的列数、类型相匹配
使用以下类型完成多表插入:
– 无条件 INSERT
– 旋转 INSERT
– 有条件 INSERT ALL
– 有条件 INSERT FIRST
insert all
into target_a values(... , ... , ...)
into target_b values(... , ... , ...)
into target_c values(... , ... , ...)
select ...
from sourcetab
where ...;
多表查询示意图:
多表插入作用如下:
使用INSERT…SELECT语句插入行到多个表中,作为一个单一的DML语句。
数据仓库系统中使用的多表INSERT语句将一个或多个操作的源数据写入到一组目标表中。
它们提供显着的性能改善:
– 单个 DML 语句与多表 INSERT…SELECT 语句
– 单个 DML 语句与使用 IF...THEN 语法完成多表插入
多表INSERT 语句的类型
以下是不同类型的多表 INSERT 语句:
无条件 INSERT
旋转 INSERT
有条件 INSERT ALL
有条件 INSERT FIRST
多表 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 :示例
有条件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
有条件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
将销售记录从非关系型数据库表中设置为关系格式
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;
限制条件
只能对表执行多表插入语句,不能对视图或物化视图执行;
不能对远端表执行多表插入语句;
不能使用表集合表达式;
不能超过999个目标列;
在RAC环境中或目标表是索引组织表或目标表上有BITMAP索引时,多表插入语句不能并行执行;
多表插入语句不支持执行计划稳定性;
多表插入语句中的子查询不能使用序列。
MERGE 语句
提供根据条件进行更新、插入、删除数据的功能
如果数据存在执行UPDATE,如果不存在则INSERT:
– 避免单独更新
– 提高了性能和易用性
– 非常适用于数据仓库
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';
相关文章推荐
- SQL语句实现查询一段时间内的数据中要注意的细节问题
- sql语句实现从一个表的查询出某些数据插入另外一个表中
- s2sh向数据库插入数据,控制台有打印sql语句,数据库查询不到数据问题
- 将Sql查询语句获取的数据插入到List列表里面
- sql语句 怎么从一张表中查询数据插入到另一张表中?
- s2sh向数据库插入数据,控制台有打印sql语句,数据库查询不到数据问题
- SQL语句:orac 3ff0 le中如何插入Date类型的数据和根据Date数据进行查询的方法
- Linux下安装的mysql数据库插入中文乱码或sql查询语句条件中有中文查不到数据
- android中常用的查询、插入、更新、删除等SQL语句以及SQLite数据类型
- Mysql数据库理论基础之三 --- 数据类型及SQL结构化查询语句使用
- SQL查询语句的书写要点(基础三、子句)(未完成,先不要看,没有实质内容)
- SQL语句实现跨Sql server数据库操作实例 - 查询远程SQL,本地SQL数据库与远程SQL的数据传递
- SQL基础(四)数据查询
- ADO带密码的数据连接、查询一个记录集、执行一SQL语句
- SQL SERVER2000教程-第五章 处理数据 第二十二节 利用UNION对多条SQL查询语句合并生成表
- 用SQL语句实现随机查询数据并不显示错误数据
- 不用SQL语句查询DataTable中的数据【原创】
- ASP.NET基础教程-使用CommandBuilder对象自动生成SQL语句对数据进行批量更新
- SQL中查询数据表字段名称的查询语句
- 现在有这样一个问题 我把上W条数据插入表内,但是发现还有一条数据没进入表里.我想查出来是哪个数据没进去. 能用查询语句查出来吗?