1. 程式人生 > >阿里雲大資料ACP認證知識點梳理4——基礎SQL語句(DDL部分)

阿里雲大資料ACP認證知識點梳理4——基礎SQL語句(DDL部分)

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後為註釋,用單引號區分。分割槽欄位單列,dt和country為兩個分割槽,根據順序dt為一級分割槽,country為二級分割槽,MAXCOMPUTE支援的最大分割槽數為6個,表的最大列數為1200列。

creat table page_view_test like

page_view;
注:like語句只複製表的結構,schema相同,新建的表中沒有資料,也不復制原表的生命週期。

creat table page_view_url as select page_url,referrer_url from page view;
注:as語句只複製表的資料,不完全複製表的結構,新建的表中沒有分割槽,也不復制原表的生命週期。

備註:Partitioned by指定表的分割槽欄位,目前支援Tinyint、Smallint、 Int、 Bigint、Varchar和String型別。分割槽值不允許有雙位元組字元(如中文),必須是以英文字母a-z,A-Z開始後可跟字母數字,名稱的長度不超過128位元組。一張表最多允許60000個分割槽,單表的分割槽層次不能超過6級。註釋內容是長度不超過1024位元組的有效字串。

desc page_view;
注:查看錶的資訊

desc extended page_view;
注:檢視外部表的資訊

drop table page_view;
注:刪除表

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

alter table page_view rename to page_view_1;
注:修改表的名字

alter table page_view set comment 'new';
注:修改表的註釋

turncate table page_view;
注:清除非分割槽表中的資訊

turncate table page_view drop partition (dt='2011-12-17);
注:清除分割槽表中某個分割槽的資訊

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

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

alter table page_view add if not exists partition (dt='2015-1-1',region='shanghai');
注:增加分割槽,僅支援新增分割槽,不支援新增分割槽欄位。如果未指定if not exists而同名的分割槽已存在,則出錯返回。目前MaxCompute單表支援的分割槽數量上限為6萬。對於多級分割槽的表,如果想新增新的分割槽,必須指明全部的分割槽值。

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

alter table page_view add columns (id bigint,url string);
注:增加列

alter table page_view change column id rename to id_1;
注:修改列的名字

alter table page_view change column id comment 'change';
注:修改列的註釋

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

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

drop view if exists data_1;
注:刪除檢視。

alter view data_1 rename to data_2;
注:修改檢視名稱。