2014-02-26 116 views

回答

40

Change Column Name/Type/Position/Comment

ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]

例:

CREATE TABLE test_change (a int, b int, c int); 

// will change column a's name to a1 
ALTER TABLE test_change CHANGE a a1 INT; 
+26

,你將有用反引號括起名字 – libjack

+0

謝謝,這個評論是非常有用的。我看了很多地方的解決方案,找不到它。對於什麼是返回勾號 - 感謝libjack,http://askubuntu.com/questions/20034/differences-between-doublequotes-singlequotes-and-backticks-%C2%B4-%C2%B4-on-comm – ShikharDua

+0

。括號不適用於我,但'做。 – fishautumn

5

命令僅當 「使用」 指令已被首先用於定義在哪裏工作數據庫表列重命名語法使用DATABASE.TABLE拋出錯誤,不起作用。版本:HIVE 0.12。

實施例:

hive> ALTER TABLE databasename.tablename CHANGE old_column_name new_column_name; 

    MismatchedTokenException(49!=90) 
     at org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(BaseRecognizer.java:617) 
     at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115) 
     at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatementSuffixExchangePartition(HiveParser.java:11492) 
     ... 

hive> use databasename; 

hive> ALTER TABLE tablename CHANGE old_column_name new_column_name; 

OK 
1
alter table table_name change old_col_name new_col_name new_col_type; 

下面是示例

重命名,其被自動地從該查詢生成的列名,如_c1,從OP _c1當
hive> alter table test change userVisit userVisit2 STRING;  
    OK 
    Time taken: 0.26 seconds 
    hive> describe test;          
    OK 
    uservisit2    string          
    category    string          
    uuid     string          
    Time taken: 0.213 seconds, Fetched: 3 row(s)