Constraints are used to specify rules for data that can go into a table.

Constraints can be both column level and table level, which ensures the accuracy and authenticity of the data in the database.

The following constraints are commonly used in SQL:

  • NOT NULL - Checks that a column cannot have a NULL value

  • UNIQUE - Checks that all values in a column are unique or different from one another

  • PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table. We can define only one Primary key in the table.

  • FOREIGN KEY - Uniquely identifies a row/record in another table.

  • CHECK - Checks that all values in a column satisfies a specific condition

  • DEFAULT - Sets a default value for a column when no value is specified.

INDEX – We can create Index on any table and retrieve data from the database very quickly.

Want More SQL Deep Dives