前提:有两个表
1、学生表:student(id,name,t_id); t_id是非空的
2、教师表:teacher(id,name)
需求:把学生表中t_id都更新成teacher表的name
sql语句:
update student t
set t.t_id=(select s.name from teacher s where s.id=t.t_id)
报ORA-01407错。
解决方法:
update student t
set t.t_id=(select s.name from teacher s where s.id=t.t_id)
where exists(select 1 from teacher s where s.id=t.t_id)说明:
如果不想报ORA-01407这个错误,有两种办法
1、student,teacher两张表的记录要完全一样
2、要update的表记录要比对照表的记录少
不然update的时候在最后就要加一个 where exists 语句,这个和merge的用法很像,当然这只是一个特例,对于没有指定not null的列,普通的update是完全没有问题的
参考链接:
http://blog.sina.com.cn/s/blog_4a5eb9980101bt8x.html