MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

Can a Primary Key Be NULL in an SQL Table?

intro

Learn why primary key columns in SQL must be NOT NULL. Understand how databases enforce non-null constraints and best practices for designing reliable keys.

Question: Can a primary key column in a database table be NULL, or can it ever contain a NULL value? This is a common doubt for those learning about database constraints, since primary keys and unique keys seem similar at first glance.

Let’s find out more about that!

Can a Primary Key Be NULL?

Quick Answer: No, a primary key cannot be NULL.

By definition, a primary key must uniquely identify each row and must have a value. SQL standards and all major databases enforce that primary key columns be NOT NULL. If you attempt to force a NULL into a primary key column, it will violate that constraint with an error like this:

Copy
        
1 ERROR: null value in column "id" of relation "table_name" violates not-null constraint

In fact, when you declare a primary key on a column, the database automatically treats that column as NOT NULL (even if you didn’t explicitly specify NOT NULL in the column definition).

Why a Primary Key Cannot Be Null

A primary key serves as a unique identifier for a record. In other words, every row in the table is identified by its primary key value. If a primary key were NULL, it would mean “unknown identifier” for that row—which defeats the purpose of having a key.

Also, NULL isn’t equal to any other NULL (NULL represents an unknown/missing value), so you couldn’t reliably use it to find or distinguish a row. In practice, database systems ensure this by not allowing NULLs in primary key columns.

When you create a primary key, here’s what happens under the hood:

  • The column(s) chosen as primary key will be set to NOT NULL implicitly (if not already). For example, in most relational database systems, if you declare a primary key on a nullable column, the system will convert it to NOT NULL. This also applies to composite keys. In that case, all columns involved in the key definition must not be NULL.
  • A unique index is created on the primary key column(s) to enforce uniqueness. This index also will not index any NULL (since none are allowed).

SQL Primary Keys Must Be Unique and Not Null

According to SQL rules, a UNIQUE column may contain null values, because NULL is not considered equal to another NULL (which is also why the = NULL expression in WHERE clauses doesn’t work as you would expect).

For example, a table could have a unique constraint on email, and you might have a few rows where email is NULL. That doesn’t violate uniqueness because those NULLs aren’t “equal” to each other in the logic of SQL.

However, a primary key is a stronger condition: it implies both uniqueness and not null. In detail, Primary Key = Unique + Not Null.

Think of a primary key as an identifier or an address for a record. If it’s missing (NULL), the record is essentially lost in terms of identification:

  • You couldn’t select a specific row by primary key if the key is NULL, because you can’t say WHERE id = NULL (again, that expression is always false in SQL).
  • You couldn’t have foreign keys reference a NULL primary key, because foreign keys reference actual values in the parent table. A foreign key referencing a primary key that is NULL would break referential integrity (no actual parent row to point to).

Primary Keys Can’t Contain NULLs

The SQL standard explicitly states that primary key columns are not allowed to contain NULLs. This is part of the definition of a primary key.

Here’s an example illustrating the rule:

Copy
        
1 CREATE TABLE students ( 2 student_id INT PRIMARY KEY, 3 name VARCHAR(100) 4 ); 5 -- student_id is implicitly NOT NULL due to PRIMARY KEY. 6 7 INSERT INTO students(student_id, name) VALUES (NULL, 'Alice'); 8 -- This will fail: cannot insert NULL into a PRIMARY KEY column.

If you need a unique identifier but also need to represent “unknown” or “missing” cases, the solution is not to use NULL in a primary key. Instead, you might:

  • Use a surrogate key (like an auto-increment/serial) as the primary key, and let missing cases have a surrogate value while a separate column stores the meaningful ID which could be NULL (but that meaningful ID wouldn’t be the primary key).
  • Or reconsider the design. If something can be NULL, it might not be a good candidate for primary key. Perhaps a combination of columns or a different column should be the primary key.

Also, what happens if you try to insert NULL into an auto-increment primary key? In some databases, like MySQL, NULL is treated as the default value, which triggers the database to automatically generate and increment the next value for that column. This doesn't mean that NULL will actually be stored in the primary key. Instead, the database treats the NULL as a signal to assign the next available auto-incremented value.

Conclusion

In summary, a primary key cannot be NULL because it must uniquely identify a record. All databases enforce this either by rule or implicitly converting the column to NOT NULL. If you attempt to declare a primary key on a nullable column, the DBMS will reject it or change it

So, when designing your schema, ensure that the primary key fields always have valid values. If a scenario arises where you think a primary key might not be known, you likely need to rethink your key strategy. Can a primary key be NULL in an SQL Table? No!

Dbvis download link img
About the author
TheTable
TheTable

The Table by DbVisualizer is where we gather together to learn about and simplify the complexity of working with database technologies.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

How Does LOAD DATA Work for MySQL?

author Antonello Zanini tags MySQL 10 min 2025-08-11
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06
title

The SELECT INTO TEMP TABLE Mechanism in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-05
title

Oracle 23ai: What’s New? Everything You Need to Know at a Glance

author Antonello Zanini tags AI ORACLE 7 min 2025-08-04
title

PostgreSQL TRUNCATE TABLE Statement: A Guide

author Leslie S. Gyamfi tags POSTGRESQL 6 min 2025-07-30
title

Beyond COALESCE: SQL Clauses That Can Help You

author Lukas Vileikis tags DbVisualizer MySQL SQL 7 min 2025-07-29
title

A Guide to the CREATE TEMPORARY TABLE SQL Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-07-28
title

The Process of Compiling MySQL from Source

author Lukas Vileikis tags MARIADB MySQL SQL 6 min 2025-07-23
title

NewSQL: Everything You Need to Know

author Antonello Zanini tags NewSQL NOSQL SQL 7 min 2025-07-22

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.