欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL中數(shù)據(jù)類型的驗證

 更新時間:2016年04月13日 11:10:33   作者:iVictor  
這篇文章主要介紹了MySQL中數(shù)據(jù)類型的驗證 的相關(guān)資料,需要的朋友可以參考下

CHAR

char (M) M字符,長度是M*字符編碼長度,M最大255。

驗證如下:

mysql> create table t1(name char(256)) default charset=utf8;
ERROR 1074 (42000): Column length too big for column 'name' (max = 255); use BLOB or TEXT instead
mysql> create table t1(name char(255)) default charset=utf8;
Query OK, 0 rows affected (0.06 sec)
mysql> insert into t1 values(repeat('整',255));
Query OK, 1 row affected (0.00 sec)
mysql> select length(name),char_length(name) from t1;
+--------------+-------------------+
| length(name) | char_length(name) |
+--------------+-------------------+
| 765 | 255 |
+--------------+-------------------+
1 row in set (0.00 sec) 

VARCHAR

VARCHAR(M),M同樣是字符,長度是M*字符編碼長度。它的限制比較特別,行的總長度不能超過65535字節(jié)。

mysql> create table t1(name varchar(65535));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table t1(name varchar(65534));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table t1(name varchar(65533));
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table t1(name varchar(65532));
Query OK, 0 rows affected (0.08 sec) 

注意,以上表的默認(rèn)字符集是latin1,字符長度是1個字節(jié),所以對于varchar,最大只能指定65532字節(jié)的長度。

如果是指定utf8,則最多只能指定21844的長度

mysql> create table t1(name varchar(65532)) default charset=utf8;
ERROR 1074 (42000): Column length too big for column 'name' (max = 21845); use BLOB or TEXT instead
mysql> create table t1(name varchar(21845)) default charset=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table t1(name varchar(21844)) default charset=utf8;
Query OK, 0 rows affected (0.07 sec) 

注意:行的長度最大為65535,只是針對除blob,text以外的其它列。

mysql> create table t1(name varchar(65528),hiredate datetime) default charset=latin1;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
mysql> create table t1(name varchar(65527),hiredate datetime) default charset=latin1;
Query OK, 0 rows affected (0.01 sec) 

確實,datetime占了5個字節(jié)。

TEXT,BLOB

mysql> create table t1(name text(255));
Query OK, 0 rows affected (0.01 sec)
mysql> create table t2(name text(256));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`name` tinytext
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`name` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec) 

通過上面的輸出可以看出text可以定義長度,如果范圍小于28(即256)則為tinytext,如果范圍小于216(即65536),則為text, 如果小于224,為mediumtext,小于232,為longtext。

上述范圍均是字節(jié)數(shù)。

如果定義的是utf8字符集,對于text,實際上只能插入21845個字符

mysql> create table t1(name text) default charset=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values(repeat('整',21846));
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t1 values(repeat('整',21845));
Query OK, 1 row affected (0.05 sec) 

DECIMAl

關(guān)于Decimal,官方的說法有點繞,

Values for DECIMAL (and NUMERIC) columns are represented using a binary format that packs nine decimal (base 10) digits into four bytes. Storage for the integer and fractional parts of each value are determined separately. Each multiple of nine digits requires four bytes, and the “l(fā)eftover” digits require some fraction of four bytes. The storage required for excess digits is given by the following table. 

還提供了一張對應(yīng)表

對于以上這段話的解讀,有以下幾點:

1. 每9位需要4個字節(jié),剩下的位數(shù)所需的空間如上所示。

2. 整數(shù)部分和小數(shù)部分是分開計算的。

譬如 Decimal(6,5),從定義可以看出,整數(shù)占1位,整數(shù)占5位,所以一共占用1+3=4個字節(jié)。

如何驗證呢?可通過InnoDB Table Monitor

如何啟動InnoDB Table Monitor,可參考:http://dev.mysql.com/doc/refman/5.7/en/innodb-enabling-monitors.html

mysql> create table t2(id decimal(6,5));
Query OK, 0 rows affected (0.01 sec)
mysql> create table t3(id decimal(9,0));
Query OK, 0 rows affected (0.01 sec)
mysql> create table t4(id decimal(8,3));
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE innodb_table_monitor (a INT) ENGINE=INNODB;
Query OK, 0 rows affected, 1 warning (0.01 sec) 

結(jié)果會輸出到錯誤日志中。

查看錯誤日志:

