Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

DEFAULT constraint

The DEFAULT constraint provides a default value to a column when an INSERT statement doesn't explicitly assign a particular value. 

if no other value is specified explicitly, default value will be added to column by sql server data engine.


Create a default constraint using SQL Server management studio:

1.        In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.

 

2.  Click on the column where you would like to create the DEFAULT constraint

3.  Enter the appropriate value in the Default Value or Binding cell of the properties page


4.  Check Default Constraint:


 

Default constraint using T-SQL:

How Create Create default constraint?

Column Level:

USE [WebShop]
GO
CREATE TABLE [dbo].[User](
     [ID] [int] IDENTITY(1,1) NOT NULL Primary Key,
     [ManagerId] [int] NOT NULL CONSTRAINT Df_ManageId DEFAULT 1,
     [UserType] [tinyint] NULL,
     [LoginName] [nvarchar](50) NULL Unique,
     [Password] [nvarchar](8) NULL,
     [CreatedDate] [datetime] NULL,
     [UpdatedDate] [datetime] NULL,
     [DeletedDate] [datetime] NULL
     )
 GO

Table Level:Not applicable

How To ADD default constraint using Alter?

ALTER TABLE [User]
ADD CONSTRAINT Df_ManageId DEFAULT 1 FOR ManagerId
GO

How to  DROP default constraint ?

ALTER TABLE [User]

DROP CONSTRAINT Df_ManageId

GO

 

 

Invalid entry,please enter valid data.

Loading