PostgreSQL Not-Null Constraint
Summary: in this tutorial, you will learn about PostgreSQL not-null constraints to ensure the values of a column are not null.
Introduction to NULL
In the database world, NULL represents unknown or missing information. NULL is not the same as an empty string or the number zero.
Suppose you need to insert the email address of a contact into a table. You can request his or her email address.
However, if you don’t know whether the contact has an email address or not, you can insert NULL into the email address column. In this case, NULL indicates that the email address is unknown at the recording time.
NULL is very special. It does not equal anything, even itself. The expression NULL = NULL
returns NULL because it makes sense that two unknown values should not be equal.
To check if a value is NULL or not, you use the IS NULL
boolean operator. For example, the following expression returns true if the value in the email address is NULL.
The IS NOT NULL
operator negates the result of the IS NULL
operator.
PostgreSQL NOT NULL constraints
To control whether a column can accept NULL, you use the NOT NULL
constraint:
If a column has a NOT NULL
constraint, any attempt to insert or update NULL in the column will result in an error.
Declaring NOT NULL columns
The following CREATE TABLE
statement creates a new table name invoices
with the not-null constraints.
This example uses the NOT NULL
keywords that follow the data type of the product_id and qty columns to declare NOT NULL
constraints.
Note that a column can have multiple constraints such as NOT NULL
, check, unique, foreign key appearing next to each other. The order of the constraints is not important. PostgreSQL may check constraints in any order.
If you use NULL
instead of NOT NULL
, the column will accept both NULL
and non-NULL values. If you don’t explicitly specify NULL
or NOT NULL
, it will accept NULL
by default.
Adding NOT NULL Constraints to existing columns
To add the NOT NULL
constraint to a column of an existing table, you use the following form of the ALTER TABLE
statement:
To add multiple NOT NULL
constraints to multiple columns, you use the following syntax:
Let’s take a look at the following example.
First, create a new table called production orders ( production_orders
):
Next, insert a new row into the production_orders
table:
Then, to make sure that the qty
field is not null, you can add the not-null constraint to the qty
column. However, the column already contains data. If you try to add the not-null constraint, PostgreSQL will issue an error.
To add the NOT NULL
constraint to a column that already contains NULL, you need to update NULL
to non-NULL first, like this:
The values in the qty
column is updated to one. Now, you can add the NOT NULL
constraint to the qty
column:
After that, you can update the not-null constraints for material_id
, start_date
, and finish_date
columns:
Add not-null constraints to multiple columns:
Finally, attempt to update values in the qty
column to NULL:
PostgreSQL issued an error message:
The special case of NOT NULL constraint
Besides the NOT NULL
constraint, you can use a CHECK constraint to force a column to accept not NULL values. The NOT NULL
constraint is equivalent to the following CHECK
constraint:
This is useful because sometimes you may want either column a
or b
is not null, but not both.
For example, you may want either username
or email
column of the user tables is not null or empty. In this case, you can use the CHECK
constraint as follows:
The following statement works.
However, the following statement will not work because it violates the CHECK
constraint:
Summary
- Use the
NOT NULL
constraint for a column to enforce a column not acceptNULL
. By default, a column can hold NULL. - To check if a value is
NULL
or not, you use theIS NULL
operator. TheIS NOT NULL
negates the result of theIS NULL
. - Never use equal operator
=
to compare a value withNULL
because it always returnsNULL
.