mysql整數(shù)數(shù)據(jù)類型深入解析
更新時間:2013年06月14日 08:51:40 作者:
本篇文章是對mysql中的整數(shù)數(shù)據(jù)類型進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
此處我們給int char沒有給出他們的寬度,系統(tǒng)默認(rèn)會給它分配一個寬度。
M指示最大顯示寬度。最大有效顯示寬度是255。顯示寬度與存儲大小或類型包含的值的范圍無關(guān)
我們來進(jìn)行下試驗(yàn)
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ù)類型給出一個默認(rèn)的寬帶值,這里這個寬度值其實(shí)只有在zerofill的作用下才能起到一定的作用。在下面我們看下其他的默認(rèn)值是多少,
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)
這里我們再來看下當(dāng)插入值大于數(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呢,原因是我們沒有給他設(shè)定無符號類型的。
M指示最大顯示寬度。最大有效顯示寬度是255。顯示寬度與存儲大小或類型包含的值的范圍無關(guān)
我們來進(jìn)行下試驗(yàn)
復(fù)制代碼 代碼如下:
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ù)類型給出一個默認(rèn)的寬帶值,這里這個寬度值其實(shí)只有在zerofill的作用下才能起到一定的作用。在下面我們看下其他的默認(rèn)值是多少,
復(fù)制代碼 代碼如下:
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)
這里我們再來看下當(dāng)插入值大于數(shù)據(jù)類型的取值范圍的情況:
復(fù)制代碼 代碼如下:
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呢,原因是我們沒有給他設(shè)定無符號類型的。
相關(guān)文章
Centos7下使用yum安裝mysql數(shù)據(jù)庫的詳細(xì)教程(增強(qiáng)版)
這篇文章主要介紹了Centos7下使用yum安裝mysql數(shù)據(jù)庫的詳細(xì)教程(增強(qiáng)版),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12解決mysql報錯:Data?source?rejected?establishment?of?connect
這篇文章主要給大家介紹了關(guān)于如何解決mysql報錯:Data?source?rejected?establishment?of?connection,?message?from?server:?\"Too?many?connectio的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02windows下如何解決mysql secure_file_priv null問題
這篇文章主要介紹了windows下如何解決mysql secure_file_priv null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01mysql日期函數(shù)TO_DAYS()函數(shù)的詳細(xì)講解
在SQL中我們經(jīng)常需要根據(jù)時間字段查詢數(shù)據(jù),今天用到一個好用的時間字段,用來查詢一整天的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于mysql日期函數(shù)TO_DAYS()函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-08-08mysql 8.0.12 安裝配置方法圖文教程(windows10)
這篇文章主要為大家詳細(xì)介紹了windows10下mysql 8.0.12 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08