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 purposetext
Error: Cannot add a NOT NULL column with default value NULLsql
ALTER TABLE employees ADD COLUMN level TEXT NOT NULL; -- error on purposetext
Error: column "level" of relation "employees" contains null valuesWith 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;| id | name | level |
|---|---|---|
| 1 | Asha | staff |
| 2 | Ravi | staff |
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;| id | name | band |
|---|---|---|
| 1 | Asha | senior |
| 2 | Ravi | standard |
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