-- 新建表
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [DEFAULT value] [COMMENT col_comment], ...)] —列信息
[COMMENT table_comment] —表注释
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] —分区信息
[AS select_statement]
-- 新建一张相同结构的表
CREATE TABLE [IF NOT EXISTS] table_name
LIKE existing_table_name
-- 创建一张分区表sale_detail。
create table if not exists sale_detail
(
shop_name string,
customer_id string,
total_price double
)
partitioned by (sale_date string,region string);
-- 创建一张保存查询结果数据的表
CREATE TABLE sale_detail_ctas2
AS
SELECT shop_name, customer_id, total_price, '2013' AS sale_date, 'China' AS region
FROM sale_detail;
-- 创建一张相同结构的表
create table sale_detail_like like sale_detail;
desc <table_name>;
desc extended <table_name>; --查看表详细扩展信息。
SHOW CREATE TABLE <table_name>;
DROP TABLE [IF EXISTS] table_name;
ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE table_name SET COMMENT 'new tbl comment';
ALTER TABLE table_name ADD COLUMNS (col_name1 type1,col_name2 type2...);
ALTER TABLE table_name CHANGE COLUMN old_col_name RENAME TO new_col_name;
ALTER TABLE table_name CHANGE COLUMN col_name COMMENT comment_string;
-- 清空非分区表里的数据
TRUNCATE TABLE table_name;
-- 删除分区表分区数据
ALTER TABLE table_name DROP PARTITION '分区字段=分区名称'
-- select计算结果插入
INSERT OVERWRITE|INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)] [(col1,col2 ...)]
select_statement
FROM from_statement;
-- values插入
INSERT INTO TABLE tablename
[PARTITION (partcol1=val1, partcol2=val2,...)][(co1name1,colname2,...)]
[VALUES (col1_value,col2_value,...),(col1_value,col2_value,...),...]
-- 分区表
insert into table sale_detail_insert partition (dt='20200610')
select shop_name, customer_id, total_price, sale_date, region from sale_detail;
-- 非分区表
insert into table sale_detail_insert
select shop_name, customer_id, total_price, sale_date, region from sale_detail;
-- values插入
insert into table srcp partition (p='abc') values ('a',1),('b',2),('c',3);
-- 分区表
insert overwrite table sale_detail_insert partition (dt='20200610')
select customer_id, shop_name, total_price from sale_detail;
-- 非分区表
insert overwrite table sale_detail_insert
select customer_id, shop_name, total_price from sale_detail;
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[ORDER BY order_condition]
[DISTRIBUTE BY distribute_condition [SORT BY sort_condition] ]
[LIMIT number]