MySQL瀛楃闆�

MySQL瀛楃闆�

1銆佸熀鏈蹇�

瀛楃(Character)锛� 聽 聽 聽 聽鏄寚浜虹被璇█涓渶灏忕殑琛ㄤ箟绗﹀彿銆備緥濡�'A'銆�'B'绛夛紱

缂栫爜锛圗ncoding锛夛細 聽 聽缁欏畾涓�绯诲垪瀛楃锛屽姣忎釜瀛楃璧嬩簣涓�涓暟鍊硷紝鐢ㄦ暟鍊兼潵浠h〃瀵瑰簲鐨勫瓧绗︼紝杩欎竴鏁板�煎氨鏄瓧绗︾殑缂栫爜

瀛楃闆�(Character Set):缁欏畾涓�绯诲垪瀛楃骞惰祴浜堝搴旂殑缂栫爜鍚庯紝鎵�鏈夎繖浜涘瓧绗﹀拰缂栫爜瀵圭粍鎴愮殑闆嗗悎灏辨槸瀛楃闆�

瀛楃搴�(Collation) 锛� 聽 聽 鎸囧湪鍚屼竴瀛楃闆嗗唴瀛楃涔嬮棿鐨勬瘮杈冭鍒�

涓�涓瓧绗﹂泦鍙互瀵瑰簲澶氱瀛楃搴忥紱

浠ュ瓧绗﹀簭瀵瑰簲鐨勫瓧绗﹂泦鍚嶇О寮�澶达細

