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

使用子查询处理数据

可以使用子查询中的数据操纵语言(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‘;

时间: 2024-12-21 12:13:49

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

SQL 基础之子查询(十一)

子查询:类型.语法.和注意事项 使用子查询能解决哪些问题? 子查询语法: select select_list from table where expr operator (select select_list from table); 子查询(内查询)在主查询(外查询)之前执行. 主查询使用子查询结果. 位置:select,where,from,having 1.查询谁的工资比Abel高 select last_name, salary from employees where salary

单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询

今日内容 表查询 单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询 单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) u

MySQL数据库 多表查询 交叉连接 自然连接 内连接 自连接 外连接 子查询 多表查询练习 单表查询练习 &#109806;

原文: http://blog.gqylpy.com/gqy/466 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

在SQL Server的子查询、视图、内联函数等数据库对象中,不应该单独使用ORDER BY语句

我们知道在SQL语句中,ORDER BY语句可以用来排序.但是在SQL Server中,如果我们在子查询.视图.内联函数等数据库对象中单独使用ORDER BY语句是不允许的,来看下面的SQL语句: SELECT * FROM ( SELECT [ID],[Code],[Name],[Age],[Sex],[Class] FROM [dbo].[Student] ORDER BY [ID] DESC ) AS T_Student 执行该语句,SQL Server会报错,错误信息如下: The OR

Orcla 数据库复习2 --子查询和表连接

子查询和表连接 ①.查询挣钱最多的人的名字 SELECT ename,sal FROM emp WHERE sal=(SELECT MAX(sal) FROM emp); ②.查询有哪些人的工资位于所有人的平均工资之上 SELECT ename,sal FROM emp WHERE sal > (SELECT AVG(sal) FROM emp); ③.求部门中哪些人的薪水最高: SELECT ename,sal FROM emp JOIN (SELECT MAX(sal) max_sal,de

SQL基础教程(第2版)第4章 数据更新:4-2 数据的删除(DELETE)

① DROP TABLE 语句可以将表完全删除② DELETE 语句会留下表(容器),而删除表中的全部数据 SQL基础教程(第2版)第4章 数据更新:4-2 数据的删除(DELETE) 原文地址:https://www.cnblogs.com/MarlonKang/p/12228542.html

SQL基础教程(第2版)第4章 数据更新:4-3 数据的更新(UPDATE)

● UPDATE语句可以将列的值更新为NULL.● 同时更新多列时,可以在UPDATE语句的SET子句中,使用逗号分隔更新对象的多个列. 指定条件的UPDATE语句(搜索型UPDATE) SET 子句中赋值表达式的右边不仅可以是单纯的值,还可以是包含列的表达式. 使用NULL进行更新 使用 UPDATE 也可以将列更新为 NULL(该更新俗称为 NULL 清空).此时只需要将赋值表达式右边的值直接写为 NULL 即可. 多列更新 SQL基础教程(第2版)第4章 数据更新:4-3 数据的更新(UP

SQL 基础之使用子查询检索数据(二十二)

多列子查询 where (manager_id, department_id) in 子查询 100 90 102 60 124 50 主查询的每行都与多行和多列的子查询进行比较 列的比较 多列的比较,包含子查询可以是: 不成对比较 成对比较 成对比较子查询1.显示与员工名为"John"同部门且同一个经理的其它员工信息 select employee_id, manager_id, department_id from empl_demo where (manager_id, depa

数据库基础(子查询、设置主键外键)

子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数: 另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的数据表. 子查询示例: 练习一: 练习二: 练习三: 分页查询语句示例: 查看总共可以分为多少页: 主键        数据库主键是指表中一个列或列的组合,其值