Skip to content

Instantly share code, notes, and snippets.

@suzukitadashi
Last active October 13, 2016 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzukitadashi/66aba529a69f7a778595edc513c282d2 to your computer and use it in GitHub Desktop.
Save suzukitadashi/66aba529a69f7a778595edc513c282d2 to your computer and use it in GitHub Desktop.
Oracleでデータ長を短くしたい時の対応手順

既存データを退避させる枠を用意

alter table テーブル名 add(tmp_カラム名 NUMBER(6));

既存データを退避

update テーブル名
set    tmp_カラム名 = カラム名

桁を変えたい項目がnot nullなのでnull可に変更

alter table テーブル名 modify(カラム名 null);

データ長を変えたい項目をnullに更新

update テーブル名
set    カラム名 = null

データ長を変更

alter table テーブル名 modify(カラム名 NUMBER(6));

退避させた項目を戻す

update テーブル名
set    カラム名 = tmp_カラム名

not null に戻す

alter table テーブル名 modify(カラム名 not null);

退避させる枠のカラムを削除

alter table テーブル名 drop(tmp_カラム名);

最後に

commit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment