天天看點

MAXCOMPUTE操作基礎SQL語句(DDL部分)

操作1:建表語句(不帶分區)

creat table page_view (user_id bigint,view_time bigint,page_url string,referrer_url string,ip string);           
注意事項:MAXCOMPUT建立表的時候必須加上table這個關鍵字,如以上語句如果為create後直接跟着表名則會報錯,語句結束時以;結尾。

操作2:建表語句(帶分區)

creat table page_view (user_id bigint,view_time bigint,page_url string,referrer_url string,ip string) partitioned by (dt string,country string);           
注意事項:分區字段接在partitioned by之後,分區字段單列,以上語句dt和country為兩個分區,根據順序dt為一級分區,country為二級分區。

操作3:加注釋的方法

creat table page_view (user_id bigint,view_time bigint,page_url string,referrer_url string,ip string comment 'creat table sql') partitioned by (dt string,country string);           
注意事項:comment後為注釋,用單引号區分。

操作4:複制表(僅複制表的結構)

creat table page_view_test like page_view;           
注意事項:like語句隻複制表的結構,目錄相同,建立的表中沒有資料,也不複制原表的生命周期。

操作5:複制表(僅複制表的資料)

creat table page_view_url as select page_url,referrer_url from page view;           
注意事項:as語句隻複制表的資料,不完全複制表的結構,建立的表中沒有分區,也不複制原表的生命周期。

操作6:檢視表的資訊

desc page_view;           
注意事項:沒有關鍵字table。

操作7:檢視外部表的資訊

desc extended page_view;           
注:沒有關鍵字table。

drop table page_view;

注:删除表

操作8:檢視外部表的資訊

drop table if exists page_view;           
注:如果不指定if exists選項而表不存在,則傳回異常。若指定此選項,無論表是否存在,皆傳回成功。

操作9:修改表的名字

alter table page_view rename to page_view_1;           

操作10:修改表的注釋

alter table page_view set comment 'new';           

操作11:清除非分區表中的資訊

turncate table page_view;           

操作12:清除非分區表中的資訊

turncate table page_view drop partition (dt='2011-12-17);           

操作13:設定表的生命周期

creat table page_view (user_id bigint) lifecycle 100;           
注:建立表并把表的生命周期設定為100天。

操作14:修改表的生命周期

alter table page_view set lifecycle 50;           
注:修改已經有的表格生命周期為50天。

操作15:增加分區

alter table page_view add if not exists partition (dt='2015-1-1',region='shanghai');           
注:僅支援新增分區,不支援新增分區字段。如果未指定if not exists而同名的分區已存在,則出錯傳回。

操作16:删除分區

alter table page_view drop if exists partiton (dt='2015-1-1',region='shanghai');           
注:如果分區不存在且未指定if exists,則報錯傳回。

操作17:增加列

alter table page_view add columns (id bigint,url string);           

操作18:修改列的名字

alter table page_view change column id rename to id_1;           

操作19:修改列的注釋

alter table page_view change column id comment 'change';           

操作20:修改分區列的值

alter table page_view partition (dt='2013-01-01') rename to partition (dt='2013-01-02');           
注:不支援修改分區列列名,隻能修改分區列對應的值。修改多級分區的一個或者多個分區值,多級分區的每一級的分區值都必須寫上。

操作21:建立視圖

creat view if not exists data_1;           
注:建立視圖時,必須有對視圖所引用表的讀權限。視圖隻能包含一個有效的select語句。視圖可以引用其它視圖,但不能引用自己,也不能循環引用。不允許向視圖寫入資料,例如使用insert into或者insert overwrite操作視圖。當建好視圖後,如果視圖的引用表發生了變更,有可能導緻視圖無法通路,例如删除被引用表。您需要自己維護引用表及視圖之間的對應關系。如果沒有指定if not exists,在視圖已經存在時用create view會導緻異常。這種情況可以用create or replace view來重建視圖,重建後視圖本身的權限保持不變。

操作22:删除視圖

drop view if exists data_1;           

操作23:修改視圖名稱

alter view data_1 rename to data_2;