title | summary | toc |
---|---|---|
ALTER COLUMN |
Use the ALTER COLUMN statement to change a column's Default constraint. |
false |
The ALTER COLUMN
statement is part of ALTER TABLE
and changes a column's Default constraint or drops the Not Null constraint.
{{site.data.alerts.callout_info}}To manage other constraints, see ADD CONSTRAINT
and DROP CONSTRAINT
{{site.data.alerts.end}}
{% include sql/diagrams/alter_column.html %}
The user must have the CREATE
privilege on the table.
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. |
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;
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;
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;