Languages
[Edit]
EN

MySQL - boolean data type

2 points
Created by:
WGates
412

Simplest solution:

`is_user_removed` TINYINT(1) NOT NULL DEFAULT '0'

 

To use BOOL or BOOLEAN in MySQL we use TINYINT(1) data type.

With TINYINT '0' is false and '1' is true.

From MySQL documentation:

BOOL, BOOLEAN
These types are synonyms for TINYINT(1). A value of zero is considered false.
Nonzero values are considered true

Full MySQL documentation can be found here.

1. Database schema with boolean example

DROP DATABASE `db_user_tests`;
CREATE DATABASE `db_user_tests`;
USE `db_user_tests`;

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`
(
	`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    `is_user_removed` TINYINT(1) NOT NULL DEFAULT '0',
	PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci' ENGINE=InnoDB;

2. Adding boolean column to existing table example

MySQL query for add boolean column to existing table (alter table):

ALTER TABLE `users`
    ADD COLUMN `is_user_removed` TINYINT(1) NOT NULL DEFAULT '0';

References

  1. Numeric type overview - MySQL docs
  2. Integer type - MySQL docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - Problems

MySQL - boolean data type
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join