当前位置:K88软件开发文章中心大数据impala → 文章内容

impala ALTER TABLE

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-26 10:07:53

由 jojocool 创建,youj 最后一次修改 2016-12-19 Impala中的Alter table语句用于对给定表执行更改。 使用此语句,我们可以添加,删除或修改现有表中的列,也可以重命名它们。 本章通过语法和示例解释了各种类型的alter语句。 首先假设我们在Impala的my_db数据库中有一个名为customers的表,具有以下数据ID NAME AGE ADDRESS SALARY--- --------- ----- ----------- --------1 Ramesh 32 Ahmedabad 200002 Khilan 25 Delhi 150003 Hardik 27 Bhopal 400004 Chaitali 25 Mumbai 350005 kaushik 23 Kota 300006 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查询。

impala ALTER TABLE