一、修改列名
1.1、MySQL修改列名的格式
添加列:alter table 表名 add column 列名 varchar(30); 删除列:alter table 表名 drop column 列名; 修改列名MySQL: alter table bbb change oldname newname int;
1.2、hive修改列的名称,类型,位置, 注释 alter table tablename change column old_name new_name 类型 comment 'xxxxx' after clo2;
1.2.1、案例:修改列name
hive (test)> desc t1;
OK
name string
age int
Time taken: 0.097 seconds, Fetched: 2 row(s)
hive (test)> alter table t1 change column name new_name string comment "change the column name to new" after age;
OK
Time taken: 0.196 seconds
hive (test)> desc t1;
OK
age int
new_name string change the column name to new
Time taken: 0.081 seconds, Fetched: 2 row(s)
hive (test)>
二、添加列名
2.1、MySQL增加列名
alter table test add column name varchar(10); --添加表列
2.2、hive添加列名, 增加列名: 注意是columns
增加列名: 注意是columns
hive (test)> alter table t1 add columns(sex string);
OK
Time taken: 0.159 seconds
hive (test)> desc t1;
OK
age int
new_name string change the column name to new
sex string
Time taken: 0.113 seconds, Fetched: 3 row(s)
hive (test)>
三、删除列
3.1、MySQL中删除列
删除列:alter table 表名 drop column 列名;
3.2、hive中没有删除列, 但是提供一种方式replace ,但是不推荐, 如果要删除列, 最好重新创建表结构
接下来看看replace的问题。
3.2.1、创建表
hive (test)> create table t2 (name string , age int) row format delimited fields terminated by ",";
OK
Time taken: 0.652 seconds
hive (test)> desc t2;
OK
name string
age int
Time taken: 0.102 seconds, Fetched: 2 row(s)
hive (test)>
3.2.2、加载样例数据
加载数据
hive (test)> load data local inpath "/home/hive/t2" into table t2;
Loading data to table test.t2
Table test.t2 stats: [numFiles=1, totalSize=51]
OK
Time taken: 0.755 seconds
hive (test)> select * from t2;
OK
zhangsan 67
lisi 89
wangwu 23
zhangliu 12
gouzi 34
Time taken: 0.415 seconds, Fetched: 5 row(s)
hive (test)>
3.2.3、增加一列
增加一列sex
hive (test)> alter table t2 add columns(sex int);
OK
Time taken: 0.177 seconds
hive (test)>
###注意: 此时sex都为NULL
hive (test)> select * from t2;
OK
zhangsan 67 NULL
lisi 89 NULL
wangwu 23 NULL
zhangliu 12 NULL
gouzi 34 NULL
Time taken: 0.485 seconds, Fetched: 5 row(s)
hive (test)>
3.2.4 使用replace 删除列, 注意age被删除,但是数据没有删除, 现在age的值变成sex的值, 导致数据混乱。所以不推荐使用replace
###使用replace 删除列
hive (test)> alter table t2 replace columns(name string, sex int);
OK
Time taken: 0.198 seconds
hive (test)> select * from t2; ###注意age被删除,但是数据没有删除, 现在age的值变成sex的值
OK
zhangsan 67
lisi 89
wangwu 23
zhangliu 12
gouzi 34
Time taken: 0.398 seconds, Fetched: 5 row(s)
hive (test)> desc t2;
OK
name string
sex int
Time taken: 0.072 seconds, Fetched: 2 row(s)
hive (test)>