MYSQL的binary解決mysql數(shù)據(jù)大小寫敏感問題的方法

<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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是數(shù)據(jù)庫? 數(shù)據(jù)庫是存儲數(shù)據(jù)的集合的單獨(dú)的應(yīng)用程序。每個數(shù)據(jù)庫具有一個或多個不同的API,用于創(chuàng)建,訪問,管理...
    chen_000閱讀 4,146評論 0 19
  • MySQL5.6從零開始學(xué) 第一章 初始mysql 1.1數(shù)據(jù)庫基礎(chǔ) 數(shù)據(jù)庫是由一批數(shù)據(jù)構(gòu)成的有序的集合,這些數(shù)據(jù)...
    星期四晚八點(diǎn)閱讀 1,237評論 0 4
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,816評論 18 399
  • 前面講了SQL優(yōu)化以及索引的使用、設(shè)計(jì)優(yōu)化了,那么接下來就到表的設(shè)計(jì)與優(yōu)化啦?。?!真實(shí)地去設(shè)計(jì)優(yōu)化單表結(jié)構(gòu)以及講述...
    JackFrost_fuzhu閱讀 4,331評論 2 28
  • 每當(dāng)我感覺到你 就聽到有花開放的聲音 …… 每當(dāng)我感覺到你 我會深信這一切 ...
    冰冰心雨閱讀 208評論 4 7

友情鏈接更多精彩內(nèi)容