Booleano vs tinyint(1) para valores booleanos en MySQL


¿Qué tipo de columna es mejor usar en una base de datos MySQL para valores booleanos? Yo uso boolean pero mi colega usa tinyint(1).

 87
Author: tom, 2010-09-20

6 answers

Estos tipos de datos son sinónimos.

 108
Author: Māris Kiseļovs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-15 18:21:49

Voy a tomar un enfoque diferente aquí y sugerir que es tan importante para sus compañeros desarrolladores entender su código como lo es para el compilador/base de datos. Usar booleano puede hacer lo mismo que usar tinyint, sin embargo, tiene la ventaja de transmitir semánticamente cuál es tu intención, y eso vale algo.

Si usas un tinyint, no es obvio que los únicos valores que deberías ver sean 0 y 1. Un booleano SIEMPRE es verdadero o falso.

 65
Author: dj_segfault,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-20 15:10:36

boolean no es un tipo de datos distinto en MySQL; es solo un sinónimo de tinyint. Vea esta página en el manual de MySQL.

Personalmente sugeriría usar tinyint como preferencia, porque booleano no hace lo que crees que hace desde el nombre, por lo que hace que el código sea potencialmente engañoso. Pero a un nivel práctico, realmente no importa both ambos hacen lo mismo, por lo que no estás ganando o perdiendo nada al usar cualquiera de los dos.

 28
Author: Spudley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-20 13:26:57

Use enum es el fácil y más rápido

No recomendaré enum o tinyint(1) ya que bit(1) solo necesita 1 bit para almacenar el valor booleano, mientras que tinyint(1) necesita 8 bits.

Ref

TINYINT vs ENUM (0, 1) para valores booleanos en MySQL

 6
Author: Pramendra Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:10:41

Si bien es cierto que bool y tinyint(1) son funcionalmente idénticos, bool debería ser la opción preferida porque lleva el significado semántico de lo que estás tratando de hacer. Además, muchosMs convertirán bool al tipo booleano nativo de su lenguaje de programación.

 2
Author: Kyle Morgan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-21 02:12:11

Mi experiencia al usar Dapper para conectarse a MySQL es que no. Cambié un bit no nullable (1) a un tinyint nullable(1) usando el siguiente script:

ALTER TABLE TableName MODIFY Setting BOOLEAN null;

Entonces Dapper comenzó a lanzar excepciones. Traté de ver la diferencia antes y después del guión. Y notó que el bit(1) había cambiado a tinyint (1).

Entonces corrí:

ALTER TABLE TableName CHANGE COLUMN Setting Setting BIT(1) NULL DEFAULT NULL;

Que resolvió el problema.

 0
Author: smerlung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-04 08:45:28