Table Constraints in SQL

Rumman Ansari   Software Engineer   2023-03-25   5966 Share
☰ Table of Contents

Table of Content:


SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the accuracy and integrity of the data inside table.

Constraints can be divided into the following two types,

  1. Column level constraints: Limits only column data.
  2. Table level constraints: Limits whole table data.

Constraints are used to make sure that the integrity of data is maintained in the database. Following are the most used constraints that can be applied to a table.

  • NOT NULL
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • CHECK
  • DEFAULT

Important Points:

  • SQL constraints are used to specify rules for the data in a table.
  • Constraints are used to limit the type of data that can go into a table. This ensures the accuracy, integrity  and reliability of the data in the table.
  • If there is any violation between the constraint and the data action, the action is aborted.

Constraint

Descriptions

NOT NULL

It Ensures that a column cannot have NULL value.

DEFAULT Constraint

It Provides a default value for a column when none is specified.

UNIQUE Constraint

It ensures that all values in a column are different.

PRIMARY Key

Uniquely identifies each row/record in a database table.

FOREIGN Key

To Uniquely identifies a row/record in any of the given database table.

CHECK Constraint

The CHECK constraint ensures that all the values in a column satisfies certain conditions.

INDEX

This is used to create and retrieve data from the database very quickly.