站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽9.聽Language Structure - MySQL 5.1参考手册中文版

绗9绔狅細璇█缁撴瀯

鏈珷璁ㄨ浜嗕娇鐢MySQL缂栧啓SQL璇彞鐨勪笅闈㈠厓绱犳椂鎵浣跨敤鐨勮鍒欙細

         瀛楃涓插拰鏁板瓧绛夋枃瀛楀

         璇嗗埆绗︼紝渚嬪琛ㄥ拰鍒楀悕

         鐢ㄦ埛鍜岀郴缁熷彉閲

         娉ㄩ噴

         淇濈暀瀛

9.1. 鏂囧瓧鍊

璇ヨ妭鎻忚堪浜嗗浣曞湪MySQL涓啓鏂囧瓧鍊笺傚寘鎷瓧绗︿覆銆佹暟鍊笺佸崄鍏繘鍒跺笺佸竷灏斿煎拰NULL銆傛湰鑺傝繕鍖呮嫭鍦MySQL涓鐞嗚繖浜涘熀鏈被鍨嬫椂浼氶亣鍒扮殑鍚勭缁嗗井宸埆鍜屸滃奖鍗扮増鈥濄

9.1.1. 瀛楃涓

瀛楃涓叉寚鐢ㄥ崟寮曞彿(')鎴栧弻寮曞彿(")寮曡捣鏉ョ殑瀛楃搴忓垪銆備緥濡傦細

'a string'
"another string"

濡傛灉SQL鏈嶅姟鍣ㄦā寮忓惎鐢ㄤ簡NSI_QUOTES锛屽彲浠ュ彧鐢ㄥ崟寮曞彿寮曠敤瀛楃涓层傜敤鍙屽紩鍙峰紩鐢ㄧ殑瀛楃涓茶瑙i噴涓轰竴涓瘑鍒銆

瀛楃涓插彲浠ユ湁涓涓彲閫夊瓧绗﹂泦寮曚粙璇嶅拰COLLATE瀛愬彞锛

[_charset_name]'string' [COLLATE collation_name]

渚嬪锛

SELECT _latin1'string';
SELECT _latin1'string' COLLATE latin1_danish_ci;

鍏充簬杩欎簺瀛楃涓茶娉曞舰寮忕殑璇︾粏淇℃伅锛屽弬瑙10.3.7鑺傦紝鈥滃瓧绗︿覆鏂囧瓧瀛楃闆嗗拰鏍″鈥

鍦ㄥ瓧绗︿覆涓紝鏌愪簺搴忓垪鍏锋湁鐗规畩鍚箟銆傝繖浜涘簭鍒楀潎鐢ㄥ弽鏂滅嚎(\)寮濮嬶紝鍗虫墍璋撶殑杞箟瀛楃MySQL璇嗗埆涓嬮潰鐨勮浆涔夊簭鍒楋細

\0

ASCII 0(NUL)瀛楃銆

\'

鍗曞紩鍙(')

\"

鍙屽紩鍙(")

\b

閫鏍肩銆

\n

鎹㈣绗︺

\r

鍥炶溅绗︺

\t

tab瀛楃銆

\Z

ASCII 26(鎺у埗锛Ctrl-Z)銆傝瀛楃鍙互缂栫爜涓衡\Z鈥欙紝浠ュ厑璁镐綘瑙e喅鍦WindowsASCII 26浠h〃鏂囦欢缁撳熬杩欎竴闂銆(濡傛灉浣犺瘯鍥句娇鐢mysql db_name < file_nameASCII 26浼氬甫鏉ラ棶棰橈級

\\

鍙嶆枩绾(\)瀛楃銆

\%

%鈥欏瓧绗︺傚弬瑙佽〃鍚庨潰鐨勬敞瑙c

\_

_鈥欏瓧绗︺傚弬瑙佽〃鍚庨潰鐨勬敞瑙c

杩欎簺搴忓垪瀵瑰ぇ灏忓啓鏁忔劅銆備緥濡傦紝鈥\b鈥欒В閲婁负閫鏍硷紝浣嗏\B鈥欒В閲婁负鈥B鈥欍

\%鈥欏拰鈥\_鈥欏簭鍒楃敤浜庢悳绱㈠彲鑳戒細瑙i噴涓洪氶厤绗︾殑妯″紡鍖归厤鐜涓殑鈥%鈥欏拰鈥_鈥欐枃瀛楀疄渚嬨傚弬瑙12.3.1鑺傦紝鈥滃瓧绗︿覆姣旇緝鍑芥暟鈥銆傝娉ㄦ剰濡傛灉浣犲湪鍏跺畠鐜涓娇鐢ㄢ\%鈥欐垨鈥\_鈥欙紝瀹冧滑杩斿洖瀛楃涓测\%鈥欏拰鈥\_鈥欙紝鑰屼笉鏄%鈥欏拰鈥_鈥欍

鍦ㄥ叾瀹冭浆涔夊簭鍒椾腑锛屽弽鏂滅嚎琚拷鐣ャ備篃灏辨槸璇达紝杞箟瀛楃瑙i噴涓轰豢浣涙病鏈夎浆涔夈

鏈夊嚑绉嶆柟寮忓彲浠ュ湪瀛楃涓蹭腑鍖呮嫭寮曞彿锛

         鍦ㄥ瓧绗︿覆鍐呯敤鈥'鈥欏紩鐢ㄧ殑鈥'鈥欏彲浠ュ啓鎴愨''鈥欍

         鍦ㄥ瓧绗︿覆鍐呯敤鈥"鈥欏紩鐢ㄧ殑鈥"鈥欏彲浠ュ啓鎴愨""鈥欍

         鍙互鍦ㄥ紩鍙峰墠鍔犺浆涔夊瓧绗(\)

         鍦ㄥ瓧绗︿覆鍐呯敤鈥"鈥欏紩鐢ㄧ殑鈥'鈥欎笉闇瑕佺壒娈婂鐞嗭紝涓嶉渶瑕佺敤鍙屽瓧绗︽垨杞箟銆傚悓鏍凤紝鍦ㄥ瓧绗︿覆鍐呯敤鈥'鈥欏紩鐢ㄧ殑鈥"鈥欎篃涓嶉渶瑕佺壒娈婂鐞嗐

涓嬮潰鐨SELECT璇彞鏄剧ず浜嗗紩鐢ㄥ拰杞箟濡備綍宸ヤ綔锛

mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+
 
mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";
+-------+---------+-----------+--------+--------+
| hello | 'hello' | ''hello'' | hel"lo | "hello |
+-------+---------+-----------+--------+--------+
 
mysql> SELECT 'This\nIs\nFour\nLines';
+--------------------+
| This
Is
Four
Lines |
+--------------------+
 
mysql> SELECT 'disappearing\ backslash';
+------------------------+
| disappearing backslash |
+------------------------+

濡傛灉浣犳兂瑕佸湪瀛楃涓插垪鍐呮彃鍏ヤ簩杩涘埗鏁版嵁(渚嬪BLOB)锛屽繀椤婚氳繃杞箟搴忓垪琛ㄧず涓嬮潰鐨勫瓧绗︼細

NUL

NUL瀛楄妭(ASCII 0)銆傜敤鈥\0琛ㄧず璇ュ瓧绗(鍙嶆枩绾垮悗闈㈣窡涓涓ASCII0鈥欏瓧绗)

\

鍙嶆枩绾(ASCII 92)銆傜敤鈥\\鈥欒〃绀鸿瀛楃銆

'

鍗曞紩鍙(ASCII 39)銆傜敤鈥\'鈥欒〃绀鸿瀛楃銆

"

鍙屽紩鍙(ASCII 34)銆傜敤鈥\"鈥欒〃绀鸿瀛楃銆

褰撶紪鍐欏簲鐢ㄧ▼搴忔椂锛屽湪鍖呭惈杩欎簺鐗规畩瀛楃鐨勫瓧绗︿覆鐢ㄤ簬鍙戦佸埌MySQL鏈嶅姟鍣ㄧ殑SQL璇彞涓殑鏁版嵁鍊间箣鍓嶏紝蹇呴』瀵瑰畠浠纭繘琛岃浆涔夈傚彲浠ョ敤涓ょ鏂规硶鏉ュ畬鎴愶細

         鐢ㄨ浆涔夌壒娈婂瓧绗︾殑鍑芥暟澶勭悊瀛楃涓层備緥濡傦紝鍦C绋嬪簭涓紝鍙互浣跨敤mysql_real_escape_string() C API鍑芥暟鏉ヨ浆涔夊瓧绗︺傚弬瑙25.2.3.52鑺傦紝鈥渕ysql_real_escape_string()鈥Perl DBI鎺ュ彛鎻愪緵涓涓quote鏂规硶鏉ュ皢鐗规畩瀛楃杞崲涓烘纭殑杞箟搴忓垪銆傚弬瑙25.4鑺傦紝鈥淢ySQL Perl API鈥

         鏄惧紡杞箟鐗规畩瀛楃锛岃澶MySQL API鎻愪緵浜嗗崰浣嶇鍔熻兘锛屽厑璁镐綘鍦ㄦ煡璇㈠瓧绗︿覆涓彃鍏ョ壒娈婃爣璁帮紝鐒跺悗褰撲綘鍙戝嚭鏌ヨ鏃跺皢鏁版嵁鍊煎悓瀹冧滑缁戝畾璧锋潵銆傚湪杩欑鎯呭喌涓嬶紝API鍏虫敞杞箟鍊间腑鐨勭壒娈婂瓧绗︺

9.1.2. 鏁板

鏁存暟鐢ㄤ竴绯诲垪闃挎媺浼暟瀛楄〃绀恒傛诞鐐规暟浣跨敤鈥.鈥欎綔涓哄崄杩涘埗闂撮殧绗︺備袱绉嶇被鍨嬬殑鏁板煎潎鍙互鍦ㄥ墠闈㈠姞涓涓-鈥欐潵琛ㄧず璐熷笺

鍚堟硶鏁存暟鐨勪緥瀛愶細

1221
0
-32

鍚堟硶娴偣鏁扮殑渚嬪瓙锛

294.42
-32032.6809e+10
148.00

鏁存暟鍙互鐢ㄥ湪娴偣鐜涓紱瀹冭瑙i噴涓轰笌娴偣鏁扮瓑鏁堛

9.1.3. 鍗佸叚杩涘埗鍊

MySQL鏀寔鍗佸叚杩涘埗鍊笺傚湪鏁板瓧涓婁笅鏂囦腑锛屽崄鍏繘鍒舵暟濡傚悓鏁存暟(64浣嶇簿搴)銆傚湪瀛楃涓蹭笂涓嬫枃锛屽鍚屼簩杩涘埗瀛楃涓诧紝姣忓鍗佸叚杩涘埗鏁板瓧琚浆鎹负涓涓瓧绗︼細

mysql> SELECT x'4D7953514C'
        -> 'MySQL'
mysql> SELECT 0xa+0
        -> 10
mysql> SELECT 0x5061756c
        -> 'Paul'

鍗佸叚杩涘埗鍊肩殑榛樿绫诲瀷鏄瓧绗︿覆銆傚鏋滄兂瑕佺‘淇濊鍊间綔涓烘暟瀛楀鐞嗭紝鍙互浣跨敤CAST(...AS UNSIGNED)

mysql> SELECT 0x41CAST(0x41 AS UNSIGNED)
        -> 'A'65

0x璇硶鍩轰簬ODBC銆傚崄鍏繘鍒跺瓧绗︿覆閫氬父鐢ㄤ簬ODBC浠ヤ究涓BLOB鍒楁彁渚涘笺xhexstring璇硶鍩轰簬鏍囧噯SQL

鍙互鐢HEX()鍑芥暟灏嗕竴涓瓧绗︿覆鎴栨暟瀛楄浆鎹负鍗佸叚杩涘埗鏍煎紡鐨勫瓧绗︿覆锛

mysql> SELECT HEX('cat')
        -> '636174'
mysql> SELECT 0x636174
        -> 'cat'

9.1.4. 甯冨皵鍊

甯搁噺TRUE绛変簬1锛屽父閲FALSE绛変簬0銆傚父閲忓悕鍙互鍐欐垚澶у啓鎴栧皬鍐欍

mysql> SELECT TRUEtrueFALSEfalse
        -> 1100

9.1.5. 浣嶅瓧娈靛

鍙互浣跨敤b'value'绗﹀彿鍐欎綅瀛楁鍊笺value鏄竴涓敤01鍐欐垚鐨勪簩杩涘埗鍊笺

浣嶅瓧娈电鍙峰彲浠ユ柟渚挎寚瀹氬垎閰嶇粰BIT鍒楃殑鍊硷細

mysql> CREATE TABLE t (b BIT(8));
mysql> INSERT INTO t SET b = b'11111111';
mysql> INSERT INTO t SET b = b'1010';
+------+----------+----------+----------+
| b+0  | BIN(b+0) | OCT(b+0) | HEX(b+0) |
+------+----------+----------+----------+
|  255 | 11111111 | 377      | FF       |
|   10 | 1010     | 12       | A        |
+------+----------+----------+----------+

9.1.6.聽NULL鍊

NULL鍊艰〃绀衡滄病鏈鏁版嵁鈥濄NULL鍙互鍐欐垚澶у啓鎴栧皬鍐欍

璇锋敞鎰NULL鍊间笉鍚屼簬鏁板瓧绫诲瀷鐨0鎴栧瓧绗︿覆绫诲瀷鐨勭┖瀛楃涓层傚弬瑙A.5.3鑺傦紝鈥滀笌NULL鍊兼湁鍏崇殑闂

瀵逛簬鐢LOAD DATA INFILESELECT ...INTO OUTFILE鎵ц鐨勬枃鏈枃浠跺鍏ユ垨瀵煎嚭鎿嶄綔锛NULL鐢ㄥ簭鍒\N琛ㄧず銆傚弬瑙13.2.5鑺傦紝鈥淟OAD DATA INFILE璇硶鈥

9.2. 鏁版嵁搴撱佽〃銆佺储寮曘佸垪鍜屽埆鍚

鏁版嵁搴撱佽〃銆佺储寮曘佸垪鍜屽埆鍚嶆槸璇嗗埆绗︺傝鑺傛弿杩颁簡鍦MySQL涓瘑鍒鐨勫厑璁哥殑璇硶銆

涓嬮潰鐨勮〃鎻忚堪浜嗘瘡绫昏瘑鍒鐨勬渶澶ч暱搴﹀拰鍏佽鐨勫瓧绗︺

璇嗗埆绗

鏈澶ч暱搴(瀛楄妭)

鍏佽鐨勫瓧绗

鏁版嵁搴

64

鐩綍鍚嶅厑璁哥殑浠讳綍瀛楃锛屼笉鍖呮嫭鈥/鈥欍佲\鈥欐垨鑰呪

64

鏂囦欢鍚嶅厑璁哥殑浠讳綍瀛楃锛屼笉鍖呮嫭鈥/鈥欍佲\鈥欐垨鑰呪

64

鎵鏈夊瓧绗

绱㈠紩

64

鎵鏈夊瓧绗

鍒悕

255

鎵鏈夊瓧绗

闄や簡琛ㄥ唴娉ㄦ槑鐨勯檺鍒讹紝璇嗗埆绗︿笉鍙互鍖呭惈ASCII 0鎴栧间负255鐨勫瓧鑺傘傛暟鎹簱銆佽〃鍜屽垪鍚嶄笉搴斾互绌烘牸缁撳熬銆傚湪璇嗗埆绗︿腑鍙互浣跨敤寮曞彿璇嗗埆绗︼紝灏界搴斿敖鍙兘閬垮厤杩欐牱浣跨敤銆

璇嗗埆绗︾敤Unicode(UTF8)淇濆瓨銆傚湪.frm鏂囦欢涓繚瀛樼殑琛ㄥ畾涔夌殑璇嗗埆绗﹀拰鍦mysql鏁版嵁搴撲腑鐨勬巿鏉冭〃淇濆瓨鐨勮瘑鍒涔熺敤Unicode(UTF8)淇濆瓨銆傚湪MySQL 5.1涓巿鏉冭〃(鍜屽叾瀹冭〃)鐨勫瓧绗︿覆鍒楃殑澶у皬绛変簬瀛楃涓暟锛涜繖璇存槑(涓嶈薄浠ュ墠鐗堟湰鐨MySQL)浣犲彲浠ュ湪杩欎簺鍒椾繚瀛樼殑鍊间腑浣跨敤澶氬瓧鑺傚瓧绗﹁屼笉闇瑕侀檷浣庡瓧绗︿釜鏁般

璇嗗埆绗﹀彲浠ュ紩璧锋潵涔熷彲浠ヤ笉寮曡捣鏉ャ傚鏋滆瘑鍒鏄竴涓繚鐣欏瓧鎴栧寘鍚壒娈婂瓧绗︼紝鏃犺浣曟椂浣跨敤锛蹇呴』灏嗗畠寮曡捣鏉ャ傚叧浜庝繚鐣欏瓧鐨勫垪琛ㄥ弬瑙9.6鑺傦紝鈥淢ySQL涓繚鐣欏瓧鐨勫鐞嗏銆傜壒娈婂瓧绗︽寚閭d簺褰撳墠瀛楃闆嗐佲_鈥欏拰鈥$鈥欎箣澶栫殑鏂囧瓧鏁板瓧瀛楃闆嗐

璇嗗埆绗︾殑寮曠敤绗︽槸鍙嶅嬀鍙(`)

mysql> SELECT * FROM `select` WHERE `select`.id > 100;

濡傛灉SQL鏈嶅姟鍣ㄦā寮忓寘鎷ANSI_QUOTES妯″紡閫夐」锛岃繕鍙互鐢ㄥ弻寮曞彿灏嗚瘑鍒寮曡捣鏉ワ細

mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax. (...)
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)

鍙傝5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊

濡傛灉浣犲紩鐢ㄨ瘑鍒锛屽彲浠ュ湪璇嗗埆绗﹀唴鍖呮嫭璇嗗埆绗﹀紩鐢ㄧ銆傚鏋滆瘑鍒鍐呭寘鎷殑瀛楃涓庡紩鐢ㄨ瘑鍒鐨勫瓧绗︾浉鍚岋紝鍒欓渶瑕佺敤鍙屽瓧绗︺備笅闈㈢殑璇彞鍒涘缓涓涓悕涓a`b鍖呭惈鍒c"d鐨勮〃锛

mysql> CREATE TABLE `a``b` (`c"d` INT)

寤鸿涓嶈浣跨敤XeX妯″紡鐨勫悕锛屼緥濡1e2e2锛屽洜涓虹被浼1e+1鐨勮〃杈惧紡姣旇緝妯$硦銆傛牴鎹笂涓嬫枃锛屽畠鍙互瑙i噴涓鸿〃杈惧紡1e + 1鎴栨暟瀛1e+1

浣跨敤MD5浜х敓琛ㄥ悕鏃跺簲浠旂粏锛屽洜涓哄畠鍙兘浜х敓涓嶅悎娉曠殑琛ㄥ悕锛屽涓婃墍杩般

9.2.1. 璇嗗埆绗﹂檺鍒舵潯浠

MySQL鍏佽浣跨敤鐢卞崟涓瘑鍒鎴栧涓瘑鍒缁勬垚鐨勫悕瀛椼傚閮ㄥ垎鍚嶅悇缁勪欢涔嬮棿搴斾互鍙ョ偣(.)闂撮殧寮銆傚閮ㄥ垎鍚嶇殑寮澶撮儴鍒嗕綔涓洪檺瀹氳瘝锛屽悗闈㈢殑璇嗗埆绗﹁瑙i噴銆

MySQL涓彲浠ュ紩鐢ㄤ笅闈㈠舰寮忕殑鍒楋細

鍒楀弬鑰

鍚箟

col_name

col_name锛屾煡璇腑浣跨敤鐨勮〃鍖呭惈鏈夋鍚嶅瓧鐨勫垪銆

tbl_name.col_name

榛樿鏁版嵁搴撲腑鐨勮〃tbl_name鐨勫垪col_name

db_name.tbl_name.col_name

鏁版嵁搴db_name涓殑琛tbl_name鐨勫垪col_name

 

濡傛灉澶氶儴鍒嗗悕鐨勭粍浠堕渶瑕佸紩鐢紝搴斿垎鍒皢瀹冧滑寮曡捣鏉ヨ屼笉瑕佸皢鏁翠釜鍚嶅紩璧锋潵銆備緥濡傦紝`my-tables`.`my-column` 鏈夋晥锛岃`my-tables.my-column`鏃犳晥銆

涓嶉渶瑕佸湪璇彞涓负鍒楁寚瀹tbl_namedb_name.tbl_name鍓嶇紑锛岄櫎闈炲垪浼氬緢妯$硦銆傚亣瀹氳〃t1t2鍚勫寘鍚竴涓垪c锛屼綘浣跨敤SELECT璇彞鍦t1t2涓悳绱c銆傚湪杩欑鎯呭喌涓嬶紝c寰堟ā绯婏紝鍥犱负瀹冨湪璇彞涓娇鐢ㄧ殑琛ㄥ唴涓嶅敮涓銆備綘蹇呴』鐢ㄨ〃鍚t1.ct2.c闄愬畾瀹冿紝琛ㄧず鎸囧摢涓〃銆傚悓鏍凤紝瑕佹兂鐢ㄥ悓涓璇彞鎼滅储鏁版嵁搴db1涓殑琛t鍜屾暟鎹簱db2涓殑琛t锛屼綘蹇呴』灏嗛偅浜涜〃涓殑鍒楁寚涓db1.t.col_namedb2.t.col_name

闄愬畾鍚嶄腑鍙ョ偣鍚庨潰鐨勫瓧蹇呴』涓轰竴涓瘑鍒锛屽洜姝や笉闇瑕佸皢瀹冨紩璧锋潵锛屽嵆浣挎槸涓涓繚鐣欏瓧銆

璇硶.tbl_name琛ㄧず褰撳墠鏁版嵁搴撲腑鐨tbl_name銆傝璇硶涓ODBC鍏煎锛屽洜涓烘煇浜ODBC绋嬪簭鍦ㄨ〃鍚嶅墠闈㈠姞鍓嶇紑鈥.鈥欏瓧绗︺

9.2.2. 璇嗗埆绗﹀ぇ灏忓啓鏁忔劅鎬

MySQL涓紝鏁版嵁搴撳搴旀暟鎹洰褰曚腑鐨勭洰褰曘傛暟鎹簱涓殑姣忎釜琛ㄨ嚦灏戝搴旀暟鎹簱鐩綍涓殑涓涓枃浠(涔熷彲鑳芥槸澶氫釜锛屽彇鍐充簬瀛樺偍寮曟搸)銆傚洜姝わ紝鎵浣跨敤鎿嶄綔绯荤粺鐨勫ぇ灏忓啓鏁忔劅鎬у喅瀹氫簡鏁版嵁搴撳悕鍜岃〃鍚嶇殑澶у皬鍐欐晱鎰熸с傝繖璇存槑鍦ㄥぇ澶氭暟Unix涓暟鎹簱鍚嶅拰琛ㄥ悕瀵瑰ぇ灏忓啓鏁忔劅锛岃屽湪Windows涓澶у皬鍐欎笉鏁忔劅銆備竴涓樉钁楃殑渚嬪鎯呭喌鏄Mac OS X锛屽畠鍩轰簬Unix浣嗕娇鐢ㄩ粯璁ゆ枃浠剁郴缁熺被鍨(HFS+)锛屽澶у皬鍐欎笉鏁忔劅銆傜劧鑰岋紝Mac OS X涔熸敮鎸UFS鍗凤紝璇ュ嵎瀵瑰ぇ灏忓啓鏁忔劅锛屽氨鍍Unix涓鏍枫傚弬瑙1.8.4鑺傦紝鈥淢ySQL瀵规爣鍑哠QL鐨勬墿灞曗

娉ㄩ噴锛灏界鍦ㄦ煇浜涘钩鍙颁腑鏁版嵁搴撳悕鍜岃〃鍚嶅澶у皬鍐欎笉鏁忔劅锛屼笉搴斿湪鍚屼竴鏌ヨ涓娇鐢ㄤ笉鍚岀殑澶у皬鍐欐潵寮曠敤缁欏畾鐨勬暟鎹簱鎴栬〃銆備笅闈㈢殑鏌ヨ涓嶄細宸ヤ綔锛屽洜涓哄畠鍚屾椂寮曠敤浜嗚〃my_tablesas MY_tables

mysql> SELECT * FROM my_table WHERE MY_TABLE.col=1;

鍒椼佺储寮曘佸瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戝櫒鍚嶅湪浠讳綍骞冲彴涓婂澶у皬鍐欎笉鏁忔劅锛屽垪鐨勫埆鍚嶄篃涓嶆晱鎰熴

榛樿鎯呭喌锛岃〃鍒悕鍦Unix涓澶у皬鍐欐晱鎰燂紝浣嗗湪WindowsMac OS X涓澶у皬鍐欎笉鏁忔劅銆備笅闈㈢殑鏌ヨ鍦Unix涓笉浼氬伐浣滐紝鍥犱负瀹冨悓鏃跺紩鐢ㄤ簡鍒悕aA

mysql> SELECT col_name FROM tbl_name AS a
    -> WHERE a.col_name = 1 OR A.col_name = 2;

鐒惰岋紝璇ユ煡璇㈠湪Windows涓槸鍙互鐨勩傝鎯抽伩鍏嶅嚭鐜板樊鍒紝鏈濂介噰鐢ㄤ竴鑷寸殑杞崲锛屼緥濡傛绘槸鐢ㄥ皬鍐欏垱寤哄苟寮曠敤鏁版嵁搴撳悕鍜岃〃鍚嶃傚湪澶у鏁扮Щ妞嶅拰浣跨敤涓缓璁娇鐢ㄨ杞崲銆

MySQL涓浣曞湪纭洏涓婁繚瀛樺拰浣跨敤琛ㄥ悕鍜屾暟鎹簱鍚嶇敱lower_case_tables_name绯荤粺鍙橀噺纭畾锛屽彲浠ュ湪鍚姩mysqld鏃惰缃lower_case_tables_name鍙互閲囩敤涓嬮潰鐨勪换涓鍊硷細

鍚箟

0

浣跨敤CREATE TABLECREATE DATABASE璇彞鎸囧畾鐨勫ぇ鍐欏拰灏忓啓鍦ㄧ‖鐩樹笂淇濆瓨琛ㄥ悕鍜屾暟鎹簱鍚嶃傚悕绉版瘮杈冨澶у皬鍐欐晱鎰熴傚湪Unix绯荤粺涓殑榛樿璁剧疆鍗冲姝ゃ傝娉ㄦ剰濡傛灉鍦ㄥぇ灏忓啓涓嶆晱鎰熺殑鏂囦欢绯荤粺涓婄敤--lower-case-table-names=0寮哄埗璁句负0锛屽苟涓斾娇鐢ㄤ笉鍚岀殑澶у皬鍐欒闂MyISAM琛ㄥ悕锛屼細瀵艰嚧绱㈠紩鐮村潖銆

1

琛ㄥ悕鍦ㄧ‖鐩樹笂浠ュ皬鍐欎繚瀛橈紝鍚嶇О姣旇緝瀵瑰ぇ灏忓啓鏁忔劅銆MySQL灏嗘墍鏈夎〃鍚嶈浆鎹负灏忓啓浠ヤ究瀛樺偍鍜屾煡鎵俱傝琛屼负涔熼傚悎鏁版嵁搴撳悕鍜岃〃鐨勫埆鍚嶃傝鍊间负WindowsMac OS X绯荤粺涓殑榛樿鍊笺

2

琛ㄥ悕鍜屾暟鎹簱鍚嶅湪纭洏涓婁娇鐢CREATE TABLECREATE DATABASE璇彞鎸囧畾鐨勫ぇ灏忓啓杩涜淇濆瓨锛屼絾MySQL灏嗗畠浠浆鎹负灏忓啓浠ヤ究鏌ユ壘銆傚悕绉版瘮杈冨澶у皬鍐欐晱鎰熴閲婏細鍦ㄥ澶у皬鍐欎笉鏁忔劅鐨勬枃浠剁郴缁熶笂閫傜敤! InnoDB琛ㄥ悕浠ュ皬鍐欎繚瀛橈紝渚嬪lower_case_tables_name=1

WindowsMac OS X涓紝lower_case_tables_name鐨 榛樿鍊兼槸1

濡傛灉鍙湪涓涓钩鍙颁笂浣跨敤MySQL锛岄氬父涓嶉渶瑕佹洿鏀lower_case_tables_name鍙橀噺銆傜劧鑰岋紝濡傛灉浣犳兂瑕佸湪瀵瑰ぇ灏忓啓鏁忔劅涓嶅悓鐨勬枃浠剁郴缁熺殑骞冲彴涔嬮棿杞Щ琛紝浼氶亣鍒板洶闅俱備緥濡傦紝鍦Unix涓紝my_tablesMY_tables鏄袱涓笉鍚岀殑琛紝浣嗗湪Windows涓紝杩欎袱涓〃鍚嶇浉鍚屻傝鎯抽伩鍏嶇敱浜庢暟鎹簱鎴栬〃鍚嶇殑澶у皬鍐欓犳垚鐨勬暟鎹浆绉婚棶棰橈紝鍙娇鐢ㄤ袱涓夐」锛

         鍦ㄤ换浣曠郴缁熶腑鍙互浣跨敤lower_case_tables_name=1銆備娇鐢ㄨ閫夐」鐨勪笉鍒╀箣澶勬槸褰撲娇鐢SHOW TABLESSHOW DATABASES鏃讹紝鐪嬩笉鍑哄悕瀛楀師鏉ユ槸鐢ㄥぇ鍐欒繕鏄皬鍐欍

         Unix涓娇鐢lower_case_tables_name=0锛屽湪Windows涓娇鐢lower_case_tables_name=2銆傝繖鏍蜂簡鍙互淇濈暀鏁版嵁搴撳悕鍜岃〃鍚嶇殑澶у皬鍐欍備笉鍒╀箣澶勬槸蹇呴』纭繚鍦Windows涓煡璇㈡绘槸鐢ㄦ纭ぇ灏忓啓寮曠敤鏁版嵁搴撳悕鍜岃〃鍚嶃傚鏋滃皢鏌ヨ杞Щ鍒Unix涓紝鐢变簬鍦Unix涓ぇ灏忓啓寰堥噸瑕侊紝濡傛灉澶у皬鍐欎笉姝g‘锛屽畠浠笉宸ヤ綔銆

渚嬪锛氬鏋滀綘姝d娇鐢InnoDB琛紝鍦ㄤ换浣曞钩鍙颁笂鍧囧簲灏lower_case_tables_name璁剧疆涓1锛屼互寮哄埗灏嗗悕杞崲涓哄皬鍐欍

璇锋敞鎰忓湪Unix涓皢lower_case_tables_name璁剧疆涓1涔嬪墠锛岄噸鍚mysqld涔嬪墠锛屽繀椤诲厛灏嗘棫鐨勬暟鎹簱鍚嶅拰琛ㄥ悕杞崲涓哄皬鍐欍

9.3. 鐢ㄦ埛鍙橀噺

鍙互鍏堝湪鐢ㄦ埛鍙橀噺涓繚瀛樺肩劧鍚庡湪浠ュ悗寮曠敤瀹冿紱杩欐牱鍙互灏嗗间粠涓涓鍙ヤ紶閫掑埌鍙︿竴涓鍙ャ鐢ㄦ埛鍙橀噺涓庤繛鎺ユ湁鍏銆備篃灏辨槸璇达紝涓涓鎴风瀹氫箟鐨勫彉閲忎笉鑳借鍏跺畠瀹㈡埛绔湅鍒版垨浣跨敤銆傚綋瀹㈡埛绔鍑烘椂锛岃瀹㈡埛绔繛鎺ョ殑鎵鏈夊彉閲忓皢鑷姩閲婃斁銆

鐢ㄦ埛鍙橀噺鐨勫舰寮忎负@var_name锛屽叾涓彉閲忓悕var_name鍙互鐢卞綋鍓嶅瓧绗﹂泦鐨勬枃瀛楁暟瀛楀瓧绗︺佲.鈥欍佲_鈥欏拰鈥$鈥欑粍鎴愩 榛樿瀛楃闆嗘槸cp1252 (Latin1)銆傚彲浠ョ敤mysqld--default-character-set閫夐」鏇存敼瀛楃闆嗐傚弬瑙5.10.1鑺傦紝鈥滄暟鎹拰鎺掑簭鐢ㄥ瓧绗﹂泦鈥銆傜敤鎴峰彉閲忓悕瀵瑰ぇ灏忓啓涓嶆晱鎰熴

璁剧疆鐢ㄦ埛鍙橀噺鐨勪竴涓斿緞鏄墽琛SET璇彞锛

SET @var_name = expr [, @var_name = expr] ...

瀵逛簬SET锛屽彲浠ヤ娇鐢=:=浣滀负鍒嗛厤绗︺傚垎閰嶇粰姣忎釜鍙橀噺鐨expr鍙互涓烘暣鏁般佸疄鏁般佸瓧绗︿覆鎴栬NULL鍊笺

涔熷彲浠ョ敤璇彞浠f浛SET鏉ヤ负鐢ㄦ埛鍙橀噺鍒嗛厤涓涓笺傚湪杩欑鎯呭喌涓嬶紝鍒嗛厤绗﹀繀椤讳负:=鑰屼笉鑳界敤=锛屽洜涓哄湪闈SET璇彞涓=琚涓轰竴涓瘮杈 鎿嶄綔绗︼細

mysql> SET @t1=0, @t2=0, @t3=0;
mysql> SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;
+----------------------+------+------+------+
| @t1:=(@t2:=1)+@t3:=4 | @t1  | @t2  | @t3  |
+----------------------+------+------+------+
|                    5 |    5 |    1 |    4 |
+----------------------+------+------+------+

鐢ㄦ埛鍙橀噺鍙互鐢ㄤ簬琛ㄨ揪寮忎腑銆傜洰鍓嶄笉鍖呮嫭鏄庢樉闇瑕佹枃瀛楀肩殑涓婁笅鏂囦腑锛屼緥濡SELECT璇彞鐨LIMIT瀛愬彞锛屾垨鑰LOAD DATA璇彞鐨IGNORE number LINES瀛愬彞銆

濡傛灉浣跨敤娌℃湁鍒濆鍖栫殑鍙橀噺锛屽叾鍊兼槸NULL

濡傛灉鐢ㄦ埛鍙橀噺鍒嗛厤浜嗕竴涓瓧绗︿覆鍊硷紝鍏跺瓧绗﹂泦鍜屾牎瀵硅鍒欎笌璇ュ瓧绗︿覆鐨勭浉鍚屻傜敤鎴峰彉閲忕殑鍙帇缂╂э紙coercibility锛夋槸闅愬惈鐨勩(鍗充负琛ㄥ垪鍊肩殑鐩稿悓鐨勫彲鍘嬬缉鎬э紙coercibility锛夈

娉ㄩ噴锛SELECT璇彞涓紝琛ㄨ揪寮忓彂閫佸埌瀹㈡埛绔悗鎵嶈繘琛岃绠椼傝繖璇存槑鍦HAVINGGROUP BY鎴栬ORDER BY瀛愬彞涓紝涓嶈兘浣跨敤鍖呭惈SELECT鍒楄〃涓墍璁剧殑鍙橀噺鐨勮〃杈惧紡銆備緥濡傦紝涓嬮潰鐨勮鍙ヤ笉鑳芥寜鏈熸湜宸ヤ綔锛

mysql> SELECT (@aa:=id) AS a(@aa+3) AS b tbl_name HAVING b=5

HAVING瀛愬彞涓紩鐢ㄤ簡SELECT鍒楄〃涓殑琛ㄨ揪寮忕殑鍒悕锛屼娇鐢@aa銆備笉鑳芥寜鏈熸湜宸ヤ綔锛@aa涓嶅寘鍚綋鍓嶈鐨勫硷紝鑰屾槸鍓嶉潰鎵閫夌殑琛岀殑id鍊笺

涓鑸師鍒欐槸涓嶈鍦ㄨ鍙ョ殑涓涓儴鍒嗕负鐢ㄦ埛鍙橀噺鍒嗛厤涓涓艰屽湪鍚屼竴璇彞鐨勫叾瀹冮儴鍒嗕娇鐢ㄨ鍙橀噺銆傚彲鑳戒細寰楀埌鏈熸湜鐨勭粨鏋滐紝浣嗕笉鑳戒繚璇併

璁剧疆鍙橀噺骞跺湪鍚屼竴璇彞涓娇鐢ㄥ畠鐨勫彟涓涓棶棰樻槸鍙橀噺鐨勯粯璁ょ粨鏋滅殑绫诲瀷鍙栧喅浜庤鍙ュ墠闈㈢殑鍙橀噺绫诲瀷銆備笅闈㈢殑渚嬪瓙璇存槑浜嗚鐐癸細

mysql> SET @a='test';
mysql> SELECT @a,(@a:=20) FROM tbl_name;

瀵逛簬璇 SELECT璇彞锛MySQL鍚戝鎴风鎶ュ憡绗1鍒楁槸涓涓瓧绗︿覆锛屽苟涓斿皢@a鐨勬墍鏈夎闂浆鎹负瀛楃涓诧紝鍗充娇@a鍦ㄧ2琛屼腑璁剧疆涓轰竴涓暟瀛椼傛墽琛屽畬SELECT璇彞鍚庯紝@a琚涓轰笅涓璇彞鐨勪竴涓暟瀛椼

瑕佹兂閬垮厤杩欑闂锛岃涔堜笉鍦ㄥ悓涓涓鍙ヤ腑璁剧疆骞朵娇鐢ㄧ浉鍚岀殑鍙橀噺锛岃涔堝湪浣跨敤鍓嶅皢鍙橀噺璁剧疆涓00.0鎴栬''浠ュ畾涔夊叾绫诲瀷銆

鏈垎閰嶇殑鍙橀噺鏈変竴涓NULL锛岀被鍨嬩负瀛楃涓层

9.4. 绯荤粺鍙橀噺

MySQL鍙互璁块棶璁稿绯荤粺鍜岃繛鎺ュ彉閲忋傚綋鏈嶅姟鍣ㄨ繍琛屾椂璁稿鍙橀噺鍙互鍔ㄦ佹洿鏀广傝繖鏍烽氬父鍏佽浣犱慨鏀规湇鍔″櫒鎿嶄綔鑰屼笉闇瑕佸仠姝㈠苟閲嶅惎鏈嶅姟鍣ㄣ

mysqld鏈嶅姟鍣ㄧ淮鎶や袱绉嶅彉閲忋傚叏灞鍙橀噺褰卞搷鏈嶅姟鍣ㄦ暣浣撴搷浣溿備細璇濆彉閲忓奖鍝嶅叿浣撳鎴风杩炴帴鐨勬搷浣溿

褰撴湇鍔″櫒鍚姩鏃讹紝瀹冨皢鎵鏈夊叏灞鍙橀噺鍒濆鍖栦负榛樿鍊笺傝繖浜涢粯璁ゅ煎彲浠ュ湪閫夐」鏂囦欢涓垨鍦ㄥ懡浠よ涓寚瀹氱殑閫夐」杩涜鏇存敼銆傛湇鍔″櫒鍚姩鍚庯紝閫氳繃杩炴帴鏈嶅姟鍣ㄥ苟鎵цSET GLOBAL var_name璇彞锛屽彲浠ュ姩鎬佹洿鏀硅繖浜涘叏灞鍙橀噺銆傝鎯虫洿鏀瑰叏灞鍙橀噺锛屽繀椤诲叿鏈SUPER鏉冮檺銆

鏈嶅姟鍣ㄨ繕涓烘瘡涓繛鎺ョ殑瀹㈡埛绔淮鎶や竴绯诲垪浼氳瘽鍙橀噺銆傚湪杩炴帴鏃朵娇鐢ㄧ浉搴斿叏灞鍙橀噺鐨勫綋鍓嶅煎瀹㈡埛绔殑浼氳瘽鍙橀噺杩涜鍒濆鍖栥傚浜庡姩鎬佷細璇濆彉閲忥紝瀹㈡埛绔彲浠ラ氳繃SET SESSION var_name璇彞鏇存敼瀹冧滑銆傝缃細璇濆彉閲忎笉闇瑕佺壒娈婃潈闄愶紝浣嗗鎴风鍙兘鏇存敼鑷繁鐨勪細璇濆彉閲忥紝鑰屼笉鑳芥洿鏀瑰叾瀹冨鎴风鐨勪細璇濆彉閲忋

瀵逛簬鍏ㄥ眬鍙橀噺鐨勬洿鏀瑰彲浠ヨ璁块棶璇ュ叏灞鍙橀噺鐨勪换浣曞鎴风鐪嬭銆傜劧鑰岋紝瀹冨彧褰卞搷鏇存敼鍚庤繛鎺ョ殑瀹㈡埛鐨勪粠璇ュ叏灞鍙橀噺鍒濆鍖栫殑鐩稿簲浼氳瘽鍙橀噺銆備笉褰卞搷鐩墠宸茬粡杩炴帴鐨勫鎴风鐨勪細璇濆彉閲(鍗充娇瀹㈡埛绔墽琛SET GLOBAL璇彞涔熶笉褰卞搷)

鍙互浣跨敤鍑犵璇硶褰㈠紡鏉ヨ缃垨妫绱㈠叏灞鎴栦細璇濆彉閲忋備笅闈㈢殑渚嬪瓙浣跨敤浜sort_buffer_sizeas浣滀负绀轰緥鍙橀噺鍚嶃

瑕佹兂璁剧疆涓涓GLOBAL鍙橀噺鐨勫硷紝浣跨敤涓嬮潰鐨勮娉曪細

mysql> SET GLOBAL sort_buffer_size=value;
mysql> SET @@global.sort_buffer_size=value;

瑕佹兂璁剧疆涓涓SESSION鍙橀噺鐨勫硷紝浣跨敤涓嬮潰鐨勮娉曪細

mysql> SET SESSION sort_buffer_size=value;
mysql> SET @@session.sort_buffer_size=value;
mysql> SET sort_buffer_size=value;

LOCALSESSION鐨勫悓涔夎瘝銆

濡傛灉璁剧疆鍙橀噺鏃朵笉鎸囧畾GLOBALSESSION鎴栬LOCAL锛岄粯璁や娇鐢SESSION銆傚弬瑙13.5.3鑺傦紝鈥淪ET璇硶鈥

瑕佹兂妫绱竴涓GLOBAL鍙橀噺鐨勫硷紝浣跨敤涓嬮潰鐨勮娉曪細

mysql> SELECT @@global.sort_buffer_size;
mysql> SHOW GLOBAL VARIABLES like 'sort_buffer_size';

瑕佹兂妫绱竴涓SESSION鍙橀噺鐨勫硷紝浣跨敤涓嬮潰鐨勮娉曪細

mysql> SELECT @@sort_buffer_size;
mysql> SELECT @@session.sort_buffer_size;
mysql> SHOW SESSION VARIABLES like 'sort_buffer_size';

杩欓噷锛LOCAL涔熸槸SESSION鐨勫悓涔夎瘝銆

褰撲綘鐢SELECT @@var_name鎼滅储涓涓彉閲忔椂(涔熷氨鏄锛屼笉鎸囧畾global.session.鎴栬local.)MySQL杩斿洖SESSION鍊硷紙濡傛灉瀛樺湪锛夛紝鍚﹀垯杩斿洖GLOBAL鍊笺

瀵逛簬SHOW VARIABLES锛屽鏋滀笉鎸囧畾GLOBALSESSION鎴栬LOCALMySQL杩斿洖SESSION鍊笺

褰撹缃GLOBAL鍙橀噺闇瑕GLOBAL鍏抽敭瀛椾絾妫绱㈡椂涓嶉渶瑕佸畠浠殑鍘熷洜鏄槻姝㈠皢鏉ュ嚭鐜伴棶棰樸傚鏋滄垜浠Щ闄や竴涓笌鏌愪釜GLOBAL鍙橀噺鍏锋湁鐩稿悓鍚嶅瓧鐨SESSION鍙橀噺锛屽叿鏈SUPER鏉冮檺鐨勫鎴峰彲鑳戒細鎰忓鍦版洿鏀GLOBAL鍙橀噺鑰屼笉鏄畠鑷繁鐨勮繛鎺ョ殑SESSION鍙橀噺銆傚鏋滄垜浠坊鍔犱竴涓笌鏌愪釜GLOBAL鍙橀噺鍏锋湁鐩稿悓鍚嶅瓧鐨SESSION鍙橀噺锛屾兂鏇存敼GLOBAL鍙橀噺鐨勫鎴峰彲鑳戒細鍙戠幇鍙湁鑷繁鐨SESSION鍙橀噺琚洿鏀逛簡銆

鍏充簬绯荤粺鍚姩閫夐」鍜岀郴缁熷彉閲忕殑璇︾粏淇℃伅鍙傝5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」鈥5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆傚湪5.3.3.1鑺傦紝鈥滃姩鎬佺郴缁熷彉閲忊涓垪鍑轰簡鍙互鍦ㄨ繍琛屾椂璁剧疆鐨勫彉閲忋

9.4.1. 缁撴瀯寮忕郴缁熷彉閲

缁撴瀯寮忓彉閲忓湪涓や釜鏂归潰涓嶅悓浜庡父瑙勭郴缁熷彉閲忥細

         鍏跺兼槸涓涓惈缁勪欢鐨勭粨鏋勶紝鍙互鎸囧畾鏈嶅姟鍣ㄥ弬鏁帮紝涓鑸揣瀵嗙浉鍏炽

         鍙兘鏄粰瀹氱被鍨嬬殑缁撴瀯寮忓彉閲忕殑鍑犱釜瀹炰緥銆傛瘡涓疄渚嬫湁涓涓笉鍚岀殑鍚嶏紝鎸囧悜鏈嶅姟鍣ㄧ淮鎶ょ殑涓嶅悓鐨勮祫婧愩

MySQL 5.1鏀寔缁撴瀯寮忓彉閲忕被鍨嬶紝鍙互鎸囧畾鐩戞帶閿肩紦瀛樻搷浣滅殑鍙傛暟銆傞敭鍊肩紦瀛樼粨鏋勫紡鍙橀噺鏈変互涓嬬粍浠讹細

         key_buffer_size

         key_cache_block_size

         key_cache_division_limit

         key_cache_age_threshold

璇ヨ妭鎻忚堪浜嗗紩鐢ㄧ粨鏋勫紡鍙橀噺鐨勮娉曘傚湪璇硶鐨勪緥瀛愪腑浣跨敤浜嗛敭鍊肩紦瀛樺彉閲忥紝浣嗗叧浜庨敭鍊肩紦瀛樺浣曟搷浣滅殑鍏蜂綋璇︽儏鍦ㄥ叾瀹冪珷鑺備腑锛屽 7.4.6鑺傦紝鈥淢yISAM閿珮閫熺紦鍐测

瑕佸紩鐢ㄧ粨鏋勫紡鍙橀噺瀹炰緥鐨勭粍浠讹紝鍙互浣跨敤instance_name.component_name鏍煎紡鐨勫鍚堝悕銆備緥濡傦細

hot_cache.key_buffer_size
hot_cache.key_cache_block_size
cold_cache.key_cache_block_size

瀵逛簬姣忎釜缁撴瀯寮忕郴缁熷彉閲忥紝鎬绘槸棰勫畾涔夊悕涓default鐨勪竴涓疄渚嬨傚鏋滀笉浣跨敤浠讳綍瀹炰緥鍚嶅紩鐢ㄧ粨鏋勫紡鍙橀噺鐨勪竴涓粍浠讹紝default瀹炰緥琚娇鐢ㄣ傝繖鏍凤紝default.key_buffer_sizekey_buffer_sizeboth鍧囨寚鍚戝悓涓绯荤粺鍙橀噺銆

缁撴瀯寮忓彉閲忓疄渚嬪拰缁勪欢鐨勫懡鍚嶈鍒欎负锛

         瀵逛簬缁欏畾绫诲瀷鐨勭粨鏋勫紡鍙橀噺锛屾瘡涓疄渚嬪繀椤绘湁涓涓湪璇ョ被鍙橀噺涓敮涓鐨勪竴涓悕銆備絾鏄紝鍦ㄤ笉鍚岀被鍨嬬殑缁撴瀯寮忓彉閲忎腑瀹炰緥鍚嶄笉闇瑕佸敮涓銆備緥濡傦紝姣忎釜缁撴瀯寮忓彉閲忔湁涓涓疄渚default锛屽洜姝ゅ湪涓嶅悓鐨勫彉閲忕被鍨嬩腑default涓嶆槸鍞竴鐨勩

         姣忎釜缁撴瀯寮忓彉閲忕被鍨嬬殑缁勪欢鍚嶅湪鎵鏈夌郴缁熷彉閲忓悕涓繀椤绘槸鍞竴鐨勩傚鏋滀笉鏄繖鏍(涔熷氨鏄锛屽鏋滀袱涓笉鍚岀被鍨嬬殑缁撴瀯寮忓彉閲忚兘澶熷叡浜粍浠舵垚鍛樺悕)锛屽皢涓嶆竻妤氫娇鐢ㄥ摢涓粯璁ょ粨鏋勫紡鍙橀噺鏉ョ敤浣滄病鏈変娇鐢ㄥ疄渚嬪悕闄愬畾鐨勬垚鍛樺悕銆

         濡傛灉缁撴瀯寮忓彉閲忓疄渚嬪悕浣滀负鏈紩璧锋潵鐨勮瘑鍒涓嶅悎娉曪紝灏嗗畠鐢ㄥ弽鍕惧彿寮曡捣鏉ヤ綔涓鸿瘑鍒銆備緥濡傦紝hot-cache涓嶅悎娉曪紝浣`hot-cache` 鍚堟硶銆

         globalsessionlocal涓嶆槸鍚堟硶瀹炰緥鍚嶃傝繖鏍峰彲浠ラ伩鍏嶄笌寮曠敤闈炵粨鏋勫紡绯荤粺鍙橀噺鐨勭鍙风殑鍐茬獊锛屼緥濡@@global.var_name

鐩墠锛岃繖鍓嶄袱涓鍒欎笉鍙兘杩濊儗锛屽洜涓哄敮涓鐨勭粨鏋勫紡鍙橀噺绫诲瀷鏄敭鍊肩紦瀛樸傚湪灏嗘潵鍒涘缓鍏跺畠绫诲瀷鐨勭粨鏋勫紡鍙橀噺锛岃繖浜涜鍒欏皢鏈夐噸瑕佺殑鎰忎箟銆

涓涓緥澶栨槸锛屽彲浠ュ湪鍙兘鍑虹幇绠鍗曞彉閲忓悕鐨勪笂涓嬫枃涓娇鐢ㄥ鍚堝悕寮曠敤缁撴瀯寮忓彉閲忕粍浠躲備緥濡傦紝鍙互浣跨敤涓涓懡浠よ閫夐」鏈煇涓粨鏋勫紡鍙橀噺鍒嗛厤涓涓硷細

shell> mysqld --hot_cache.key_buffer_size=64K

鍦ㄩ夐」鏂囦欢涓紝浣跨敤锛

[mysqld]
hot_cache.key_buffer_size=64K

濡傛灉鐢ㄨ閫夐」鍚姩鏈嶅姟鍣紝闄や簡榛樿澶у皬涓8MB鐨勯粯璁ら敭鍊肩紦瀛橈紝杩樺垱寤轰竴涓悕涓hot_cache鐨勯敭鍊肩紦瀛橈紝澶у皬涓64KB

鍋囧畾浣犺繖鏍峰惎鍔ㄦ湇鍔″櫒锛

shell> mysqld --key_buffer_size=256K \
         --extra_cache.key_buffer_size=128K \
         --extra_cache.key_cache_block_size=2048

鍦ㄨ繖绉嶆儏鍐典笅锛屾湇鍔″櫒灏嗛粯璁ら敭鍊肩紦瀛樼殑澶у皬璁惧畾涓256KB(涔熷彲浠ュ啓鎴--default.key_buffer_size=256K骞朵笖锛屾湇鍔″櫒鍒涘缓涓涓悕涓extra_cache鐨勭2涓敭鍊肩紦瀛橈紝澶у皬涓128KB锛岀紦瀛樿〃绱㈠紩鍧楃殑鍧楃紦瀛樺尯鐨勫ぇ灏忚缃负2048瀛楄妭銆

鍦ㄤ笅闈㈢殑渚嬪瓙涓紝鐢3涓笉鍚岀殑閿肩紦瀛橈紙澶у皬姣斾緥涓3:1:1锛夊惎鍔ㄦ湇鍔″櫒锛

shell> mysqld --key_buffer_size=6M \
         --hot_cache.key_buffer_size=2M \
         --cold_cache.key_buffer_size=2M

涔熷彲浠ュ湪杩愯鏃惰缃拰妫绱㈢粨鏋勫紡鍙橀噺鍊笺備緥濡傦紝瑕佹兂灏嗗悕涓hot_cache鐨勪竴涓敭鍊肩紦瀛樼殑澶у皬璁剧疆涓10MB锛屼娇鐢ㄤ笅闈换浣曚竴涓鍙ワ細

mysql> SET GLOBAL hot_cache.key_buffer_size = 10*1024*1024;
mysql> SET @@global.hot_cache.key_buffer_size = 10*1024*1024;

瑕佹兂妫绱㈢紦瀛樺ぇ灏忥紝鎵ц锛

mysql> SELECT @@global.hot_cache.key_buffer_size;

浣嗘槸锛屼笅闈㈢殑璇彞涓嶅伐浣溿傚彉閲忎笉瑙i噴涓轰竴涓鍚堝悕锛岃屾槸瑙i噴涓LIKE妯″紡鍖归厤鎿嶄綔鐨勭畝鍗曞瓧绗︿覆锛

mysql> SHOW GLOBAL VARIABLES LIKE 'hot_cache.key_buffer_size';

杩欐槸涓涓湪鍙兘鍑虹幇绠鍗曠殑鍙橀噺鍚嶆椂浣跨敤缁撴瀯寮忓彉閲忓悕鐨勪緥瀛愩

9.5. 娉ㄩ噴璇硶

MySQL鏈嶅姟鍣ㄦ敮鎸3绉嶆敞閲婇鏍硷細

         浠庘#鈥欏瓧绗︿粠琛屽熬銆

         浠庘-- 鈥欏簭鍒楀埌琛屽熬銆傝娉ㄦ剰鈥-- (鍙岀牬鎶樺彿)娉ㄩ噴椋庢牸瑕佹眰绗2涓牬鎶樺彿鍚庨潰鑷冲皯璺熶竴涓┖鏍肩(渚嬪绌烘牸銆tab銆佹崲琛岀绛夌瓑)銆傝璇硶涓庢爣鍑SQL娉ㄩ噴璇硶绋嶆湁涓嶅悓锛屽悗鑰呭皢鍦1.8.5.7, 鈥溾--鈥欎綔涓烘敞閲婅捣濮嬫爣璁扳涓璁恒

         /*搴忓垪鍒板悗闈㈢殑*/搴忓垪銆傜粨鏉熷簭鍒椾笉涓瀹氬湪鍚屼竴琛屼腑锛屽洜姝よ璇硶鍏佽娉ㄩ噴璺ㄨ秺澶氳銆

涓嬮潰鐨勪緥瀛愭樉绀轰簡3绉嶉鏍肩殑娉ㄩ噴锛

mysql> SELECT 1+1;     # This comment continues to the end of line
mysql> SELECT 1+1;     -- This comment continues to the end of line
mysql> SELECT 1 /* this is an in-line comment */ + 1;
mysql> SELECT 1+
/*
this is a
multiple-line comment
*/
1;

涓婅堪鐨勬敞閲婅娉曢傜敤浜mysqld鏈嶅姟鍣ㄥ浣曞垎鏋SQL璇彞銆傚彂閫佸埌鏈嶅姟鍣ㄤ箣鍓嶏紝mysql瀹㈡埛绋嬪簭涔熸墽琛岄儴鍒嗚鍙ヨВ鏋愩(渚嬪锛屽畠閫氳繃瑙f瀽鏉ョ‘瀹氬湪澶氳鍙ヨ涓殑璇彞杈圭晫锛

MySQL 5.1涓紝mysql瑙f瀽/* ...*/娉ㄩ噴鐨勫敮涓灞闄愭ф槸缁撳悎璇ラ鏍肩殑娉ㄩ噴瀹氱晫绗︿娇鐢ㄧ殑鍙瑰彿鏍囪浜嗘湁鏉′欢鎵ц鐨SQL璇彞閮ㄥ垎銆傞傜敤浜庝氦浜掑紡杩愯mysql鍜屽皢鍛戒护鏀惧叆涓涓枃浠朵腑锛屽苟浠ユ壒澶勭悊妯″紡浣跨敤mysql鏉ュ鐞mysql < file_name鐨勬枃浠躲傝缁嗕俊鎭拰渚嬪瓙鍙傝1.8.4鑺傦紝鈥淢ySQL瀵规爣鍑哠QL鐨勬墿灞曗

9.6. MySQL涓繚鐣欏瓧鐨勫鐞

灏濊瘯浣跨敤涓涓瘑鍒锛屼緥濡備娇鐢ㄥ祵鍏ュ紡MySQL鏁版嵁绫诲瀷鎴栧嚱鏁板悕浣滀负琛ㄥ悕鎴栧垪鍚嶏紝渚嬪TIMESTAMPGROUP锛屼細閫犳垚涓涓父瑙侀棶棰樸傚厑璁镐綘杩欐牱鎿嶄綔(渚嬪锛ABS鍙互浣滀负涓涓垪鍚)銆備絾鏄紝榛樿鎯呭喌涓嬶紝鍦ㄦ暟璋冪敤涓湪鍑芥暟鍚嶅拰鍚庨潰鐨勨(鈥欏瓧绗︿箣闂翠笉鍏佽鏈夌┖鏍笺傝瑕佹眰浣垮嚱鏁拌皟鐢ㄤ笌鍒楀悕寮曠敤涓嶅悓銆

璇ヨ涓虹殑涓嶅埄缁撴灉鏄湪鏌愪簺涓婁笅鏂囦腑鐪佺暐涓涓┖鏍间細浣胯瘑鍒瑙i噴涓哄嚱鏁板悕銆備緥濡傦紝璇ヨ鍙ュ悎娉曪細

mysql> CREATE TABLE abs (val INT)

浣嗙渷鐣abs鍚庨潰鐨勭┖鏍间細閫犳垚璇硶閿欒锛屽洜涓虹渷鐣ュ悗璇ヨ鍙ュソ鍍忚璋冪敤ABS()鍑芥暟锛

mysql> CREATE TABLE abs(val INT);

濡傛灉SQL鏈嶅姟鍣ㄦā寮忓寘鎷IGNORE_SPACE妯″紡鍊硷紝鏈嶅姟鍣ㄥ厑璁稿嚱鏁拌皟鐢ㄦ椂鍦ㄥ嚱鏁板悕鍜屽悗闈㈢殑鈥(鈥欏瓧绗︿箣闂存湁绌烘牸銆傝繖鏍蜂娇鍑芥暟鍚嶈瑙嗕负淇濈暀瀛椼傜粨鏋滄槸锛屼笌鍑芥暟鍚嶇浉鍚岀殑璇嗗埆绗﹀繀椤绘寜鐓9.2鑺傦紝鈥滄暟鎹簱銆佽〃銆佺储寮曘佸垪鍜屽埆鍚嶁涓墍鎻忚堪鐨勫紩璧锋潵銆SQL鏈嶅姟鍣ㄦā寮忔寜鐓5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊涓墍鎻忚堪鐨勮繘琛屾帶鍒躲

闄愬畾鍚嶄腑鍙ョ偣鍚庨潰鐨勫瓧蹇呴』涓轰竴涓瘑鍒锛屽洜姝や笉闇瑕佸皢瀹冨紩璧锋潵锛屽嵆浣垮畠鏄竴涓繚鐣欏瓧銆

MySQL涓紝涓嬭〃涓殑瀛楁樉寮忚淇濈暀銆傚叾涓ぇ澶氭暟瀛楄繘鍒惰鏍囧噯SQL鐢ㄤ綔鍒楀悕鍜/鎴栬〃鍚(渚嬪锛GROUP)銆傚皯鏁拌淇濈暀浜嗭紝鍥犱负MySQL闇瑕佸畠浠紝(鐩墠)浣跨敤yacc瑙f瀽绋嬪簭銆備繚鐣欏瓧琚紩璧锋潵鍚庡彲浠ョ敤浣滆瘑鍒銆

ADDALLALTER
ANALYZEANDAS
ASCASENSITIVEBEFORE
BETWEENBIGINTBINARY
BLOBBOTHBY
CALLCASCADECASE
CHANGECHARCHARACTER
CHECKCOLLATECOLUMN
CONDITIONCONNECTIONCONSTRAINT
CONTINUECONVERTCREATE
CROSSCURRENT_DATECURRENT_TIME
CURRENT_TIMESTAMPCURRENT_USERCURSOR
DATABASEDATABASESDAY_HOUR
DAY_MICROSECONDDAY_MINUTEDAY_SECOND
DECDECIMALDECLARE
DEFAULTDELAYEDDELETE
DESCDESCRIBEDETERMINISTIC
DISTINCTDISTINCTROWDIV
DOUBLEDROPDUAL
EACHELSEELSEIF
ENCLOSEDESCAPEDEXISTS
EXITEXPLAINFALSE
FETCHFLOATFLOAT4
FLOAT8FORFORCE
FOREIGNFROMFULLTEXT
GOTOGRANTGROUP
HAVINGHIGH_PRIORITYHOUR_MICROSECOND
HOUR_MINUTEHOUR_SECONDIF
IGNOREININDEX
INFILEINNERINOUT
INSENSITIVEINSERTINT
INT1INT2INT3
INT4INT8INTEGER
INTERVALINTOIS
ITERATEJOINKEY
KEYSKILLLABEL
LEADINGLEAVELEFT
LIKELIMITLINEAR
LINESLOADLOCALTIME
LOCALTIMESTAMPLOCKLONG
LONGBLOBLONGTEXTLOOP
LOW_PRIORITYMATCHMEDIUMBLOB
MEDIUMINTMEDIUMTEXTMIDDLEINT
MINUTE_MICROSECONDMINUTE_SECONDMOD
MODIFIESNATURALNOT
NO_WRITE_TO_BINLOGNULLNUMERIC
ONOPTIMIZEOPTION
OPTIONALLYORORDER
OUTOUTEROUTFILE
PRECISIONPRIMARYPROCEDURE
PURGERAID0RANGE
READREADSREAL
REFERENCESREGEXPRELEASE
RENAMEREPEATREPLACE
REQUIRERESTRICTRETURN
REVOKERIGHTRLIKE
SCHEMASCHEMASSECOND_MICROSECOND
SELECTSENSITIVESEPARATOR
SETSHOWSMALLINT
SPATIALSPECIFICSQL
SQLEXCEPTIONSQLSTATESQLWARNING
SQL_BIG_RESULTSQL_CALC_FOUND_ROWSSQL_SMALL_RESULT
SSLSTARTINGSTRAIGHT_JOIN
TABLETERMINATEDTHEN
TINYBLOBTINYINTTINYTEXT
TOTRAILINGTRIGGER
TRUEUNDOUNION
UNIQUEUNLOCKUNSIGNED
UPDATEUSAGEUSE
USINGUTC_DATEUTC_TIME
UTC_TIMESTAMPVALUESVARBINARY
VARCHARVARCHARACTERVARYING
WHENWHEREWHILE
WITHWRITEX509
XORYEAR_MONTHZEROFILL

MySQL鍏佽閮ㄥ垎鍏抽敭瀛楃敤鍋氭湭寮曡捣鏉ョ殑璇嗗埆绗︼紝鍥犱负璁稿浜轰互鍓嶆浘浣跨敤杩囧畠浠備笅闈㈠垪鍑轰簡涓浜涗緥瀛愶細

  • ACTION

  • BIT

  • DATE

  • ENUM

  • NO

  • TEXT

  • TIME

  • TIMESTAMP


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com.銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