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

To manage other constraints, see ADD CONSTRAINT and DROP CONSTRAINT

Synopsis

ALTER TABLE IF EXISTS table_name ALTER 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 whose Default Value you want to modify.
name The name of the column you want to modify.
a_expr The new Default Value you want to use.

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