1、修改表名
alter table table_name rename to new_table_name;
2、修改列名
alter table tablename change column column_orign column_new int(修改后列的屬性) comment 'column_name'
after severity;//可以把該列放到指定列的后面,或者使用‘first’放到第一位
將表tablename中的列column_orign修改成column_new,同時指定修改后的列名稱的屬性,comment是這個列的注釋
3、增加列
alter table tablename add columns(column1 string comment 'xxxx',column2 long comment 'yyyy')
4、查看表的屬性
desc formatted tablename;
5、修改表的屬性
(1)alter table table_name set tblproperties('property_name'='new_value');
將table_name表中的comment屬性值修改成'new_value';
(2)alter table table_name set serdepropertyes('field.delim'='\t');
將表table_name中的字段分割符修改成'\t',注意,這是在表沒有分區的情況下
例1:create table t8(time string,country string,province string,city string)row format delimited fields terminated by '#' lines terminated by '\n' stored as textfile;
alter table t8 set serdepropertyes('field.delim'='\t');這條語句將t8表中的字段分隔符'#'修改成'\t';
例2:create table t9(time string,country string,province string,city string) partitioned by(dt=string) row foramt delimited fields terminated by '\n' stored as textfile;
alter table t9 partition(dt='20140901') set serdepropertyes('field.delim=\t');
(3)alter table table_name[partition] set location 'path'
alter table table_name set TBLPROPERTIES('EXTERNAL'='TRUE');//內部表轉化成外部表
alter table table_name set TBLPROPERTIES('EXTERNAL'='FALSE');//外部表轉成內部表
Tip:首先在Hadoop上新建一個文件夾,'hadoop fs -mkdir /fould_name',然后向文件夾下面上傳數據:'hadoop fs copyFromLocal /root/data /location',這句命令
代表了將'/root/data'目錄下的data文件上傳到hadoop的location文件夾下,可以通過命令'hadoop fs -ls /location'命令查看location文件夾下的文件。
在(2)中,t9表的位置在:'/hive/t9'目錄下,然后通過命令:'alter table city set location 'hdfs://hadoop:9000/location'可以將表t9的位置更改為:hadoop下的/location文件夾下面;
同時還必須注意的是,由於t9默認是內表,所以在刪除表t9時,它的數據包括文件目錄全部被刪除,為了防止這種情況發生,可以將表t9修改成外表,通過語句:alter table t9 set tblproperties('EXTERNAL'='TRUE');
以此類推,同樣也可以將外部表修改為內部表,可以通過語句:alter table city set tblproperties('EXTERNAL'='FALSE');
(4)其他修改表屬性的命令:alter table properties; alter serde properties; alter table/partition file format; alter table storage properties; alter table rename partition; alter table set location;
詳細說明可以參考:Hive官網,在官網中找:wiki LanguageManual DDL來查詢詳細介紹
