课程表

Impala 基础

Table 特定语句

Impala 子句

工具箱
速查手册

impala 修改表

当前位置:免费教程 » 大数据/云 » Impala

Impala中的Alter table语句用于对给定表执行更改。 使用此语句,我们可以添加,删除或修改现有表中的列,也可以重命名它们。

本章通过语法和示例解释了各种类型的alter语句。 首先假设我们在Impala的my_db数据库中有一个名为customers的表,具有以下数据

  1. ID NAME AGE ADDRESS SALARY
  2. --- --------- ----- ----------- --------
  3. 1 Ramesh 32 Ahmedabad 20000
  4. 2 Khilan 25 Delhi 15000
  5. 3 Hardik 27 Bhopal 40000
  6. 4 Chaitali 25 Mumbai 35000
  7. 5 kaushik 23 Kota 30000
  8. 6 Komal 22 Mp 32000

并且,如果获取数据库my_db中的表列表,可以在其中找到customers表,如下所示。

  1. [quickstart.cloudera:21000] > show tables;
  2.  
  3. Query: show tables
  4. +-----------+
  5. | name |
  6. +-----------+
  7. | customers |
  8. | employee |
  9. | student |
  10. | student1 |
  11. +-----------+

更改表的名称

语法

ALTER TABLE重命名现有表的基本语法如下 -

  1. ALTER TABLE [old_db_name.]old_table_name RENAME TO [new_db_name.]new_table_name

下面是使用alter语句更改表名称的示例。 这里我们将表客户的名称更改为用户。

  1. [quickstart.cloudera:21000] > ALTER TABLE my_db.customers RENAME TO my_db.users;

执行上述查询后,Impala根据需要更改表的名称,并显示以下消息。

  1. Query: alter TABLE my_db.customers RENAME TO my_db.users

您可以使用show tables语句验证当前数据库中的表的列表。 您可以找到名为users而不是customers的表。

  1. Query: show tables
  2. +----------+
  3. | name |
  4. +----------+
  5. | employee |
  6. | student |
  7. | student1 |
  8. | users |
  9. +----------+
  10. Fetched 4 row(s) in 0.10s

向表中添加列

语法

ALTER TABLE向现有表中添加列的基本语法如下 -

  1. ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])

以下查询是演示如何向现有表中添加列的示例。 这里我们在users表中添加两列account_no和phone_number(两者都是bigint数据类型)。

  1. [quickstart.cloudera:21000] > ALTER TABLE users ADD COLUMNS (account_no BIGINT,
  2. phone_no BIGINT);

在执行上面的查询时,它会将指定的列添加到名为student的表中,并显示以下消息。

  1. Query: alter TABLE users ADD COLUMNS (account_no BIGINT, phone_no BIGINT)

如果您验证表用户的模式,您可以在其中找到新添加的列,如下所示。

  1. quickstart.cloudera:21000] > describe users;
  2. Query: describe users
  3. +------------+--------+---------+
  4. | name | type | comment |
  5. +------------+--------+---------+
  6. | id | int | |
  7. | name | string | |
  8. | age | int | |
  9. | address | string | |
  10. | salary | bigint | |
  11. | account_no | bigint | |
  12. | phone_no | bigint | |
  13. +------------+--------+---------+
  14. Fetched 7 row(s) in 0.20s

从表中删除列

语法

现有表中ALTER TABLE到DROP COLUMN的基本语法如下 -

  1. ALTER TABLE name DROP [COLUMN] column_name

以下查询是从现有表中删除列的示例。 这里我们删除名为account_no的列。

  1. [quickstart.cloudera:21000] > ALTER TABLE users DROP account_no;

在执行上面的查询时,Impala删除名为account的列,不显示以下消息。

  1. Query: alter TABLE users DROP account_no

如果验证表用户的模式,则在删除之后,将找不到名为account_no的列。

  1. [quickstart.cloudera:21000] > describe users;
  2. Query: describe users
  3. +----------+--------+---------+
  4. | name | type | comment |
  5. +----------+--------+---------+
  6. | id | int | |
  7. | name | string | |
  8. | age | int | |
  9. | address | string | |
  10. | salary | bigint | |
  11. | phone_no | bigint | |
  12. +----------+--------+---------+
  13. Fetched 6 row(s) in 0.11s

更改列的名称和类型

语法

ALTER TABLE更改现有表中的列的名称和数据类型的基本语法如下 -

  1. ALTER TABLE name CHANGE column_name new_name new_type

以下是使用alter语句更改列的名称和数据类型的示例。 这里我们将列phone_no的名称更改为电子邮件,将其数据类型更改为字符串。

  1. [quickstart.cloudera:21000] > ALTER TABLE users CHANGE phone_no e_mail string;

在执行上述查询时,Impala执行指定的更改,显示以下消息。

  1. Query: alter TABLE users CHANGE phone_no e_mail string

您可以使用describe语句验证表用户的元数据。 您可以观察到Impala已对指定的列进行了必要的更改。

  1. [quickstart.cloudera:21000] > describe users;
  2. Query: describe users
  3. +----------+--------+---------+
  4. | name | type | comment |
  5. +----------+--------+---------+
  6. | id | int | |
  7. | name | string | |
  8. | age | int | |
  9. | address | string | |
  10. | salary | bigint | |
  11. | phone_no | bigint | |
  12. +----------+--------+---------+
  13. Fetched 6 row(s) in 0.11s

使用Hue改变表

打开Impala查询编辑器并在其中键入alter语句,然后单击执行按钮,如下面的屏幕截图所示。

变更表

在执行上述查询时,它会将表customer的名称更改为用户。 以同样的方式,我们可以执行所有的alter查询。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号