本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
ALTER TABLE 範例
以下範例示範 ALTER TABLE 命令的基本用法。
重新命名資料表或檢視
以下命令會將 USERS 資料表重新命名為 USERS_BKUP:
alter table users rename to users_bkup;
您也可以使用此類型的命令來重新命名檢視。
變更資料表或檢視的擁有者
以下命令會將 VENUE 資料表擁有者變更為使用者 DWUSER:
alter table venue owner to dwuser;
以下命令會建立檢視,然後變更其擁有者:
create view vdate as select * from date; alter table vdate owner to vuser;
重新命名欄位
以下命令會將 VENUE 資料表中的 VENUESEATS 資料欄重新命名為 VENUESIZE:
alter table venue rename column venueseats to venuesize;
丟棄資料表限制條件
若要捨棄資料表限制條件,像是主索引鍵、外部索引鍵或唯一限制條件,請先找到限制條件的內部名稱。然後在 ALTER TABLE 命令中指定限制條件名稱。以下範例會尋找 CATEGORY 資料表的限制條件,然後刪除名稱為 category_pkey
的主索引鍵。
select constraint_name, constraint_type from information_schema.table_constraints where constraint_schema ='public' and table_name = 'category';
constraint_name | constraint_type ----------------+---------------- category_pkey | PRIMARY KEY
alter table category drop constraint category_pkey;
變更 VARCHAR 資料欄
若要節省儲存體,您可以定義一個資料表,其中的 VARCHAR 資料欄一開始具有您目前資料需求所需的最低大小。之後若要容納較長的字串,則可以變更資料表以增加資料欄的大小。
以下範例會將 EVENTNAME 資料欄的大小增加為 VARCHAR (300)。
alter table event alter column eventname type varchar(300);
修改資料欄的壓縮編碼。
您可以修改資料欄的壓縮編碼。您可以在下方找到一組示範此方法的範例。這些範例的資料表定義如下。
create table t1(c0 int encode lzo, c1 bigint encode zstd, c2 varchar(16) encode lzo, c3 varchar(32) encode zstd);
下列陳述式會將資料欄 c0 的壓縮編碼從 LZO 編碼修改為 AZ64 編碼。
alter table t1 alter column c0 encode az64;
下列陳述式會將資料欄 c1 的壓縮編碼從 Zstandard 編碼修改為 AZ64 編碼。
alter table t1 alter column c1 encode az64;
下列陳述式會將資料欄 c2 的壓縮編碼從 LZO 編碼修改為 Byte-Dictionary 編碼。
alter table t1 alter column c2 encode bytedict;
下列陳述式會將資料欄 c3 的壓縮編碼從 Zstandard 編碼修改為 Runlength 編碼。
alter table t1 alter column c3 encode runlength;
修改 DISTSTYLE KEY DISTKEY 欄位
下列範例示範如何變更資料表的 DISTSTYLE 和 DISTKEY。
建立採用 EVEN 分佈樣式的資料表。SVV_TABLE_INFO 檢視顯示 DISTSTYLE 為 EVEN。
create table inventory( inv_date_sk int4 not null , inv_item_sk int4 not null , inv_warehouse_sk int4 not null , inv_quantity_on_hand int4 ) diststyle even; Insert into inventory values(1,1,1,1); select "table", "diststyle" from svv_table_info;
table | diststyle -----------+---------------- inventory | EVEN
將資料表 DISTKEY 改為 inv_warehouse_sk
。SVV_TABLE_INFO 檢視將 inv_warehouse_sk
資料欄顯示為產生的分佈金鑰。
alter table inventory alter diststyle key distkey inv_warehouse_sk; select "table", "diststyle" from svv_table_info;
table | diststyle -----------+----------------------- inventory | KEY(inv_warehouse_sk)
將資料表 DISTKEY 改為 inv_item_sk
。SVV_TABLE_INFO 檢視將 inv_item_sk
資料欄顯示為產生的分佈金鑰。
alter table inventory alter distkey inv_item_sk; select "table", "diststyle" from svv_table_info;
table | diststyle -----------+----------------------- inventory | KEY(inv_item_sk)
將資料表改為 DISTSTYLE ALL
下列範例示範如何將資料表變更為 DISTSTYLE ALL。
建立採用 EVEN 分佈樣式的資料表。SVV_TABLE_INFO 檢視顯示 DISTSTYLE 為 EVEN。
create table inventory( inv_date_sk int4 not null , inv_item_sk int4 not null , inv_warehouse_sk int4 not null , inv_quantity_on_hand int4 ) diststyle even; Insert into inventory values(1,1,1,1); select "table", "diststyle" from svv_table_info;
table | diststyle -----------+---------------- inventory | EVEN
將資料表 DISTSTYLE 改為 ALL。SVV_TABLE_INFO 檢視會顯示已變更的 DISTSYTLE。
alter table inventory alter diststyle all; select "table", "diststyle" from svv_table_info;
table | diststyle -----------+---------------- inventory | ALL
修改資料表 SORTKEY
您可以修改資料表以具有複合排序索引鍵或沒有排序鍵。
在下列資料表定義中,資料表 t1
是以交錯排序索引鍵來定義。
create table t1 (c0 int, c1 int) interleaved sortkey(c0, c1);
下列命令會將資料表從交錯排序索引鍵修改為複合排序索引鍵。
alter table t1 alter sortkey(c0, c1);
下列命令會修改資料表以移除交錯排序索引鍵。
alter table t1 alter sortkey none;
在下列資料表定義中,資料表 t1
是以 c0
作為交錯排序索引鍵來定義。
create table t1 (c0 int, c1 int) sortkey(c0);
下列命令會將資料表 t1
修改為複合排序索引鍵。
alter table t1 alter sortkey(c0, c1);
將資料表修改為 ENCODE AUTO
下列範例顯示如何將資料表修改為 ENCODE AUTO。
此範例的資料表定義如下。資料欄 c0
是以編碼類型 AZ64 定義,而資料欄 c1
是以編碼類型 LZO 定義。
create table t1(c0 int encode AZ64, c1 varchar encode LZO);
對於此資料表,下列陳述式會將編碼變更為 AUTO。
alter table t1 alter encode auto;
下列範例會顯示如何將資料表修改為移除 ENCODE AUTO 設定。
此範例的資料表定義如下。資料表資料欄不需編碼即可定義。在這種情況下,編碼會預設為 ENCODE AUTO。
create table t2(c0 int, c1 varchar);
對於此資料表,下列陳述式會將資料欄 c0 的編碼變更為 LZO。資料表編碼不再設定為 ENCODE AUTO。
alter table t2 alter column c0 encode lzo;;
修改資料列層級安全性控制
下列命令會關閉資料表的 RLS:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY OFF;
下列命令會開啟資料表的 RLS:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY ON;
下列命令會開啟資料表的 RLS,並使其可透過資料共用存取:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY ON; ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY FOR DATASHARES OFF;
下列命令會開啟資料表的 RLS,並使其不可透過資料共用存取:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY ON; ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY FOR DATASHARES ON;
下列命令會開啟 RLS,並將資料表的 RLS 結合類型設定為 OR:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY ON CONJUNCTION TYPE OR;
下列命令會開啟 RLS,並將資料表的 RLS 結合類型設定為 AND:
ALTER TABLE tickit_category_redshift ROW LEVEL SECURITY ON CONJUNCTION TYPE AND;