最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

CHAR

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

驗(yàn)證如下:

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同樣是字符,長(zhǎng)度是M*字符編碼長(zhǎng)度。它的限制比較特別,行的總長(zhǎng)度不能超過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,字符長(zhǎng)度是1個(gè)字節(jié),所以對(duì)于varchar,最大只能指定65532字節(jié)的長(zhǎng)度。

如果是指定utf8,則最多只能指定21844的長(zhǎng)度

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) 

注意:行的長(zhǎng)度最大為65535,只是針對(duì)除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) 

確實(shí),datetime占了5個(gè)字節(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可以定義長(zhǎng)度,如果范圍小于28(即256)則為tinytext,如果范圍小于216(即65536),則為text, 如果小于224,為mediumtext,小于232,為longtext。

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

如果定義的是utf8字符集,對(duì)于text,實(shí)際上只能插入21845個(gè)字符

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,官方的說法有點(diǎn)繞,

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. 

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

對(duì)于以上這段話的解讀,有以下幾點(diǎn):

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

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

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

如何驗(yàn)證呢?可通過InnoDB Table Monitor

如何啟動(dòng)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é)果會(huì)輸出到錯(cuò)誤日志中。

查看錯(cuò)誤日志:

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

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

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

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

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

相關(guān)文章

  • Mysql使用索引實(shí)現(xiàn)查詢優(yōu)化

    Mysql使用索引實(shí)現(xiàn)查詢優(yōu)化

    索引的目的在于提高查詢效率,本文給大家介紹Mysql使用索引實(shí)現(xiàn)查詢優(yōu)化技巧,涉及到索引的優(yōu)點(diǎn)等方面的知識(shí)點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-07-07
  • mysql datetime查詢異常問題解決

    mysql datetime查詢異常問題解決

    這篇文章主要介紹了mysql datetime查詢異常問題解決的相關(guān)資料,這里對(duì)異常進(jìn)行了詳細(xì)的介紹和該如何解決,需要的朋友可以參考下
    2016-11-11
  • Mysql主從GTID與binlog如何使用

    Mysql主從GTID與binlog如何使用

    MySQL的GTID和binlog是實(shí)現(xiàn)高效數(shù)據(jù)復(fù)制和恢復(fù)的重要機(jī)制,GTID保證事務(wù)的唯一標(biāo)識(shí),避免復(fù)制沖突;binlog記錄數(shù)據(jù)變更,用于主從復(fù)制和數(shù)據(jù)恢復(fù),兩者配合,提高M(jìn)ySQL復(fù)制的準(zhǔn)確性和管理便捷性
    2024-10-10
  • Mysql桌面工具之SQLyog資源及激活使用方法告別黑白命令行

    Mysql桌面工具之SQLyog資源及激活使用方法告別黑白命令行

    這篇文章主要介紹了Mysql桌面工具之SQLyog資源及激活使用方法告別黑白命令行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Linux下安裝MySQL5.7.19問題小結(jié)

    Linux下安裝MySQL5.7.19問題小結(jié)

    第一次在自己虛機(jī)上安裝mysql 中間碰到很多問題 在這里記下來,特此分享到腳本之家平臺(tái)供大家參考
    2017-08-08
  • MySQL使用中遇到的問題記錄

    MySQL使用中遇到的問題記錄

    本文給大家匯總介紹了作者在mysql的使用過程中遇到的問題以及最終的解決方案,非常的實(shí)用,有需要的小伙伴可以參考下
    2017-11-11
  • mysql不包含模糊查詢問題

    mysql不包含模糊查詢問題

    這篇文章主要介紹了mysql不包含模糊查詢問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • mysql用戶權(quán)限管理實(shí)例分析

    mysql用戶權(quán)限管理實(shí)例分析

    這篇文章主要介紹了mysql用戶權(quán)限管理,結(jié)合實(shí)例形式分析了mysql用戶權(quán)限管理概念、原理及用戶權(quán)限的查看、修改、刪除等操作技巧,需要的朋友可以參考下
    2020-04-04
  • 一文詳解MySQL是如何解決幻讀的

    一文詳解MySQL是如何解決幻讀的

    事務(wù)A按照一定條件進(jìn)行數(shù)據(jù)讀取,期間事務(wù)B插入了相同搜索條件的新數(shù)據(jù),事務(wù)A再次按照原先條件進(jìn)行讀取操作修改時(shí),發(fā)現(xiàn)了事務(wù)B新插入的數(shù)據(jù)稱之為幻讀,這篇文章主要給大家介紹了關(guān)于MySQL是如何解決幻讀的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • MySQL修改字符集的實(shí)戰(zhàn)教程

    MySQL修改字符集的實(shí)戰(zhàn)教程

    這篇文章主要介紹了MySQL修改字符集的方法,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下
    2021-01-01

最新評(píng)論

钟祥市| 云林县| 寿光市| 万盛区| 岚皋县| 吉木乃县| 澄城县| 沧州市| 漾濞| 海南省| 德令哈市| 横峰县| 龙里县| 彩票| 额济纳旗| 金乡县| 同江市| 新安县| 东方市| 藁城市| 忻州市| 静海县| 尖扎县| 堆龙德庆县| 吉木乃县| 阜城县| 阿拉善左旗| 邵武市| 诸城市| 黄浦区| 平陆县| 黄骅市| 铜川市| 浙江省| 景谷| 蒙自县| 新乡市| 湘阴县| 如皋市| 儋州市| 沁源县|