NOT NULL Constraint

SQL · Working with Tables

Theory

NOT NULL says a column must always have a value. Any INSERT that leaves the column out (with no DEFAULT) or sets it to NULL, and any UPDATE that sets it to NULL, is rejected with an error. By default columns are nullable, so you add NOT NULL to required fields like name, order_date or amount.

NOT NULL pairs well with DEFAULT: created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP is filled automatically when omitted. It does not stop empty strings or zero, which are real values; use CHECK for that. Adding NOT NULL to an existing column fails if the column already holds NULLs, so you first fill them with UPDATE. Primary key columns are implicitly NOT NULL in standard SQL.

Why we use it

Missing required values break reports, joins and application logic. NOT NULL stops them at the door, so every query can trust that the column is filled and does not need extra NULL handling.

When to use it

Put NOT NULL on any column the business treats as required. Leave a column nullable only when "unknown" or "not applicable" is a real state, like an end_date for a current employee.

How it works

  1. INSERT or UPDATE arrives
  2. Find the value for each NOT NULL column
  3. Was the column left out? Yes: Use its DEFAULT if one exists. No: Use the given value.
  4. Is the final value NULL? Yes: Reject with an error. No: Accept the row.
  5. Row stored with all required values

Syntax

column datatype NOT NULL

Example

CREATE TABLE employees (
  name VARCHAR(100) NOT NULL
);

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic