ALTER TABLE

説明

ALTER TABLE ステートメントは、テーブルのスキーマまたはプロパティを変更します。

RENAME

ALTER TABLE RENAME TO ステートメントは、データベース内の既存のテーブルのテーブル名を変更します。テーブル名の変更コマンドは、テーブルをデータベース間で移動するために使用することはできず、同じデータベース内でのみテーブル名を変更するために使用できます。

テーブルがキャッシュされている場合、コマンドはテーブルのキャッシュされたデータをクリアします。キャッシュは、次回テーブルにアクセスされたときに遅延してロードされます。さらに、

構文

ALTER TABLE table_identifier RENAME TO table_identifier

ALTER TABLE table_identifier partition_spec RENAME TO partition_spec

パラメータ

ADD COLUMNS

ALTER TABLE ADD COLUMNS ステートメントは、既存のテーブルに指定された列を追加します。

構文

ALTER TABLE table_identifier ADD COLUMNS ( col_spec [ , ... ] )

パラメータ

DROP COLUMNS

ALTER TABLE DROP COLUMNS ステートメントは、既存のテーブルから指定された列を削除します。このステートメントは v2 テーブルでのみサポートされていることに注意してください。

構文

ALTER TABLE table_identifier DROP { COLUMN | COLUMNS } [ ( ] col_name [ , ... ] [ ) ]

パラメータ

RENAME COLUMN

ALTER TABLE RENAME COLUMN ステートメントは、既存のテーブルの列名を変更します。このステートメントは v2 テーブルでのみサポートされていることに注意してください。

構文

ALTER TABLE table_identifier RENAME COLUMN col_name TO col_name

パラメータ

ALTER OR CHANGE COLUMN

ALTER TABLE ALTER COLUMN または ALTER TABLE CHANGE COLUMN ステートメントは、列の定義を変更します。

構文

ALTER TABLE table_identifier { ALTER | CHANGE } [ COLUMN ] col_name alterColumnAction

パラメータ

REPLACE COLUMNS

ALTER TABLE REPLACE COLUMNS ステートメントは、すべての既存の列を削除し、新しい列セットを追加します。このステートメントは v2 テーブルでのみサポートされていることに注意してください。

構文

ALTER TABLE table_identifier [ partition_spec ] REPLACE COLUMNS  
  [ ( ] qualified_col_type_with_position_list [ ) ]

パラメータ

ADD AND DROP PARTITION

ADD PARTITION

ALTER TABLE ADD ステートメントは、パーティション化されたテーブルにパーティションを追加します。

テーブルがキャッシュされている場合、コマンドはテーブルとそのテーブルを参照するすべての依存関係のキャッシュされたデータをクリアします。キャッシュは、次回テーブルまたは依存関係にアクセスされたときに遅延してロードされます。

構文
ALTER TABLE table_identifier ADD [IF NOT EXISTS] 
    ( partition_spec [ partition_spec ... ] )
パラメータ

DROP PARTITION

ALTER TABLE DROP ステートメントは、テーブルのパーティションを削除します。

テーブルがキャッシュされている場合、コマンドはテーブルとそのテーブルを参照するすべての依存関係のキャッシュされたデータをクリアします。キャッシュは、次回テーブルまたは依存関係にアクセスされたときに遅延してロードされます。

構文
ALTER TABLE table_identifier DROP [ IF EXISTS ] partition_spec [PURGE]
パラメータ

CLUSTER BY

ALTER TABLE CLUSTER BY コマンドは、既存のテーブルのクラスタリング列を変更または削除するためにも使用できます。

構文
-- Changing Clustering Columns
ALTER TABLE table_identifier CLUSTER BY ( col_name [ , ... ] )

-- Removing Clustering Columns
ALTER TABLE table_identifier CLUSTER BY NONE

パラメータ

SET AND UNSET

SET PROPERTIES

ALTER TABLE SET コマンドは、テーブルプロパティを設定するために使用されます。特定のプロパティが既に設定されていた場合、古い値は新しい値で上書きされます。

構文
-- Set Properties
ALTER TABLE table_identifier SET TBLPROPERTIES ( key1 = val1, key2 = val2, ... )

UNSET PROPERTIES

ALTER TABLE UNSET コマンドは、テーブルプロパティを削除するために使用されます。

注意: 指定されたプロパティキーが存在しない場合、IF EXISTS を指定するかどうかに関わらず、コマンドはそれを無視して最終的に成功します。

構文
-- Unset Properties
ALTER TABLE table_identifier UNSET TBLPROPERTIES ( key1, key2, ... )

SET SERDE

ALTER TABLE SET コマンドは、Hive テーブルの SERDE または SERDE プロパティを設定するために使用されます。特定のプロパティが既に設定されていた場合、古い値は新しい値で上書きされます。

構文
-- Set SERDE Properties
ALTER TABLE table_identifier [ partition_spec ]
    SET SERDEPROPERTIES ( key1 = val1, key2 = val2, ... )

ALTER TABLE table_identifier [ partition_spec ] SET SERDE serde_class_name
    [ WITH SERDEPROPERTIES ( key1 = val1, key2 = val2, ... ) ]

SET LOCATION And SET FILE FORMAT

ALTER TABLE SET コマンドは、既存のテーブルのファイル場所とファイル形式を変更するためにも使用できます。

テーブルがキャッシュされている場合、ALTER TABLE .. SET LOCATION コマンドは、テーブルとそのテーブルを参照するすべての依存関係のキャッシュされたデータをクリアします。キャッシュは、次回テーブルまたは依存関係にアクセスされたときに遅延してロードされます。

構文
-- Changing File Format
ALTER TABLE table_identifier [ partition_spec ] SET FILEFORMAT file_format

