1. sql
  2. /references
  3. /constraints

Constraints in SQL


In SQL, a constraint is a rule that is used to limit the type of data that can be inserted into a table. If a constraint is violated, the statement will fail and an error will be returned. Constraints can be applied to columns or to the whole table.

Here are some common types of constraints:

  • NOT NULL: This constraint specifies that a column cannot contain a null value.
  • UNIQUE: This constraint ensures that all values in a column are unique.
  • PRIMARY KEY: This constraint specifies a column or set of columns that uniquely identifies each row in the table. A primary key column cannot contain null values.
  • FOREIGN KEY: This constraint specifies a column or set of columns that reference the primary key of another table.
  • CHECK: This constraint allows you to specify a condition that must be met for a row to be inserted or updated.


Here's an example of how to create a table with some constraints:

    username TEXT UNIQUE NOT NULL, 
    password TEXT NOT NULL,
    age INTEGER CHECK (age > 0)

This creates a table called users with four columns: id, username, password, and age. The id column is the primary key and cannot contain null values. The username column is unique and cannot contain null values. The age column has a CHECK constraint that ensures that the value is greater than zero.

Best Practices

There are several best practices to follow when working with constraints in SQL:

  • Use constraints to enforce business rules and data integrity at the database level. This can help prevent data inconsistencies and errors.

  • Use appropriate constraint types for the specific use case. For example, use a primary key constraint for a unique identifier, and use a foreign key constraint for enforcing referential integrity.

  • Define constraints with clear and meaningful names, so that it's easy to understand what they are used for.

  • Test constraints thoroughly before deploying to production, to ensure they are working as intended.

  • Avoid using constraints to enforce complex business rules, as this can make the database design more complex and harder to maintain. In some cases, it is better to implement complex logic in the application code.

  • Be mindful of the performance impact of constraints. Some types of constraints, such as check constraints, can have a high overhead.

  • You should have one primary key constraint preferably clustered on every table in your database.

  • To maintain data integrity, use unique constraints when a column is not expected to hold duplicate values in any case.

  • We can specify constraints at the time of creating the table using CREATE TABLE statement.

  • We can also specify the constraints after creating a table using ALTER TABLE statement.

  • The constraint is used to limit the value range that can be placed in a column.

  • If you define a constraint on a column it will allow only certain values for this column.

  • If you define a constraint on a table it can limit the values in certain columns based on values in other columns in the row.