---
title: "Constraints in SQL"  
description: "In this blog, I’m explaining about constraints in SQL.Description: SQL constraints are used to restrict the type of data that can insert into a databa"  
author: "priyanka kushwaha"  
published: 2015-01-28  
updated: 2015-01-28  
canonical: https://www.mindstick.com/blog/741/constraints-in-sql  
category: "mssql server"  
tags: ["sql server"]  
reading_time: 2 minutes  

---

# Constraints in SQL

In this blog, I’m explaining about constraints in SQL.

Description:

[SQL constraints](https://www.mindstick.com/blog/11170/sql-constraints) are used to restrict the type of data that can insert into a [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table). Constraints can be defined in two ways.

Column Level : limits on column data

Table Level : Limits on [table data](https://www.mindstick.com/forum/33744/how-to-use-angularjs-table-data-binding-in-asp-dot-net)

##### Types of SQL constraints

\
· [Not null](https://www.mindstick.com/interview/1933/what-is-not-null-constraint)

\

· [Unique key](https://www.mindstick.com/interview/768/what-s-the-dot-net-datatype-that-allows-the-retrieval-of-data-by-a-unique-key)

\

· [Primary key](https://www.mindstick.com/blog/214/primary-key)

\

· [Foreign key](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint)

\

· Check

\

· Default

\
Not Null Constraints

\
This constraint ensures that all rows in the database table must contain

\

value for the column which specified as not null means a [null value](https://www.mindstick.com/forum/159364/difference-between-two-expression-results-with-a-null-value) is not

\

allowed in the column.

\

##### Syntax:

\

\

```
Create table table_name( col1 datatype[CONSTRAINT cons_name] not null,Col2 datatype,)
```

##### Unique key Constraints

Unique key is a set of one or more columns of a table that uniquely

\

identify each record in database table. It is like a Primary key but it can

\

accept only one null value and it can not have [duplicate values](https://www.mindstick.com/forum/157764/how-to-find-duplicate-values-in-a-sql-table).

\

##### Syntax:

```
Create table table_name( col1 datatype[CONSTRAINT cons_name] unique,Col2 datatype,)
```

##### Primary key Constraints

##### \

Primary key is a set of one or more columns of a table that uniquely

\

identify each in database table. It can not accept null, duplicate values.

\

\

##### Syntax:

```
Create table table_name( col1 datatype[CONSTRAINT cons_name] PRIMARY KEY,Col2 datatype,) 
```

Foreign Key Constraints

\
Foreign key is a field in database table that is Primary key in another table.

\

It can accept multiple null ,duplicate values.

\

##### Syntax:

\

```
Create table table_name( col1 datatype[CONSTRAINT cons_name] references reference_table_name (reference_table_column_name),col2 datatype)
```

##### Check Constraints

This constraint defines a business rules on a column in the database table

\

that each row of the table must follow this rule.

\

\

##### Syntax:

```
Create table table_name( Col1 datatype[CONSTRAINT cons_name] check(condition)Col2 datatype)
```

##### Default constraints

The Default constraint is used to insert a [default value](https://www.mindstick.com/forum/2035/default-value-when-using-singleordefault) into a column.

\

The default value will be added to all new records, if no other value is

\

specified.

##### Syntax:

```
Create table table_name( Col1 datatype,Col2  datatype Default Getdate())
```

---

Original Source: https://www.mindstick.com/blog/741/constraints-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
