MySQL: Tinyint (2) vs tinyint (1) - ¿cuál es la diferencia?


Sabía booleano en mysql como tinyint (1).

Hoy veo una mesa con definido un entero como tinyint(2), y también otros como int(4), int(6) ...

¿Qué significa el tamaño en el campo de tipo entero y tinyint ?

Author: phoops, 2012-10-11

4 answers

Significa ancho de pantalla

Ya sea que use tinyint(1) o tinyint(2), no hace ninguna diferencia.

Siempre uso tinyint(1) e int(11), utilicé varios clientes mysql (navicat, sequel pro).

No significa nada EN absoluto! Hice una prueba, todos los clientes anteriores o incluso el cliente de línea de comandos parece ignorar esto.

Pero, display width es más importante si está utilizando la opción ZEROFILL, por ejemplo, su tabla tiene la siguiente 2 columnas:

A tinyint (2) zerofill

B tinyint (4) zerofill

Ambas columnas tienen el valor de 1, la salida para la columna A {[5] } sería 01 y 0001 para B , como se ve en la captura de pantalla a continuación :)

zerofill con ancho de pantalla

 31
Author: AamirR,
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
2016-12-02 18:39:51

El (m) indica el ancho de visualización de la columna; aplicaciones como el cliente MySQL hacen uso de esto al mostrar los resultados de la consulta.

Por ejemplo:

| v   | a   |  b  |   c |
+-----+-----+-----+-----+
| 1   | 1   |  1  |   1 |
| 10  | 10  | 10  |  10 |
| 100 | 100 | 100 | 100 |

Aquí a, b y c están usando TINYINT(1), TINYINT(2) y TINYINT(3) respectivamente. Como puede ver, rellena los valores en el lado izquierdo usando el ancho de pantalla.

Es importante tener en cuenta que no afecta el rango aceptado de valores para ese tipo en particular, es decir, TINYINT(1) todavía acepta [-128 .. 127].

 196
Author: Ja͢ck,
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
2014-09-03 09:22:30
mysql> CREATE TABLE tin3(id int PRIMARY KEY,val TINYINT(10) ZEROFILL);
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO tin3 VALUES(1,12),(2,7),(4,101);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tin3;
+----+------------+
| id | val        |
+----+------------+
|  1 | 0000000012 |
|  2 | 0000000007 |
|  4 | 0000000101 |
+----+------------+
3 rows in set (0.00 sec)

mysql>

mysql> SELECT LENGTH(val) FROM tin3 WHERE id=2;
+-------------+
| LENGTH(val) |
+-------------+
|          10 |
+-------------+
1 row in set (0.01 sec)


mysql> SELECT val+1 FROM tin3 WHERE id=2;
+-------+
| val+1 |
+-------+
|     8 |
+-------+
1 row in set (0.00 sec)
 18
Author: zloctb,
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
2014-04-17 07:09:19

Acerca de la INT, TINYINT... Estos son diferentes tipos de datos, INT es un número de 4 bytes, TINYINT es un número de 1 byte. Más información aquí - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT.

La sintaxis del tipo de datos TINYINT es TINYINT(M), donde M indica el ancho máximo de visualización (utilizado solo si su cliente MySQL lo soporta).

Atributos de tipo Numérico .

 13
Author: Devart,
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
2013-12-18 06:06:27