<font size="4" style="background-color: inherit;">mysql> select binary 'ABCD'='abcd' COM1, 'ABCD'='abcd' COM2;
+--------+-----------+
| COM1 | COM2 |
+--------+-----------+
| 0 | 1 |
+---------+-----------+
1 row in set (0.00 sec)</font>
<font size="4" style="background-color: inherit;">(僅僅有些而已!4.以前)
因?yàn)橛械腗ySQL特別是4.以前的對于中文檢索會有不準(zhǔn)確的問題摔癣,可以在檢索的時候加上binary奴饮。
建表:</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">create TABLE usertest (
id int(9) unsigned NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
primary key (id)
)</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">插入數(shù)據(jù):</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">insert into usertest (username) VALUES('美文');
insert into usertest (username) VALUES('美國項(xiàng)目');
insert into usertest (username) VALUES('李文');
insert into usertest (username) VALUES('老唐');
insert into usertest (username) VALUES('夢漂');
insert into usertest (username) VALUES('龍武');
insert into usertest (username) VALUES('夏');</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">例如:select * from usertest where username like '%夏%' ,結(jié)果七條記錄都出來了择浊,比較郁悶戴卜。
如果使用=而不是like的時候,select * from usertest where username = '夏' 琢岩,只出現(xiàn)一個結(jié)果投剥。因?yàn)閙ysql 的LIKE操作是按照ASCII 操作的,所以LIKE的時候是可能有問題的担孔。問題繼續(xù):如果再加上:</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:insert into usertest (username) VALUES('文');
insert into usertest (username) VALUES('唐');
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">還是使用select * from usertest where username = '夏' 江锨,結(jié)果還是出現(xiàn)3條記錄,又郁悶了攒磨。解決辦法如下:
1.在create的時候就使用binary泳桦,而不是在query的時候加。</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">username varchar(30) BINARY NOT NULL default '', 如果表已經(jīng)建好了娩缰,使用:
alter table usertest modify username varchar(32) binary; 來就該表的屬性。</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">2.在query的時候加上binary谒府,select * from usertest where username like binary '%夏%' 拼坎,就可以準(zhǔn)確的查詢出一條記錄來浮毯。
char使用固定長度的空間進(jìn)行存儲,char(4)存儲4個字符泰鸡,根據(jù)編碼方式的不同占用不同的字節(jié)债蓝,gbk編碼方式,不論是中文還是英文盛龄,每個字符占用2個字節(jié)的空間饰迹,utf8編碼方式,每個字符占用3個字節(jié)的空間余舶。
如果需要存儲的字符串的長度跟所有值的平均長度相差不大啊鸭,適合用char,如MD5匿值。
對于經(jīng)常改變的值赠制,char優(yōu)于varchar,原因是固定長度的行不容易產(chǎn)生碎片挟憔。
對于很短的列钟些,char優(yōu)于varchar,原因是varchar需要額外一個或兩個字節(jié)存儲字符串的長度绊谭。
varchar保存可變長度的字符串政恍,使用額外的一個或兩個字節(jié)存儲字符串長度,varchar(10),除了需要存儲10個字符达传,還需要1個字節(jié)存儲長度信息(10),超過255的長度需要2個字節(jié)來存儲
例外:Myisam引擎中使用ROW_FORMAT=FIXED時抚垃,每行使用相同的空間,造成浪費(fèi)
char和varchar后面如果有空格趟大,char會自動去掉空格后存儲鹤树,varchar雖然不會去掉空格,但在進(jìn)行字符串比較時逊朽,會去掉空格進(jìn)行比較</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(4) | YES | | NULL | |
| addr | char(8) | YES | | NULL | |
| bn | varbinary(4) | YES | | NULL | |
| b | binary(8) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
+----------------------+----------------------+
| concat("$",name,"$") | concat("$",addr,"$") |
+----------------------+----------------------+
| $asdf$ | $a$ |
| $asdf$ | $a$ |
| $a $ | $a$ |
| $a$ | $a$ |
| $t a$ | $a$ |
+----------------------+----------------------+
mysql> select * from zcy where name='a '; //由于name是varchar罕伯,比較時,'a '自動轉(zhuǎn)換為'a'
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 3 | a | a | ab | ab |
| 4 | a | a | ab | a |
+----+------+------+------+----------+
2 rows in set (0.00 sec)
mysql> select * from zcy where name='a';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 3 | a | a | ab | ab |
| 4 | a | a | ab | a |
+----+------+------+------+----------+
2 rows in set (0.00 sec)
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(4) | YES | | NULL | |
| addr | char(8) | YES | | NULL | |
| bn | varbinary(4) | YES | | NULL | |
| b | binary(8) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
+--------------------+-------------------+
| concat("$",bn,"$") | concat("$",b,"$") |
+--------------------+-------------------+
| $ab a$ | NULL |
| $ab $ | $ab $ |
| $ab$ | $ab $ |
| $ab $ | $a $ |
| NULL | $a $ |
| NULL | $abcde $ |
| NULL | $abcd1234$ |
+--------------------+-------------------+</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">binary保存二進(jìn)制字符串叽讳,它保存的是字節(jié)而不是字符追他,沒有字符集限制
binary(8)可以保存8個字符,每個字符占1個字節(jié)岛蚤,共占8個字節(jié)
進(jìn)行比較時是按字節(jié)進(jìn)行比較邑狸,而不是按字符(char),按字節(jié)比較比字符簡單快速
按字符比較不區(qū)分大小寫涤妒,而binary區(qū)分大小寫单雾,結(jié)尾使用\0填充,而不是空格</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">mysql> select * from zcy where b='a\0\0\0\0\0\0\0';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 5 | t a | a | NULL | a |
+----+------+------+------+----------+
mysql> select * from zcy where b='a \0\0\0\0\0\0';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 4 | a | a | ab | a |
+----+------+------+------+----------+
varbinary保存變長的字符串,后面不會補(bǔ)\0
mysql> select * from zcy where bn='ab';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 3 | a | a | ab | ab |
+----+------+------+------+----------+
1 row in set (0.01 sec)
mysql> select * from zcy where bn='ab ';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 2 | asdf | a | ab | ab |
+----+------+------+------+----------+
1 row in set (0.00 sec)
mysql> select * from zcy where bn='ab ';
+----+------+------+------+----------+
| id | name | addr | bn | b |
+----+------+------+------+----------+
| 4 | a | a | ab | a |
+----+------+------+------+----------+
1 row in set (0.00 sec)</font>
<font size="4" style="background-color: inherit;">MySql中Blob與Text的區(qū)別
BLOB是一個二進(jìn)制大對象硅堆,可以容納可變數(shù)量的數(shù)據(jù)屿储。有4種BLOB類型:TINYBLOB、BLOB渐逃、MEDIUMBLOB和LONGBLOB够掠。它們只是可容納值的最大長度不同。
有4種TEXT類型:TINYTEXT茄菊、TEXT疯潭、MEDIUMTEXT和LONGTEXT。這些對應(yīng)4種BLOB類型面殖,有相同的最大長度和存儲需求竖哩。
BLOB 列被視為二進(jìn)制字符串(字節(jié)字符串)。TEXT列被視為非二進(jìn)制字符串(字符字符串)畜普。BLOB列沒有字符集期丰,并且排序和比較基于列值字節(jié)的數(shù)值值。TEXT列有一個字符集吃挑,并且根據(jù)字符集的 校對規(guī)則對值進(jìn)行排序和比較钝荡。
在TEXT或BLOB列的存儲或檢索過程中,不存在大小寫轉(zhuǎn)換舶衬。
當(dāng)未運(yùn)行在嚴(yán)格模式時埠通,如果你為BLOB或TEXT列分配一個超過該列類型的最大長度的值值,值被截取以保證適合逛犹。如果截掉的字符不是空格端辱,將會產(chǎn)生一條警告。使用嚴(yán)格SQL模式虽画,會產(chǎn)生錯誤舞蔽,并且值將被拒絕而不是截取并給出警告。
在大多數(shù)方面码撰,可以將BLOB列視為能夠足夠大的VARBINARY列渗柿。同樣,可以將TEXT列視為VARCHAR列脖岛。BLOB和TEXT在以下幾個方面不同于VARBINARY和VARCHAR:
·當(dāng)保存或檢索BLOB和TEXT列的值時不刪除尾部空格朵栖。(這與VARBINARY和VARCHAR列相同)。
請注意比較時將用空格對TEXT進(jìn)行擴(kuò)充以適合比較的對象柴梆,正如CHAR和VARCHAR陨溅。
·對于BLOB和TEXT列的索引,必須指定索引前綴的長度绍在。對于CHAR和VARCHAR门扇,前綴長度是可選的雹有。
·BLOB和TEXT列不能有 默認(rèn)值。
LONG和LONG VARCHAR對應(yīng)MEDIUMTEXT數(shù)據(jù)類型悯嗓。這是為了保證兼容性件舵。如果TEXT列類型使用BINARY屬性卸察,將為列分配列字符集的二元 校對規(guī)則脯厨。
MySQL連接程序/ODBC將BLOB值定義為LONGVARBINARY,將TEXT值定義為LONGVARCHAR坑质。
由于BLOB和TEXT值可能會非常長合武,使用它們時可能遇到一些約束:
·當(dāng)排序時只使用該列的前max_sort_length個字節(jié)。max_sort_length的 默認(rèn)值是1024涡扼;該值可以在啟動mysqld服務(wù)器時使用--max_sort_length選項(xiàng)進(jìn)行更改稼跳。
運(yùn)行時增加max_sort_length的值可以在排序或組合時使更多的字節(jié)有意義。任何客戶端可以更改其會話max_sort_length變量的值:</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">mysql> SET max_sort_length = 2000;
mysql> SELECT id, comment FROM tbl_name
-> ORDER BY comment;</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">當(dāng)你想要使超過max_sort_length的字節(jié)有意義吃沪,對含長值的BLOB或TEXT列使用GROUP BY或ORDER BY的另一種方式是將列值轉(zhuǎn)換為固定長度的對象汤善。標(biāo)準(zhǔn)方法是使用SUBSTRING函數(shù)。例如票彪,下面的語句對comment列的2000個字節(jié)進(jìn)行排序:</font>
<font size="4" style="background-color: inherit;"><a style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);"><u style="background-color: inherit; font-style: normal; font-weight: normal; color: rgb(51, 51, 51);">復(fù)制代碼</u></a>
代碼如下:</font>
<font size="4" style="background-color: inherit;">mysql> SELECT id, SUBSTRING(comment,1,2000) FROM tbl_name
-> ORDER BY SUBSTRING(comment,1,2000);</font>
<font size="4" style="background-color: inherit; color: rgb(0, 0, 0); font-family: "Microsoft YaHei", STXihei; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">·BLOB或TEXT對象的最大大小由其類型確定红淡,但在客戶端和服務(wù)器之間實(shí)際可以傳遞的最大值由可用內(nèi)存數(shù)量和通信緩存區(qū)大小確定。你可以通過更改max_allowed_packet變量的值更改消息緩存區(qū)的大小降铸,但必須同時修改服務(wù)器和客戶端程序在旱。例如,可以使用mysql和mysqldump來更改客戶端的max_allowed_packet值推掸。
每個BLOB或TEXT值分別由內(nèi)部分配的對象表示桶蝎。這與其它列類型形成對比,后者是當(dāng)打開表時為每1列分配存儲引擎谅畅。</font>