範例 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

範例

以下範例示範 Amazon Redshift CREATE TABLE 陳述式中各種不同的資料欄和資料表屬性。如需 CREATE TABLE 的相關資訊,包括參數定義,請參閱 CREATE TABLE

許多範例都使用 TICKIT 範例資料集中的資料表和資料。如需詳細資訊,請參閱 tz 資料庫

您可以在 CREATE TABLE 命令中使用資料庫名稱和結構描述名稱作為資料表名稱的字首。例如:dev_database.public.sales。資料庫名稱必須是您所連線的資料庫。任何在另一個資料庫中建立資料庫物件的嘗試都會失敗,並顯示操作無效錯誤。

建立含有分佈索引鍵、複合排序索引鍵和壓縮的資料表

下列範例會在 TICKIT 資料庫中建立 SALES 資料表,並且為數個資料欄定義壓縮。LISTID 會宣告為分佈索引鍵,而 LISTID 和 SELLERID 會宣告為多資料欄複合排序索引鍵。另外也會為資料表定義主索引鍵和外部索引鍵限制條件。在範例中建立資料表之前,如果限制條件不存在,您可能需要將 UNIQUE 條件限制新增至外部索引鍵參考的每個資料行。

create table sales( salesid integer not null, listid integer not null, sellerid integer not null, buyerid integer not null, eventid integer not null encode mostly16, dateid smallint not null, qtysold smallint not null encode mostly8, pricepaid decimal(8,2) encode delta32k, commission decimal(8,2) encode delta32k, saletime timestamp, primary key(salesid), foreign key(listid) references listing(listid), foreign key(sellerid) references users(userid), foreign key(buyerid) references users(userid), foreign key(dateid) references date(dateid)) distkey(listid) compound sortkey(listid,sellerid);

結果如下:

schemaname | tablename | column | type | encoding | distkey | sortkey | notnull -----------+-----------+------------+-----------------------------+----------+---------+---------+-------- public | sales | salesid | integer | lzo | false | 0 | true public | sales | listid | integer | none | true | 1 | true public | sales | sellerid | integer | none | false | 2 | true public | sales | buyerid | integer | lzo | false | 0 | true public | sales | eventid | integer | mostly16 | false | 0 | true public | sales | dateid | smallint | lzo | false | 0 | true public | sales | qtysold | smallint | mostly8 | false | 0 | true public | sales | pricepaid | numeric(8,2) | delta32k | false | 0 | false public | sales | commission | numeric(8,2) | delta32k | false | 0 | false public | sales | saletime | timestamp without time zone | lzo | false | 0 | false

下列範例會使用不區分大小寫的資料欄 col1 建立資料表 t1。

create table T1 ( col1 Varchar(20) collate case_insensitive ); insert into T1 values ('bob'), ('john'), ('Tom'), ('JOHN'), ('Bob');

查詢資料表:

select * from T1 where col1 = 'John'; col1 ------ john JOHN (2 rows)

使用交錯排序索引鍵建立資料表

以下範例會建立具有交錯排序索引鍵的 CUSTOMER 資料表。

create table customer_interleaved ( c_custkey integer not null, c_name varchar(25) not null, c_address varchar(25) not null, c_city varchar(10) not null, c_nation varchar(15) not null, c_region varchar(12) not null, c_phone varchar(15) not null, c_mktsegment varchar(10) not null) diststyle all interleaved sortkey (c_custkey, c_city, c_mktsegment);

使用 IF NOT EXISTS 建立資料表

下列範例會建立 CITIES 資料表,或不執行任何動作,並於該資料表已存在時傳回訊息:

create table if not exists cities( cityid integer not null, city varchar(100) not null, state char(2) not null);

建立採用 ALL 分佈的資料表

以下範例會建立採用 ALL 分佈的 VENUE 資料表。

create table venue( venueid smallint not null, venuename varchar(100), venuecity varchar(30), venuestate char(2), venueseats integer, primary key(venueid)) diststyle all;

建立採用 EVEN 分佈的資料表

以下範例會建立名為 MYEVENT 且包含三個資料欄的資料表。

create table myevent( eventid int, eventname varchar(200), eventcity varchar(30)) diststyle even;

資料表會均勻分佈且不會排序。資料表沒有宣告的 DISTKEY 或 SORTKEY 欄。

select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 'myevent'; column | type | encoding | distkey | sortkey -----------+------------------------+----------+---------+--------- eventid | integer | lzo | f | 0 eventname | character varying(200) | lzo | f | 0 eventcity | character varying(30) | lzo | f | 0 (3 rows)

建立 LIKE (類似) 另一個資料表的暫時資料表

以下範例會建立名為 TEMPEVENT 的臨時資料表,它的資料欄繼承自 EVENT 資料表。

create temp table tempevent(like event);

此資料表也會繼承其父資料表的 DISTKEY 和 SORTKEY 屬性:

select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 'tempevent'; column | type | encoding | distkey | sortkey -----------+-----------------------------+----------+---------+--------- eventid | integer | none | t | 1 venueid | smallint | none | f | 0 catid | smallint | none | f | 0 dateid | smallint | none | f | 0 eventname | character varying(200) | lzo | f | 0 starttime | timestamp without time zone | bytedict | f | 0 (6 rows)

建立具有 IDENTITY 欄位的資料表

