SQL DEFAULT Constraint

The SQL DEFAULT Constraint is used to set a default value to a column when we insert a new row to a table without providing a value for the column.

We can use the DEFAULT keyword to specify a default value for a column.

    UserID int NOT NULL,
    FirstName varchar(100) NOT NULL,
    LastName varchar(100) NOT NULL,
    Email varchar(200),
    Phone varchar(50),
    City varchar(50) DEFAULT 'Toronto',
    CreatedDate Date DEFAULT Getdate()    

The above script will create a table named “Users”, the default value of column “City” will be “Toronto” and the default value of column “CreatedDate” will be the date when the new row inserted.

When we execute the following script:

INSERT INTO Users(UserID, FirstName, LastName, Email, Phone) 
VALUES (1, 'Tom', 'Smith', 'tom.smith@sql-statements.com', '1-000-0000')

We didn’t specify the value for the cloumn City and CreatedDate, so the default value will be set for them:

UserID FirstName LastName Email Phone City CreatedDate
1 Tom Smith tom.smith@sql-statements.com 1-000-0000 Toronto 2011-12-27

Leave a Comment