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