SQL Default Constraint

SQL Default Constraint is used to assign default values to the SQL table columns. In general, every column accepts either Nulls or a Value. If the user forgot to enter the value, then SQL Server will assign a NULL value to the column. In those cases, we can use the SQL Server Default constraint to replace those Nulls with the default value.

Create SQL Default Constraint

We can create a SQL Server Default Constraint using both the Transact SQL query, and the Management Studio.

Set Default Constraint using SSMS

To create a SQL Server Default Constraint using Management Studio, Please go to the Object Explorer. Expand the Database folder in which the table had. Please select the table to create a Default Constraint (here it is Custrecords), and right-click on it will open the context menu. Please select the Design option

SQL Default Constraint 1

Once you click the Design option, the SQL management studio will open the corresponding table in design mode. As you see, table had seven columns of different data types, and our task is to add the Default Constraint to the Profession column.

Please select the SQL Server Column name and provide the default values at the Default Value or Binding Column property. For this example, we are assigning the Software Developer as the default value for the profession column.

SQL Default Constraint 3

Let me insert a value into the SQL Server table using the INSERT Statement.

-- Inserting Default Values in SQL Server
INSERT INTO [dbo].[CustRecords]
           ([CustKey], [FirstName], [LastName], [BirthDate], [EmailAddress], [Yearly Income])
     VALUES
           (1, 'Imran', 'Khan', '10-08-1985', '[email protected]', 15900, 'Skilled Professional')
GO

Output

Messages
--------
(1 row(s) affected)

Let me show you the Data

SQL Default Constraint 5

Though, it has the default values, SQL is inserting the data that I want to insert. Now, Let me insert the values without the profession column

-- Inserting Default Values in SQL Server
INSERT INTO [dbo].[CustRecords]
           ([CustKey], [FirstName], [LastName], [BirthDate], [EmailAddress], [Yearly Income])
     VALUES
           (2, 'Steve', 'Williams', '10-04-1989', '[email protected]', 15500),
    (3, 'Doe', 'Lara', '10-08-1985', '[email protected]', 15000 ),
    (4, 'Ramesh', 'Kumar', '10-08-1985', '[email protected]', 65000)
GO
SQL Default Constraint 6

SQL Server is inserting the default values to the profession column.

SQL Default Constraint 7

Create SQL Default Constraint using query

Let us create a default constraint in SQL server using the Transact SQL statement. Here, we will set the default values at the time of table creation. Please refer to Create Table article.

CREATE TABLE [dbo].[CustRecords](
	[CustKey] [int] NOT NULL PRIMARY KEY,
	[FirstName] [varchar](50) NOT NULL,
	[LastName] [varchar](50) NULL,
	[BirthDate] [date] NULL,
	[EmailAddress] [nvarchar](50) NULL,
	[Yearly Income] [money] NULL,
	[Profession] [nvarchar](100) NOT NULL DEFAULT ('Software Developer'),
)
GO

OR

CREATE TABLE [dbo].[CustRecords](
	[CustKey] [int] NOT NULL PRIMARY KEY,
	[FirstName] [varchar](50) NOT NULL,
	[LastName] [varchar](50) NULL,
	[BirthDate] [date] NULL,
	[EmailAddress] [nvarchar](50) NULL,
	[Yearly Income] [money] NULL,
	[Profession] [nvarchar](100) NULL CONSTRAINT [DF_CustRecords_Profession]  DEFAULT ('Software Developer'),
)
GO

Output

Messages
--------
Command(s) completed successfully.

We added the default constraint with default value. It is same as we create any other constraint. SSMS is intelligent enough to set default value for you.

Let me insert the values without the profession column

-- Inserting Default Values in SQL Server
INSERT INTO [dbo].[CustRecords]
	  ([CustKey], [FirstName], [LastName], [BirthDate], [EmailAddress], [Yearly Income])
     VALUES
           (1, 'Tutorial', 'Gateway', '10-04-1995', '[email protected]', 12500),
	   (2, 'Steve', 'Smith', '10-04-1989', '[email protected]', 15500),
	   (3, 'Jack', 'Lara', '10-08-1985', '[email protected]', 15000 ),
	   (4, 'Ramesh', 'Kumar', '10-05-1983', '[email protected]', 65000)
GO
SQL Default Constraint 9

SQL Default Constraint on Existing table

How to add a default constraint on existing table?. For this, we create a new table.

CREATE TABLE [dbo].[CustRecords](
	[CustKey] [int] NOT NULL PRIMARY KEY,
	[FirstName] [varchar](50) NOT NULL,
	[LastName] [varchar](50) NULL,
	[BirthDate] [date] NULL,
	[EmailAddress] [nvarchar](50) NULL,
	[Yearly Income] [money] NULL,
	[Profession] [nvarchar](100) NOT NULL
)
GO

Now let me add the default constraint. As you can see from the below, we are using the Alter Table Statement to alter the table content. Then used the ADD Constraint statement to add the default constraint.

ALTER TABLE [dbo].[CustRecords]   
ADD CONSTRAINT DF_CustRecords_Profession DEFAULT ('Software Developer') FOR Profession;  
GO

Let me show you the internal code generated by the SQL, by right-clicking on the table -> script as – > create to new query window option

SQL Default Constraint 10

Delete Default Constraint using SSMS

Please select the table -> Column on which your default constraint holds, then go to the Column properties, and remove the default value.

SQL Default Constraint 11

Delete Default Constraint using Drop Constraint

If you know the SQL Server Default constraint name, use the DROP Constraint statement along with the ALTER TABLE Statement

ALTER TABLE [dbo].[CustRecords]   
DROP CONSTRAINT DF_CustRecords_Profession;  
GO

If you don’t know the Default constraint name, use the first SELECT Statement to get all the Constraint name.

-- To find the Default Constraint Name
SELECT name  
FROM sys.default_constraints  
WHERE OBJECT_NAME(parent_object_id) = N'CustRecords';  
GO  
-- Delete or Drop the Constraint 
ALTER TABLE [CustmerRecords]  
DROP CONSTRAINT DF_CustRecords_Profession
SQL Default Constraint 12