ALTER TABLE

Function

ALTER TABLE is used to modify 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.

Important Notes

Syntax

Parameter Description

Example 1: Operations on Tables

Move a table to another schema.

1
ALTER TABLE tpcds.warehouse_t19 SET SCHEMA joe;

When renaming an existing table, the new table name cannot be prefixed with the schema name of the original table.

1
ALTER TABLE joe.warehouse_t19 RENAME TO warehouse_t23;

Change the distribution mode of the tpcds.warehouse_t22 table to REPLICATION.

1
ALTER TABLE tpcds.warehouse_t22 DISTRIBUTE BY REPLICATION;

Change the distribution column of the tpcds.warehouse_t22 table to W_WAREHOUSE_SK.

1
ALTER TABLE tpcds.warehouse_t22 DISTRIBUTE BY HASH(W_WAREHOUSE_SK);

Switch the storage format of a column-store table.

1
ALTER TABLE tpcds.warehouse_t18 SET (COLVERSION = 1.0);

Disable the delta table function of the column-store table.

1
ALTER TABLE tpcds.warehouse_t21 SET (ENABLE_DELTA = OFF);

Disable the SKIP_FPI_HINT function of the table.

1
ALTER TABLE tpcds.warehouse_t22 SET (SKIP_FPI_HINT = FALSE);

Change the data temperature for a single table.

1
ALTER TABLE tpcds.warehouse_t23 REFRESH STORAGE;

Change the data temperature for multiple tables in batches.

SELECT pg_refresh_storage();

Example 2: Operations on Table Constraints

Create an index ds_warehouse_t1_index1 for the table tpcds.warehouse_t1. Then add primary key constraints, and rename the created index.

1
2
CREATE UNIQUE INDEX ds_warehouse_t1_index1 ON tpcds.warehouse_t1(W_WAREHOUSE_SK);
ALTER TABLE tpcds.warehouse_t1 ADD CONSTRAINT ds_warehouse_t1_index2 PRIMARY KEY USING INDEX ds_warehouse_t1_index1;

Delete the primary key ds_warehouse_t1_index2 from the table tpcds.warehouse_t1.

1
ALTER TABLE tpcds.warehouse_t1 DROP CONSTRAINT ds_warehouse_t1_index2;

If no partial clusters have been specified in a column-store table, add a partial cluster to the table.

1
ALTER TABLE tpcds.warehouse_t17 ADD PARTIAL CLUSTER KEY(W_WAREHOUSE_SK);

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

1
ALTER TABLE tpcds.warehouse_t17 DROP CONSTRAINT warehouse_t17_cluster;

Add a Not-Null constraint to an existing column.

1
ALTER TABLE tpcds.warehouse_t19 ALTER COLUMN W_GOODS_CATEGORY SET NOT NULL;

Remove Not-Null constraints from an existing column.

1
ALTER TABLE tpcds.warehouse_t19 ALTER COLUMN W_GOODS_CATEGORY DROP NOT NULL;

Add a check constraint to the tpcds.warehouse_t19 table.

1
ALTER TABLE tpcds.warehouse_t19 ADD CONSTRAINT W_CONSTR_KEY4 CHECK (W_STATE <> '');

Example 3: Operations on Columns

Add a primary key to the tpcds.warehouse_t1 table.

1
ALTER TABLE tpcds.warehouse_t1 ADD PRIMARY KEY(W_WAREHOUSE_SK);

Add a varchar column to the tpcds.warehouse_t19 table.

1
ALTER TABLE tpcds.warehouse_t19 ADD W_GOODS_CATEGORY varchar(30);

Use one statement to alter the types of two existing columns.

1
2
3
ALTER TABLE tpcds.warehouse_t19
ALTER COLUMN W_GOODS_CATEGORY TYPE varchar(80),
ALTER COLUMN W_STREET_NAME TYPE varchar(100);

This statement is equivalent to the preceding statement.

1
ALTER TABLE tpcds.warehouse_t19 MODIFY (W_GOODS_CATEGORY varchar(30), W_STREET_NAME varchar(60));

Delete a column from the tpcds.warehouse_t23 table.

1
ALTER TABLE tpcds.warehouse_t23 DROP COLUMN W_STREET_NAME;

Links

CREATE TABLE, DROP TABLE