EN
MySQL - boolean data type
2
points
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 forTINYINT(1). A value of zero is consideredfalse.
Nonzero values are consideredtrue
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';