oracle多表关联更新(update)/删除(delete)数据表的的写法
2017-04-08 09:13
441 查看
1) 两表(多表)关联update --
仅在where字句中的连接
SQL 代码
--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
update customers a -- 使用别名
set customer_type='01' --01
为vip,00为普通
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
2) 两表(多表)关联update --
被修改值由另一个表运算而来
SQL 代码
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city
b where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
-- update 超过2个值
update customers a -- 使用别名
set (city_name,customer_type)=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
注意在这个语句中,
=(select b.city_name,b.customer_type from tmp_cust_city
b
where b.customer_id=a.customer_id )
与
(select 1 from tmp_cust_city
b
where b.customer_id=a.customer_id)
是两个独立的子查询,查看执行计划可知,对b表/索引扫描了2篇;
3) 在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:
SQL 代码
update (select a.city_name,b.city_name as new_name
from customers a,
tmp_cust_city b
where b.customer_id=a.customer_id
)
二、oracle多表关联删除的两种方法
第一种使用exists方法
delete
from tableA
where exits
(
select 1
from tableB
Where tableA.id = tableB.id
)
第二种使用匿名表方式进行删除
delete
from
(
select 1
from tableA,TableB
where tableA.id = tableB.id
)
仅在where字句中的连接
SQL 代码
--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
update customers a -- 使用别名
set customer_type='01' --01
为vip,00为普通
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
2) 两表(多表)关联update --
被修改值由另一个表运算而来
SQL 代码
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city
b where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
-- update 超过2个值
update customers a -- 使用别名
set (city_name,customer_type)=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
注意在这个语句中,
=(select b.city_name,b.customer_type from tmp_cust_city
b
where b.customer_id=a.customer_id )
与
(select 1 from tmp_cust_city
b
where b.customer_id=a.customer_id)
是两个独立的子查询,查看执行计划可知,对b表/索引扫描了2篇;
3) 在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:
SQL 代码
update (select a.city_name,b.city_name as new_name
from customers a,
tmp_cust_city b
where b.customer_id=a.customer_id
)
二、oracle多表关联删除的两种方法
第一种使用exists方法
delete
from tableA
where exits
(
select 1
from tableB
Where tableA.id = tableB.id
)
第二种使用匿名表方式进行删除
delete
from
(
select 1
from tableA,TableB
where tableA.id = tableB.id
)
相关文章推荐
- 数据添加(insert),删除(delete),更新(update)
- 更新和删除数据(UPDATE\DELETE)
- discuz 如何添加数据DB::insert和更新数据DB::update 查询DB::query删除DB::delete
- 数据库 误操作(更新数据(Update),删除数据(delete),当然也包括增加大量数据(insert))之后,给出的解决方案
- 关联关系的crud(增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete))的理解(casecade和fetch)
- MySQL--操作简记(create创建操纵表、insert插入、update更新、delete删除数据)
- hibernate (多对多) 执行update更新操作时,关联表数据会被删除
- discuz 如何添加数据DB::insert和更新数据DB::update 查询DB::query删除DB::delete
- 关于 oracle 多表关联更新的语句写法——仅仅是给自己看的
- Oracle关联更新语法(T-SQL中的update...from)
- Oracle:外键关联导致数据无法删除
- update关联更新在sqlserver和oracle中的实现
- oracle 多表删除 同时删除多表中关联数据
- ORACLE 闪回删除的表/数据,闪回更新数据
- Oracle:外键关联导致数据无法删除
- oracle 多表删除 同时删除多表中关联数据
- Oracle 参数 游标[游标更新删除数据]
- update,delete与INNER JOIN 以及删除重复数据
- Oracle里用delete或者drop命令删除的数据或者表可以通过闪回功能找回数据
- 在ASP.NET 2.0中操作数据::研究插入、更新和删除的关联事件