MySQL BOOLEAN and TINYINT(1)

In MySQL, BOOL and BOOLEAN are synonyms for TINYINT(1). FALSE is an alias for 0 and TRUE is an alias for 1; in Boolean expressions, any nonzero non-NULL numeric value evaluates as true.

SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;

Output:

1 IS TRUE0 IS FALSENULL is UNKNOWN
111

For two-state application flags, storing only 0 and 1 keeps the meaning clear. However, TINYINT(1) does not restrict storage to those two values. The sample data below deliberately includes 2, 4, and NULL to show the difference.

On this page — Show page sectionsHide page sections

Compare stored values

SELECT * FROM plus2_boolean WHERE feb = TRUE;
SELECT * FROM plus2_boolean WHERE feb = 1;

Output:

namejanfebmar
Ronn010
Lone114
RajuNULL10
SELECT * FROM plus2_boolean WHERE feb = FALSE;

Output:

namejanfebmar
Alex101
John102

Full sample table:

namejanfebmar
Alex101
Ronn010
John102
Lone114
RajuNULL10
KingNULLNULL1

= TRUE versus IS TRUE

TRUE is an alias for 1, so mar = TRUE matches only rows that store 1. A Boolean test such as mar IS TRUE accepts any nonzero, non-NULL value. The mar column in our sample includes 1, 2 and 4.

SELECT name, mar
FROM plus2_boolean
WHERE mar = TRUE
ORDER BY name;

Output for exact equality:

namemar
Alex1
King1
SELECT name, mar
FROM plus2_boolean
WHERE mar IS TRUE
ORDER BY name;

Output for a Boolean test:

namemar
Alex1
John2
King1
Lone4

IS FALSE matches zero, while IS UNKNOWN matches NULL. IS NOT TRUE can match both zero and NULL; they are different from IS FALSE.

Use IS with Boolean expressions

SELECT * FROM plus2_boolean WHERE mar IS TRUE;

Output:

namejanfebmar
Alex101
John102
Lone114
KingNULLNULL1
SELECT * FROM plus2_boolean WHERE jan IS NOT TRUE;

Output:

namejanfebmar
Ronn010
RajuNULL10
KingNULLNULL1
SELECT * FROM plus2_boolean WHERE jan IS UNKNOWN;

Output:

namejanfebmar
RajuNULL10
KingNULLNULL1

IS TRUE / IS FALSE / IS UNKNOWN is useful when NULL needs to be distinguished from false.

NULL-safe equality

SELECT * FROM plus2_boolean WHERE jan = feb;

Output:

namejanfebmar
Lone114

With ordinary =, comparisons involving NULL evaluate to NULL.

SELECT * FROM plus2_boolean WHERE jan <=> feb;

Output:

namejanfebmar
Lone114
KingNULLNULL1

The NULL-safe equality operator <=> returns 1 when both operands are NULL and 0 or 1 for other comparisons.

Display Boolean values with IF()

SELECT name,
  IF(jan, 'OK', 'NOT OK') AS jan,
  IF(feb, 'OK', 'NOT OK') AS feb,
  IF(mar, 'OK', 'NOT OK') AS mar
FROM plus2_boolean;

Output:

namejanfebmar
AlexOKNOT OKOK
RonnNOT OKOKNOT OK
JohnOKNOT OKOK
LoneOKOKOK
RajuNOT OKOKNOT OK
KingNOT OKNOT OKOK

In IF(jan, 'OK', 'NOT OK'), MySQL treats 0 and NULL as not true, so both display NOT OK. If NULL must be shown separately, test it first with IS NULL or use a CASE expression.

Displaying Boolean Values as Checkboxes

MySQL Boolean-style values can be displayed as HTML checkboxes. A nonzero value produces a checked checkbox, while 0 or NULL produces an unchecked checkbox in this IF() example.

SELECT name,
  IF(jan, '<input type="checkbox" checked>', '<input type="checkbox">') AS jan,
  IF(feb, '<input type="checkbox" checked>', '<input type="checkbox">') AS feb,
  IF(mar, '<input type="checkbox" checked>', '<input type="checkbox">') AS mar
FROM plus2_boolean;

The following checkboxes show the initial values stored in the sample table. You can check or uncheck them to see how checkboxes behave.

name jan feb mar
Alex
Ronn
John
Lone
Raju
King

These checkboxes are a visual demonstration. Changing their status here does not update the MySQL database. NULL values are also displayed as unchecked because of the IF() logic.

Want to update MySQL data using checkboxes?

Learn how jQuery sends checkbox changes to PHP and updates the corresponding database record without reloading the page.

Try the jQuery Data Matrix Example →
For application code: return the 0/1/NULL values from SQL and create the checkbox HTML in PHP or the template layer. This keeps database data separate from presentation and lets you handle escaping and accessibility correctly.

Updating Boolean-style values

UPDATE plus2_boolean SET mar = TRUE WHERE name = 'Ronn';
UPDATE plus2_boolean SET mar = 0 WHERE name = 'Ronn';
UPDATE plus2_boolean SET mar = NOT mar WHERE name = 'Ronn';

In the toggle example above, NOT mar becomes 0 for any nonzero value, 1 for zero, and remains NULL for NULL. Use it for known 0/1 flags rather than to preserve arbitrary numeric values. Run these UPDATE examples on a copy of the sample data.

Insert Boolean-style values

INSERT INTO plus2_boolean (name, jan, feb, mar) VALUES
('Alex', 1, 0, 1), ('Ronn', 0, 1, 0);
INSERT INTO plus2_boolean (name, jan, feb, mar) VALUES
('Alex2', TRUE, FALSE, TRUE), ('Ronn2', FALSE, TRUE, FALSE);

See the related checkbox data-matrix tutorial

The INSERT examples add new records; running them more than once can create duplicates or conflicts depending on the table definition. Test INSERT and UPDATE statements on a practice copy.

Restrict a new flag to 0 and 1

Declaring a column BOOLEAN alone does not prevent values such as 2. For a new two-state flag, a NOT NULL column and CHECK constraint make that requirement explicit:

CREATE TABLE boolean_flags_demo (
  id INT PRIMARY KEY,
  is_active BOOLEAN NOT NULL DEFAULT FALSE,
  CONSTRAINT chk_active_binary CHECK (is_active IN (0, 1))
);

MySQL 8.0.16 and later enforce CHECK constraints. This is a separate example table; the plus2_boolean sample intentionally allows values 2, 4 and NULL for comparison demonstrations.

Download the sample data

Related operations: comparison operators, UPDATE, and INSERT.




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer