mysql整數(shù)數(shù)據(jù)類型深入解析
更新時間:2013年06月14日 08:51:40 作者:
本篇文章是對mysql中的整數(shù)數(shù)據(jù)類型進行了詳細的分析介紹,需要的朋友參考下
此處我們給int char沒有給出他們的寬度,系統(tǒng)默認會給它分配一個寬度。
M指示最大顯示寬度。最大有效顯示寬度是255。顯示寬度與存儲大小或類型包含的值的范圍無關
我們來進行下試驗
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)
那么我們可以看到這里,系統(tǒng)會自動為我們的數(shù)據(jù)類型給出一個默認的寬帶值,這里這個寬度值其實只有在zerofill的作用下才能起到一定的作用。在下面我們看下其他的默認值是多少,
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)
這里我們再來看下當插入值大于數(shù)據(jù)類型的取值范圍的情況:
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)
這里的tinyint是占有一個字節(jié),就是可以表示從0-255這個范圍的整數(shù),可是這里為什么直到127呢,原因是我們沒有給他設定無符號類型的。
M指示最大顯示寬度。最大有效顯示寬度是255。顯示寬度與存儲大小或類型包含的值的范圍無關
我們來進行下試驗
復制代碼 代碼如下:
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)
那么我們可以看到這里,系統(tǒng)會自動為我們的數(shù)據(jù)類型給出一個默認的寬帶值,這里這個寬度值其實只有在zerofill的作用下才能起到一定的作用。在下面我們看下其他的默認值是多少,
復制代碼 代碼如下:
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)
這里我們再來看下當插入值大于數(shù)據(jù)類型的取值范圍的情況:
復制代碼 代碼如下:
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)
這里的tinyint是占有一個字節(jié),就是可以表示從0-255這個范圍的整數(shù),可是這里為什么直到127呢,原因是我們沒有給他設定無符號類型的。
相關文章
有效查詢MySQL表中重復數(shù)據(jù)的方法和技巧分享
在MySQL數(shù)據(jù)庫中,偶爾會遇到需要查找表中出現(xiàn)的重復數(shù)據(jù)的情況,這種情況下,我們可以通過編寫一些SQL查詢語句輕松地找到并處理這些重復行,本文將介紹一些常見的方法和技巧,幫助你有效地查詢MySQL表中的重復數(shù)據(jù),需要的朋友可以參考下2023-10-10

