Click here to Skip to main content
13,050,606 members (86,387 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 10 Dec 2013

Know More about SQL Constrains

, 21 Aug 2014
Rate this:
Please Sign up or sign in to vote.
SQL Constrains details and its types

SQL Constrains 

Constraints are used to apply business rules for the database tables.Also uses constraint definitions to build high-performance query execution plans.

This is usually associated with a table and is created with a CREATE CONSTRAINT or CREATE ASSERTION SQL statement.


They can apply to a column, a whole table, more than one table or an entire schema. A reliable database system ensures that constraints hold at all times (except possibly inside a transaction, for so called deferred constraints).


Constraints can be defined in two ways 

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


To know the usage of constrains and its all types in SQL server. 

Common kinds of constraints are: 

Not Null

  • value in a column must not be NULL this: 


  • value(s) in specified column(s) must be unique for each row in a table, it inclues one NULL
  • enforce the uniqueness of the values in a set of column


  • an expression is specified, which must evaluate to true for constraint to be satisfied.
  • You can apply multiple CHECK constraints to a single column.
  • there must be at least one row in table CheckTable
  • not validated during DELETE statements, may produce unexpected results.
  • the Database Engine inserts NULL and does not return an error when condition fails.

Primary Key

  • value(s) in specified column(s) must be unique for each row in a table and not be NULL
  • a column or combination of columns that contain values that uniquely identify each row in the table.
  • a table can have only one PRIMARY KEY
  • Normally each table in a database should have a primary key - it is used to identify individual records.

Foreign Key

  • value(s) in specified column(s) must reference an existing record in another table (via it's primary key or some other unique constraint)
  • can reference columns in tables in the same database or within the same table. These are called self-referencing tables.
  • SQL Server does not have a predefined limit on either the number of FOREIGN KEY constraints a table, recommend that a table contain no more than 253 FOREIGN KEY constraints.


  • are created as separate objects and then bound to the column. 
  • Avoid using this feature in new develop ment work, and plan to modify applications that currently use this feature. Use CHECK constraints instead, Since This feature will be removed in a future version of Microsoft SQL Server.


  • Defaults specify what values are used in a column if you do not specify a value for the column when you insert a row.
  • Query To Create Default Constraint Over Table Column

Candidate key

  • A column, or set of columns that can uniquely identify a row in a table.
  • can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.

Candidate keys that follow all the three rules

  1. Not Null
  2. Unique Value in Table
  3. Static

Alternate key

  • Any candidate key that has not been selected as the primary key.
  • the column may not be primary key but still it is the unique key of the column.

Composite key

  • A key that is composed of more than one column.

Super Key

  • Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table.
  • Example : Primary key, Unique key, Alternate key are subset of Super Keys.

List All The Constraint of Database – Find Primary Key and Foreign Key Constraint in Database 

SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects

Points of Interest

A key is a single or combination of multiple fields in a table. Its is used to fetch or retrieve records/data-rows from data table according to the condition/requirement. Keys are also used to create relationship among different database tables or views.


10 Dec  2014, Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohammed Nazer
Software Developer
India India
Dot Net Developer

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 21 Aug 2014
Article Copyright 2013 by Mohammed Nazer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid