What are the different types of Constraints in SQL?
4054
27-Sep-2016
Updated on 22-Sep-2020
Abhishek Srivasatava
27-Sep-2016Constraint:
Constraint means limitation. As the name suggests it used to limit something. In SQL server constraints are used to limit the type of data that need to insert or update in the table.
In technical term Constraints can be rule that could be table level or column level.
Following are the some of the Constraints that are commonly used in SQL server :
1: Not Null: It Ensure that a column can't be empty.
2: Default: It provide the default value, if there is nothing specified.
3: Unique: It ensures that all the values in the column are unique.
4: Primary Key: It is used to uniquely identified row in the database table.
5: FOREIGN KEY: It is used to uniquely identified row in another database table.
6: Check: It ensure that all values in columns satisfy certain condition
7: Index: It is used to create or retrieve data from the database very quickly.
TABLE statement to create constraint even after the table is created.
8: Dropping Constraints:
We can drop any constraint whatever we have define by using the ALTER TABEL with DROP CONSTRAINT.
E.g. ALTER TABLE EE_EMPL_CAT DROP CONSTRAINT PRSN_INTN_ID;
We can use the shortcut for dropping the constraint by using keyword like PRIMARY key,
E.g. ALTER TABLE EE_EMPL_CAT DROP PRIMARY KEY;
Note: PRIMARY KEY and UNIQUE can't be define for single column.