面对大型的数据表格,当我们需要对数据进行转移,分类,写入,复制等操作的时候以前的单行查询或者简单的数据管理就不适用了,必须有一类对大型数据集合操作的方法,这次咱就说说这个。
(一)copying rows from another table
从其他数据表中拷贝一条记录
我们直接来看一个例子:
insert into sales_reps(id, name, salary, commission_pct)
select employee_id, last_name, salary, commission_pct
from employees
where job_id like '%rep%'
这个例子中的关键就是子查询,我们通过一条子查询来获得了employees表中的记录,然后按照相同的字段顺序插入sales_reps中去。
(二)通过重新定义数据源并使用子查询来取回数据。
你看这名字长的挺吓人的,其实很简单,看例子:
select a.last_name, a.salary, a.department_id, b.salavg
from employees a, (select department_id, avg(salary) salavg
from employees
group by department_id) b
where a.department_id = b.department_id
and a.salary > b.salavg
分析:这个查询实现的是显示工资大于此人分组平均工资的纪录。
首先我们要把employee表分成2个部分,一个部分用来显示正常信息,另一个用来显示每个分组的平均工资,然后将2个部分进行对比计算,输出分组内大于平均工资人的信息。
我们来看中间的那个子查询:
(select department_id, avg(salary) salavg
from employees
group by department_id) b
其实这个子查询就是一个重新定义数据源的过程,生成了一个表,包含department_id, avg(salary)
然后根据这个表的数据和a表中数据对比。得出结果。
LAST_NAME | SALARY | DEPARTMENT_ID | SALAVG |
---|
Hartstein | 13000 | 20 | 9500 |
Mourgos | 5800 | 50 | 3725 |
Hunold | 9000 | 60 | 6400 |
Zlotkey | 10500 | 80 | 8175 |
Abel | 11000 | 80 | 8175 |
Taylor | 8600 | 80 | 8175 |
King | 24000 | 90 | 19333.3333 |
Higgins | 12000 | 110 | 10150 |
(三)通过子查询实现多个字段的更新
看例子:要求--更新emp13中employe114相应的job_id,salary,使其值分别=employees表中employee205和168
update empl3
set job_id = (select job_id
from employees
where employee_id = 205),
salary = (select job_id
from employees
where employee_id = 168)
where employee_id = 114;
题干看不明白看看解决过程就会恍然大悟!
(四)基于其他表的数据记录更新
example:
update empl3
set department_id = (select department_id from employees where employee_id=100)
where job_id = (select job_id from employees where employee_id =200)
超级简单,不用说了。
(五)基于其他表格的数据删除
example:
delete from emp13
where department_id = (select department_id
from departments
where department_name like '%public%');
(六)默认值的插入
我们在定义表格的时候通常为一些字段定义了默认值,这里讲一下如何插入默认值
insert into deptm3
(department_id, department_name, manager_id)
values(300, 'engineering', default)
update deptm3
set manager_id = default
where department_id = 10;
(七)unconditional 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
我们可以看出,这个操作是从emplyees表中调出employee_id > 200的数据,插入sal_history和mgr_history 两个表中。
(八)有条件插入 conditional insert all
example:
insert all
when sal > 10000 then
into sal_history values (empid, hiredate, sal)
when mgr > 200 then
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 first
example:
insert first
when sal > 5000 then
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;
逻辑性很强,不再解释
(十)旋转插入
example:
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;
(十一)数据并和
example:
megre into empl3 c
using 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
.....
c.department_id = e.department_id
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_di);
这个例子是用来插入并更新empl3表中与employees相符合的信息的。