← Back to SQL, page 10

sql-0182 · Medium mid

How do you add a NOT NULL column to a table that already has rows?

Add the column with a DEFAULT, so existing rows get a value and the constraint holds. Without a default, the ALTER fails because existing rows would contain NULL. For computed values, add the column as nullable, backfill with UPDATE, then add NOT NULL (PostgreSQL) or rebuild the table (SQLite).

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
ALTER TABLE employees ADD COLUMN level TEXT NOT NULL;  -- error on purpose
text
Error: Cannot add a NOT NULL column with default value NULL
sql
ALTER TABLE employees ADD COLUMN level TEXT NOT NULL;  -- error on purpose
text
Error: column "level" of relation "employees" contains null values

With a default the change succeeds and existing rows show it:

sql
ALTER TABLE employees ADD COLUMN level TEXT NOT NULL DEFAULT 'staff';
SELECT id, name, level FROM employees WHERE id <= 2 ORDER BY id;
idnamelevel
1Ashastaff
2Ravistaff

When the value must be computed per row, a three-step migration works on PostgreSQL:

sql
ALTER TABLE employees ADD COLUMN band TEXT;
UPDATE employees SET band = CASE WHEN salary >= 100000 THEN 'senior' ELSE 'standard' END;
ALTER TABLE employees ALTER COLUMN band SET NOT NULL;
SELECT id, name, band FROM employees WHERE id <= 2 ORDER BY id;
idnameband
1Ashasenior
2Ravistandard

SQLite's ALTER TABLE cannot add NOT NULL to an existing column; the usual path is to create a new table, copy the data and rename it. On large PostgreSQL tables, adding a column with a constant default is fast (since version 11), while a backfill UPDATE rewrites rows and should run in batches.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

-- error on purpose
ALTER TABLE employees ADD COLUMN level TEXT NOT NULL;

ALTER TABLE employees ADD COLUMN level TEXT NOT NULL DEFAULT 'staff';
SELECT id, name, level FROM employees WHERE id <= 2 ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • NOT NULL without a default fails on a non-empty table
  • A DEFAULT fills existing rows
  • Backfill then SET NOT NULL for computed values
  • SQLite needs a table rebuild to change constraints

Common mistakes

  • Running a huge single backfill UPDATE during peak traffic
  • Adding NOT NULL before backfilling

Follow-ups

  • How would you rename a column safely in a live system?
  • Why are constant defaults fast in PostgreSQL 11+?

Tips

  • Describe the expand and backfill steps