SQL constraints are used to define rules for the data in a table. Constraints can be used as column level or table level.
Constraints are used to restrict the limit of data type that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted. That means data will not take place in table.
SQL Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
CREATE TABLE Statement
Syntax
CREATE TABLE table_name (column1 datatype constraint,column2 datatype
constraint,column3 datatype constraint, ....)
After the table is created with the ALTER TABLE statement, You can also add constraints.
ALTER TABLE Statement
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);
The below defines the different types Constraints which can be used as column level or table level
The following constraints are mainly used as column level or table level in SQL Server: