ALTER COLUMN

On this page Carat arrow pointing down

The ALTER COLUMN statement is part of ALTER TABLE and sets, changes, or drops a column's Default constraint or drops the Not Null constraint.

Note:
To manage other constraints, see ADD CONSTRAINT and DROP CONSTRAINT

Synopsis

ALTER TABLE IF EXISTS table_name ALTER COLUMN column_name SET DEFAULT a_expr DROP DEFAULT NOT NULL

Required Privileges

The user must have the CREATE privilege on the table.

Parameters

Parameter Description
table_name The name of the table with the column you want to modify.
column_name The name of the column you want to modify.
a_expr The new Default Value you want to use.

Viewing Schema Changes

Whenever you initiate a schema change, CockroachDB registers it as a job, which you can view with SHOW JOBS.

Examples

Set or Change a Default Value

Setting the Default Value constraint inserts the value when data's written to the table without explicitly defining the value for the column. If the column already has a Default Value set, you can use this statement to change it.

The below example inserts the Boolean value true whenever you inserted data to the subscriptions table without defining a value for the newsletter column.

> ALTER TABLE subscriptions ALTER COLUMN newsletter SET DEFAULT true;

Remove Default Constraint

If the column has a defined Default Value, you can remove the constraint, which means the column will no longer insert a value by default if one is not explicitly defined for the column.

> ALTER TABLE subscriptions ALTER COLUMN newsletter DROP DEFAULT;

Remove Not Null Constraint

If the column has the Not Null constraint applied to it, you can remove the constraint, which means the column becomes optional and can have NULL values written into it.

> ALTER TABLE subscriptions ALTER COLUMN newsletter DROP NOT NULL;

See Also


Yes No
On this page

Yes No