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; |
Query: alter TABLE my_db.customers RENAME TO my_db.users |
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); |
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; |
Query: alter TABLE users DROP 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; |
Query: alter TABLE users CHANGE phone_no e_mail string |
[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
打开Impala查询编辑器并在其中键入alter语句,然后单击执行按钮,如下面的屏幕截图所示。
在执行上述查询时,它会将表customer的名称更改为用户。 以同样的方式,我们可以执行所有的alter查询。