同表兩欄位值互換+設定列預設值+設定主鍵值
阿新 • • 發佈:2019-01-22
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;
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;
更新後資料: