Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can SQLite require a value while still allowing an empty string?

Quick answer

Use NOT NULL to reject a missing SQL value (NULL), and allow '' if an empty string is acceptable. If the value must have nonzero length, add CHECK(length(value) > 0). If it must contain something other than ordinary spaces, use CHECK(length(trim(value)) > 0); SQLite’s default trim() removes ordinary spaces at the ends, not every possible whitespace character. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want a SQLite text column to require a value, but I’m not sure whether “required” means that '' should be rejected. What constraint should I use if an empty string is valid?

Geez

If an empty string is valid, declare the column NOT NULL and don’t add a check that rejects zero-length text. NOT NULL rejects SQL NULL, but by itself it does not reject an empty string. [S1]

For example:

CREATE TABLE notes (
  label TEXT NOT NULL
);

With this table definition, NULL is rejected, while '' and 'draft' are allowed. The important distinction is that NULL is a missing SQL value, whereas '' is a text value with no characters. Choose the rule based on what your application means by “required.”

Follow-up question

I actually need the field to contain at least one character. How do I reject '' without rejecting normal text?

Geez

Add a length check alongside NOT NULL:

CREATE TABLE notes (
  label TEXT NOT NULL CHECK (length(label) > 0)
);

NOT NULL rejects a missing SQL value; the check rejects text whose length is zero. This combination allows ordinary nonempty text and rejects ''. [S1]

Here are illustrative outcomes:

  1. NULL — rejected by NOT NULL.
  2. '' — rejected because its length is zero.
  3. 'draft' — accepted because its length is greater than zero.

This is a length rule, not a rule about whether the text looks meaningful. A string containing spaces has characters, so this check does not express “must contain non-space text.”

Follow-up question

What if someone enters only spaces? I want to reject that too, but I don’t want to assume every kind of whitespace behaves the same.

Geez

If you mean ordinary spaces at the beginning or end of the text, check the trimmed value:

CREATE TABLE notes (
  label TEXT NOT NULL CHECK (length(trim(label)) > 0)
);

With SQLite’s default behavior, trim() removes ordinary spaces from both ends. Thus the check rejects an empty string and a string made only of ordinary spaces, while allowing text such as ' draft '. [S2] This behavior should not be described as removing every possible whitespace character. Decide which characters your input rules should treat as whitespace, then choose a rule for that definition.

Assuming the intended rule concerns ordinary spaces, the outcomes are:

  1. NULL — rejected by NOT NULL.
  2. '' — rejected by the check.
  3. ' ' — rejected after trimming.
  4. ' draft ' — accepted because text remains after trimming.

Follow-up question

Can you give me a compact version I can copy, and a way to check that I chose the right rule before I use it?

Geez

Use the version that matches your intended meaning:

-- Empty text is allowed; NULL is not.
CREATE TABLE notes (
  label TEXT NOT NULL
);

-- Text of nonzero length is required; NULL and '' are rejected.
CREATE TABLE notes_nonempty (
  label TEXT NOT NULL CHECK (length(label) > 0)
);

-- Text remaining after trimming ordinary spaces is required.
CREATE TABLE notes_nonspace (
  label TEXT NOT NULL CHECK (length(trim(label)) > 0)
);

These are alternatives, each defining its own table. Before adopting one, write down whether '' should pass and whether input containing only ordinary spaces should pass. Then compare those answers with the examples above. If your requirement includes tabs or other whitespace characters, the behavior described here for default trim() does not settle that broader rule; specify the exact characters you intend to handle and verify an expression for them. [S1] [S2]

A practical checklist

  • Decide whether '' is valid; use NOT NULL alone if it is.

  • If empty text must fail, add CHECK(length(label) > 0).

  • If ordinary spaces alone must fail, consider CHECK(length(trim(label)) > 0).

  • Specify whether the rule covers only ordinary spaces or other whitespace too.

  • Check the expected outcomes for NULL, '', spaces, and ordinary text against your intended rule.

A few more useful answers

Does `NOT NULL` reject an empty string in SQLite?

No. NOT NULL rejects SQL NULL; it does not by itself reject an empty text value. [S1]

Does `length(trim(label)) > 0` reject every whitespace-only string?

Not necessarily. SQLite’s default trim() removes ordinary spaces at the ends, and should not be described as removing every possible whitespace character. [S2]

Which constraint should I use if an empty string is allowed?

Use NOT NULL without a nonempty-text check. That rejects NULL while allowing ''. [S1]

Sources

References used for this guide.

  1. [S1] SQLite CREATE TABLE: NOT NULL and CHECK constraints
    sqlite.org · Checked
  2. [S2] SQLite Built-In Scalar SQL Functions: trim
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work