SQL BOOLEAN type
What TRUE and FALSE actually are on each of the five engines.
- 類別
- 條件
- Returns
boolean / bit / tinyint / integer- In this category
- 31 of 43
語法
col BOOLEAN | col BIT | col BOOLEAN -- really TINYINT(1)
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | BIT: 0, 1 or NULL; no TRUE/FALSE literals |
| PostgreSQL | 是 | real BOOLEAN type |
| MySQL | Different name or syntax | BOOLEAN is an alias for TINYINT(1); TRUE and FALSE are just 1 and 0 |
| SQLite | Different name or syntax | INTEGER 0/1; TRUE and FALSE keywords since 3.23 |
| Oracle | Different name or syntax | BOOLEAN column type and TRUE/FALSE literals in 23ai; earlier releases use NUMBER(1) plus a CHECK |
範例
PostgreSQL
SELECT true, false, NULL::bool;
SQL Server
SELECT CAST(1 AS BIT) AS t, CAST(0 AS BIT) AS f;
MySQL
SELECT TRUE = 1 AS one, TRUE + 1 AS two;
小知識
- MySQL's BOOLEAN stores any tinyint: 2 is not an error, so 'boolean' columns need a CHECK to stay 0/1.
- SQL Server has no TRUE/FALSE literals: write 1 and 0, or CAST(1 AS BIT). The strings 'TRUE' and 'FALSE' do convert to BIT 1 and 0.
- Oracle only gained a SQL BOOLEAN with 23ai; for decades the pattern has been NUMBER(1) with a CHECK constraint.
- PostgreSQL and Oracle 23ai are the two where a boolean column holds real TRUE/FALSE values and conditions can be selected and compared as values.
Questions about BOOLEAN
Is MySQL's BOOLEAN a real boolean?
No, it is TINYINT(1) with TRUE/FALSE mapped to 1/0; any value from -128 to 127 fits unless you add a CHECK.
How do I write TRUE in SQL Server?
You cannot; use 1 (or CAST(1 AS BIT)) in comparisons.
Does Oracle support BOOLEAN in SQL?
Only since 23ai; before that NUMBER(1) with a CHECK (col IN (0, 1)).
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.