MySQL 数据类型之整数类型


• MySQL不仅支持标准SQL中的integer和smallint类型,还支持一些自己的扩展的整数类型
• 下表中指明了具体的类型,存储消耗的字节数,最小最大取值范围,unsigned代表不允许负数,则正整数的取值范围扩大一倍

mysql> use course;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table temp(id tinyint);
Query OK, 0 rows affected (0.20 sec)

mysql> insert into temp values(-128);
Query OK, 1 row affected (0.02 sec)

mysql> insert into temp values(127);
Query OK, 1 row affected (0.06 sec)

mysql> select * from temp;
+------+
| id   |
+------+
| -128 |
|  127 |
+------+
2 rows in set (0.00 sec)

mysql> insert into temp values(128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1

mysql> insert into temp values(0);
Query OK, 1 row affected (0.11 sec)

mysql> insert into temp values(-129);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into temp values(128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from temp;
+------+
| id   |
+------+
| -128 |
|  127 |
|    0 |
+------+
3 rows in set (0.00 sec)

mysql> alter table temp modify id smallint;
Query OK, 3 rows affected (0.33 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from temp;
+------+
| id   |
+------+
| -128 |
|  127 |
|    0 |
+------+
3 rows in set (0.00 sec)

mysql> desc temp;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | smallint(6) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into temp values(129);
Query OK, 1 row affected (0.07 sec)

mysql> select * from temp;
+------+
| id   |
+------+
| -128 |
|  127 |
|    0 |
|  129 |
+------+
4 rows in set (0.00 sec)

mysql> drop table temp;
Query OK, 0 rows affected (0.18 sec)

mysql> create table temp(id tinyint unsigned);
Query OK, 0 rows affected (0.06 sec)

mysql> desc temp;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | tinyint(3) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> insert into temp values(-128);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into temp values(-1);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into temp values(0);
Query OK, 1 row affected (0.09 sec)

mysql> insert into temp values(127);
Query OK, 1 row affected (0.13 sec)

mysql> insert into temp values(128);
Query OK, 1 row affected (0.01 sec)

mysql> insert into temp values(256);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into temp values(250);
Query OK, 1 row affected (0.03 sec)

mysql> 
mysql> 
mysql> desc teacher;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(64) | YES  |     | NULL    |                |
| dept_id | int(11)     | NO   | MUL | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> drop table temp;
Query OK, 0 rows affected (0.13 sec)

mysql> create table temp(id int,name varchar(5));
Query OK, 0 rows affected (0.13 sec)

mysql> insert into temp values(1,'aaaaa');
Query OK, 1 row affected (0.10 sec)

mysql> insert into temp values(1,'aaaaaa');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> drop table temp;
Query OK, 0 rows affected (0.04 sec)

mysql> create table temp(id int(2),id2 int(10));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into temp values(1000000,1000000);
Query OK, 1 row affected (0.09 sec)

mysql> select * from temp;
+---------+---------+
| id      | id2     |
+---------+---------+
| 1000000 | 1000000 |
+---------+---------+
1 row in set (0.00 sec)

mysql> alter table temp modify id int(10) zerofill;
Query OK, 1 row affected (0.12 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from temp;
+------------+---------+
| id         | id2     |
+------------+---------+
| 0001000000 | 1000000 |
+------------+---------+
1 row in set (0.00 sec)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM