Set Default Value for Column
Use an ALTER TABLE statement:
Execute the following query, replacing
your_table_name
with the actual name of your table anddefault_value
with the desired default value:ALTER TABLE your_table_name ADD DEFAULT default_value FOR column_name;
- This will add a default constraint to the specified column, assigning the specified default value to any new rows inserted into the table without a value provided for that column.
Modify the existing default constraint:
If the column already has a default constraint, you can modify its value using the following query:
ALTER TABLE your_table_name DROP CONSTRAINT default_constraint_name; ALTER TABLE your_table_name ADD DEFAULT new_default_value FOR column_name;
- Replace
default_constraint_name
with the actual name of the existing default constraint andnew_default_value
with the new desired default value.
- Replace
Example:
Assuming you have a table named Customers
with a column named City
, you can set a default value of "Unknown" for this column using the following query:
ALTER TABLE Customers
ADD DEFAULT 'Unknown' FOR City;
Additional notes:
- The default value must be compatible with the data type of the column.
- You can use expressions or functions to calculate the default value.
- If you need to remove the default constraint entirely, use the
DROP CONSTRAINT
statement without adding a new default value.
Setting a Default Value for an Existing Column in SQL Server
Example 1: Adding a Default Value to a New Column
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY IDENTITY(1,1),
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
City NVARCHAR(50),
Country NVARCHAR(50) DEFAULT 'USA'
);
In this example, the Country
column is created with a default value of "USA". Whenever a new row is inserted into the Customers
table, the Country
column will automatically be populated with "USA" unless a different value is explicitly provided.
ALTER TABLE Customers
ADD DEFAULT 'Unknown' FOR City;
This statement adds a default value of "Unknown" to the existing City
column in the Customers
table. If a new row is inserted without specifying a value for City
, it will be automatically set to "Unknown".
Setting a Default Value for a Column When Creating a Table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY IDENTITY(1,1),
CustomerID INT,
OrderDate DATE DEFAULT GETDATE(),
TotalAmount DECIMAL(10,2)
);
In this example, the OrderDate
column is created with a default value of GETDATE()
, which will automatically set the order date to the current date and time when a new row is inserted.
- Default Constraints: The
DEFAULT
clause creates a default constraint. You can drop or modify this constraint using theALTER TABLE
statement. - Expressions and Functions: You can use expressions and functions to calculate the default value.
- NULL Values: If the column is nullable and you want to enforce the default value for all rows, including existing rows without a value, use the
WITH VALUES
clause:
ALTER TABLE Customers
ADD DEFAULT 'Unknown' FOR City WITH VALUES;
Alternative Methods for Setting Default Values in SQL Server
While the ALTER TABLE
statement is the primary method for setting default values for existing columns in SQL Server, there are a few alternative approaches you can consider:
Using a Trigger:
- Create a trigger: Create an
INSERT
trigger on the table. - Check for null values: In the trigger's body, check if the column's value is null.
- Set the default value: If the value is null, set it to the desired default value.
CREATE TRIGGER trg_UpdateCityDefault
ON Customers
AFTER INSERT
AS
BEGIN
UPDATE Customers
SET City = 'Unknown'
WHERE City IS NULL;
END;
Advantages:
- Provides more flexibility for complex default value calculations or business logic.
- Can be used to enforce additional constraints or perform other actions when a new row is inserted.
- Can introduce performance overhead, especially for large tables with frequent inserts.
- May require additional maintenance if the default value or business logic changes.
Using a Stored Procedure:
- Create a stored procedure: Create a stored procedure to insert new rows into the table.
- Set the default value: Within the procedure, check if the column's value is null and set it to the desired default value before inserting the row.
CREATE PROCEDURE sp_InsertCustomer
@FirstName NVARCHAR(50),
@LastName NVARCHAR(50),
@City NVARCHAR(50),
@Country NVARCHAR(50)
AS
BEGIN
DECLARE @DefaultCity NVARCHAR(50) = 'Unknown';
IF @City IS NULL
SET @City = @DefaultCity;
INSERT INTO Customers (FirstName, LastName, City, Country)
VALUES (@FirstName, @LastName, @City, @Country);
END;
- Provides more control over the insertion process and can include additional validation or business logic.
- Can be used to enforce complex default value calculations or business rules.
- Requires additional development and maintenance.
- May introduce performance overhead, especially if the stored procedure is called frequently.
sql-server sql-server-2008 t-sql