In depth parsing of mysql integer data types

  • 2020-05-17 06:47:39
  • OfStack

Here we give int char does not give their width, the system will assign it a width by default.
M indicates the maximum display width. The maximum effective display width is 255. The display width is independent of the storage size or the range of values contained by the type
Let's do an experiment

mysql(root@localhost:test 03:19:00)>create table c (
-> id int not null,
-> name char not null);
Query OK, 0 rows affected (0.25 sec)
mysql(root@localhost:test 03:19:34)>desc c;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(1) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

So we can see that here, the system will automatically give us a default bandwidth value for our data type, and this width value is only valid for zerofill. So let's see what the other defaults are,

mysql(root@localhost:test 03:34:53)>alter table c modify id smallint;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql(root@localhost:test 03:39:39)>desc c;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | smallint(6) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql(root@localhost:test 03:39:44)>alter table c modify id bigint;
Query OK, 4 rows affected (0.23 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql(root@localhost:test 03:40:12)>desc c;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | bigint(20) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

Here we will look at the case when the insert value is larger than the value range of the data type:

mysql(root@localhost:test 03:25:58)>insert into c values(300,'chen');
Query OK, 1 row affected, 2 warnings (0.08 sec)
mysql(root@localhost:test 03:26:20)>show warnings;
+---------+------+---------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column 'id' at row 1 |
| Warning | 1265 | Data truncated for column 'name' at row 1 |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)
mysql(root@localhost:test 03:26:27)>select * from c;
+------+------+
| id | name |
+------+------+
| 127 | c |
+------+------+
1 row in set (0.02 sec)
mysql(root@localhost:test 03:26:40)>insert into c values(320,'chen');
Query OK, 1 row affected, 2 warnings (0.05 sec)
mysql(root@localhost:test 03:26:53)>select * from c;
+------+------+
| id | name |
+------+------+
| 127 | c |
| 127 | c |
+------+------+
2 rows in set (0.00 sec)

The tinyint here is a byte long, which is an integer that can represent a range from 0 to 255, but the reason why it goes all the way up to 127 is because we didn't give it an unsigned type.

Related articles: