同表两字段值互换+设置列默认值+设置主键值

来源:互联网 发布:淘宝速成沼泽 编辑:程序博客网 时间:2024/05/21 19:30
t2这张表存在两个问题:1 ID有部分历史数据是null,现在为了线上线下实现同步,需要设置主键值。并且使用sys_guid()函数设置默认值
2 数据数据的name列和name1列值出现错误,需要互换
1、①:修改历史数据
update t2 set id  = sys_guid() where id is null;
commit;
②:设置默认值
 alter table  t2  modify id default sys_guid() not null;
③:设置主键值
alter table t2 add constraint PK_load_unicode primary key (id);
UPDATE t2 a, t2 b SET a.id = b.name, a.name = b.id
2、更新前数据




方法1:
update t2 a
   set (name, name1) =
       (select name1, name from t2 b where a.id = b.id);
commit;
方法2:
merge into t2 a using t2 b
on(a.id = b.id)
when matched then 
update set a.name=b.name1 ,a.name1=b.name;
commit;

更新后数据: