oracle 笔记 II 之DML:数据操作语言

 DML:Data Manipulation Language 数据操作语言
包括:CRUD


1. insert语句
(1) 从其它表中复制数据,实现方法:在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';

(2) update中使用子查询
 update employees set job_id =  (select job_id from employees where employee_id = 205),
                               salary =  (select salary from employees where employee_id = 205)
           where employee_id = 114;


   如:更新114号员工的工作和工资使其与205号员工相同
   update employees set job_id =  (select job_id from employees where employee_id = 205),
      salary = (select salary from employees where employee_id = 205)
      where employee_id = 114


   再看一个问题,仔细体会解决步骤:
    更改 108 员工的信息: 使其 工资变为所在部门中的最高工资,job变为公司中平均工资最低的job
     分析:
 第1 步 首先查询 108 所在部门的最高工资是多少
    select max(salary) from employees where department_id =
  ( select department_id from employees where employee_id = 108)
 第 2 步 查询公司中平均工资最低的 job_id
     select job_id from employees group by job_id having avg(salary) =
     (select min(avg_sal) from(select avg(salary) avg_sal from employees  group by job_id) )
 第 3 步 实现更新
          update employees set salary = (  select max(salary) from employees 
             where department_id = (select department_id from employees where employee_id = 108),
             job_id = ( select job_id from employees group by job_id
             having avg(salary) =(select min(avg_sal) from(select avg(salary)avg_sql from employees
             group by job_id ) ) )
   where job_id = 108  

 

(3)在delete 中使用子查询
  eg: 删除 108 号员工所在部门的工资最低的员工
     delete from employees where salary =(select min(salary) from employees where
  department_id = (select department_id from employees where employee_id = 108 ) )
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值