A boolean column should be NOT NULL with a default in almost every schema, because a nullable boolean holds three states and every query and every line of application code has to guess what the third one means. When a third state is real, such as a user who was never asked, give it a name instead of borrowing NULL for it.

Take a column most products have, a marketing consent flag added to users without a constraint:

CREATE TABLE users (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email text NOT NULL UNIQUE,
  marketing_opt_in boolean
);
INSERT INTO users (email, marketing_opt_in) VALUES
  ('[email protected]', true),
  ('[email protected]', false),
  ('[email protected]', NULL);

Ana said yes, Ben said no, and nobody knows what Cy said. Everything below ran on PostgreSQL 18.3 in a throwaway container, and the MySQL 8.4.11 and SQL Server 2022 results come from the same table written in their own types (VARCHAR(255) and BOOLEAN on MySQL, nvarchar(255) and bit on SQL Server).

What goes wrong when a boolean column allows NULL?

Two queries that each look like “everyone who did not opt in” return different rows, and neither one agrees with the application.

SQL compares NULL as unknown, so a row with NULL matches neither marketing_opt_in nor NOT marketing_opt_in. On the three rows above:

QueryRowsIncludes Cy?
WHERE marketing_opt_in1No
WHERE NOT marketing_opt_in1No
WHERE marketing_opt_in = false1No
WHERE marketing_opt_in IS NOT TRUE2Yes
WHERE NOT coalesce(marketing_opt_in, false)2Yes
count(*)3Yes

Opted in plus opted out is 2, and the table has 3 users. A report that splits users into “subscribed” and “unsubscribed” loses Cy, and a dashboard that subtracts one from the total counts Cy as unsubscribed. Application code adds its own reading: in JavaScript, if (!user.marketingOptIn) is true for null, as if not user.marketing_opt_in is for None in Python, so the code that lists “users who did not opt in” returns Ben and Cy while the SQL export of “users who declined” returns only Ben. Each call site made a reasonable choice, and the two lists still disagree about who declined.

The same thing happens on MySQL and SQL Server. SUM(marketing_opt_in = FALSE) returned 1 and SUM(marketing_opt_in IS NOT TRUE) returned 2 on MySQL, and on SQL Server WHERE marketing_opt_in <> 1 returned 1, not 2, because NULL <> 1 is unknown there too.

Should boolean columns be nullable?

Only when you can write down what NULL means, and even then a named state usually beats it.

The choice comes down to how many states the business actually has, and whether it needs to know when the state changed:

What the column recordsUseWhy
Two states, such as is_archived or is_adminboolean NOT NULL DEFAULT falseEvery row is true or false, and every query agrees
Something that happens once, such as a verified emailemail_verified_at timestamptz, nullableNULL has one meaning, “not yet”, and you also keep when
Three real states, such as consent that was never askedtext NOT NULL DEFAULT 'not_asked' with CHECK (marketing_consent IN ('not_asked', 'granted', 'declined'))The third state has a name every query has to spell out
Three states, documented and acceptedboolean without NOT NULLLegitimate, but every reader has to know the rule

The timestamp row is the one most often missed. email_verified boolean cannot answer when the address was verified, email_verified_at answers both questions, and its NULL has one intended meaning, not yet. For the three-state row, the choice between a CHECK, an enum type and a lookup table is the same one covered in PostgreSQL enum vs check constraint vs lookup table, with MySQL’s version in MySQL ENUM vs CHECK constraint.

The last row exists. Some teams keep a nullable flag on purpose, for example a survey answer where “skipped” really is unknown and no report needs to count it. That is a decision, and it is fine as long as it is written down where the next engineer will look.

How do you make an existing nullable boolean NOT NULL?

Decide what each NULL row meant, write that value into the rows, and only then add the constraint.

Setting a default does not touch existing rows, so the obvious migration fails:

ALTER TABLE users ALTER COLUMN marketing_opt_in SET DEFAULT false;
ALTER TABLE users ALTER COLUMN marketing_opt_in SET NOT NULL;
ERROR:  column "marketing_opt_in" of relation "users" contains null values

The UPDATE that fixes it is where the real decision lives. For marketing consent, NULL can only safely mean “no”, because emailing someone who never agreed is the expensive mistake:

BEGIN;
UPDATE users SET marketing_opt_in = false WHERE marketing_opt_in IS NULL;
ALTER TABLE users
  ALTER COLUMN marketing_opt_in SET DEFAULT false,
  ALTER COLUMN marketing_opt_in SET NOT NULL;
