Impala中的Alter table语句用于对给定表执行更改。 使用此语句,我们可以添加,删除或修改现有表中的列,也可以重命名它们。
本章通过语法和示例解释了各种类型的alter语句。 首先假设我们在Impala的my_db数据库中有一个名为customers的表,具有以下数据
- ID NAME AGE ADDRESS SALARY
- --- --------- ----- ----------- --------
- 1 Ramesh 32 Ahmedabad 20000
- 2 Khilan 25 Delhi 15000
- 3 Hardik 27 Bhopal 40000
- 4 Chaitali 25 Mumbai 35000
- 5 kaushik 23 Kota 30000
- 6 Komal 22 Mp 32000
并且,如果获取数据库my_db中的表列表,可以在其中找到customers表,如下所示。
- [quickstart.cloudera:21000] > show tables;
- Query: show tables
- +-----------+
- | name |
- +-----------+
- | customers |
- | employee |
- | student |
- | student1 |
- +-----------+
更改表的名称
语法
ALTER TABLE重命名现有表的基本语法如下 -
- ALTER TABLE [old_db_name.]old_table_name RENAME TO [new_db_name.]new_table_name
例
下面是使用alter语句更改表名称的示例。 这里我们将表客户的名称更改为用户。
- [quickstart.cloudera:21000] > ALTER TABLE my_db.customers RENAME TO my_db.users;
执行上述查询后,Impala根据需要更改表的名称,并显示以下消息。
- Query: alter TABLE my_db.customers RENAME TO my_db.users
您可以使用show tables语句验证当前数据库中的表的列表。 您可以找到名为users而不是customers的表。
- Query: show tables
- +----------+
- | name |
- +----------+
- | employee |
- | student |
- | student1 |
- | users |
- +----------+
- Fetched 4 row(s) in 0.10s
向表中添加列
语法
ALTER TABLE向现有表中添加列的基本语法如下 -
- ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])
例
以下查询是演示如何向现有表中添加列的示例。 这里我们在users表中添加两列account_no和phone_number(两者都是bigint数据类型)。
- [quickstart.cloudera:21000] > ALTER TABLE users ADD COLUMNS (account_no BIGINT,
- phone_no BIGINT);
在执行上面的查询时,它会将指定的列添加到名为student的表中,并显示以下消息。
- Query: alter TABLE users ADD COLUMNS (account_no BIGINT, phone_no BIGINT)
如果您验证表用户的模式,您可以在其中找到新添加的列,如下所示。
- quickstart.cloudera:21000] > describe users;
- Query: describe users
- +------------+--------+---------+
- | name | type | comment |
- +------------+--------+---------+
- | id | int | |
- | name | string | |
- | age | int | |
- | address | string | |
- | salary | bigint | |
- | account_no | bigint | |
- | phone_no | bigint | |
- +------------+--------+---------+
- Fetched 7 row(s) in 0.20s
从表中删除列
语法
现有表中ALTER TABLE到DROP COLUMN的基本语法如下 -
- ALTER TABLE name DROP [COLUMN] column_name
例
以下查询是从现有表中删除列的示例。 这里我们删除名为account_no的列。
- [quickstart.cloudera:21000] > ALTER TABLE users DROP account_no;
在执行上面的查询时,Impala删除名为account的列,不显示以下消息。
- Query: alter TABLE users DROP account_no
如果验证表用户的模式,则在删除之后,将找不到名为account_no的列。
[quickstart.cloudera:21000] > describe users;
Query: describe users
+----------+--------+---------+
| name | type | comment |
+----------+--------+---------+
| id | int | |
| name | string | |
| age | int | |
| address | string | |
| salary | bigint | |
| phone_no | bigint | |
+----------+--------+---------+
Fetched 6 row(s) in 0.11s
更改列的名称和类型
语法
ALTER TABLE更改现有表中的列的名称和数据类型的基本语法如下 -
- ALTER TABLE name CHANGE column_name new_name new_type
例
以下是使用alter语句更改列的名称和数据类型的示例。 这里我们将列phone_no的名称更改为电子邮件,将其数据类型更改为字符串。
- [quickstart.cloudera:21000] > ALTER TABLE users CHANGE phone_no e_mail string;
在执行上述查询时,Impala执行指定的更改,显示以下消息。
- Query: alter TABLE users CHANGE phone_no e_mail string
您可以使用describe语句验证表用户的元数据。 您可以观察到Impala已对指定的列进行了必要的更改。
- [quickstart.cloudera:21000] > describe users;
- Query: describe users
- +----------+--------+---------+
- | name | type | comment |
- +----------+--------+---------+
- | id | int | |
- | name | string | |
- | age | int | |
- | address | string | |
- | salary | bigint | |
- | phone_no | bigint | |
- +----------+--------+---------+
- Fetched 6 row(s) in 0.11s
使用Hue改变表
打开Impala查询编辑器并在其中键入alter语句,然后单击执行按钮,如下面的屏幕截图所示。

在执行上述查询时,它会将表customer的名称更改为用户。 以同样的方式,我们可以执行所有的alter查询。
转载本站内容时,请务必注明来自W3xue,违者必究。