关于oracle数据库的一些典型操作记录

两表关联更新

参考:https://blog.csdn.net/zcouy/article/details/54313862
存在两张表TA,TB,两者都有id,name,rank字段;试图将TB中id与TA中id相同的数据,更新到TA中。
Oracle实现:

update TA a set(name,rank) values(select b.name,b.rank from tb b where b.id=a.id)
where exists(select 1 from TB b where b.id=a.id)

Note:
这里用了别名:update TA a;
如果不用exists语句,TA中关联不到对应id的name和rank将被更新为null;

With as用法

参考:https://www.cnblogs.com/mingforyou/p/8295239.html
举例:

with 
tmp1 as select * from tablea,
tmp2 as select * from tableb
select id from tmp1

Note:
相当于通过with as语句,建立了一个临时表,tmp1、tmp2;
with as短语,又叫做子查询部分,定义sql片段,以便在以后使用;
结构清晰,提高可读性;

exists语句

参考:http://yangzhonglei.iteye.com/blog/699673
exists()语句,返回值为true或false;当其中的sql子句查询结果为空时,返回false;不为空时,返回true;
举例:

select * from student where exists(select * from student where 1=1);
--exists子句返回true,所以上述语句返回student表中所有数据;
select * from student where exists(select * from student where 1=2);
--exists子句返回false,上述语句查询不到数据;

Note:
由EXISTS引出的子查询,其目标列表达式通常都用*,因为带EXISTS的子查询只返回真值或假值, 给出列名无实际意义;
相关子查询:

select a.name from temp a where exists(select * from b where b.depno=a.depno);
--上述语句等价于:
select a.name from temp a where a.depno in (select depno from b )

exists与in的区别

关于exists与in的一个总结:
in用于不相关子查询中,exists用于相关子查询中;
以下摘自:http://yangzhonglei.iteye.com/blog/699673
性能上的比较
比如

Select * from T1 where x in ( select y from T2 ) 

执行的过程相当于:

  select * from t1, ( select distinct y from t2 ) t2 
  where t1.x = t2.y; 

相对的

select * from t1 where exists ( select null from t2 where y = x ) 

执行的过程相当于:

  for x in ( select * from t1 ) 
     loop 
        if ( exists ( select null from t2 where y = x.x ) ) 
        then 
    OUTPUT THE RECORD 
        end if 
  end loop 

表 T1 不可避免的要被完全扫描一遍 (即对于t1中的每条数据,都要检测是否在t2表中存在对应列相等的数据)
分别适用在什么情况?
以子查询 ( select y from T2 )为考虑方向,如果子查询的结果集很大需要消耗很多时间,
但是T1比较小执行( select null from t2 where y = x.x )非常快,那么exists就比较适合用在这里。
相对应得子查询的结果集比较小的时候就应该使用in.
总结
提供给我一个以后优化sql语句的方向;多表查询时,应该会经常用到in和exists;可从以上角度考虑哪种更节省时间。
举例
有两张表,student(sid,sname,…)和register(id,sid,…);现要查找已注册了的学生,三种实现方式:

--第一种
--一个注意点是,……,inner join后面跟的不是where,是on,用于限定用哪一列将两表inner join起来;
select * from student inner join register on student.sid=register.sid
--第二种
select * from student where student.sid in (select sid from register)
--第三种
select * from student where exists(select * from register where student.sid=register.sid)

各有千秋!哈!
大佬是不可能成为大佬的,只能刷刷csdn,然后跟着学学这样。csdn的人又聪明写的笔记又好。

多表关联更新

一些我的脑子的坑:
更新用update,它的基本语法是:

update ta set ta.a1=value1 where ta.a2=value2

几种情况:
1)单表更新,语句同基本语法,不赘述;
2)两张表关联更新为固定值:

--更新student表,将student与newtable中相同的id,对应的的记录的性别,改为“女”;
update student a set a.sex="女" where exists(select * from newtable b where a.id=b.id)

3)关联更新数据来源于第二张表:

--更新表a中的价格为c中id相等的记录的价格
update a set a.price=(select price from b)where exist (select * from b where a.id=b.id)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值