ALTER TABLE¶
Modifies the properties, columns, or constraints for an existing table.
- See also:
ALTER TABLE … ALTER COLUMN , CREATE TABLE , DROP TABLE , SHOW TABLES , DESCRIBE TABLE
Syntax¶
ALTER TABLE [ IF EXISTS ] RENAME TO
ALTER TABLE [ IF EXISTS ] SWAP WITH
ALTER TABLE [ IF EXISTS ] { clusteringAction | tableColumnAction | constraintAction }
ALTER TABLE [ IF EXISTS ] dataMetricFunctionAction
ALTER TABLE [ IF EXISTS ] dataGovnPolicyTagAction
ALTER TABLE [ IF EXISTS ] extTableColumnAction
ALTER TABLE [ IF EXISTS ] searchOptimizationAction
ALTER TABLE [ IF EXISTS ] SET
[ DATA_RETENTION_TIME_IN_DAYS = ]
[ MAX_DATA_EXTENSION_TIME_IN_DAYS = ]
[ CHANGE_TRACKING = { TRUE | FALSE } ]
[ DEFAULT_DDL_COLLATION = '' ]
[ ENABLE_SCHEMA_EVOLUTION = { TRUE | FALSE } ]
[ CONTACT ( = [ , = ... ] ) ]
[ COMMENT = '' ]
ALTER TABLE [ IF EXISTS ] UNSET {
DATA_RETENTION_TIME_IN_DAYS |
MAX_DATA_EXTENSION_TIME_IN_DAYS |
CHANGE_TRACKING |
DEFAULT_DDL_COLLATION |
ENABLE_SCHEMA_EVOLUTION |
CONTACT |
COMMENT |
}
[ , ... ]
Where:
clusteringAction ::= { CLUSTER BY ([ , , ... ] ) /* RECLUSTER is deprecated */ | RECLUSTER [ MAX_SIZE = ] [ WHERE ] /* { SUSPEND | RESUME } RECLUSTER is valid action */ | { SUSPEND | RESUME } RECLUSTER | DROP CLUSTERING KEY } tableColumnAction ::= { ADD [ COLUMN ] [ IF NOT EXISTS ][ { DEFAULT | { AUTOINCREMENT | IDENTITY } /* AUTOINCREMENT (or IDENTITY) is supported only for */ /* columns with numeric data types (NUMBER, INT, FLOAT, etc.). */ /* Also, if the table is not empty (that is, if the table contains */ /* any rows), only DEFAULT can be altered. */ [ { ( , ) | START INCREMENT } ] [ { ORDER | NOORDER } ] } ] [ inlineConstraint ] [ COLLATE ' ' ] | RENAME COLUMNTO | ALTER | MODIFY [ ( ] [ COLUMN ] DROP DEFAULT , [ COLUMN ] SET DEFAULT .NEXTVAL , [ COLUMN ] { [ SET ] NOT NULL | DROP NOT NULL } , [ COLUMN ] [ [ SET DATA ] TYPE ] , [ COLUMN ] COMMENT ' ' , [ COLUMN ]UNSET COMMENT [ , [ COLUMN ] ... ] [ , ... ] [ ) ] | DROP [ COLUMN ] [ IF EXISTS ] [, ... ] } inlineConstraint ::= [ NOT NULL ] [ CONSTRAINT ] { UNIQUE | PRIMARY KEY | { [ FOREIGN KEY ] REFERENCES [ ( ) ] } } [ ] For detailed syntax and examples for altering columns, see ALTER TABLE … ALTER COLUMN. .
For detailed syntax and examples for creating/altering inline constraints, see CREATE | ALTER TABLE … CONSTRAINT.
dataMetricFunctionAction ::= SET DATA_METRIC_SCHEDULE = { 'MINUTE' | 'USING CRON| 'TRIGGER_ON_CHANGES' } | UNSET DATA_METRIC_SCHEDULE | { ADD | DROP } DATA METRIC FUNCTION ' ON ( [ , ... ] [ , TABLE ( [ , ... ] ) ] ) [ , ON ( [ , ... ] [ , TABLE ( [ , ... ] ) ] ) ] | MODIFY DATA METRIC FUNCTION ON ( [ , ... ] [ , TABLE ( [ , ... ] ) ] ) { SUSPEND | RESUME } [ , ON ( [ , ... ] [ , TABLE ( [ , ... ] ) ] ) { SUSPEND | RESUME } ] dataGovnPolicyTagAction ::= { SET TAG= ' ' [ ,= ' ' ... ] | UNSET TAG[ , ... ] } | { ADD ROW ACCESS POLICY ON ( [ , ... ] ) | DROP ROW ACCESS POLICY | DROP ROW ACCESS POLICY , ADD ROW ACCESS POLICY ON ( [ , ... ] ) | DROP ALL ROW ACCESS POLICIES } | { SET AGGREGATION POLICY [ ENTITY KEY ( [, ... ] ) ] [ FORCE ] | UNSET AGGREGATION POLICY } | { SET JOIN POLICY [ FORCE ] | UNSET JOIN POLICY } | ADD [ COLUMN ] [ IF NOT EXISTS ] [ [ WITH ] MASKING POLICY [ USING ( , , ... ) ] ] [ [ WITH ] PROJECTION POLICY ] [ [ WITH ] TAG ( = ' ' [ ,= ' ' , ... ] ) ] | { { ALTER | MODIFY } [ COLUMN ]SET MASKING POLICY [ USING ( , , ... ) ] [ FORCE ] | UNSET MASKING POLICY } | { { ALTER | MODIFY } [ COLUMN ] SET PROJECTION POLICY [ FORCE ] | UNSET PROJECTION POLICY } | { ALTER | MODIFY } [ COLUMN ] SET TAG = ' ' [ ,= ' ' ... ] , [ COLUMN ]SET TAG = ' ' [ ,= ' ' ... ] | { ALTER | MODIFY } [ COLUMN ]UNSET TAG [ , ... ] , [ COLUMN ] UNSET TAG [ , ... ] extTableColumnAction ::= { ADD [ COLUMN ] [ IF NOT EXISTS ]AS ( ) | RENAME COLUMN TO | DROP [ COLUMN ] [ IF EXISTS ] [, ... ] } constraintAction ::= { ADD outoflineConstraint | RENAME CONSTRAINTTO | { ALTER | MODIFY } { CONSTRAINT | PRIMARY KEY | UNIQUE | FOREIGN KEY } ( [ , ... ] ) [ [ NOT ] ENFORCED ] [ VALIDATE | NOVALIDATE ] [ RELY | NORELY ] | DROP { CONSTRAINT | PRIMARY KEY | UNIQUE | FOREIGN KEY } ( [ , ... ] ) [ CASCADE | RESTRICT ] } outoflineConstraint ::= [ CONSTRAINT ] { UNIQUE [ ( [ , , ... ] ) ] | PRIMARY KEY [ ( [ , , ... ] ) ] | [ FOREIGN KEY ] [ ( [ , , ... ] ) ] REFERENCES [ ( [ , , ... ] ) ] } [ ] For detailed syntax and examples for creating/altering out-of-line constraints, see CREATE | ALTER TABLE … CONSTRAINT.
searchOptimizationAction ::= { ADD SEARCH OPTIMIZATION [ ON[ , ... ] ] | DROP SEARCH OPTIMIZATION [ ON { | | } [ , ... ] ] } For details, see Search optimization actions (searchOptimizationAction).
Parameters¶
name
Identifier for the table to alter. If the identifier contains spaces or special characters, the entire string must be enclosed in double quotes. Identifiers enclosed in double quotes are also case sensitive.
RENAME TO new_table_name
Renames the specified table with a new identifier that is not currently used by any other tables in the schema.
For more information about table identifiers, see Identifier requirements.
You can move the object to a different database and/or schema while optionally renaming the object. To do so, specify a qualified
new_name
value that includes the new database and/or schema name in the formdb_name.schema_name.object_name
orschema_name.object_name
, respectively.Note
The destination database and/or schema must already exist. In addition, an object with the same name cannot already exist in the new location; otherwise, the statement returns an error.
Moving an object to a managed access schema is prohibited unless the object owner (that is, the role that has the OWNERSHIP privilege on the object) also owns the target schema.
When an object (table, column, etc.) is renamed, other objects that reference it must be updated with the new name.
SWAP WITH target_table_name
Swap renames two tables in a single transaction.
Note that swapping a permanent or transient table with a temporary table, which persists only for the duration of the user session in which it was created, is not allowed. This restriction prevents a naming conflict that could occur when a temporary table is swapped with a permanent or transient table, and an existing permanent or transient table has the same name as the temporary table. To swap a permanent or transient table with a temporary table, use three
ALTER TABLE ... RENAME TO
statements: Rename tablea
toc
,b
toa
, and thenc
tob
.
Note
To rename a table or swap two tables, the role used to perform the operation must have OWNERSHIP privileges on the table(s). In addition, renaming a table requires the CREATE TABLE privilege on the schema for the table.
SET ...
Specifies one or more properties/parameters to set for the table (separated by blank spaces, commas, or new lines):
DATA_RETENTION_TIME_IN_DAYS = integer
Object-level parameter that modifies the retention period for the table for Time Travel. For more information, see Understanding & using Time Travel and Working with Temporary and Transient Tables.
For a detailed description of this parameter, as well as more information about object parameters, see Parameters.
Values:
Standard Edition:
0
or1
Enterprise Edition:
0
to90
for permanent tables0
or1
for temporary and transient tables
Note
A value of
0
effectively disables Time Travel for the table.MAX_DATA_EXTENSION_TIME_IN_DAYS = integer
Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for the table to prevent streams on the table from becoming stale.
For a detailed description of this parameter, see MAX_DATA_EXTENSION_TIME_IN_DAYS.
CHANGE_TRACKING = TRUE | FALSE
Specifies to enable or disable change tracking on the table.
TRUE
enables change tracking on the table. This option adds several hidden columns to the source table and begins storing change tracking metadata in the columns. These columns consume a small amount of storage.The change tracking metadata can be queried using the CHANGES clause for SELECT statements, or by creating and querying one or more streams on the table.
FALSE
disables change tracking on the table. Associated hidden columns are dropped from the table.
DEFAULT_DDL_COLLATION = 'collation_specification'
Specifies a default collation specification for any new columns added to the table.
Setting the parameter does not change the collation specification for any existing columns.
For more information about the parameter, see DEFAULT_DDL_COLLATION.
ENABLE_SCHEMA_EVOLUTION = { TRUE | FALSE }
Enables or disables automatic changes to the table schema from data loaded into the table from source files, including:
Added columns.
By default, schema evolution is limited to a maximum of 10 added columns per load operation. To request more than 10 added columns per load operation, contact Snowflake Support.
The NOT NULL constraint can be dropped from any number of columns missing in new data files.
Setting it to
TRUE
enables automatic table schema evolution. The defaultFALSE
disables automatic table schema evolution.Note
Loading data from files evolves the table columns when all of the following are true:
The COPY INTO