The Database Constraints are the business rules that are implemented on the column level or the table level so that the restricted and qualified data can be stored in the table and database accuracy can be maintained. The column level constraints are applied at one column whereas the table level constraint will be applied at the table level.

There are many types of Database Constraints such as Check Constraints, Not-Null Constraints, Unique Constraints, Primary Keys, and Foreign Keys which are used for different types of use cases and business needs.

In this section of the PostgreSQL Tutorial, we will go through database constraints and see with the example.

1. Check Constraints

The Database Check Constraint ensures that the condition which is defined for the column should be fulfilled. The check condition must satisfy the Boolean (truth-value) expression.

In the following example, we have defined a Check Constraint on the table PRODUCTS_TAB in which the column PRODUCT_PRICE value should be always grater than 0.


postgres=# CREATE TABLE products_tab (
    product_number INTEGER,
    product_name TEXT,
    product_price NUMERIC CHECK (product_price > 0)