How to add a column with a default value to an existing table in SQL Server
How to add a column with a default value to an existing table in SQL Server
27612-Apr-2023
Updated on 13-Apr-2023
Home / DeveloperSection / Forums / How to add a column with a default value to an existing table in SQL Server
How to add a column with a default value to an existing table in SQL Server
Krishnapriya Rajeev
13-Apr-2023To add a column with a default value to an existing table in SQL Server, we can use the ALTER TABLE statement with the ADD COLUMN clause. Here's the basic syntax:
In this syntax, table_name is the name of the table to which we want to add the column, column_name is the name of the new column, data_type is the data type of the column, and default_value is the default value that we want to assign to the column.
Here's an example SQL query that demonstrates how we can add a column with a default value to an existing table:
This query will add a new column named membership_status to the customer's table with a data type of VARCHAR(20), and a default value of Regular. If a new row is inserted into the table without specifying a value for the membership_status column, it will automatically be assigned the value Regular.