Thursday , December 14 2017
Home / DataBase / SQL SERVER / Constraints in SQL Server

Constraints in SQL Server

Constraint are using to restricted the insertion of unwanted data in any columns. we can create constraints on single or multiple columns of any table. It maintain the data integrity of  the table.

Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).

SQL CREATE TABLE + CONSTRAINT Syntax

CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
….
);

There are 6 types of constraints in Sql Server:-

  1. Primary key constraint.
  2. Foreign Key constraint.
  3. Unique Key constraint.
  4. Not Null constraint.
  5. Check constraint
  6. Default constraint

 In SQL, we have the following constraints:

  • PRIMARY KEY – A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly
  • FOREIGN KEY – Ensure the referential integrity of the data in one table to match values in another table
  • UNIQUE – Ensures that each row for a column must have a unique value
  • NOT NULL – Indicates that a column cannot store NULL value
  • CHECK – Ensures that the value in a column meets a specific condition
  • DEFAULT – Specifies a default value for a column

 

Print Friendly, PDF & Email

Give us your valuable feedback

comments

About Samba

Check Also

sql server backup script

Backup all databases in SQL Instance with one script

Backup: A copy of SQL Server data that can be used to restore and recover the …

%d bloggers like this: