ALTER TABLE

Function

Modifies tables, including modifying table definitions, renaming tables, renaming specified columns in tables, renaming table constraints, setting table schemas, enabling or disabling row-level access control, and adding or updating multiple columns.

Precautions

Syntax

Parameter Description

Table Operation Examples

Rename a table:

1
ALTER TABLE CUSTOMER RENAME TO CUSTOMER_t;

Add a new table constraint:

1
ALTER TABLE customer_address ADD PRIMARY KEY(ca_address_sk);

Adds primary key constraint or unique constraint based on the unique index.

Create an index CUSTOMER_constraint1 for the table CUSTOMER. Then add primary key constraints, and rename the created index.

1
2
CREATE UNIQUE INDEX CUSTOMER_constraint1 ON CUSTOMER(C_CUSTKEY);
ALTER TABLE CUSTOMER ADD CONSTRAINT CUSTOMER_constraint2 PRIMARY KEY USING INDEX CUSTOMER_constraint1;

Rename a table constraint:

1
ALTER TABLE CUSTOMER RENAME CONSTRAINT CUSTOMER_constraint2 TO CUSTOMER_constraint;

Delete a table constraint:

1
ALTER TABLE CUSTOMER DROP CONSTRAINT CUSTOMER_constraint;

Add a table index:

1
ALTER TABLE CUSTOMER ADD INDEX CUSTOMER_index(C_CUSTKEY);

Delete a table index:

1
2
ALTER TABLE CUSTOMER DROP INDEX CUSTOMER_index;
ALTER TABLE CUSTOMER DROP KEY CUSTOMER_index;

Add a partial cluster key to a column-store table:

1
ALTER TABLE customer_address ADD CONSTRAINT customer_address_cluster PARTIAL CLUSTER KEY(ca_address_sk);

Delete a partial cluster column from the column-store table.

1
ALTER TABLE customer_address DROP CONSTRAINT customer_address_cluster;

Switch the storage format of a column-store table:

1
ALTER TABLE customer_address SET (COLVERSION = 1.0);

Change the distribution mode of a table:

1
ALTER TABLE customer_address DISTRIBUTE BY REPLICATION;

Change the schema of a table:

1
ALTER TABLE customer_address SET SCHEMA tpcds;

Change the data temperature for a single table:

1
ALTER TABLE cold_hot_table REFRESH STORAGE;

Change a column-store partitioned table to a hot and cold table.

1
2
3
4
5
6
7
CREATE table test_1(id int,d_time date)
WITH(ORIENTATION=COLUMN)
DISTRIBUTE BY HASH (id)
PARTITION BY RANGE (d_time)
(PARTITION p1 START('2022-01-01') END('2022-01-31') EVERY(interval '1 day'))

ALTER TABLE test_1 SET (storage_policy = 'LMT:100');

Column Operation Examples

Add a column to a table:

1
ALTER TABLE warehouse_t ADD W_GOODS_CATEGORY int;

Modify the column name and column field information in the table:

1
ALTER TABLE warehouse_t CHANGE W_GOODS_CATEGORY W_GOODS_CATEGORY2 DECIMAL NOT NULL COMMENT 'W_GOODS_CATEGORY';

Add a primary key to a table:

1
ALTER TABLE warehouse_t ADD PRIMARY KEY(w_warehouse_name);

Rename a column:

1
ALTER TABLE CUSTOMER RENAME C_PHONE TO new_C_PHONE;

Add columns to a table:

1
ALTER TABLE CUSTOMER ADD (C_COMMENT VARCHAR(117) NOT NULL, C_COUNT int);

Change the data type of a column in the table and set the column constraint to NOT NULL:

1
ALTER TABLE CUSTOMER MODIFY C_MKTSEGMENT varchar(20) NOT NULL;

Add the NOT NULL constraint to a certain column in the table:

1
ALTER TABLE CUSTOMER ALTER COLUMN C_PHONE SET NOT NULL;

Delete a column from a table:

1
ALTER TABLE CUSTOMER DROP COLUMN C_COUNT;

Add an index to a column in the table:

1
ALTER TABLE customer_address MODIFY ca_address_id varchar(20) CONSTRAINT ca_address_index CHECK (ca_address_id > 0);

Add a timestamp column with the ON UPDATE expression to the customer_address table:

1
ALTER TABLE customer_address ADD COLUMN C_TIME timestamp on update current_timestamp;

Delete the timestamp column with the ON UPDATE expression from the customer_address:

1
ALTER TABLE customer_address MODIFY COLUMN C_TIME timestamp on update NULL;

Helpful Links

CREATE TABLE, 12.101-RENAME TABLE, and DROP TABLE