Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
The not-null constraint is a restriction placed on a column in a relational database table. It enforces the condition that, in that column, every row of data must contain a value - it cannot be left blank during insert or update operations. If this column is left blank, this will produce an error message and the entire insert or update operation will fail.
Consider a table named CUSTOMER_MASTER that stores customer details for a bank’s database. Every customer must have at least a surname and must belong to a certain gender. The two columns dealing with surname and gender can then be marked as “NOT NULL” when creating the Customer_Master table.
A sample SQL script for doing this is given below:
CREATE TABLE customer_master (
custid INTEGER PRIMARY KEY,
surname CHAR NOT NULL,
date_of_birth DATE NOT NULL,
gender CHAR NOT NULL)
The not-null constraint is a useful tool for database designers for enforcing business logic. Instead of using programming code to enforce the “must always have a value" concept, they simply use a built-in database feature.
Sometimes the not-null constraint is implicit. When a column is marked as a primary key (see the “custid” column in our example above), it is used to uniquely identify all rows in the table so that they obviously cannot be left blank.