對于decimal(6,5),整數(shù)占1位,小數(shù)占5位,一共占用空間1+3=4個字節(jié)

對于decimal(9,0),整數(shù)部分9位,每9位需要4個字節(jié),一共占用空間4個字節(jié)

對于decimal(8,3),整數(shù)占5位,小數(shù)占3位,一共占用空間3+2=5個字節(jié)。

至此,常用的MySQL數(shù)據(jù)類型驗證完畢~

對于CHAR,VARCHAR和TEXT等字符類型,M指定的都是字符的個數(shù)。對于CHAR,最大的字符數(shù)是255。對于VARCHAR,最大的字符數(shù)與字符集有關(guān),如果字符集是latin1,則最大的字符數(shù)是65532(畢竟每一個字符只占用一個字節(jié)),對于utf8,最大的字符數(shù)是21844,因為一個字符占用三個字節(jié)。本質(zhì)上,VARCHAR更多的是受到行大小的限制(最大為65535個字節(jié))。對于TEXT,不受行大小的限制,但受到自身定義的限制。

相關(guān)文章

  • Mysql日期格式以及內(nèi)置日期函數(shù)用法詳解

    Mysql日期格式以及內(nèi)置日期函數(shù)用法詳解

    MySQL中有多種數(shù)據(jù)類型可以用于日期和時間的表示,這篇文章主要給大家介紹了關(guān)于Mysql日期格式以及內(nèi)置日期函數(shù)用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • MySQL數(shù)據(jù)庫中sql表設(shè)計的注意事項

    MySQL數(shù)據(jù)庫中sql表設(shè)計的注意事項

    實際開發(fā)中一個項目通常需要很多張表才能完成,這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫中sql表設(shè)計的注意事項,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • 一文帶你了解MySQL中的子查詢

    一文帶你了解MySQL中的子查詢

    子查詢指一個查詢語句嵌套在另一個查詢語句內(nèi)部的查詢,這個特性從MySQL 4.1開始引入,SQL中子查詢的使用大大增強(qiáng)了SELECT 查詢的能力,本文帶大家詳細(xì)了解MySQL中的子查詢,需要的朋友可以參考下
    2023-06-06
  • MySQL8的主要目錄結(jié)構(gòu)解讀

    MySQL8的主要目錄結(jié)構(gòu)解讀

    這篇文章主要介紹了MySQL8的主要目錄結(jié)構(gòu),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • MySQL ifnull()函數(shù)的具體使用

    MySQL ifnull()函數(shù)的具體使用

    本文主要介紹了MySQL ifnull()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 詳解Mysql數(shù)據(jù)庫date, datetime類型設(shè)置0000-00-00默認(rèn)值(default)報錯問題

    詳解Mysql數(shù)據(jù)庫date, datetime類型設(shè)置0000-00-00默認(rèn)值(default)報錯問題

    這篇文章主要介紹了詳解Mysql數(shù)據(jù)庫date, datetime類型設(shè)置0000-00-00默認(rèn)值(default)報錯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • MySQL圖形化管理工具Navicat安裝步驟

    MySQL圖形化管理工具Navicat安裝步驟

    大家好,本篇文章主要講的是MySQL圖形化管理工具Navicat安裝步驟,感興趣的同學(xué)趕快來看看吧,對你有幫助的話記得收藏一下哦
    2021-12-12
  • MySQL中的間隙鎖代碼示例講解

    MySQL中的間隙鎖代碼示例講解

    鎖是mysql提供的一種保證不同事務(wù)讀寫隔離的重要措施,通過鎖機(jī)制可以有效提升決多線程下并發(fā)處理事務(wù)能力,不同的鎖劃分對應(yīng)著不同的使用場景,本文來深入探討一下mysql的另一種容易被忽視的鎖,即間隙鎖,以及與之相關(guān)的相關(guān)問題,需要的朋友可以參考下
    2023-08-08
  • mysql中四種備份模式

    mysql中四種備份模式

    本文主要介紹了mysql中四種備份模式,無論使用哪種備份方式,都需要根據(jù)業(yè)務(wù)需求和數(shù)據(jù)量大小來選擇合適的備份策略,并定期驗證備份是否有效,感興趣的可以了解一下
    2023-11-11
  • mysql存儲過程中使用游標(biāo)的實例

    mysql存儲過程中使用游標(biāo)的實例

    使用MYSQL存儲過程,可以實現(xiàn)諸多的功能,下面將為您介紹一個MYSQL存儲過程中使用游標(biāo)的實例
    2014-01-01

最新評論