5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQLiteでテーブルのカラム名を変更したい時

Posted at

SQLiteではカラム名変更のコマンドが用意されていないとのこと(驚き)
つまり、カラム名は変更できない。
なので、カラム名を変更したテーブルを新たに作成して、既存のテーブルの内容をインサートする。
インサートしおわったら既存の古いテーブルは削除する。

-- 既存のテーブルをリネーム
ALTER TABLE tbl_name RENAME TO tbl_temp;
-- 新しいテーブルを作成(元々のテーブル名と同じ名前で)
CREATE TABLE tbl_name(col1 TEXT, col2_new TEXT);
-- レコードを全て移す
INSERT INTO tbl_name(col1, col2_new) SELECT col1, col2 FROM tbl_temp;
-- 元のテーブルを削除
DROP TABLE tbl_temp;

上記コマンドの引用元

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?