HIVE SQL DDL语句实例

创建新表(三种方式)

1.直接建表

drop table if exists student_score;
create table if not exists student_score(
	id string comment '记录id'
	,student_id string comment '学生学号'
	,subject_id string comment '科目id'
	,score string comment '成绩'
 )
partitioned by (university_name string comment '大学名称')--指定university_name字段作为分区字段
row format delimited fields terminated by '\u0001' --指定列分隔符
lines terminated by '\n'--指定行分隔符,hive默认使用\n作为行分隔符,所以此段可以不加
stored as parquet--指定hive文件的存储格式
location '存放地址'--指定存放地址
;

其中关于Hive的三种不同的文件存储格式可参考这篇文章https://blog.csdn.net/chenfeng_sky/article/details/107361081

2.create table as (直接使用查询结果插入到一张新表)

drop table if exists student_score_new;
create table student_score_new as 
(
select 
*
from student_score
)

通过执行show create table student_score_new,可以发现student_score_new并没有将student_score的分隔符、存储格式、分区字段、字段comment等表结构信息复制过来,只是复制了查询的字段和字段对应的值。

3.like (复制表结构)

create table student_score_structure_copy
like
student_score

student_score_structure_copy只是将student_score的表结构信息复制过来,但是没有复制student_score表的记录。所以student_score_structure_copy是一个空表。

更改列的数据类型

#将class列数据类型更改为string
alter table student.class
change class class string;

清空表数据,保留数据结构

 truncate table table_name;

版权声明:本文为p1306252原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。