SQL/Oracle 两表关联更新
2014-12-24 12:50
316 查看
http://blog.csdn.net/cassaba/article/details/6987040
有TA, TB两表,假设均有三个栏位id, name, remark. 现在需要把TB表的name, remark两个栏位通过id关联,更新到TA表的对应栏位。
建表脚本:
[sql]
view plaincopy
drop table TA;
create table TA
(
id number not null,
name varchar(10) not null,
remark varchar(10) not null
);
drop table TB;
create table TB
(
id number not null,
name varchar(10) not null,
remark varchar(10) not null
);
truncate table TA;
insert into TA values(1, 'Aname1', 'Aremak1');
insert into TA values(2, 'Aname2', 'Aremak2');
commit;
truncate table TB;
insert into TB values(1, 'Bname1', 'Bremak1');
insert into TB values(3, 'Bname3', 'Bremak3');
commit;
select * from TA;
select * from TB;
SQLServer/Oracle版本的Update写法分别如下:
1. SQLServer
[sql]
view plaincopy
update TA set name=b.name, remark=b.remark from TA a inner join TB b on a.id = b.id
或者
[sql]
view plaincopy
update TA set name=b.name, remark=b.remark from TA a, TB b where a.id = b.id
注意不要在被更新表的的栏位前面加别名前缀,否则语法静态检查没问题,实际执行会报错。
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.name" could not be bound.
2. Oracle
[sql]
view plaincopy
update TA a set(name, remark)=(select b.name, b.remark from TB b where b.id=a.id)
where exists(select 1 from TB b where b.id=a.id)
注意如果不添加后面的exists语句,TA关联不到的行name, remark栏位将被更新为NULL值, 如果name, remark栏位不允许为null,则报错。 这不是我们希望看到的。
[sql]
view plaincopy
--when name, remark is not null, cause error.
--if allow null, rows in TA not matched will be update to null.
update TA a set(name, remark)=(select b.name, b.remark from TB b where b.id=a.id);
可考虑的替代方法:
[sql]
view plaincopy
update TA a set name= nvl((select b.name from TB b where b.id=a.id), a.name);
update TA a set remark= nvl((select b.remark from TB b where b.id=a.id), a.remark);
如果TA.id, TB.id是unique index或primary key
可以使用视图更新的语法:
[sql]
view plaincopy
ALTER TABLE TA ADD CONSTRAINT TA_PK
PRIMARY KEY (
ID
)
ENABLE
VALIDATE
;
ALTER TABLE TB ADD CONSTRAINT TB_PK
PRIMARY KEY (
ID
)
ENABLE
VALIDATE
;
update (select a.name, b.name as newname,
a.remark, b.remark as newremark from TA a, TB b where a.id=b.id)
set name=newname, remark=newremark;
更加详尽的对比分析参考下面的文章ORACLE 多表关联 UPDATE 语句
有TA, TB两表,假设均有三个栏位id, name, remark. 现在需要把TB表的name, remark两个栏位通过id关联,更新到TA表的对应栏位。
建表脚本:
[sql]
view plaincopy
drop table TA;
create table TA
(
id number not null,
name varchar(10) not null,
remark varchar(10) not null
);
drop table TB;
create table TB
(
id number not null,
name varchar(10) not null,
remark varchar(10) not null
);
truncate table TA;
insert into TA values(1, 'Aname1', 'Aremak1');
insert into TA values(2, 'Aname2', 'Aremak2');
commit;
truncate table TB;
insert into TB values(1, 'Bname1', 'Bremak1');
insert into TB values(3, 'Bname3', 'Bremak3');
commit;
select * from TA;
select * from TB;
SQLServer/Oracle版本的Update写法分别如下:
1. SQLServer
[sql]
view plaincopy
update TA set name=b.name, remark=b.remark from TA a inner join TB b on a.id = b.id
或者
[sql]
view plaincopy
update TA set name=b.name, remark=b.remark from TA a, TB b where a.id = b.id
注意不要在被更新表的的栏位前面加别名前缀,否则语法静态检查没问题,实际执行会报错。
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.name" could not be bound.
2. Oracle
[sql]
view plaincopy
update TA a set(name, remark)=(select b.name, b.remark from TB b where b.id=a.id)
where exists(select 1 from TB b where b.id=a.id)
注意如果不添加后面的exists语句,TA关联不到的行name, remark栏位将被更新为NULL值, 如果name, remark栏位不允许为null,则报错。 这不是我们希望看到的。
[sql]
view plaincopy
--when name, remark is not null, cause error.
--if allow null, rows in TA not matched will be update to null.
update TA a set(name, remark)=(select b.name, b.remark from TB b where b.id=a.id);
可考虑的替代方法:
[sql]
view plaincopy
update TA a set name= nvl((select b.name from TB b where b.id=a.id), a.name);
update TA a set remark= nvl((select b.remark from TB b where b.id=a.id), a.remark);
如果TA.id, TB.id是unique index或primary key
可以使用视图更新的语法:
[sql]
view plaincopy
ALTER TABLE TA ADD CONSTRAINT TA_PK
PRIMARY KEY (
ID
)
ENABLE
VALIDATE
;
ALTER TABLE TB ADD CONSTRAINT TB_PK
PRIMARY KEY (
ID
)
ENABLE
VALIDATE
;
update (select a.name, b.name as newname,
a.remark, b.remark as newremark from TA a, TB b where a.id=b.id)
set name=newname, remark=newremark;
更加详尽的对比分析参考下面的文章ORACLE 多表关联 UPDATE 语句
相关文章推荐
- Oracle 2张表关联更新表信息的四个SQL
- Oracle 2张表关联更新表信息的四个SQL
- SQL/Oracle 两表关联更新
- oracle与sql两表关联更新
- Oracle\MS SQL Server Update多表关联更新
- Oracle关联更新语法(T-SQL中的update...from)
- sql 更新语句 关联两张表
- oracle UPDATE 多表关联更新
- Oracle Sql 使用-持续更新
- update关联更新在sqlserver和oracle中的实现
- 对于oracle两表关联更新的问题解决办法
- Oracle多表关联更新
- Oracle和SQL Server的对比 - 连接表的更新操作Update SQL 语句
- Sql调用Oracle的函数更新本地数据
- 【转】 ORACLE中的多表关联更新
- ORACLE问题收集(或者说针对MS-SQL的不同点)--2011年3月16日更新
- oracle 9i 中的关联更新跟oracle10g的区别
- oracle关联更新
- Oracle做关联更新
- oracle 不能更新 PL/SQL 点击“edit data”报“ these query results are not updateable”