SQL Constraints - Default Constraint - DotNet and DataBase

Wednesday, 1 August 2018

SQL Constraints - Default Constraint




Default constraint is to maintain default value in the column. We have to mention the default value to the column while creating the table. In case no value inserted in that column, it will be filled with the default value which was supplied while creating the table.


Syntax:
CREATE TABLE <Table Name>(
      <COLUMN NAME> Data Type DEFAULT(VALUE)

);

Example:

CREATE TABLE Employees (
    ID int NOT NULL Primary Key IDENTITY(1,1),
    LastName varchar(255) NOT NULL UNIQUE,
    FirstName varchar(255),
    Age int CHECK(Age>17 and Age<61),
       Department Varchar(100) DEFAULT('Resource pool')
);


INSERT INTO Employees(LastName,FirstName,Age)VALUES('Naga','P',33);

Here no value supplied to the "Department" column, it will get default value 'Resource pool' as below

SELECT * FROM Employees

LastName FirstName Age ID Department
Naga          P         33      1 Resource pool

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.