Sub topic 4
Setting Defaults and NOT NULL
This chapter assumes you know CREATE TABLE, INSERT, UPDATE, and basic constraints. Examples use PostgreSQL syntax. Other database systems use similar ideas, but they can differ in how they handle existing rows and schema changes.
A default is a value the database supplies when an INSERT statement omits a column. NOT NULL is a rule that rejects NULL, which represents a missing or unknown value. These features solve different problems:
- A default supplies a value when no value is provided.
NOT NULLrequires a value to exist.- A default does not automatically make a column
NOT NULL. NOT NULLdoes not tell the database which value to use.
Adding a nullable column with a default
Start with a table containing existing rows:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
INSERT INTO customers (id, name)
VALUES (1, 'Amina'), (2, 'Luis');
ALTER TABLE customers
ADD COLUMN status VARCHAR(20) DEFAULT 'active';The new status column is nullable because the statement did not include NOT NULL. New inserts that omit status receive 'active':
INSERT INTO customers (id, name)
VALUES (3, 'Ravi');
SELECT id, name, status
FROM customers
ORDER BY id;The result includes active for the new row. Depending on the database version and system, existing rows may appear with the default value when the column is added. Do not assume this behavior is identical across database products. Check the documentation for the database you are changing.
A default applies when the column is omitted. It does not replace an explicitly supplied NULL:
INSERT INTO customers (id, name, status)
VALUES (4, 'Noor', NULL);This insert succeeds because status is still nullable.
Making an existing column NOT NULL
Before adding a NOT NULL rule, find and fix missing values:
SELECT COUNT(*) AS missing_statuses
FROM customers
WHERE status IS NULL;
UPDATE customers
SET status = 'active'
WHERE status IS NULL;
ALTER TABLE customers
ALTER COLUMN status SET NOT NULL;The ALTER TABLE statement fails if even one row still contains NULL. After the rule is installed, both of these statements fail:
-- Fails because status is omitted and has no default
INSERT INTO customers (id, name)
VALUES (5, 'Marta');
-- Fails because NULL violates NOT NULL
INSERT INTO customers (id, name, status)
VALUES (6, 'Chen', NULL);If future inserts should receive a value automatically, set a default as well:
ALTER TABLE customers
ALTER COLUMN status SET DEFAULT 'active';The order matters for a large table. First backfill valid values, then add the constraint. A direct change may hold a lock while the database checks every row.
The migration flow
For an existing table, this staged approach separates data repair from constraint enforcement. It also gives you a clear point at which to verify the migration.
| Goal | Typical action |
|---|---|
| Supply a value for omitted inserts | `SET DEFAULT` |
| Reject missing values | `SET NOT NULL` |
| Repair existing missing values | `UPDATE ... WHERE column IS NULL` |
Exercise
Why can adding a default alone still allow a row with NULL in the column?
Show answer
A default is used only when the column is omitted. An explicitly supplied NULL is accepted until a NOT NULL constraint is added.