テーブルから列を削除 ALTER TABLE、DROP COLUMN
テーブルから列(カラム)を削除するは、ALTER コマンドと DROP コマンドを組み合わせます。DROP コマンドでカラム名を指定します。
ALTER TABLE table_name DROP COLUMN column_name;
Example
例えば、テーブルを作成し、その後でotherカラムを削除します。まず以下のようなテーブルを用意します。
CREATE TABLE example_table ( id INT ,title varchar(16) ,other INT );
テーブルのカラムを確認してみます。
=> \d example_table Table "public.example_table" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | | title | character varying(16) | | | other | integer | | |
ALTER、DROPコマンドでotherカラムを削除します。
DROP COLUMN コマンドで削除するカラム名を指定します。
ALTER TABLE example_table DROP COLUMN other;
削除されたかテーブルのカラム一覧を確認してみます。
=> \d example_table Table "public.example_table" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- id | integer | | | title | character varying(16) | | |