COMMIT;

On a copy with 1,000,000 users, a third of them NULL, the UPDATE touched 333,333 rows in 1.37 seconds and SET NOT NULL scanned the table in 94 ms. Both hold locks: the UPDATE locks every row it changes, and SET NOT NULL takes an ACCESS EXCLUSIVE lock, which blocks reads as well as writes, for its scan. On a large, busy table, batch the backfill, add the constraint NOT VALID and then run VALIDATE CONSTRAINT, which scans without blocking reads or writes, as described in adding a NOT NULL column to a large PostgreSQL table.

If NULL meant “never asked”, the column has three states, and the right migration names them instead of collapsing them. The rename comes first, so the constraint takes the new name, and the type change is one rewrite of the table under one ACCESS EXCLUSIVE lock for its whole length, so plan it like any other rewrite:

ALTER TABLE users RENAME COLUMN marketing_opt_in TO marketing_consent;
ALTER TABLE users
  ALTER COLUMN marketing_consent TYPE text USING CASE marketing_consent
    WHEN true THEN 'granted' WHEN false THEN 'declined' ELSE 'not_asked' END,
  ALTER COLUMN marketing_consent SET DEFAULT 'not_asked',
  ALTER COLUMN marketing_consent SET NOT NULL,
  ADD CONSTRAINT users_marketing_consent_check
    CHECK (marketing_consent IN ('not_asked', 'granted', 'declined'));

Ana became granted, Ben declined and Cy not_asked, and from then on no query can forget that Cy exists. The rename is also what protects the application: code still testing if (!user.marketingOptIn) would read the string 'declined' as true, while a renamed column makes that code fail loudly instead, so ship the application change with the migration.

Do MySQL and SQL Server change the answer?

No, but each adds a trap of its own: MySQL’s BOOLEAN is a small integer, and SQL Server’s default constraint does not fill existing rows.

MySQL has no boolean type. BOOLEAN is stored as tinyint(1), which SHOW CREATE TABLE printed back, and NOT NULL does not make it two-valued: with the column already NOT NULL, INSERT INTO users (email, marketing_opt_in) VALUES ('[email protected]', 2) succeeded under the default strict mode and stored 2. Add CHECK (marketing_opt_in IN (0, 1)) if two values is the rule. Under the default strict mode the nullable-to-NOT NULL change fails the same way as on PostgreSQL, ALTER TABLE users MODIFY marketing_opt_in BOOLEAN NOT NULL DEFAULT FALSE returning ERROR 1138 (22004): Invalid use of NULL value, so the backfill comes first there too. With strict mode off it is worse: the same statement succeeded with warning 1265, Data truncated for column 'marketing_opt_in' at row 3, and Cy’s NULL became 0. MySQL made the decision for you, and made it silently.

SQL Server’s BIT really holds only 0 and 1: inserting 2 stored 1, because SQL Server converts any nonzero value to 1, as Microsoft’s bit documentation states. A nullable BIT is still three states, though. ALTER TABLE users ADD CONSTRAINT df_users_marketing_opt_in DEFAULT 0 FOR marketing_opt_in changed nothing in the existing rows, and the next statement, ALTER TABLE users ALTER COLUMN marketing_opt_in bit NOT NULL, failed with Msg 515, “Cannot insert the value NULL into column ‘marketing_opt_in’”. Run the UPDATE, then the ALTER COLUMN.

PostgreSQL 18MySQL 8.4SQL Server 2022
Boolean typebooleanBOOLEAN, stored as tinyint(1)bit
Accepts a value of 2NoYes, unless a CHECK refuses itStored as 1
NOT NULL while NULL rows existcontains null valuesERROR 1138 in strict mode, NULL becomes 0 without itMsg 515
New default fills old NULL rowsNoNoNo

How Schemity finds nullable booleans and shows what fixing one costs

In Schemity Pro, lint lists every nullable boolean in the schema with a Fix that makes it NOT NULL, and impact analysis then shows what that migration risks on the live table.

Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. In Schemity Pro, the nullable-boolean rule of schema lint reports each such column with “Boolean column is nullable, admitting a third state alongside true and false”, and the detail line “Every read has to decide what NULL means, and different call sites will decide differently.” It covers boolean on PostgreSQL, BOOLEAN on MySQL, which Schemity reads from tinyint(1), and a nullable BIT on SQL Server. It is filed under Convention, not as an error, because the last row of the table above is a legitimate choice, and you can switch the rule off.