以下範例會建立名為 VENUE_IDENT 的資料表,其中有一個名為 VENUEID 的 IDENTITY 資料欄。此資料欄從 0 開始,並隨每筆記錄遞增 1。VENUEID 也會宣告為資料表的主索引鍵。

create table venue_ident(venueid bigint identity(0, 1), venuename varchar(100), venuecity varchar(30), venuestate char(2), venueseats integer, primary key(venueid));

建立具有預設 IDENTITY 欄位的資料表

以下範例會建立名為 t1 的資料表。此資料表具有名為 hist_id 的 IDENTITY 資料欄,以及名為 base_id 的預設 IDENTITY 資料欄。

CREATE TABLE t1( hist_id BIGINT IDENTITY NOT NULL, /* Cannot be overridden */ base_id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, /* Can be overridden */ business_key varchar(10) , some_field varchar(10) );

將資料列插入至資料表顯示已同時產生 hist_idbase_id 值。

INSERT INTO T1 (business_key, some_field) values ('A','MM');
SELECT * FROM t1; hist_id | base_id | business_key | some_field ---------+---------+--------------+------------ 1 | 1 | A | MM

插入第二列顯示已產生 base_id 的預設值。

INSERT INTO T1 (base_id, business_key, some_field) values (DEFAULT, 'B','MNOP');
SELECT * FROM t1; hist_id | base_id | business_key | some_field ---------+---------+--------------+------------ 1 | 1 | A | MM 2 | 2 | B | MNOP

插入第三列顯示 base_id 的值不需要是唯一的。

INSERT INTO T1 (base_id, business_key, some_field) values (2,'B','MNNN');
SELECT * FROM t1; hist_id | base_id | business_key | some_field ---------+---------+--------------+------------ 1 | 1 | A | MM 2 | 2 | B | MNOP 3 | 2 | B | MNNN

建立具有 DEFAULT 欄位的資料表

下列範例會建立 CATEGORYDEF 資料表,它會宣告每個資料欄的預設值:

create table categorydef( catid smallint not null default 0, catgroup varchar(10) default 'Special', catname varchar(10) default 'Other', catdesc varchar(50) default 'Special events', primary key(catid)); insert into categorydef values(default,default,default,default);
select * from categorydef; catid | catgroup | catname | catdesc -------+----------+---------+---------------- 0 | Special | Other | Special events (1 row)

DISTSTYLE、DISTKEY 和 SORTKEY 選項

下列範例說明 DISTKEY、SORTKEY 和 DISTSTYLE 選項的運作方式。在此範例中,COL1 是分佈索引鍵,因此分佈樣式必須設定為 KEY,或是不設定。根據預設,資料表沒有排序索引鍵,因此不會排序:

create table t1(col1 int distkey, col2 int) diststyle key;
select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 't1'; column | type | encoding | distkey | sortkey -------+---------+----------+---------+--------- col1 | integer | az64 | t | 0 col2 | integer | az64 | f | 0

在下列範例中,會將同一個資料欄定義為分佈索引鍵和排序索引鍵。同樣地,分佈樣式必須設定為 KEY,或是不設定。

create table t2(col1 int distkey sortkey, col2 int);
select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 't2'; column | type | encoding | distkey | sortkey -------+---------+----------+---------+--------- col1 | integer | none | t | 1 col2 | integer | az64 | f | 0

在下列範例中,不會將任何資料欄設定為分佈索引鍵,COL2 會設定為排序索引鍵,而分佈樣式會設定為 ALL:

create table t3(col1 int, col2 int sortkey) diststyle all;
select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 't3'; Column | Type | Encoding | DistKey | SortKey -------+---------+----------+---------+-------- col1 | integer | az64 | f | 0 col2 | integer | none | f | 1

在下列範例中,分佈樣式會設定為 EVEN,但不會明確定義任何排序索引鍵;因此,資料表會均勻分佈,但不會排序。

create table t4(col1 int, col2 int) diststyle even;
select "column", type, encoding, distkey, sortkey from pg_table_def where tablename = 't4'; column | type |encoding | distkey | sortkey --------+---------+---------+---------+-------- col1 | integer | az64 | f | 0 col2 | integer | az64 | f | 0

使用 ENCODE AUTO 選項建立資料表

下列範例會建立具有自動壓縮編碼的 t1 資料表。如果您沒有在任何資料欄上指定編碼類型,ENCODE AUTO 就是資料表的預設值。

create table t1(c0 int, c1 varchar);

下列範例會透過指定 ENCODE AUTO 建立具有自動壓縮編碼的 t2 資料表。

create table t2(c0 int, c1 varchar) encode auto;

下列範例會透過指定 ENCODE AUTO 建立具有自動壓縮編碼的 t3 資料表。資料欄 c0 是以初始編碼類型 DELTA 所定義。如果其他編碼提供更好的查詢效能,Amazon Redshift 可以變更編碼。

create table t3(c0 int encode delta, c1 varchar) encode auto;

下列範例會透過指定 ENCODE AUTO 建立具有自動壓縮編碼的 t4 資料表。資料欄 c0 是以初始編碼 DELTA 來定義,而資料欄 c1 則以 LZO 初始編碼來定義。如果其他編碼提供更好的查詢效能,Amazon Redshift 可以變更這些編碼。

create table t4(c0 int encode delta, c1 varchar encode lzo) encode auto;