Constraints

Constraints are rules and restrictions applied on a column or a table such that unwanted data can't be inserted into tables. This ensures the accuracy and reliability of the data in the database. We can create constraints on single or multiple columns of any table. Constraints maintain the data integrity and accuracy in the table.

Constraints can be classified into the following two types.

Column Types Constraints

Definitions of these types of constraints is given when the table is created.

  1. Create Table My_Constraint  
  2. (  
  3. IID int NOT NULL,  
  4. Salary int CHECK(Salary>5000)  

Table Types Constraints

Definitions of these types of constraints is given after the creation of the table using the Alter Command.

  1. Alter Table My_Cosntraint  
  2. Add constraint Check_Constraint Check(Age>50) 

SQL Server contains the following 6 types of constraints:

  • Not Null Constraint
  • Check Constraint
  • Default Constraint
  • Unique Constraint
  • Primary Constraint
  • Foreign Constraint

Let us understand each constraint briefly.

Not Null Constraint

A Not null constraint restrict the insertion of null values into a column. If we are using a Not Null Constraint for a column then we cannot ignore the value of this column during an insert of data into the table.

Column Level

Syntax

  1. CREATE TABLE Table_Name  
  2. (  
  3.    Column_Name Datatype CONSTRAINT Constraint_Name NOT NULL,  
  4. ); 

Example

  1. Create Table My_Constraint  
  2. (  
  3.    IID int NOT NULL,  
  4.    Name nvarchar(50) CONSTRAINT Cons_NotNull not null,  
  5.    Age int Not Null,  

Table Level

Syntax

  1. ALTER TABLE Table_Name  
  2. ALTER COLUMN Column_Name Datatype NOT NULL 

Example

  1. Alter Table My_Constraint  
  2. Alter Column IId int Not Null 

Without SQL Command

We can also create a Not Null constraint in Microsoft SQL Server without execution of a SQL query.

First right-click on the table and select and click on the design option. Now check all the columns in the “Allow Nulls” option that should have a Null Value.



Figure 1: Table

Check Constraint

A Check constraint checks for a specific condition before inserting data into a table. If the data passes all the Check constraints then the data will be inserted into the table otherwise the data for insertion will be discarded. The CHECK constraint ensures that all values in a column satisfies certain conditions.

Column Level

Syntax

  1. Create Table Table_Name  
  2. (  
  3.    Column_Name Datatype Constraint Constraint_Name Check(Condition)  

Example

  1. Create Table Constraint_ 
  2. (  
  3.    IId int Constraint Constraint_Name Check(IId>100)  

Table Level

Syntax

  1. Alter Table Table_Name  
  2. Add Constraint Constraint_Name Check(Condition) 

Example

  1. Alter table Constraint_ 
  2. Add constraint Cons_Name Check(IId>150) 

Without SQL Command

First go to Table Design then right-click on the Column_Name that will contain a check constraint and select the “Check Constraint” option then a new window will be shown. In this window add a constraint and provide its definition in the Expression Field.



Figure 2: Check Constraint



Figure 3: Select Check Constraint


Default Constraint

Specifies a default value for when a value is not specified for this column. If in an insertion query any value is not specified for this column then the default value will be inserted into the column.

Column Level

Syntax

  1. Create Table Table_Name  
  2. (  
  3.    Column_Name DataType Constraint Constraint_Name Default(Value),  

Example

  1. Create Table My_Table1  
  2. (  
  3.    IId int default(1500),  
  4.    Name Nvarchar(50)Constraint Name_Default Default('Pankaj'),  
  5.    Age Int,  
  6.    Salary Int Default(100)  

Table Level

Syntax

  1. Alter Table Tabel_Name  
  2. Add Constraint Constraint_Name Default(Value) for[Column_Name] 

Example

  1. Alter Table My_Table1 
  2. Add Constraint cons_Default Default(40) for[Age] 

Without SQL Command

Go to Table Design then click on the specific column name that should have a default value and go to the column Property and provide the default value.



Figure 4: Column Property

Unique Constraint

It ensures that each row for a column must have a unique value. It is like a Primary key but it can accept only one null value. In a table one or more column can contain a Unique Constraint.

Column Level

Syntax

  1. Create Table Table_Name  
  2. (  
  3.    Column_Name Datatype Constraint Constraint_Name Unique  

Example

  1. Create Table MY_Tab  
  2. (  
  3.    IId int constraint Unique_Cons Unique ,  
  4.    Name nvarchar(50)  

Table Level

Syntax

  1. Alter Table_Name  
  2. Add Constraint Constraint_Name Unique(Column_Name) 

Example

  1. Alter Table My_Tab 
  2. Add Constraint Unique_Cons_ Unique(Name) 

Without SQL Command

First go to Table definition and select a column and right-click on that column. Now select the option Index/Keys. Now a window will be shown. Add a constrint and mark its “Is Unique” option as True.



Figure 5: Indexes & Keys



Figure 6: Select Indexes

Primary Key Constraint


A Primary key uniquly identifies each row in a table. It cannot accept null and duplicate data. One or more of the columns of a table can contain a Primary key.

Column Level

Syntax

  1. Create Table Table_Name  
  2. (  
  3.    Column_Name Datatype Constraint Constraint_Name Primary Key,  

Example

  1. Create Table Employee  
  2. (  
  3.    IId int constraint Const_primary_IId primary key,  
  4.    Name nvarchar(50)  

Table Level

Syntax

  1. Alter Table Table_Name  
  2. Add constraint Constraint_Name Primary Key(Column_Name) 

Example

  1. Alter Table Employee
  2. Add constraint Constraint_Name Primary Key(IId) 

Without SqlQuery

First go to table design and right-click on Column and select the “Set Primary Key” Option.



Figure 7: Set Primary Key

Foreign Key Constraint


A Foreign Key is a field in a database table that is a Primary key in another table. A Foreign key creates a relation between two tables. The first table contains a primary key and the second table contains a foreign key.

Column Level

Syntax

  1. Create Table Table_Name  
  2. (  
  3.    Column_Name Datatype Constraint Constraint_Name References Reference_Table_Name(Reference_Column_Name)  

Example

  1. Create Table Employee_  
  2. (  
  3.    IId int constraint Cons_Reference References My_Constraint(IId),  
  4.    Age int,  
  5.    Salary int  

Table Level

Syntax

  1. ALTER TABLE Table_Name  
  2. ADD CONSTRAINT Constraint_Name FOREIGN KEY(Column_Name)  
  3. REFERENCES Reference_Table (Column_Name) 

Example

  1. ALTER TABLE Employee_ 
  2. ADD CONSTRAINT Cons_Emp_Foreign FOREIGN KEY(IId)  
  3. REFERENCES My_Constraint(IId) 

Without SQL Command

First go to table design than right-click on the column and select the “Relationship” option. Now a window will be shown. In this window click on the “Table and Column Specificat” option and select Primary Key table, Column name and Column name for foreign key.


Figure 8: Column Relationships



Figure 9: Foreign Key Relationships