---
title: "hive修改表模式"
author: "hbjava1985"
date: "2015-02-06T10:58:42Z"
tags: []
categories: ["开源软件"]
slug: "hive"
draft: false
lastmod: "2015-02-06T10:58:41Z"
id: 2183960
url: "https://hbjava1985.iteye.com/blog/2183960"
---

hive用户可以通过alter语句更改table属性
**Alter Partitions**
增加partitions:

```sql

ALTER TABLE table_name 
      ADD [if not exists] 
      PARTITION partition_spec [location 'location1'] 
                partition_spec [location 'location2'] ...
partition_spec: 
      (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

```

删除partitions:

```sql

ALTER TABLE table_name DROP [if exists] partition_spec, partition_spec,...

```

注意：
1. hive可以同时增加或者删除多个partition
2. 使用location关键字时，增加的partition以类似extend table数据的形式存在外部。

**Alter Column**
修改column属性（列名，列字段类型，列注释）：

```sql

ALTER TABLE table_name 
      CHANGE [column] col_old_name col_new_name 
      column_type [comment col_comment] [first|after column_name]

```

增加/替换column（可以使用replace来删除不需要的字段）：

```sql

ALTER TABLE table_name 
      ADD|REPLACE COLUMNS (col_name data_type [comment col_comment], ...)

```

注意：column alter仅仅修改table的元数据，而不会修改数据。

其它：
hive alter语句还支持：
1.Alter Table Properties
2.Alter SerDe Properties
3.Alter Table/Partition File Format
4.Alter Table Storage Properties
5.Alter Table/Partition Location
6.Alter Table Touch
7.Alter Table (Un)Archive
8.Alter Table/Partition Protections
9.Alter Table Rename Partition
reference:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-AlterTable%2FPartitionStatements