You are on page 1of 4

SQL Server Constraints

 SQL constraints are used to specify rules for the data in a table.
 Constraints are used to limit the type of data that can go into a table.
 This ensures the accuracy and reliability of the data in the table.
 If there is any violation between the constraint and the data action, the action is aborted.
 Constraints can be column level or table level.
 Column level constraints apply to a column, and table level constraints apply to the
whole table.

The following constraints are commonly used in SQL Server:

 NOT NULL - Ensures that a column cannot have a NULL value.


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table.
 FOREIGN KEY - Uniquely identifies a row/record in another table
 CHECK - Ensures that all values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column when no value is specified
 INDEX - Use to create and retrieve data from the database very quickly

Employee Table - Create Table Query


CREATE TABLE employee_details
(
id int,
name varchar(50) not null,
gender varchar(50) not null,
designation varchar(50) not null,
city varchar(50) not null,
salary int not null
);

Employee Table - Insert Into Query


insert into employee_details VALUES
(1,'Ali','Male','Manager','Hyderabad',24000),
(2,'Usman','Male','Assistant','Karachi',22000),
(3,'Yahya','Male','Incharge','Lahore',21000),
(4,'Basit','Male','Accountant','Hyderabad',26000),
(5,'Zubair','Male','Operator','Karachi',20000),
(6,'Bilal','Male','Manager','Lahore',29000),
(7,'Iffat','Female','IT-Incharge','Karachi',22000),
(8,'Saif','Male','PA','Karachi',26000),
(9,'Nasir','Male','Incharge','Lahore',21000),
(10,'Anwar','Male','Manager','Hyderabad',27000),
(11,'Noman','Male','PA','Karachi',24000);
What Is Not Null Constraint In SQL ?
 In SQL Server, by default, a column can hold NULL (Nothing) values.
 SQL Server NOT NULL constraint allows a column to NOT accept NULL (Nothing)
values.
 SQL Server NOT NULL constraint enforces a field to always have a value, which means
that we can't insert a new record, or update a record without adding a non null value to
this field.

Query For SQL SERVER - NOT NULL ON CREATE


TABLE
CREATE TABLE employee
(
id int,
name varchar(50) not null,
gender varchar(50) not null,
salary int not null
);

What Is SQL SERVER - UNIQUE Constraint ?


 SQL Server's UNIQUE constraint make sure that all values in a column are unique /
different.
 In SQL Server, both UNIQUE and PRIMARY KEY constraints provide a 100% surety
for uniqueness for a column or a set of columns.
 In SQL Server, a PRIMARY KEY constraint by default has a UNIQUE constraint.
 In SQL Server, you can have many UNIQUE constraints in a single table but you can
have only 1 PRIMARY KEY constraint in a single table, you cannot have multiple
primary keys in a single table.

Query For SQL SERVER UNIQUE Constraint - ON


CREATE TABLE
CREATE TABLE employee
(
id int not null unique,
name varchar(50) not null,
gender varchar(50) not null,
salary int not null
);

What Is SQL Server's DEFAULT Constraint ?


 In SQL Server, DEFAULT constraint is used to provide a default value to a column.
 In SQL Server, the default value will be added to all new records, if no other value is
specified at the time of insertion of data.
 Like we can set "Anonymous" default value on a column and it is inserted automatically
when no other value is specified at the time of insertion.

Query For SQL Server DEFAULT on CREATE TABLE


CREATE TABLE employee
(
id int,
name varchar(50) default 'Anonymous',
gender varchar(50) not null,
salary int not null
);

Note: SQL Server's DEFAULT constraint can also be used to insert system generated values, by
using functions like GETDATE().

CREATE TABLE employee


(
id int,
name varchar(50) default 'Anonymous',
gender varchar(50) not null,
salary int not null,
Hiring_Date varchar(100) default getdate()
);

What Is SQL Server's CHECK Constraint ?


 By using SQL Server's CHECK Constraint we can specify certain conditions to a
column.
 In SQL Server, CHECK constraint is used to limit or control the value range that can be
inserted in a column.
 In SQL Server, If you specify a CHECK constraint on a column it will allow only certain
values for this column.
 In SQL Server, If you define a CHECK constraint on a table it can limit or control the
values in certain columns based on values in other columns in the row.

Query For SQL Server CHECK on CREATE TABLE


CREATE TABLE employee
(
id int,
name varchar(50) default 'Anonymous',
gender varchar(50) not null,
age int CHECK(age >= 18)
salary int not null,
);
NOTE: In SQL Server, To define a CHECK constraint on multiple columns, use the following
SQL Server syntax given below:

CREATE TABLE employee


(
id int,
name varchar(50) default 'Anonymous',
age int,
City varchar(100),
salary int not null,
CONSTRAINT Emp_Check CHECK (Age>=18 AND City='Hyderabad')
);

You might also like