Boolean & Binary Types
Alongside numbers, text, and dates, most databases give you a dedicated type for true/false flags and a family of types for storing raw binary data. Both are used less often than numeric or character columns, but understanding them — and their quirks — saves real confusion later.
BOOLEAN
A BOOLEAN column holds one of three possible values: TRUE, FALSE, or NULL. That third option is easy to forget — a nullable boolean isn't really a two-valued flag, it's a three-valued one, and NULL means "unknown," not "false." This matters a lot once the column is used inside a WHERE clause, because SQL's three-valued logic treats comparisons against NULL specially (covered in depth on the NULL pages).
BOOLEAN with three possible states
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
title VARCHAR(200) NOT NULL,
is_complete BOOLEAN NOT NULL DEFAULT FALSE, -- NOT NULL avoids the 3rd state
is_urgent BOOLEAN -- nullable: TRUE, FALSE, or unknown
);
SELECT * FROM tasks WHERE is_complete = FALSE;
SELECT * FROM tasks WHERE is_urgent IS NULL; -- "urgency not yet decided"Binary types
Binary types store raw bytes rather than text — arbitrary data that isn't meant to be interpreted as characters in any particular encoding. Common names are BYTEA (PostgreSQL), BLOB (MySQL and many others), and VARBINARY (SQL Server, MySQL).
Type | Dialect | Typical use |
|---|---|---|
BYTEA | PostgreSQL | Arbitrary binary data, small-to-medium size |
BLOB | MySQL / general SQL | Binary Large OBject — images, files, serialized data |
VARBINARY(n) | SQL Server / MySQL | Variable-length binary data up to n bytes |
A binary column for a small hash or token
CREATE TABLE api_keys (
id SERIAL PRIMARY KEY,
key_hash BYTEA NOT NULL -- store a hashed token, not raw binary files
);Should you store files in the database?
It is technically possible to store entire images, PDFs, or videos directly in a binary column, but doing so at any real scale usually causes problems: database backups balloon in size, replication and queries get slower, and the database becomes a bottleneck for what is fundamentally simple file storage.
Prefer
BOOLEANwithNOT NULL DEFAULT FALSEfor simple flags where "unknown" is not a meaningful state.Allow
NULLon a boolean only when "not yet decided" is genuinely different from both true and false.Use binary types for small, always-fetched-together binary values, not general file storage.
Store a URL or path (as
VARCHAR/TEXT) instead of the file itself for anything beyond a few kilobytes.