浠ci(琛ㄧず澶у皬鍐欎笉鏁忔劅)锛屼緥濡傦細鍦ㄥ瓧绗﹀簭``utf8_general_ci''涓嬶紝瀛楃``a''鍜宍`A''鏄瓑浠风殑锛�

_cs(琛ㄧず澶у皬鍐欐晱鎰�)鎴朹bin(琛ㄧず鎸夌紪鐮佸�兼瘮杈�)缁撳熬銆�

鏈夊摢浜涘父鐢ㄧ殑瀛楃闆�

mysql涓瓧绗﹂泦 utf8 鍜寀tf8mb4 鏈変粈涔堝尯鍒�?

杩欎釜utf8mb4 鏄痬ysql5.5鎵嶆湁鐨勫瓧绗︿簡锛屽彲浠ョ畝鍗曠殑鐞嗚В utf8mb4 鏄洰鍓嶆渶澶х殑涓�涓瓧绗︾紪鐮�,鏀寔浠绘剰鏂囧瓧.

涓轰粈涔堜細鏈塙TF8MB4锛�

MySQL鏀寔鐨剈tf8缂栫爜锛屾渶澶у瓧绗﹂暱搴︿负 3 瀛楄妭锛屽鏋滈亣鍒� 4 瀛楄妭鐨勫瓧绗﹀氨浼氬嚭鐜伴敊璇簡銆�

涓変釜瀛楄妭鐨� UTF-8 鏈�澶ц兘缂栫爜鐨� Unicode 瀛楃鏄� 0xFFFF锛屼篃灏辨槸 Unicode 涓殑鍩烘湰澶氭枃骞抽潰锛圔MP锛夈��

涔熷氨鏄锛屼换浣曚笉鍦ㄥ熀鏈鏂囧钩闈㈢殑 Unicode瀛楃锛屾渶甯歌鐨勫氨鏄疎moji 琛ㄦ儏锛圗moji 鏄竴绉嶇壒娈婄殑 Unicode 缂栫爜锛屽父瑙佷簬 ios 鍜� android 鎵嬫満涓婏級锛屽拰涓�浜涗笉甯哥敤鐨勬眽瀛楋紝浠ュ強浠讳綍鏂板鐨� Unicode 瀛楃绛夌瓑锛岄兘鏃犳硶浣跨敤MySQL鍘熸湁鐨� utf8 瀛楃闆嗗瓨鍌ㄣ��

2銆佸瓧绗﹂泦鐩稿叧鍙橀噺

character_set_client锛氬鎴风鏉ユ簮鏁版嵁浣跨敤鐨勫瓧绗﹂泦

character_set_connection锛氳繛鎺ュ眰瀛楃闆�

character_set_results锛氭煡璇㈢粨鏋滃瓧绗﹂泦

鍐呴儴瀛楃闆嗭細

character_set_server锛氶粯璁ょ殑鍐呴儴鎿嶄綔瀛楃闆�

character_set_database锛氬綋鍓嶉�変腑鏁版嵁搴撶殑榛樿瀛楃闆�

character_set_system锛氱郴缁熷厓鏁版嵁(瀛楁鍚嶇瓑)瀛楃闆�

杩樻湁浠ollation_寮�澶寸殑鍚屼笂闈㈠搴旂殑鍙橀噺锛岀敤鏉ユ弿杩板瓧绗﹀簭銆�

鍐呴儴鎿嶄綔瀛楃闆嗕紭鍏堢骇鍏崇郴锛�

鏈嶅姟鍣ㄥ瓧绗﹂泦 server < 鏁版嵁搴撳瓧绗﹂泦database < 琛ㄥ瓧绗﹂泦 < 鍒楀瓧绗﹂泦锛屽鏋滄病鏈夎缃紝榛樿浠庝笂涓�绾х户鎵裤��

3銆佽浆鎹㈣繃绋�

MySQL Server鏀跺埌璇锋眰鏃跺皢璇锋眰鏁版嵁浠巆haracter_set_client杞崲涓篶haracter_set_connection锛�

杩涜鍐呴儴鎿嶄綔鍓嶅皢璇锋眰鏁版嵁浠巆haracter_set_connection杞崲涓哄唴閮ㄦ搷浣滃瓧绗﹂泦锛�

浣跨敤姣忎釜鏁版嵁瀛楁鐨凜HARACTER SET璁惧畾鍊硷紱

鑻ヤ笂杩板�间笉瀛樺湪锛屽垯浣跨敤瀵瑰簲鏁版嵁琛ㄧ殑DEFAULT CHARACTER SET璁惧畾鍊�(MySQL鎵╁睍锛岄潪SQL鏍囧噯)锛�

鑻ヤ笂杩板�间笉瀛樺湪锛屽垯浣跨敤瀵瑰簲鏁版嵁搴撶殑DEFAULT CHARACTER SET璁惧畾鍊硷紱

鑻ヤ笂杩板�间笉瀛樺湪锛屽垯浣跨敤character_set_server璁惧畾鍊笺��


MySQL瀛楃闆嗙患鍚堣В鎯戜互鍙婃渶浣冲疄璺�

mysql> insert into t20(name) values ('浣犲ソ');

寰�t20琛ㄧ殑name 鍒椾笂鎻掑叆鏁版嵁鈥樹綘濂解��

瀹㈡埛绔�樹綘濂解�欒杩涜浜嗙紪鐮侊紝鍋囪缂栨垚浜嗏��9190,9193鈥�

Mysql鎺ュ埌SQL璇彞涔嬪悗锛屽彂鐜版湁瀛楃

1銆両nsert

2銆乀20(name) varchar鍒楁湁瀛楃锛屽氨闇�瑕佺紪鐮�

3銆佲��9190,9193鈥�

瀹㈡埛绔�氳繃character_set_client鍛婅瘔mysql鐢ㄧ殑浠�涔堝瓧绗﹂泦缂栫爜锛宑haracter_set_client涓巆haracter_set_connection缂栫爜鏂瑰紡涓嶅悓鏃讹紝浼氳繘琛岃浆鎹€��

鍘熷垯锛氬瓨鏁版嵁鐨勬椂鍊欎笉鍙戠敓杞崲鎴栬浆鎹㈠彧鍙戠敓涓�娆★紝鍙互鍙戠敓鍦�1鎴�2

===============================

insert into t1 values ('浣�'); /select * from t1;

1銆佹瀛樻鍙栵紙瀹岀編锛�

2銆佹瀛橀敊鍙栵紙灏忎簨锛�

鍙槸character_set_results鐨勫�兼病鏈夋纭樉绀哄鎴风鐨勫瓧绗﹂泦锛屾湁鍙兘浼氬嚭鐜颁贡鐮佹儏鍐碉紝浣嗘槸鏁版嵁鐨勫瓨鍌ㄦ槸姝g‘鐨�

3銆侀敊瀛樻鍙栵紙浜哄懡锛�

IMG_256

閿欏瓨姝e彇鐨勮繃绋嬪垎鏋愶細

瀹㈡埛绔殑瀛楃闆嗘槸gbk锛宑haracter_set_client=character_set_connection=character_set_results 鏄� dec8锛屽亣瀹�'浣�'鍦╣bk涓殑缂栫爜鏄�001锛屽綋鍦ㄥ鎴风鎻掑叆鏁版嵁'浣�'鏃讹紝瀹㈡埛绔細鎶�'浣�' 缂栨垚001锛�001浼氫紶鍏ユ暟鎹簱锛屽綋鏁版嵁搴撴帴鏀朵俊鎭椂浼氳闂�001鏄敤浠�涔堢紪鐨勶紝character_set_client 鍛婅瘔鏁版嵁搴撴槸鐢� dec8缂栫殑锛屾暟鎹簱浼氭妸001璇戞垚dec8瀵瑰簲鐨勫瓧绗︼紝鍋囪涓�'a'锛岀劧鍚庢妸 a 缂栨垚utf8鐨勭紪鐮侊紝鍋囧畾涓�010锛屾鏃跺瓨鍏ユ暟鎹簱涓殑鏄� 'a'锛屽彇鏁版嵁鏃惰闂槸鐢ㄤ粈涔堝彇锛宑haracter_set_results鍛婅瘔鏁版嵁搴撶敤 dec8鍙栵紝鏁版嵁搴撳皢 utf8鐨�010 --> a -->dec8 鐨�001锛岀劧鍚庡湪瀹㈡埛绔細鏄剧ずgbk涓�001瀵瑰簲鐨�'浣�'

===================

鏍″瑙勫垯(collation)锛氭槸涓�缁勮鍒欙紝璐熻矗鍐冲畾鏌愪竴瀛楃闆嗗唴鐨勫瓧绗﹁繘琛屾瘮杈冨拰鎺掑簭鐨勭粨鏋�

姣忎釜瀛楃闆嗘湁涓�涓垨澶氫釜鏍″瑙勫垯锛屽苟涓旀瘡涓牎瀵硅鍒欏彧鑳藉睘浜庝竴涓瓧绗﹂泦

姣忎釜瀛楃闆嗘湁涓�涓粯璁�(default)鏍″瑙勫垯銆� utf8鐨勯粯璁ゆ牎瀵硅鍒欐槸utf8_general_ci

鏍″瑙勫垯鐨勫懡鍚嶇害瀹氾細浠ュ瓧绗﹂泦鍚嶅紑濮嬶紝涓棿鍖呮嫭涓�涓瑷�鍚嶏紝鏈�鍚庝互鍚庣紑_ci銆乢cs鎴朾in缁撴潫

*_bin:聽 binary case sensitive collation 鍖哄垎澶у皬鍐�,鐩存帴姣旇緝瀛楃鐨勭紪鐮�

*_cs: case sensitive collation聽 聽聽 鍖哄垎澶у皬鍐�

*_ci: case insensitive collation聽 涓嶅尯鍒嗗ぇ灏忓啓

Utf8

澶у皬鍐欐槸鍚︽晱鎰熺殑杩欎箞涓�涓叧浜庢瘮杈冨拰鎺掑簭鐨勫弬鏁般��

Mysql榛樿閮芥槸澶у皬鍐欎笉鏁忔劅锛岃繖浼氬奖鍝嶆帓搴忓拰where姣旇緝锛岃嫢瑕佸尯鍒嗗ぇ灏忓啓锛岄渶瑕佸湪寤哄簱锛屽缓琛紝寤哄垪鐨勬椂鍊欑壒娈婃寚瀹氫竴涓嬨��

mysql> create table t1(id int,name varchar(20));

mysql> insert into t1 values(1,'a');聽 //鎻掑叆A銆乤銆丅銆乥鍥涗釜鍊�

鐢╳here鍙栫殑鏃跺�欎笉浼氬尯鍒嗗ぇ灏忓啓

mysql> create table t2(id int,name varchar(20)) CHARACTER SET gbk CollATE=gbk_bin;聽聽 //鎸囧畾鍖哄垎澶у皬鍐�

鐢╳here鐨勬椂鍊欏氨鍖哄垎澶у皬鍐欎簡

======

create table t1(id int,name varchar(5)) character set utf8 collate=utf8_general_ci;

IMG_256

create table t2(id int,name varchar(5)) character set utf8 collate=utf8_bin;

IMG_256

======

鏌ョ湅鏁版嵁搴撴敮鎸佺殑鎵�鏈夋牎瀵硅鍒�

show collation;

鏌ョ湅褰撳墠瀛楃闆嗗拰鏍″瑙勫垯璁剧疆

show variables like 'collation_%';

IMG_256

鏉ユ簮锛� http://www.111cn.net/database/mysql/114544.htm

你可能感兴趣的:(MySQL瀛楃闆�)