-- Changing File Location
ALTER TABLE table_identifier [ partition_spec ] SET LOCATION 'new_location'

パラメータ

RECOVER PARTITIONS

ALTER TABLE RECOVER PARTITIONS ステートメントは、テーブルのディレクトリ内のすべてのパーティションを回復し、Hive メタストアを更新します。パーティションを回復する別の方法は、MSCK REPAIR TABLE を使用することです。

構文

ALTER TABLE table_identifier RECOVER PARTITIONS

パラメータ

-- RENAME table 
DESC student;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE Student RENAME TO StudentInfo;

-- After Renaming the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- RENAME partition

SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=10|
|   age=11|
|   age=12|
+---------+

ALTER TABLE default.StudentInfo PARTITION (age='10') RENAME TO PARTITION (age='15');

-- After renaming Partition
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

-- Add new columns to a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp);

-- After Adding New columns to the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|               LastName|   string|   NULL|
|                    DOB|timestamp|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- Drop columns of a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|               LastName|   string|   NULL|
|                    DOB|timestamp|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo DROP columns (LastName, DOB);

-- After dropping columns of the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- Rename a column of a table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|                   name|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;

-- After renaming a column of the table
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|              FirstName|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

-- ALTER OR CHANGE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-------+
|               col_name|data_type|comment|
+-----------------------+---------+-------+
|              FirstName|   string|   NULL|
|                 rollno|      int|   NULL|
|                    age|      int|   NULL|
|# Partition Information|         |       |
|             # col_name|data_type|comment|
|                    age|      int|   NULL|
+-----------------------+---------+-------+

ALTER TABLE StudentInfo ALTER COLUMN FirstName COMMENT "new comment";

-- After ALTER or CHANGE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-----------+
|               col_name|data_type|    comment|
+-----------------------+---------+-----------+
|              FirstName|   string|new comment|
|                 rollno|      int|       NULL|
|                    age|      int|       NULL|
|# Partition Information|         |           |
|             # col_name|data_type|    comment|
|                    age|      int|       NULL|
+-----------------------+---------+-----------+

-- REPLACE COLUMNS
DESC StudentInfo;
+-----------------------+---------+-----------+
|               col_name|data_type|    comment|
+-----------------------+---------+-----------+
|              FirstName|   string|new comment|
|                 rollno|      int|       NULL|
|                    age|      int|       NULL|
|# Partition Information|         |           |
|             # col_name|data_type|    comment|
|                    age|      int|       NULL|
+-----------------------+---------+-----------+

ALTER TABLE StudentInfo REPLACE COLUMNS (name string, ID int COMMENT 'new comment');

-- After replacing COLUMNS
DESC StudentInfo;
+-----=---------+---------+-----------+
|       col_name|data_type|    comment|
+---------------+---------+-----------+
|           name|   string|       NULL|
|             ID|      int|new comment|
| # Partitioning|         |           |
|Not partitioned|         |           |
+---------------+---------+-----------+

-- Add a new partition to a table 
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18);

-- After adding a new partition to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
+---------+

-- Drop a partition from the table 
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
+---------+

ALTER TABLE StudentInfo DROP IF EXISTS PARTITION (age=18);

-- After dropping the partition of the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

-- Adding multiple partitions to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
+---------+

ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);

-- After adding multiple partitions to the table
SHOW PARTITIONS StudentInfo;
+---------+
|partition|
+---------+
|   age=11|
|   age=12|
|   age=15|
|   age=18|
|   age=20|
+---------+

-- CLUSTER BY
DESC Teacher;
+------------------------+---------+-------+
|                col_name|data_type|comment|
+------------------------+---------+-------+
|                    name|   string|   NULL|
|                  gender|   string|   NULL|
|                 country|   string|   NULL|
|                     age|      int|   NULL|
|# Clustering Information|         |       |
|              # col_name|data_type|comment|
|                  gender|   string|   NULL|
+------------------------+---------+-------+

ALTER TABLE Teacher CLUSTER BY (gender, country);

-- After changing clustering columns
DESC Teacher;
+------------------------+---------+-------+
|                col_name|data_type|comment|
+------------------------+---------+-------+
|                    name|   string|   NULL|
|                  gender|   string|   NULL|
|                 country|   string|   NULL|
|                     age|      int|   NULL|
|# Clustering Information|         |       |
|              # col_name|data_type|comment|
|                  gender|   string|   NULL|
|                 country|   string|   NULL|
+------------------------+---------+-------+

ALTER TABLE Teacher CLUSTER BY NONE;

-- After removing clustering columns
DESC Teacher;
+------------------------+---------+-------+
|                col_name|data_type|comment|
+------------------------+---------+-------+
|                    name|   string|   NULL|
|                  gender|   string|   NULL|
|                 country|   string|   NULL|
|                     age|      int|   NULL|
|# Clustering Information|         |       |
+------------------------+---------+-------+

-- Change the fileformat
ALTER TABLE loc_orc SET fileformat orc;

ALTER TABLE p1 partition (month=2, day=2) SET fileformat parquet;

-- Change the file Location
ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways';

-- SET SERDE/ SERDE Properties
ALTER TABLE test_tab SET SERDE 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe';

ALTER TABLE dbx.tab1 SET SERDE 'org.apache.hadoop' WITH SERDEPROPERTIES ('k' = 'v', 'kay' = 'vee');

-- SET TABLE PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');

-- SET TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'A table comment.');

-- Alter TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'This is a new comment.');

-- DROP TABLE PROPERTIES
ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');

-- RECOVER PARTITIONS
ALTER TABLE dbx.tab1 RECOVER PARTITIONS;