mysql通过插入数据来查看tinyint的大小

查看表结构

MariaDB [film]> show create table test;
+-------+------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                         |
+-------+------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-------+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

插入int类型的id,大于tinyint的数值

MariaDB [film]> insert into test set id = 9999;
Query OK, 1 row affected, 1 warning (0.01 sec)

MariaDB [film]> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column 'id' at row 1 |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)

查看写入的数据

数据写进去了,但是被截断了,如果在MySQL数据库严格模式下,这条数据将无法写入

MariaDB [film]> select *from test;
+-----+
| id  |
+-----+
|   2 |
| 127 |
+-----+
2 rows in set (0.00 sec)

插入一个负数

我们得到了同样的结果,

MariaDB [film]> insert into test set id = -9999;
Query OK, 1 row affected, 1 warning (0.02 sec)
MariaDB [film]> select *from test;
+------+
| id   |
+------+
|    2 |
|  127 |
| -128 |
+------+
3 rows in set (0.00 sec)

总结

MySQL数据类型tinyint的大小为-128 ~ 127,那么如果是无符号的tinyint呢?数值范围是:0 ~ 255

0 comments

To reply to the article, please Login or registered