Schemity's Lint drawer on a PostgreSQL 18.3 diagram of users, whose marketing_opt_in BOOLEAN column carries the N marker for nullable: under Convention (1), the finding on users.marketing_opt_in reads Boolean column is nullable, admitting a third state alongside true and false, with the detail line Every read has to decide what NULL means, and different call sites will decide differently, and the actions Ignore, Copy, Show on canvas and Fix

The finding has a Fix button, part of Pro lint like the rule itself. The dialog offers “Make it NOT NULL with default FALSE” or “with default TRUE” (0 and 1 on SQL Server), or simply “Make it NOT NULL” when the column already has a default, and applies the choice as one unsaved, undoable edit on the diagram, after listing what it will change. Nothing touches the database yet.

Schemity's Fix dialog for the finding Boolean column is nullable, admitting a third state alongside true and false, on users.marketing_opt_in: Choose one offers Make it NOT NULL with default FALSE, selected, and Make it NOT NULL with default TRUE, and This will lists Make users.marketing_opt_in NOT NULL and Set the default of users.marketing_opt_in to FALSE, above the Cancel and Apply fix buttons

That edit is where the cost shows. Impact analysis, also in Schemity Pro, reads the planned migration against the live database. On the 1,000,000-row copy it reports that the step “Sets users.marketing_opt_in NOT NULL, fails if any of ~1M rows is NULL”, and that “Checking marketing_opt_in for NULL holds back reads and writes to users” while it reads those rows. The ~1M is the catalog’s estimate of the whole table; a Count exactly button runs a read-only count of the NULL rows when you ask for it, which is how many rows your UPDATE has to decide.

Schemity's Impact drawer after the fix, users.marketing_opt_in now BOOLEAN with default FALSE on the canvas: under Can fail on existing data (1), Sets users.marketing_opt_in NOT NULL, fails if any of ~1M rows is NULL, with Show on canvas and Count exactly buttons; under Holds back other statements (1), Checking marketing_opt_in for NULL holds back reads and writes to users while it reads ~1M rows, 147 MB on disk

The migration SQL diff in Preview changes shows the planned statements, ALTER TABLE "users" ALTER COLUMN "marketing_opt_in" SET NOT NULL; and then SET DEFAULT FALSE, and no UPDATE.

Schemity's Preview changes for the fix: the planned migration is ALTER TABLE users ALTER COLUMN marketing_opt_in SET NOT NULL followed by SET DEFAULT FALSE, planned changes list one altered column, Lint: 0, and Impact: 2 repeats the two findings on ~1M rows

Schemity does not write the UPDATE for you, on purpose: only you know whether Cy meant no or never asked. If the answer is “never asked”, the better edit is the text column with its CHECK, which is how you design the next one rather than patch this one.

The short version

Default to NOT NULL DEFAULT false, name a third state when it is real, and backfill before you constrain.

The same “what does NULL mean here” question decides unique constraints on nullable columns, and schema linting vs migration linting explains why a finding like this one reports a fact rather than a verdict.

Frequently asked questions

When is a nullable boolean column the right choice?

Almost never. It has three states, and WHERE flag = false, WHERE NOT flag and application code like if (!user.flag) disagree about the third one. Use NOT NULL DEFAULT false, and if you really need a third state, name it with a text column and a CHECK constraint or record it as a nullable timestamp.

How do I change a nullable boolean to NOT NULL in PostgreSQL?

Fill the NULL rows first, then set the constraint: UPDATE users SET marketing_opt_in = false WHERE marketing_opt_in IS NULL; followed by ALTER TABLE users ALTER COLUMN marketing_opt_in SET DEFAULT false, ALTER COLUMN marketing_opt_in SET NOT NULL;. Without the update, PostgreSQL refuses with column contains null values. Setting the default alone changes no existing row.

Is a MySQL BOOLEAN NOT NULL column limited to true and false?

No. MySQL's BOOLEAN is TINYINT(1), so NOT NULL keeps out NULL but MySQL 8.4 still stored the value 2 when we inserted it. Add CHECK (marketing_opt_in IN (0, 1)) if the column must hold only two values. SQL Server's BIT has no such gap: it turns any nonzero value into 1.