SQL Constraints


SQL Constraints are used to give some rules for the columns of a table. we can create and define constraints on one or more column of table. This maintains the data integrity of the table.

Constraints can be divided into two parts :-

1. Column Level Constraints :- It is apply to only column.

2. Table Level Constraints     :- It is apply to whole table.

Syntax : -

CREATE TABLE table_name

(

column1 datatype constraints,

column1 datatype constraints,

- - - - - - - - - - - - - - - - - - - - - 

- - - - - - - - - - - - - - - - - - - - - 

columnN datatype constraints

);

Types of Constraints in SQL 

  • NOT NULL         : It cannot have any NULL value in column.
  • FOREIGN KEY  : It is used to unique identifies a records row/record in another table.
  • UNIQUE   : Unique constraint is used to uniquely identify each row in the table.
  • DEFAULT     : Default constraint is used to add default value.
  • CHECK        : Check constraint is used to restrict the value of a column b/w a range.
  • INDEX    : Index constraint is used to create indexes in tables.