ALTER TABLE … ALTER COLUMN (original) (raw)

This topic describes how to modify one or more column properties for a table using an ALTER COLUMN clause in aALTER TABLE statement.

The following table describes the supported/unsupported actions for modifying column properties:

Action Supported Unsupported Notes
Default Values
Drop the default for a column (i.e. DROP DEFAULT). Not allowed if the column and default were defined by an ALTER TABLE command. For details, see the Usage Notes below.
Change the default sequence for a column (i.e. SET DEFAULT seq_name.NEXTVAL). Use only for columns that have a sequence already.
Change the default for a column, unless the default is a sequence.
Add a default for a column.
Nullability
Change the nullability of a column (i.e. SET NOT NULL or DROP NOT NULL).
Data Types
Change a column data type to a synonymous type (for example, STRING to VARCHAR).
Change a column data type to a different type (for example, STRING to NUMBER).
Increase the length of a text string column (for example, VARCHAR(50) to VARCHAR(100)).
Decrease the length of a text string column (for example, VARCHAR(50) to VARCHAR(25)).
Increase the length of a binary string column (for example, BINARY(50) to BINARY(100)).
Decrease the length of a binary string column (for example, BINARY(50) to BINARY(25)).
Increase the precision of a number column (for example, NUMBER(10,2) to NUMBER(20,2)).
Decrease the precision of a number column (for example, NUMBER(20,2) to NUMBER(10,2)). Only allowed if the new precision is sufficient to hold all values currently in the column. In addition, decreasing the precision can impact Time Travel (see Usage Notes for details).
Change the scale of a number column (for example, NUMBER(10,2) to NUMBER(10,4)).
Comments
Set or unset the comment for a column.
Masking Policy
Set or unset a masking policy on a column.
Projection Policy
Set or unset a projection policy on a column.
Object Tagging
Set or unset a tag on a column A column can support up to 20 tags, and the maximum number of characters for a tag string value is 256.

See also:

ALTER TABLE , CREATE TABLE , DROP TABLE , SHOW TABLES , DESCRIBE TABLE

Syntax

ALTER TABLE { 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 ] ... ] [ , ... ] [ ) ]

ALTER TABLE { ALTER | MODIFY } [ COLUMN ] dataGovnPolicyTagAction

Usage notes

Examples

Example setup:

CREATE OR REPLACE TABLE t1 ( c1 NUMBER NOT NULL, c2 NUMBER DEFAULT 3, c3 NUMBER DEFAULT seq1.nextval, c4 VARCHAR(20) DEFAULT 'abcde', c5 STRING);

DESC TABLE t1;

+------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------+ | name | type | kind | null? | default | primary key | unique key | check | expression | comment | |------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------| | C1 | NUMBER(38,0) | COLUMN | N | NULL | N | N | NULL | NULL | NULL | | C2 | NUMBER(38,0) | COLUMN | Y | 3 | N | N | NULL | NULL | NULL | | C3 | NUMBER(38,0) | COLUMN | Y | DB1.PUBLIC.SEQ1.NEXTVAL | N | N | NULL | NULL | NULL | | C4 | VARCHAR(20) | COLUMN | Y | 'abcde' | N | N | NULL | NULL | NULL | | C5 | VARCHAR(16777216) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | +------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------+

Make the following changes to t1:

ALTER TABLE t1 ALTER COLUMN c1 DROP NOT NULL;

ALTER TABLE t1 MODIFY c2 DROP DEFAULT, c3 SET DEFAULT seq5.nextval ;

ALTER TABLE t1 ALTER c4 SET DATA TYPE VARCHAR(50), COLUMN c4 DROP DEFAULT;

ALTER TABLE t1 ALTER c5 COMMENT '50 character column';

DESC TABLE t1;

+------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------------------+ | name | type | kind | null? | default | primary key | unique key | check | expression | comment | |------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------------------| | C1 | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | | C2 | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | | C3 | NUMBER(38,0) | COLUMN | Y | DB1.PUBLIC.SEQ5.NEXTVAL | N | N | NULL | NULL | NULL | | C4 | VARCHAR(50) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | | C5 | VARCHAR(16777216) | COLUMN | Y | NULL | N | N | NULL | NULL | 50 character column | +------+-------------------+--------+-------+-------------------------+-------------+------------+-------+------------+---------------------+

Same as previous example, but with the following changes to illustrate the versatility/flexibility of the command:

ALTER TABLE t1 ALTER ( c1 DROP NOT NULL, c5 COMMENT '50 character column', c4 TYPE VARCHAR(50), c2 DROP DEFAULT, COLUMN c4 DROP DEFAULT, COLUMN c3 SET DEFAULT seq5.nextval );

This example produces the same results.

Apply a Column-level Security masking policy to a table column:

-- single column

ALTER TABLE empl_info MODIFY COLUMN empl_id SET MASKING POLICY mask_empl_id;

-- multiple columns

ALTER TABLE empl_info MODIFY COLUMN empl_id SET MASKING POLICY mask_empl_id , COLUMN empl_dob SET MASKING POLICY mask_empl_dob ;

Unset a Column-level Security masking policy from a table column:

-- single column

ALTER TABLE empl_info modify column empl_id unset masking policy;

-- multiple columns

ALTER TABLE empl_info MODIFY COLUMN empl_id UNSET MASKING POLICY , COLUMN empl_dob UNSET MASKING POLICY ;