Skip to main content

Constraints in sql server

Constraints - Constraints are the rules enforced on data columns on the table. This ensures the accuracy and reliability of the data in the database. Constraints define some conditions that restrict the column to remain true while inserting or updating or deleting data in a column.


Constraints can be defined in two ways 
1) The constraints can be specified immediately after the column definition. This is called the column-level definition. 
2) The constraints can be specified after all the columns are defined.
This is called the table-level definition. 

Types of Constraints in SQL-Server

NOT NULLThis constraint is responsible for a column to confirm that a column cannot have a null value.

DEFAULT – The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified.

UNIQUEThis constraint ensures that each row for the column has a different value.

PRIMARY KEYPrimary key constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. This constraint ensures that the specific column for a table has a unique identity.

FOREIGN KEYthis constraint responsible to uniquely identified a rows/records in any another database table.

CHECKThe CHECK responsible for enables a condition to check the value being entered into a record