2
10

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 5 years have passed since last update.

PostgreSQL カラム追加、削除、構造変更

Last updated at Posted at 2019-08-22

カラム追加

基本的なカラム追加のALTER文は以下。
テーブル名、データ型、追加するカラム名を指定する。

SQL
ALTER TABLE user_tbl ADD delete_flg character varying(1);

また、以下のようにすれば、NOT NULL制約やデフォルト値の設定ができる。

SQL
ALTER TABLE user_tbl ADD delete_flg character varying(1) DEFAULT '0' NOT NULL;

複数カラム追加の場合

SQL
ALTER TABLE user_tbl 
  ADD delete_flg character varying(1) DEFAULT '0' NOT NULL,
  ADD remarks text;

カラム名変更

RENAME [変更前カラム名] TO [変更後カラム名] で記述する

SQL
ALTER TABLE 
  work_user_tbl 
RENAME 
  delete_flg TO del_flg;

コメントの設定

データベース、テーブル、カラムにコメントを設定する

SQL
COMMENT ON TABLE user_tbl IS '使用者情報テーブル';
COMMENT ON COLUMN user.id IS 'ユーザーID';

カラム削除

SQL
ALTER TABLE user_tbl DROP COLUMN delete_flg;

複数削除は以下。

SQL
ALTER TABLE user_tbl 
  DROP COLUMN delete_flg,
  DROP COLUMN rating_remarks;

制約削除

NOT NULL制約を削除したい場合は以下。

SQL
ALTER TABLE user_tbl ALTER COLUMN delete_flg DROP NOT NULL;
2
10
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
2
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?