hive常见的对表操作语句-修改表/字段名、增字段、删分区、复制表等

现有分区表table1表,表结构如下:

+--------------------------+-----------------------+-----------------------+--+
|         col_name         |       data_type       |        comment        |
+--------------------------+-----------------------+-----------------------+--+
| userid                   | string                | 用户ID                  |
| birth                    | string                | 用户生日                  |
| age                      | string                | 用户年龄                  |
| gender                   | string                | 用户性别                  |
| pt                       | string                | 用户注册时间                |
|                          | NULL                  | NULL                  |
| # Partition Information  | NULL                  | NULL                  |
| # col_name               | data_type             | comment               |
|                          | NULL                  | NULL                  |
| pt                       | string                | 用户注册时间                |

1、创建结构一样的表,但不复制数据

--想创建一个和表table1结构一样的table2:
create table tmp_db.tmp_table2 like tmp_db.tmp_user_info;

2、修改表名

--将table1的表名修改为table2:
alter table table1 rename to table2;

3、修改字段名或注释

--将table1的的字段useid修改为user:

alter table table1 change userid user string comment'用户ID';
--修改后的字段如下
+-----------+------------+----------+--+
| col_name  | data_type  | comment  |
+-----------+------------+----------+--+
| user      | string     | 用户ID     |

4、添加、修改表注释

--将table1的表注释修改为'用户基础信息':
alter table table1 tblproperties(comment='用户基础信息');

5、删除表分区

--删除日期为2021-01-17的分区:
alter table table1 drop partition(pt='2021-01-17');

6、新增字段

--新增phone字段:
alter table table add columns(phone string comment'电话');
--此时表结构如下:
+--------------------------+-----------------------+-----------------------+--+
|         col_name         |       data_type       |        comment        |
+--------------------------+-----------------------+-----------------------+--+
| userid                   | string                | 用户ID                  |
| birth                    | string                | 用户生日                  |
| age                      | string                | 用户年龄                  |
| gender                   | string                | 用户性别                  |
| phone                    | string                | 电话                    |
| pt                       | string                | 用户注册时间                |
|                          | NULL                  | NULL                  |
| # Partition Information  | NULL                  | NULL                  |
| # col_name               | data_type             | comment               |
|                          | NULL                  | NULL                  |
| pt                       | string                | 用户注册时间                |
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值