Skip to main content
Log inGet a demo
← SQL Dictionary
Constraints

SQL NOT NULL

What is SQL NOT NULL?

A NOT NULL constraint in SQL is a database constraint that ensures a column must always contain a value. It prohibits the insertion of NULL values, making it mandatory for every row to have a non-null value in the specified column. The NOT NULL constraint enforces data integrity and consistency by preventing missing or unknown data in essential columns.

When you would use it

You would use a NOT NULL constraint in SQL when you need to:

  1. Enforce Data Integrity: Guarantee that specific columns contain valid and non-null data, preventing missing or unknown information in critical fields.

  2. Ensure Data Quality: Improve data accuracy by requiring that essential information is provided for every row, reducing the risk of incomplete records.

  3. Implement Business Rules: Enforce business rules that dictate the presence of data in particular columns.

  4. Enhance Query Performance: Enable efficient querying by ensuring that columns used in search conditions or calculations always contain valid data.

Syntax

The syntax for defining a NOT NULL constraint in SQL is as follows:

CREATE TABLE table_name (
    column1 data_type NOT NULL,
    column2 data_type,
    -- ...
);
  • table_name: The name of the table for which you're creating the NOT NULL constraint.
  • column1, column2, ...: The columns in the table.
  • data_type: The data type of the column.
  • NOT NULL: The keyword that specifies the NOT NULL constraint.

Parameter values

  • table_name: The name of the table for which you're defining the NOT NULL constraint.
  • column1, column2, ...: The columns within the table to which you want to apply the NOT NULL constraint.
  • data_type: The data type of the column, specifying the type of data it can store.

Example query

Here's an example SQL query that creates an "employees" table with NOT NULL constraints on the "employee_id" and "first_name" columns:

CREATE TABLE employees (
    employee_id INT NOT NULL,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50),
    date_of_birth DATE,
    -- Other columns
);

Example table response

The "employees" table is created with NOT NULL constraints on the "employee_id" and "first_name" columns. The table might contain data like this:

employee_idfirst_namelast_namedate_of_birth
1JohnDoe2000-05-15
2JaneSmith2005-09-22
3BobJohnson1999-12-10

In this example, both "employee_id" and "first_name" columns are required to have non-null values for every row.

Use cases

  1. Enforce Data Integrity: To guarantee that specific columns contain valid and non-null data, preventing missing or unknown information in critical fields.

  2. Ensure Data Quality: To improve data accuracy by requiring that essential information is provided for every row, reducing the risk of incomplete records.

  3. Implement Business Rules: To enforce business rules that dictate the presence of data in particular columns, such as requiring an employee's first name and ID.

  4. Enhance Query Performance: To enable efficient querying by ensuring that columns used in search conditions or calculations always contain valid data, preventing errors caused by missing values.

SQL Languages Availability

The NOT NULL constraint is a fundamental feature and is available in most SQL-based relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • IBM Db2
  • SQLite

The syntax for defining NOT NULL constraints is consistent across different database systems, making it widely supported in SQL databases.


Keep reading

SQL DEFAULT

Read article

SQL Subqueries

Read article

SQL Window Functions

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.