search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

MySQL | BOOLEAN

Database
chevron_right
MySQL
chevron_right
Documentation
chevron_right
Data types
chevron_right
Numeric
schedule Jul 1, 2022
Last updated
local_offer MySQL
Tags
tocTable of Contents
expand_more

MySQL inherently does not support native Boolean data types. However, MySQL still provides us with the datatypes: BOOLEAN and BOOL, which are just aliases for TINYINT.

NOTE

The unfortunate consequence of this mapping is that TINYINT can take on a value within the interval of 0 to 255. This means that, if we are not careful, we can insert values like 5, which clearly is not desirable.

In MySQL, the keyword TRUE evaluates to 1, while FALSE evaluates to 0. When inserting values into the database, we can use the aliases TRUE and FALSE instead of messing around with numerical values.

Example

To create a table with a boolean column is_married:

CREATE TABLE people (name VARCHAR(20), age INT, is_married BOOLEAN);
Query OK, 0 rows affected (0.04 sec)

To insert values for is_married using TRUE and FALSE aliases:

INSERT INTO people (name, age, is_married) VALUES ("Alex", 30, TRUE);
INSERT INTO people (name, age, is_married) VALUES ("Bob", 15, FALSE);

To check how the inserted values look:

SELECT * FROM people;
+------+------+------------+
| name | age | is_married |
+------+------+------------+
| Alex | 30 | 1 |
| Bob | 15 | 0 |
+------+------+------------+

Notice how the aliases TRUE and FALSE are automatically converted to 1 and 0 when stored.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!