MySql: Tinyint (2) vs tinyint(1) - what is the difference?

前端 未结 4 602
轮回少年
轮回少年 2020-11-29 15:38

I knew boolean in mysql as tinyint (1).

Today I see a table with defined an integer like tinyint(2), and also others like int(4)

4条回答
  •  一个人的身影
    2020-11-29 16:29

    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)
    

提交回复
热议问题