SQL Server Default Values: Complete Guide
In SQL Server, you can use the DEFAULT constraint to set default values. Here are the steps to set default values:
When creating a table, you can use the DEFAULT keyword in the field definition to set a default value. For example:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT DEFAULT 30
);
In the example above, the default value for the field “Age” is set to 30.
If you need to add a default value to an existing table, you can use the ALTER TABLE statement to add a DEFAULT constraint. For example:
ALTER TABLE Employees
ADD CONSTRAINT DF_Employees_Age DEFAULT 30 FOR Age;
In the example above, the default value for the field “Age” is set to 30.
To change an existing default value, you can use the ALTER TABLE statement to modify the existing DEFAULT constraint. For example:
ALTER TABLE Employees
ADD CONSTRAINT DF_Employees_Age DEFAULT 40 FOR Age;
In the example above, the default value of the field “Age” has been changed to 40.
By using the above methods, you can set and modify default values for fields in SQL Server.