search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

MySQL | BOOLEAN

schedule Aug 12, 2023
Last updated
local_offer
MySQL
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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
Comment
Citation
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!