鐩綍
ALTER {DATABASE | SCHEMA} [db_name] alter_specification [, alter_specification] ... alter_specification: [DEFAULT] CHARACTER SET charset_name | [DEFAULT] COLLATE collation_name
ALTER DATABASE鐢ㄤ簬鏇存敼鏁版嵁搴撶殑鍏ㄥ眬鐗规с傝繖浜涚壒鎬у偍瀛樺湪鏁版嵁搴撶洰褰曚腑鐨db.opt鏂囦欢涓傝浣跨敤ALTER DATABASE锛屾偍闇瑕佽幏寰楁暟鎹簱ALTER鏉冮檺銆
CHARACTER SET瀛愬彞鐢ㄤ簬鏇存敼榛樿鐨勬暟鎹簱瀛楃闆嗐COLLATE瀛愬彞鐢ㄤ簬鏇存敼榛樿鐨勬暟鎹簱鏁村簭銆傚湪绗10绔锛瀛楃闆嗘敮鎸涓瀛楃闆嗗拰鏁村簭鍚嶇О杩涜浜嗚璁恒
鏁版嵁搴撳悕绉板彲浠ュ拷鐣ワ紝姝ゆ椂锛岃鍙ュ搴斾簬榛樿鏁版嵁搴撱備篃鍙互浣跨敤ALTER SCHEMA銆
ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ... alter_specification: ADD [COLUMN] column_definition [FIRST | AFTER col_name ] | ADD [COLUMN] (column_definition,...) | ADD INDEX [index_name] [index_type] (index_col_name,...) | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) | ADD [CONSTRAINT [symbol]] UNIQUE [index_name] [index_type] (index_col_name,...) | ADD [FULLTEXT|SPATIAL] [index_name] (index_col_name,...) | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) [reference_definition] | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} | CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name] | MODIFY [COLUMN] column_definition [FIRST | AFTER col_name] | DROP [COLUMN] col_name | DROP PRIMARY KEY | DROP INDEX index_name | DROP FOREIGN KEY fk_symbol | DISABLE KEYS | ENABLE KEYS | RENAME [TO] new_tbl_name | ORDER BY col_name | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name] | [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name] | DISCARD TABLESPACE | IMPORT TABLESPACE | table_options | partition_options | ADD PARTITION partition_definition | DROP PARTITION partition_names | COALESCE PARTITION number | REORGANIZE PARTITION partition_names INTO (partition_definitions) | ANALYZE PARTITION partition_names | CHECK PARTITION partition_names | OPTIMIZE PARTITION partition_names | REBUILD PARTITION partition_names | REPAIR PARTITION partition_names
ALTER TABLE鐢ㄤ簬鏇存敼鍘熸湁琛ㄧ殑缁撴瀯銆備緥濡傦紝鎮ㄥ彲浠ュ鍔犳垨鍒犲噺鍒楋紝鍒涘缓鎴栧彇娑堢储寮曪紝鏇存敼鍘熸湁鍒楃殑绫诲瀷锛屾垨閲嶆柊鍛藉悕鍒楁垨琛ㄣ傛偍杩樺彲浠ユ洿鏀硅〃鐨勮瘎娉ㄥ拰琛ㄧ殑绫诲瀷銆
鍏佽杩涜鐨勫彉鏇翠腑锛岃澶氬瓙鍙ョ殑璇硶涓CREATE TABLE涓殑瀛愬彞鐨勮娉曠浉杩戙傚叾涓寘鎷table_options淇敼锛岄夐」鏈ENGINE, AUTO_INCREMENT鍜AVG_ROW_LENGTH绛夈傝瑙13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆
瀛樺偍寮曟搸涓嶆敮鎸佹湁浜涙搷浣滐紝濡傛灉杩涜杩欎簺鎿嶄綔锛屼細鍑虹幇璀﹀憡銆備娇鐢SHOW WARNINGS鍙互鏄剧ず鍑鸿繖浜涜鍛娿傝鍙傝13.5.4.22鑺傦紝鈥淪HOW WARNINGS璇硶鈥銆
濡傛灉鎮ㄤ娇鐢ALTER TABLE鏇存敼鍒楄绾︼紝浣嗘槸DESCRIBE tbl_name鎻愮ず鎮ㄥ垪瑙勭害骞舵病鏈夋敼鍙橈紝鍒欏彲鑳芥槸鍥犱负MySQL蹇界暐浜嗘偍鎵鍋氱殑鏇存敼銆傚拷鐣ユ洿鏀圭殑鍘熷洜瑙13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹銆備緥濡傦紝濡傛灉鎮ㄨ瘯鍥炬妸VARCHAR鍒楁洿鏀逛负CHAR鍒楋紝姝ゆ椂锛屽鏋滆〃鍖呭惈鍏跺畠闀垮害鍙彉鐨勫垪锛屽垯MySQL浠嶄細浣跨敤VARCHAR銆
ALTER TABLE杩愯鏃朵細瀵瑰師琛ㄨ繘琛屼复鏃跺鍒讹紝鍦ㄥ壇鏈笂杩涜鏇存敼锛岀劧鍚庡垹闄ゅ師琛紝鍐嶅鏂拌〃杩涜閲嶅懡鍚嶃傚湪鎵цALTER TABLE鏃讹紝鍏跺畠鐢ㄦ埛鍙互闃呰鍘熻〃锛屼絾鏄琛ㄧ殑鏇存柊鍜屼慨鏀圭殑鎿嶄綔灏嗚寤惰繜锛岀洿鍒版柊琛ㄧ敓鎴愪负姝€傛柊琛ㄧ敓鎴愬悗锛岃繖浜涙洿鏂板拰淇敼淇℃伅浼氳嚜鍔ㄨ浆绉诲埌鏂拌〃涓娿
娉ㄦ剰锛屽鏋滄偍鍦ㄦ墽琛ALTER TABLE鏃朵娇鐢ㄩ櫎浜RENAME浠ュ鐨勯夐」锛屽垯MySQL浼氬垱寤轰竴涓复鏃惰〃銆傚嵆浣挎暟鎹苟涓嶉渶瑕佽繘琛屽鍒讹紙渚嬪褰撴偍鏇存敼鍒楃殑鍚嶇О鏃讹級锛MySQL涔熶細杩欎箞鎿嶄綔銆傚浜MyISAM琛紝鎮ㄥ彲浠ラ氳繃鎶myisam_sort_buffer_size绯荤粺鍙橀噺璁剧疆鍒颁竴涓緝楂樼殑鍊硷紝鏉ュ姞蹇噸鏂板垱寤虹储寮曪紙璇ユ搷浣滄槸鍙樻洿杩囩▼涓熷害鏈鎱㈢殑涓閮ㄥ垎锛夌殑閫熷害銆
路 瑕佷娇鐢ALTER TABLE锛屾偍闇瑕佽幏寰楄〃鐨ALTER, INSERT鍜CREATE鏉冮檺銆
路 IGNORE鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚鏋滃湪鏂拌〃涓湁閲嶅鍏抽敭瀛楋紝鎴栬呭綋STRICT妯″紡鍚姩鍚庡嚭鐜拌鍛婏紝鍒欎娇鐢IGNORE鎺у埗ALTER TABLE鐨勮繍琛屻傚鏋滄病鏈夋寚瀹IGNORE锛屽綋閲嶅鍏抽敭瀛楅敊璇彂鐢熸椂锛屽鍒舵搷浣滆鏀惧純锛岃繑鍥炲墠涓姝ラ銆傚鏋滄寚瀹氫簡IGNORE锛屽垯瀵逛簬鏈夐噸澶嶅叧閿瓧鐨勮锛屽彧浣跨敤绗竴琛岋紝鍏跺畠鏈夊啿绐佺殑琛岃鍒犻櫎銆傚苟涓旓紝瀵归敊璇艰繘琛屼慨姝o紝浣夸箣灏介噺鎺ヨ繎姝g‘鍊笺
路 鎮ㄥ彲浠ュ湪涓涓ALTER TABLE璇彞閲屽啓鍏ュ涓ADD, ALTER, DROP鍜CHANGE瀛愬彞锛屼腑闂寸敤閫楀彿鍒嗗紑銆傝繖鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚湪鏍囧噯SQL涓紝姣忎釜ALTER TABLE璇彞涓瘡涓瓙鍙ュ彧鍏佽浣跨敤涓娆°備緥濡傦紝鍦ㄤ竴涓鍙ヤ腑鍙栨秷澶氫釜鍒楋細
路 mysql> ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
路 CHANGE col_name, DROP col_name鍜DROP INDEX鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘
路 MODIFY鏄Oracle瀵ALTER TABLE鐨勬墿灞曘
路 COLUMN鍙槸鑷夐」鐩紝鍙互蹇界暐銆
路 濡傛灉鎮ㄤ娇鐢ALTER TABLE tbl_name RENAME TO new_tbl_name骞朵笖娌℃湁鍏跺畠閫夐」锛屽垯MySQL鍙涓table tbl_name鐩稿搴旂殑鏂囦欢杩涜閲嶅懡鍚嶃備笉闇瑕佸垱寤轰竴涓复鏃惰〃銆傦紙鎮ㄤ篃鍙互浣跨敤RENAME TABLE璇彞瀵硅〃杩涜閲嶅懡鍚嶃傝鍙傝13.1.9鑺傦紝鈥淩ENAME TABLE璇硶鈥銆傦級
路 column_definition瀛愬彞浣跨敤涓CREATE TABLE涓殑ADD鍜CHANGE瀛愬彞鐩稿悓鐨勮娉曘傛敞鎰忥紝姝よ娉曞寘鎷垪鍚嶇О锛岃屼笉鍙槸鍒楃被鍨嬨傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆
路 鎮ㄥ彲浠ヤ娇鐢CHANGE old_col_name column_definition瀛愬彞瀵瑰垪杩涜閲嶅懡鍚嶃傞噸鍛藉悕鏃讹紝闇缁欏畾鏃х殑鍜屾柊鐨勫垪鍚嶇О鍜屽垪褰撳墠鐨勭被鍨嬨備緥濡傦細瑕佹妸涓涓INTEGER鍒楃殑鍚嶇О浠a鍙樻洿鍒b锛屾偍闇瑕佸涓嬫搷浣滐細
路 mysql> ALTER TABLE t1 CHANGE a b INTEGER;
濡傛灉鎮ㄦ兂瑕佹洿鏀瑰垪鐨勭被鍨嬭屼笉鏄悕绉帮紝 CHANGE璇硶浠嶇劧瑕佹眰鏃х殑鍜屾柊鐨勫垪鍚嶇О锛屽嵆浣挎棫鐨勫拰鏂扮殑鍒楀悕绉版槸涓鏍风殑銆備緥濡傦細
mysql> ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
鎮ㄤ篃鍙互浣跨敤MODIFY鏉ユ敼鍙樺垪鐨勭被鍨嬶紝姝ゆ椂涓嶉渶瑕侀噸鍛藉悕锛
mysql> ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
路 濡傛灉鎮ㄤ娇鐢CHANGE鎴MODITY缂╃煭鍒楅暱鏃讹紝鍒椾腑瀛樺湪鏈夌储寮曪紝骞朵笖缂╃煭鍚庣殑鍒楅暱灏忎簬绱㈠紩闀垮害锛屽垯MySQL浼氳嚜鍔ㄧ缉鐭储寮曠殑闀垮害銆
路 褰撴偍浣跨敤CHANGE鎴MODIFY鏇存敼鍒楃殑绫诲瀷鏃讹紝MySQL浼氬敖閲忔妸鍘熸湁鐨勫垪鍊艰浆鍖栦负鏂扮殑绫诲瀷銆
路 鎮ㄥ彲浠ヤ娇鐢FIRST鎴AFTER col_name鍦ㄤ竴涓〃琛屼腑鐨勬煇涓壒瀹氫綅缃坊鍔犲垪銆傞粯璁ゆ妸鍒楁坊鍔犲埌鏈鍚庛傛偍涔熷彲浠ュ湪CHANGE鎴MODIFY璇彞涓娇鐢FIRST鍜AFTER銆
路 AFTER COLUMN鐢ㄤ簬鎸囧畾鍒楃殑鏂伴粯璁ゅ硷紝鎴栧垹闄ゆ棫鐨勯粯璁ゅ笺傚鏋滄棫鐨勯粯璁ゅ艰鍒犻櫎鍚屾椂鍒楀间负NULL锛屽垯鏂扮殑榛樿鍊间负NULL銆傚鏋滃垪鍊间笉鑳戒负NULL锛MySQL浼氭寚瀹氫竴涓粯璁ゅ硷紝璇峰弬瑙13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆
路 DROP INDEX鐢ㄤ簬鍙栨秷绱㈠紩銆傝繖鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傝鍙傝13.1.7鑺傦紝鈥淒ROP INDEX璇硶鈥銆
路 濡傛灉鍒椾粠琛ㄤ腑琚彇娑堜簡锛屽垯杩欎簺鍒椾篃浠庣浉搴旂殑绱㈠紩涓鍙栨秷銆傚鏋滅粍鎴愪竴涓储寮曠殑鎵鏈夊垪鍧囪鍙栨秷锛屽垯璇ョ储寮曚篃琚彇娑堛
路 濡傛灉涓涓〃鍙寘鍚竴鍒楋紝鍒欐鍒椾笉鑳借鍙栨秷銆傚鏋滄偍鎯宠鍙栨秷琛紝搴斾娇鐢DROP TABLE銆
路 DROP PRIMAY DEY鐢ㄤ簬鍙栨秷涓荤储寮曘傛敞閲婏細鍦MySQL杈冩棭鐨勭増鏈腑锛屽鏋滄病鏈変富绱㈠紩锛屽垯DROP PRIMARY KEY浼氬彇娑堣〃涓殑绗竴涓UNIQUE绱㈠紩銆傚湪MySQL 5.1涓笉浼氬嚭鐜拌繖绉嶆儏鍐点傚鏋滃湪MySQL 5.1涓娌℃湁涓婚敭鐨勮〃浣跨敤DROP PRIMARY KEY锛屽垯浼氬嚭鐜伴敊璇俊鎭
濡傛灉鎮ㄥ悜琛ㄤ腑娣诲姞UNIQUE KEY鎴PRIMARY KEY锛屽垯UNIQUE KEY鎴PRIMARY KEY浼氳鍌ㄥ瓨鍦ㄩ潪鍞竴绱㈠紩涔嬪墠锛岃繖鏍MySQL灏卞彲浠ュ敖鏃╁湴妫鏌ュ嚭閲嶅鍏抽敭瀛椼
路 ORDER BY鐢ㄤ簬鍦ㄥ垱寤烘柊琛ㄦ椂锛岃鍚勮鎸変竴瀹氱殑椤哄簭鎺掑垪銆傛敞鎰忥紝鍦ㄦ彃鍏ュ拰鍒犻櫎鍚庯紝琛ㄤ笉浼氫粛淇濇寔姝ら『搴忋傚綋鎮ㄧ煡閬撳鏁版儏鍐典笅鎮ㄤ細鎸夌収鐗瑰畾鐨勯『搴忔煡璇㈠悇琛屾椂锛屽彲浠ヤ娇鐢ㄨ繖涓夐」锛涘湪瀵硅〃杩涜浜嗗ぇ鐨勬敼鍔ㄥ悗锛岄氳繃浣跨敤姝ら夐」锛屾偍鍙互鎻愰珮鏌ヨ鏁堢巼銆傚湪鏈変簺鎯呭喌涓嬶紝濡傛灉琛ㄦ寜鍒楁帓搴忥紝瀵逛簬MySQL鏉ヨ锛屾帓搴忓彲鑳戒細鏇寸畝鍗曘
路 濡傛灉鎮ㄥ涓涓MyISAM琛ㄤ娇鐢ALTER TABLE锛屽垯鎵鏈夐潪鍞竴绱㈠紩浼氳鍒涘缓鍒颁竴涓崟鐙殑鎵归噷锛堝拰REPAIR TABLE鐩稿悓锛夈傚綋鎮ㄦ湁璁稿绱㈠紩鏃讹紝杩欐牱鍋氬彲浠ヤ娇ALTER TABLE鐨勯熷害鏇村揩銆
杩欓」鍔熻兘鍙互鏄庣‘婵娲汇ALTER TABLE...DISABLE KEYS璁MySQL鍋滄鏇存柊MyISAM琛ㄤ腑鐨勯潪鍞竴绱㈠紩銆傜劧鍚庝娇鐢ALTER TABLE ... ENABLE KEYS閲嶆柊鍒涘缓涓㈠け鐨勭储寮曘傝繘琛屾鎿嶄綔鏃讹紝MySQL閲囩敤涓绉嶇壒娈婄殑绠楁硶锛屾瘮涓涓帴涓涓湴鎻掑叆鍏抽敭瀛楄蹇緢澶氥傚洜姝わ紝鍦ㄨ繘琛屾垚鎵规彃鍏ユ搷浣滃墠鍏堜娇鍏抽敭瀛楃鐢ㄥ彲浠ュぇ澶у湴鍔犲揩閫熷害銆備娇鐢ALTER TABLE ... DISABLE KEYS闄や簡闇瑕佽幏寰椾互鍓嶆彁鍒扮殑鏉冮檺浠ュ锛岃繕闇瑕佽幏寰INDEX鏉冮檺銆
路 Innodb瀛樺偍寮曟搸鏀寔FOREIGN KEY鍜REFERENCES瀛愬彞銆Innodb瀛樺偍寮曟搸鎵цADD [CONSTRAINT [symbol]] FOREIGN KEY (...) REFERENCES ... (...)銆傝鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥銆傚浜庡叾瀹冨瓨鍌ㄥ紩鎿庯紝杩欎簺瀛愬彞浼氳鍒嗘瀽锛屼絾鏄細琚拷鐣ャ傚浜庢墍鏈夌殑瀛樺偍寮曟搸锛CHECK瀛愬彞浼氳鍒嗘瀽锛屼絾鏄細琚拷鐣ャ傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆傛帴鍙楄繖浜涘瓙鍙ヤ絾鍙堝拷鐣ュ瓙鍙ョ殑鍘熷洜鏄负浜嗘彁楂樺吋瀹规э紝浠ヤ究鏇村鏄撳湴浠庡叾瀹SQL鏈嶅姟鍣ㄤ腑瀵煎叆浠g爜锛屽苟杩愯搴旂敤绋嬪簭锛屽垱寤哄甫鍙傝冩暟鎹殑琛ㄣ傝鍙傝1.8.5鑺傦紝鈥淢ySQL涓庢爣鍑哠QL鐨勫樊鍒銆
路 InnoDB鏀寔浣跨敤ALTER TABLE鏉ュ彇娑堝閿細
路 ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;
瑕佷簡瑙f洿澶氫俊鎭紝璇峰弬瑙15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥銆
路 ALTER TABLE蹇界暐DATA DIRECTORY鍜INDEX DIRECTORY琛ㄩ夐」銆
路 濡傛灉鎮ㄦ兂瑕佹妸琛ㄩ粯璁ょ殑瀛楃闆嗗拰鎵鏈夊瓧绗﹀垪锛CHAR, VARCHAR, TEXT锛夋敼涓烘柊鐨勫瓧绗﹂泦锛屽簲浣跨敤濡備笅璇彞锛
路 ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
璀﹀憡锛氬墠闈㈢殑鎿嶄綔杞崲浜嗗瓧绗﹂泦涔嬮棿鐨勫垪绫诲瀷銆傚鏋滄偍鏈変竴鍒椾娇鐢ㄤ竴绉嶅瓧绗﹂泦锛堝latin1锛夛紝浣嗘槸瀛樺偍鐨勫煎疄闄呬笂浣跨敤浜嗗叾瀹冪殑瀛楃闆嗭紙濡utf8锛夛紝杩欑鎯呭喌涓嶆槸鎮ㄦ兂瑕佺殑銆傛鏃讹紝鎮ㄥ繀椤诲杩欐牱鐨勫垪杩涜浠ヤ笅鎿嶄綔銆
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
杩欑鏂规硶鑳藉瀹炵幇姝ゅ姛鑳界殑鍘熷洜鏄紝褰撴偍杞崲鍒BLOB鍒楁垨浠BLOB鍒楄浆鎹㈣繃鏉ユ椂锛屽苟娌℃湁鍙戠敓杞崲銆
濡傛灉鎮ㄦ寚瀹CONVERT TO CHARACTER SET涓轰簩杩涘埗锛屽垯TEXT鍒楄杞崲鍒扮浉搴旂殑浜岃繘鍒跺瓧绗︿覆绫诲瀷锛BINARY, VARBINARY, BLOB锛夈傝繖鎰忓懗鐫杩欎簺鍒楀皢涓嶅啀鏈夊瓧绗﹂泦锛屾帴涓嬫潵鐨CONVERT TO鎿嶄綔涔熷皢涓嶉傜敤浜庤繖浜涘垪銆
瑕佷粎浠呮敼鍙樹竴涓〃鐨勯粯璁ゅ瓧绗﹂泦锛屽簲浣跨敤姝よ鍙ワ細
ALTER TABLE tbl_name DEFAULT CHARACTER SET charset_name;
璇嶈DEFAULT涓鸿嚜閫夐」銆傚鏋滄偍鍦ㄥ悜琛ㄤ腑娣诲姞涓涓柊鍒楁椂锛堜緥濡傦紝浣跨敤ALTER TABLE...ADD column锛夋病鏈夋寚瀹氬瓧绗﹂泦锛屽垯姝ゆ椂浣跨敤鐨勫瓧绗﹂泦涓洪粯璁ゅ瓧绗﹂泦銆
璀﹀憡锛ALTER TABLE...DEFAULT CHARACTER SET鍜ALTER TABLE...CHARACTER SET鏄瓑浠风殑锛屽彧鐢ㄤ簬鏇存敼榛樿鐨勮〃瀛楃闆嗐
路 濡傛灉InnoDB琛ㄥ湪鍒涘缓鏃讹紝浣跨敤浜.ibd鏂囦欢涓殑鑷繁鐨勮〃绌洪棿锛屽垯杩欐牱鐨勬枃浠跺彲浠ヨ鍒犻櫎鍜屽鍏ャ備娇鐢ㄦ璇彞鍒犻櫎.ibd鏂囦欢锛
路 ALTER TABLE tbl_name DISCARD TABLESPACE;
姝よ鍙ョ敤浜庡垹闄ゅ綋鍓嶇殑.ibd鏂囦欢锛屾墍浠ュ簲棣栧厛纭鎮ㄦ湁涓涓浠姐傚鏋滃湪琛ㄧ┖闂磋鍒犻櫎鍚庡皾璇曟墦寮琛ㄦ牸锛屽垯浼氬嚭鐜伴敊璇
瑕佹妸澶囦唤鐨.ibd鏂囦欢杩樺師鍒拌〃涓紝闇鎶婃鏂囦欢澶嶅埗鍒版暟鎹簱鐩綍涓紝鐒跺悗涔﹀啓姝よ鍙ワ細
ALTER TABLE tbl_name IMPORT TABLESPACE;
路 浣跨敤mysql_info() C API鍑芥暟锛屾偍鍙互浜嗚В鏈夊灏戣褰曞凡琚鍒讹紝浠ュ強锛堝綋浣跨敤IGNORE鏃讹級鏈夊灏戣褰曠敱浜庨噸澶嶅叧閿瓧鐨勫師鍥犲凡琚垹闄ゃ傝鍙傝25.2.3.34鑺傦紝鈥渕ysql_info()鈥銆
路 ALTER TABLE涔熷彲浠ョ敤浜庡甯﹀垎鍖虹殑琛ㄨ繘琛岄噸鏂板垎鍖猴紝鍔熻兘鍖呮嫭娣诲姞銆佸彇娑堛佸悎骞跺拰鎷嗗垎鍚勫垎鍖猴紝杩樺彲浠ョ敤浜庤繘琛屽垎鍖虹淮鎶ゃ
瀵瑰甫鍒嗗尯鐨勮〃浣跨敤partition_options瀛愬彞鍜ALTER TABLE鍙互瀵硅〃杩涜閲嶆柊鍒嗗尯锛屼娇鐢ㄦ椂渚濇嵁partition_options瀹氫箟鐨勫垎鍖烘柟娉曘傛湰瀛愬彞浠PARTITION BY涓哄紑澶达紝鐒跺悗浣跨敤涓庣敤浜CREATE TABLE鐨partition_options瀛愬彞涓鏍风殑璇硶鍜岃鍒欙紙瑕佷簡瑙h缁嗕俊鎭紝璇峰弬瑙13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥锛夈傛敞閲婏細MySQL 5.1鏈嶅姟鍣ㄧ洰鍓嶆帴鍙楁璇硶锛屼絾鏄笉瀹為檯鎵ц锛涚瓑MySQL 5.1寮鍙戝嚭鏉ュ悗锛屽皢鎵ц姝よ娉曘
鐢ㄤ簬ALTER TABLE ADD PARTITION鐨partition_definition瀛愬彞鏀寔鐢ㄤ簬CREATE TABLE璇彞鐨partition_definition瀛愬彞鐨勫悓鏍峰悕绉扮殑閫夐」銆傦紙瑕佷簡瑙h娉曞拰浠嬬粛锛岃鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆傦級渚嬪锛屽亣璁炬偍鏈変竴涓寜鐓т互涓嬫柟寮忓垱寤虹殑甯﹀垎鍖虹殑琛細
CREATE TABLE t1 (
id INT,
year_col INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999)
);
鎮ㄥ彲浠ュ湪琛ㄤ腑澧炲姞涓涓柊鐨勫垎鍖p3锛岃鍒嗗尯鐢ㄤ簬鍌ㄥ瓨灏忎簬2002鐨勫笺傛坊鍔犳柟娉曞涓嬶細
ALTER TABLE t1 ADD PARTITION p3 VALUES LESS THAN (2002);
娉ㄩ噴锛氭偍涓嶈兘浣跨敤ALTER TABLE鍚戜竴涓病鏈夎繘琛屽垎鍖虹殑琛ㄦ坊鍔犲垎鍖恒
DROP PARTITION鐢ㄤ簬鍙栨秷涓涓垨澶氫釜RANGE鎴LIST鍒嗗尯銆傛鍛戒护涓嶈兘鐢ㄤ簬HASH鎴KEY 鍒嗗尯锛涚敤浜庤繖涓や釜鍒嗗尯鏃讹紝搴斾娇鐢COALESCE PARTITION锛堣鍚庯級銆傚鏋滆鍙栨秷鐨勫垎鍖哄叾鍚嶇О鍒椾簬partition_names娓呭崟涓紝鍒欏偍瀛樺湪姝ゅ垎鍖轰腑鐨勬暟鎹篃琚彇娑堛備緥濡傦紝濡傛灉浠ュ墠宸插畾涔夌殑琛t1锛屾偍鍙互閲囩敤濡備笅鏂规硶鍙栨秷鍚嶇О涓p0鍜p1鐨勫垎鍖猴細
ALTER TABLE DROP PARTITION p0, p1;
ADD PARTITION鍜DROP PARTITION鐩墠涓嶆敮鎸IF [NOT] EXISTS銆備篃涓嶅彲鑳藉涓涓垎鍖烘垨涓涓凡鍒嗗尯鐨勮〃杩涜閲嶅懡鍚嶃傚鏋滄偍甯屾湜瀵逛竴涓垎鍖鸿繘琛岄噸鍛藉悕锛屾偍蹇呴』鍙栨秷鍒嗗尯锛屽啀閲嶆柊寤虹珛锛涘鏋滄偍甯屾湜瀵逛竴涓凡鍒嗗尯鐨勮〃杩涜閲嶆柊鍛藉悕锛屾偍蹇呴』鍙栨秷鎵鏈夊垎鍖猴紝鐒跺悗瀵硅〃杩涜閲嶅懡鍚嶏紝鍐嶆坊鍔犺鍙栨秷鐨勫垎鍖恒
COALESCE PARTITION鍙互鐢ㄤ簬浣跨敤HASH鎴KEY杩涜鍒嗗尯鐨勮〃锛屼互渚夸娇鐢number鏉ュ噺灏戝垎鍖虹殑鏁扮洰銆備緥濡傦紝鍋囪鎮ㄤ娇鐢ㄤ笅鍒楁柟娉曞垱寤轰簡琛t2锛
CREATE TABLE t2 (
name VARCHAR (30),
started DATE
)
PARTITION BY HASH(YEAR(started))
PARTITIONS (6);
鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬪懡浠わ紝鎶t2浣跨敤鐨勫垎鍖虹殑鏁扮洰鐢6涓噺灏戝埌4涓細
ALTER TABLE t2 COALESCE PARTITION 2;
鍖呭惈鍦ㄦ渶鍚庝竴涓number鍒嗗尯涓殑鏁版嵁灏嗚鍚堝苟鍒板叾浣欑殑鍒嗗尯涓傚湪姝ゆ儏鍐典笅锛屽垎鍖4鍜屽垎鍖5灏嗚鍚堝苟鍒板墠4涓垎鍖轰腑锛堢紪鍙蜂负0銆1銆2鍜3鐨勫垎鍖猴級銆
濡傛灉瑕佹洿鏀归儴鍒嗗垎鍖猴紝浣嗕笉鏇存敼鎵鏈夌殑鍒嗗尯锛屾偍鍙互浣跨敤REORGANIZE PARTITION銆傝繖涓懡浠ゆ湁澶氱浣跨敤鏂规硶锛
o 鎶婂涓垎鍖哄悎骞朵负涓涓垎鍖恒傞氳繃鎶婂涓垎鍖虹殑鍚嶇О鍒楀叆partition_names娓呭崟锛屽苟涓partition_definition鎻愪緵涓涓崟涓鐨勫畾涔夛紝鍙互瀹炵幇杩欎釜鍔熻兘銆
o 鎶婁竴涓師鏈夌殑鍒嗗尯鎷嗗垎涓哄涓垎鍖恒傞氳繃涓partition_names鍛藉悕涓涓垎鍖猴紝骞舵彁渚涘涓partition_definitions锛屽彲浠ュ疄鐜拌繖涓姛鑳姐
o 鏇存敼浣跨敤VALUES LESS THAN瀹氫箟鐨勫垎鍖哄瓙闆嗙殑鑼冨洿鎴栨洿鏀逛娇鐢VALUES IN瀹氫箟鐨勫垎鍖哄瓙闆嗙殑鍊兼竻鍗曘
娉ㄩ噴锛氬浜庢病鏈夋槑纭懡鍚嶇殑鍒嗗尯锛MySQL浼氳嚜鍔ㄦ彁渚涢粯璁ゅ悕绉p0, p1, p2绛夈
瑕佷簡瑙f湁鍏ALTER TALBE...REORANIZE PARTITION鍛戒护鐨勮缁嗕俊鎭紝璇峰弬瑙18.3鑺傦紝鈥滃垎鍖虹鐞嗏銆
路 澶氫釜闄勫姞瀛愬彞鐢ㄤ簬鎻愪緵鍒嗗尯缁存姢鍜屼慨琛ュ姛鑳姐傝繖浜涘姛鑳戒笌鐢ㄤ簬闈炲垎鍖鸿〃鐨勫姛鑳界被浼笺傝繖浜涘姛鑳界敱CHECK TABLE鍜REPAIR TABLE绛夊懡浠わ紙杩欎簺鍛戒护涓嶆敮鎸佺敤浜庡垎鍖鸿〃锛夋墽琛屻傝繖浜涘瓙鍙ュ寘鎷ANALYZE PARTITION, CHECK PARTITION, OPTIMIZE PARTITION, REBUILD PARTITION鍜REPAIR PARTITION.姣忎釜閫夐」鍧囦负涓涓partition_names瀛愬彞锛屽寘鎷竴涓垨澶氫釜鍒嗗尯鍚嶇О銆傞渶瑕佹洿鏀圭殑琛ㄤ腑蹇呴』宸插瓨鍦ㄨ繖浜涘垎鍖恒傚涓垎鍖哄悕绉扮敤閫楀彿鍒嗛殧銆傝浜嗚В鏇村淇℃伅锛屾垨瑕佷簡瑙d妇渚嬭鏄庯紝璇峰弬瑙18.3.3鑺傦紝鈥滃垎鍖虹淮鎶も銆
浠ヤ笅渚嬪瓙灞曠ず浜ALTER TABLE鐨勪娇鐢ㄣ傞鍏堝睍绀鸿〃t1銆傝〃t1閲囩敤濡備笅鏂规硶鍒涘缓锛
mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));
鎶婅〃t1閲嶆柊鍛藉悕涓t2锛
mysql> ALTER TABLE t1 RENAME t2;
鎶婂垪a浠INTERGER鏇存敼涓TINYINT NOT NULL锛堝悕绉颁繚鎸佷笉鍙橈級锛屽苟鎶婂垪b浠CHAR(10)鏇存敼涓CHAR(20)锛屽悓鏃舵妸鍒b閲嶆柊鍛藉悕涓哄垪c锛
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
娣诲姞涓涓柊鐨TIMESTAMP鍒楋紝鍚嶇О涓d锛
mysql> ALTER TABLE t2 ADD d TIMESTAMP;
鍦ㄥ垪d鍜屽垪a涓坊鍔犵储寮曪細
mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);
鍒犻櫎鍒c锛
mysql> ALTER TABLE t2 DROP COLUMN c;
娣诲姞涓涓柊鐨AUTO_INCREMENT鏁存暟鍒楋紝鍚嶇О涓c锛
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> ADD PRIMARY KEY (c);
娉ㄦ剰鎴戜滑涓c缂栧埗浜嗙储寮曪紙浣滀负PRIMARY KEY锛夛紝鍥犱负AUTO_INCREMENT鍒楀繀椤荤紪鍒剁储寮曘傚悓鏃舵垜浠畾涔c涓NOT NULL锛屽洜涓轰富閿垪涓嶈兘涓NULL銆
褰撴偍娣诲姞涓涓AUTO_INCREMENT鍒楁椂锛屽垪鍊艰鑷姩鍦版寜搴忓彿濉叆銆傚浜MyISAM琛紝鎮ㄥ彲浠ュ湪ALTER TABLE涔嬪墠鎵цSET INSERT_ID=value鏉ヨ缃涓涓簭鍙凤紝涔熷彲浠ヤ娇鐢AUTO_INCREMENT=value琛ㄩ夐」鏉ヨ缃傝鍙傝13.5.3鑺傦紝鈥淪ET璇硶鈥銆
濡傛灉鍊煎ぇ浜AUTO_INCREMENT鍒椾腑鐨勬渶澶у硷紝鍒欐偍鍙互浣跨敤鐢ㄤ簬InnoDB琛ㄧ殑ALTER TALBE...AUTO_INCREMENT=value琛ㄩ夐」锛屾潵涓烘柊琛岃缃簭鍙枫傚鏋滃煎皬浜庡垪涓綋鍓嶇殑鏈澶у硷紝涓嶄細鍑虹幇閿欒淇℃伅锛屽綋鍓嶇殑搴忓垪鍊间篃涓嶆敼鍙樸
浣跨敤MyISAM琛ㄦ椂锛屽鏋滄偍涓嶆洿鏀AUTO_INCREMENT鍒楋紝鍒欏簭鍒楀彿涓嶅彈褰卞搷銆傚鏋滄偍鍙栨秷涓涓AUTO_INCREMENT鍒楋紝鐒跺悗娣诲姞鍙︿竴涓AUTO_INCREMENT鍒楋紝鍒欏簭鍙烽噸鏂版帓鍒楋紝浠1寮濮嬨
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
| [DEFAULT] COLLATE collation_name
CREATE DATABASE鐢ㄤ簬鍒涘缓鏁版嵁搴擄紝骞惰繘琛屽懡鍚嶃傚鏋滆浣跨敤CREATE DATABASE锛屾偍闇瑕佽幏寰楁暟鎹簱CREATE鏉冮檺銆
鏈夊叧鍚堟硶鏁版嵁搴撳悕绉扮殑瑙勫畾鍒椾簬9.2鑺傦紝鈥滄暟鎹簱銆佽〃銆佺储寮曘佸垪鍜屽埆鍚嶁銆傚鏋滃瓨鍦ㄦ暟鎹簱锛屽苟涓旀偍娌℃湁鎸囧畾IF NOT EXISTS锛屽垯浼氬嚭鐜伴敊璇
create_specification閫夐」鐢ㄤ簬鎸囧畾鏁版嵁搴撶殑鐗规с傛暟鎹簱鐗规у偍瀛樺湪鏁版嵁搴撶洰褰曚腑鐨db.opt鏂囦欢涓CHARACTER SET瀛愬彞鐢ㄤ簬鎸囧畾榛樿鐨勬暟鎹簱瀛楃闆嗐COLLATE瀛愬彞鐢ㄤ簬鎸囧畾榛樿鐨勬暟鎹簱鏁村簭銆傚瓧绗﹂泦鍜屾暣搴忓悕绉板湪绗10绔锛瀛楃闆嗘敮鎸涓璁恒
鏈変簺鐩綍鍖呭惈鏂囦欢锛岃繖浜涙枃浠朵笌鏁版嵁搴撲腑鐨勮〃瀵瑰簲銆MySQL涓殑鏁版嵁搴撶殑鎵ц鏂规硶涓庤繖浜涚洰褰曠殑鎵ц鏂规硶鐩稿悓銆傚洜涓哄綋鏁版嵁搴撳垰鍒氳鍒涘缓鏃讹紝鍦ㄦ暟鎹簱涓病鏈夎〃锛屾墍浠CREATE DATABASE鍙垱寤轰竴涓洰褰曘傝繖涓洰褰曚綅浜MySQL鏁版嵁鐩綍鍜db.opt鏂囦欢涔嬩笅銆
濡傛灉鎮ㄦ墜鍔ㄥ湪鏁版嵁鐩綍涔嬩笅鍒涘缓涓涓洰褰曪紙渚嬪锛屼娇鐢mkdir锛夛紝鍒欐湇鍔″櫒浼氳涓鸿繖鏄竴涓暟鎹簱鐩綍锛屽苟鍦SHOW DATABASES鐨勮緭鍑轰腑鏄剧ず鍑烘潵銆
涔熷彲浠ヤ娇鐢CREATE SCHEMA銆
鎮ㄨ繕鍙互浣跨敤mysqladmin绋嬪簭鍒涘缓鏁版嵁搴撱傝鍙傝8.5鑺傦紝鈥渕ysqladmin锛氱敤浜庣鐞哅ySQL鏈嶅姟鍣ㄧ殑瀹㈡埛绔銆
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[USING index_type]
ON tbl_name (index_col_name,...)
index_col_name:
col_name [(length)] [ASC | DESC]
CREATE INDEX琚槧灏勫埌涓涓ALTER TABLE璇彞涓婏紝鐢ㄤ簬鍒涘缓绱㈠紩銆傝鍙傝13.1.2鑺傦紝鈥淎LTER TABLE璇硶鈥銆
閫氬父锛屽綋浣跨敤CREATE TABLE鍒涘缓琛ㄦ椂锛屼篃鍚屾椂鍦ㄨ〃涓垱寤轰簡鎵鏈夌殑绱㈠紩銆傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆CREATE INDEX鍏佽鎮ㄥ悜宸叉湁鐨勮〃涓坊鍔犵储寮曘
鏍煎紡涓猴紙col1, col2,...锛夌殑涓涓垪娓呭崟鍒涘缓鍑轰竴涓鍒楃储寮曘傞氳繃涓叉帴缁欏畾鍒椾腑鐨勫硷紝纭畾绱㈠紩鍊肩殑鏍煎紡銆
瀵逛簬CHAR鍜VARCHAR鍒楋紝鍙敤涓鍒楃殑涓閮ㄥ垎灏卞彲鍒涘缓绱㈠紩銆傚垱寤虹储寮曟椂锛屼娇鐢col_name(length)璇硶锛屽鍓嶇紑缂栧埗绱㈠紩銆傚墠缂鍖呮嫭姣忓垪鍊肩殑鍓length涓瀛楃銆BLOB鍜TEXT鍒椾篃鍙互缂栧埗绱㈠紩锛屼絾鏄繀椤荤粰鍑哄墠缂闀垮害銆
姝ゅ灞曠ず鐨勮鍙ョ敤浜庡垱寤轰竴涓储寮曪紝绱㈠紩浣跨敤鍒楀悕绉扮殑鍓10涓瓧绗︺
CREATE INDEX part_of_name ON customer (name(10));
鍥犱负澶氭暟鍚嶇О鐨勫墠10涓瓧绗﹂氬父涓嶅悓锛屾墍浠ユ绱㈠紩涓嶄細姣斾娇鐢ㄥ垪鐨勫叏鍚嶅垱寤虹殑绱㈠紩閫熷害鎱㈠緢澶氥傚彟澶栵紝浣跨敤鍒楃殑涓閮ㄥ垎鍒涘缓绱㈠紩鍙互浣跨储寮曟枃浠跺ぇ澶у噺灏忥紝浠庤岃妭鐪佷簡澶ч噺鐨勭鐩樼┖闂达紝鏈夊彲鑳芥彁楂INSERT鎿嶄綔鐨勯熷害銆
鍓嶇紑鏈闀夸负255瀛楄妭銆傚浜MyISAM鍜InnoDB琛紝鍓嶇紑鏈闀夸负1000瀛楄妭銆傛敞鎰忓墠缂鐨勯檺闀夸互瀛楄妭璁★紝鑰CREATE INDEX璇彞涓殑鍓嶇紑闀垮害鎸囩殑鏄瓧绗︾殑鏁扮洰銆傚浜庝娇鐢ㄥ瀛楄妭瀛楃闆嗙殑鍒楋紝鍦ㄦ寚瀹氬垪鐨勫墠缂闀垮害鏃讹紝瑕佽冭檻杩欎竴鐐广
鍦MySQL 5.1涓細
路 鍙湁褰撴偍姝e湪浣跨敤MyISAM, InnoDB鎴BDB琛ㄧ被鍨嬫椂锛屾偍鍙互鍚戞湁NULL鍊肩殑鍒椾腑娣诲姞绱㈠紩銆
路 鍙湁褰撴偍姝e湪浣跨敤MyISAM, BDB鎴InnoDB琛ㄧ被鍨嬫椂锛屾偍鍙互鍚BLOB鎴TEXT鍒椾腑娣诲姞绱㈠紩銆
涓涓index_col_name瑙勭害鍙互浠ASC鎴DESC涓虹粨灏俱傝繖浜涘叧閿瘝灏嗘潵鍙互鎵╁睍锛岀敤浜庢寚瀹氶掑鎴栭掑噺绱㈠紩鍊煎瓨鍌ㄣ傜洰鍓嶏紝杩欎簺鍏抽敭璇嶈鍒嗘瀽锛屼絾鏄蹇界暐锛涚储寮曞煎潎浠ラ掑椤哄簭瀛樺偍銆
閮ㄥ垎鍌ㄥ瓨寮曟搸鍏佽鍦ㄥ垱寤虹储寮曟椂鎸囧畾绱㈠紩绫诲瀷銆index_type鎸囧畾璇彞鐨勮娉曟槸USING type_name銆備笉鍚岀殑鍌ㄥ瓨寮曟搸鎵鏀寔鐨type_name鍊煎凡鏄剧ず鍦ㄤ笅琛ㄤ腑銆傚鏋滃垪鏈夊涓储寮曠被鍨嬶紝褰撴病鏈夋寚瀹index_type鏃讹紝绗竴涓被鍨嬫槸榛樿鍊銆
瀛樺偍寮曟搸 |
鍏佽鐨勭储寮曠被鍨 |
MyISAM |
BTREE |
InnoDB |
BTREE |
MEMORY/HEAP |
HASH, BTREE |
绀轰緥锛
CREATE TABLE lookup (id INT) ENGINE = MEMORY;
CREATE INDEX id_index USING BTREE ON lookup (id);
TYPE type_name鍙互浣滀负USING type_name鐨勫悓涔夎瘝锛岀敤浜庢寚瀹氱储寮曠被鍨嬨備絾鏄紝USING鏄閫夌殑鏍煎紡銆傚彟澶栵紝鍦ㄧ储寮曡绾﹁娉曚腑锛屼綅浜庣储寮曠被鍨嬪墠闈㈢殑绱㈠紩鍚嶇О涓嶈兘浣跨敤TYPE銆傝繖鏄洜涓猴紝涓USING涓嶅悓锛TYPE涓嶆槸淇濈暀璇嶏紝鍥犳浼氳璁や负鏄竴涓储寮曞悕绉般
濡傛灉鎮ㄦ寚瀹氱殑绱㈠紩绫诲瀷鍦ㄧ粰瀹氱殑鍌ㄥ瓨寮曟搸涓笉鍚堟硶锛屼絾鏄湁鍏跺畠鐨勭储寮曠被鍨嬮傚悎寮曟搸浣跨敤锛屽苟涓斾笉浼氬奖鍝嶆煡璇㈠姛鑳斤紝鍒欏紩鎿庡簲浣跨敤姝ょ被鍨嬨
瑕佷簡瑙f洿澶氭湁鍏MySQL濡備綍浣跨敤绱㈠紩鐨勪俊鎭紝璇峰弬瑙7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩鈥銆
FULLTEXT绱㈠紩鍙兘瀵CHAR, VARCHAR鍜TEXT鍒楃紪鍒剁储寮曪紝骞朵笖鍙兘鍦MyISAM琛ㄤ腑缂栧埗銆傝鍙傝12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解銆
SPATIAL绱㈠紩鍙兘瀵圭┖闂村垪缂栧埗绱㈠紩锛屽苟涓斿彧鑳藉湪MyISAM琛ㄤ腑缂栧埗銆傜┖闂村垪绫诲瀷鍦绗19绔狅細MySQL涓殑绌洪棿鎵╁睍涓繘琛屼簡鎻忚堪銆
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options] [select_statement]
鎴栵細
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(] LIKE old_tbl_name [)];
create_definition:
column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
| KEY [index_name] [index_type] (index_col_name,...)
| INDEX [index_name] [index_type] (index_col_name,...)
| [CONSTRAINT [symbol]] UNIQUE [INDEX]
[index_name] [index_type] (index_col_name,...)
| [FULLTEXT|SPATIAL] [INDEX] [index_name] (index_col_name,...)
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) [reference_definition]
| CHECK (expr)
column_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string'] [reference_definition]
type:
TINYINT[(length)] [UNSIGNED] [ZEROFILL]
| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
| MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
| INT[(length)] [UNSIGNED] [ZEROFILL]
| INTEGER[(length)] [UNSIGNED] [ZEROFILL]
| BIGINT[(length)] [UNSIGNED] [ZEROFILL]
| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
| NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
| DATE
| TIME
| TIMESTAMP
| DATETIME
| CHAR(length) [BINARY | ASCII | UNICODE]
| VARCHAR(length) [BINARY]
| TINYBLOB
| BLOB
| MEDIUMBLOB
| LONGBLOB
| TINYTEXT [BINARY]
| TEXT [BINARY]
| MEDIUMTEXT [BINARY]
| LONGTEXT [BINARY]
| ENUM(value1,value2,value3,...)
| SET(value1,value2,value3,...)
| spatial_type
index_col_name:
col_name [(length)] [ASC | DESC]
reference_definition:
REFERENCES tbl_name [(index_col_name,...)]
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
table_options: table_option [table_option] ...
table_option:
{ENGINE|TYPE} = engine_name
| AUTO_INCREMENT = value
| AVG_ROW_LENGTH = value
| [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
| CHECKSUM = {0 | 1}
| COMMENT = 'string'
| CONNECTION = 'connect_string'
| MAX_ROWS = value
| MIN_ROWS = value
| PACK_KEYS = {0 | 1 | DEFAULT}
| PASSWORD = 'string'
| DELAY_KEY_WRITE = {0 | 1}
| ROW_FORMAT = {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| UNION = (tbl_name[,tbl_name]...)
| INSERT_METHOD = { NO | FIRST | LAST }
| DATA DIRECTORY = 'absolute path to directory'
| INDEX DIRECTORY = 'absolute path to directory'
partition_options:
PARTITION BY
[LINEAR] HASH(expr)
| [LINEAR] KEY(column_list)
| RANGE(expr)
| LIST(column_list)
[PARTITIONS num]
[ SUBPARTITION BY
[LINEAR] HASH(expr)
| [LINEAR] KEY(column_list)
[SUBPARTITIONS(num)]
]
[(partition_definition), [(partition_definition)], ...]
partition_definition:
PARTITION partition_name
[VALUES {
LESS THAN (expr) | MAXVALUE
| IN (value_list) }]
[[STORAGE] ENGINE [=] engine-name]
[COMMENT [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] (tablespace_name)]
[NODEGROUP [=] node_group_id]
[(subpartition_definition), [(subpartition_definition)], ...]
subpartition_definition:
SUBPARTITION logical_name
[[STORAGE] ENGINE [=] engine-name]
[COMMENT [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] (tablespace_name)]
[NODEGROUP [=] node_group_id]
select_statement:
[IGNORE | REPLACE] [AS] SELECT ... (Some legal select statement)
CREATE TABLE鐢ㄤ簬鍒涘缓甯︾粰瀹氬悕绉扮殑琛ㄣ傛偍蹇呴』鎷ユ湁琛CREATE鏉冮檺銆
鍏佽鐨勮〃鍚嶇О鐨勮鍒欏垪浜9.2鑺傦紝鈥滄暟鎹簱銆佽〃銆佺储寮曘佸垪鍜屽埆鍚嶁涓傞粯璁ょ殑鎯呭喌鏄紝琛ㄨ鍒涘缓鍒板綋鍓嶇殑鏁版嵁搴撲腑銆傚鏋滆〃宸插瓨鍦紝鎴栬呭鏋滄病鏈夊綋鍓嶆暟鎹簱锛屾垨鑰呭鏋滄暟鎹簱涓嶅瓨鍦紝鍒欎細鍑虹幇閿欒銆
琛ㄥ悕绉拌鎸囧畾涓db_name.tbl_name锛屼互渚垮湪鐗瑰畾鐨勬暟鎹簱涓垱寤鸿〃銆備笉璁烘槸鍚︽湁褰撳墠鏁版嵁搴擄紝閮藉彲浠ラ氳繃杩欑鏂瑰紡鍒涘缓琛ㄣ傚鏋滄偍浣跨敤鍔犲紩鍙风殑璇嗗埆鍚嶏紝鍒欏簲瀵规暟鎹簱鍜岃〃鍚嶇О鍒嗗埆鍔犲紩鍙枫備緥濡傦紝`mydb`.`mytbl`鏄悎娉曠殑锛屼絾鏄`mydb.mytbl`涓嶅悎娉曘
鍦ㄥ垱寤鸿〃鏍兼椂锛屾偍鍙互浣跨敤TEMPORARY鍏抽敭璇嶃傚彧鏈夊湪褰撳墠杩炴帴鎯呭喌涓嬶紝TEMPORARY琛ㄦ墠鏄彲瑙佺殑銆傚綋杩炴帴鍏抽棴鏃讹紝TEMPORARY琛ㄨ鑷姩鍙栨秷銆傝繖鎰忓懗鐫涓や釜涓嶅悓鐨勮繛鎺ュ彲浠ヤ娇鐢ㄧ浉鍚岀殑涓存椂琛ㄥ悕绉帮紝鍚屾椂涓や釜涓存椂琛ㄤ笉浼氫簰鐩稿啿绐侊紝涔熶笉涓庡師鏈夌殑鍚屽悕鐨勯潪涓存椂琛ㄥ啿绐併傦紙鍘熸湁鐨勮〃琚殣钘忥紝鐩村埌涓存椂琛ㄨ鍙栨秷鏃朵负姝€傦級鎮ㄥ繀椤绘嫢鏈CREATE TEMPORARY TABLES鏉冮檺锛屾墠鑳藉垱寤轰复鏃惰〃銆
濡傛灉琛ㄥ凡瀛樺湪锛屽垯浣跨敤鍏抽敭璇IF NOT EXISTS鍙互闃叉鍙戠敓閿欒銆傛敞鎰忥紝鍘熸湁琛ㄧ殑缁撴瀯涓CREATE TABLE璇彞涓〃绀虹殑琛ㄧ殑缁撴瀯鏄惁鐩稿悓锛岃繖涓鐐规病鏈夐獙璇併傛敞閲婏細濡傛灉鎮ㄥ湪CREATE TABLE...SELECT璇彞涓娇鐢IF NOT EXISTS锛屽垯涓嶈琛ㄦ槸鍚﹀凡瀛樺湪锛岀敱SELECT閮ㄥ垎閫夋嫨鐨勮褰曢兘浼氳鎻掑叆銆
MySQL閫氳繃鏁版嵁搴撶洰褰曚腑鐨.frm琛ㄦ牸寮忥紙瀹氫箟锛夋枃浠惰〃绀烘瘡涓〃銆傝〃鐨勫瓨鍌ㄥ紩鎿庝篃鍙兘浼氬垱寤哄叾瀹冩枃浠躲傚浜MyISAM琛紝瀛樺偍寮曟搸鍙互鍒涘缓鏁版嵁鍜岀储寮曟枃浠躲傚洜姝わ紝瀵逛簬姣忎釜MyISAM琛tbl_name锛屾湁涓変釜纾佺洏鏂囦欢锛
鏂囦欢 |
浣滅敤 |
tbl_name.frm |
琛ㄦ牸寮忥紙瀹氫箟锛夋枃浠 |
tbl_name.MYD |
鏁版嵁鏂囦欢 |
tbl_name.MYI |
绱㈠紩鏂囦欢 |
鐢ㄤ簬琛ㄧず琛ㄧ殑鐢卞瓨鍌ㄥ紩鎿庡垱寤虹殑鏂囦欢鍦绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷涓弿杩般
瑕佷簡瑙f湁鍏冲悇绉嶅垪绫诲瀷鐨勬ц川鐨勪竴鑸鏄庯紝璇峰弬瑙绗11绔狅細鍒楃被鍨銆傝浜嗚В鏈夊叧绌洪棿鍒楃被鍨嬬殑璇存槑锛岃鍙傝绗19绔狅細MySQL涓殑绌洪棿鎵╁睍銆
路 濡傛灉娌℃湁鎸囧畾鏄NULL鎴栨槸NOT NULL锛屽垯鍒楀湪鍒涘缓鏃跺亣瀹氭寚瀹氫负NULL銆
路 涓涓暣鏁板垪鍙互鎷ユ湁涓涓檮鍔犲睘鎬AUTO_INCREMENT銆傚綋鎮ㄥ悜涓涓凡缂栧叆绱㈠紩鐨AUTO_INCREMENT鍒椾腑鎻掑叆涓涓NULL鍊硷紙寤鸿锛夋垨0鏃讹紝姝ゅ垪琚缃负涓嬩竴涓簭鍒楃殑鍊笺傞氬父鎯呭喌涓嬩负value+1锛屾澶value鏄綋鍓嶅湪琛ㄤ腑鐨勫垪鐨勬渶澶у笺AUTO_INCREMENT搴忓垪浠1寮濮嬨傝繖鏍风殑鍒楀繀椤昏瀹氫箟涓轰竴绉嶆暣鏁扮被鍨嬶紝璇峰弬瑙11.1.1鑺傦紝鈥滄暟鍊肩被鍨嬫杩扳涓殑鍙欒堪銆傦紙鍊1.0涓嶆槸鏁存暟锛夈傝鍙傝25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥銆
涓--sql-mode鏈嶅姟鍣ㄩ夐」鎴sql_mode绯荤粺鍙橀噺鎸囧畾NO_AUTO_VALUE_ON_ZERO鐗瑰緛浣嶏紝杩欐牱鍙互鎶0瀛樺偍鍒AUTO_INCREMENT鍒椾腑锛屽悓鏃朵笉鐢熸垚涓涓柊鐨勫簭鍒楀笺傝鍙傝5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」鈥銆
娉ㄩ噴锛氭湁鏃跺欙紝姣忎釜琛ㄥ彧鏈変竴涓AUTO_INCREMENT鍒楋紝姝ゅ垪蹇呴』缂栧埗绱㈠紩锛屼笉鑳芥湁DEFAULT鍊笺備竴涓AUTO_INCREMENT鍒楀彧鏈夊湪鍙寘鍚鏁扮殑鎯呭喌涓嬶紝鎵嶈兘杩愯姝e父銆傛彃鍏ヤ竴涓礋鏁颁細琚涓烘槸鎻掑叆浜嗕竴涓潪甯稿ぇ鐨勬鏁般傝繖鏍峰仛鏄负浜嗛伩鍏嶅綋鏁板瓧鐢辨鏁拌浆涓鸿礋鏁版椂鍑虹幇绮惧害闂锛屽悓鏃朵篃涓轰簡纭繚AUTO_INCREMENT鍒椾腑涓嶄細鍖呭惈0銆
瀵逛簬MyISAM鍜BDB琛紝鎮ㄥ彲浠ュ湪涓涓鍒楀叧閿瓧涓寚瀹氫竴涓AUTO_INCREMENT娆$骇鍒椼傝鍙傝3.6.9鑺傦紝鈥滀娇鐢ˋUTO_INCREMENT鈥銆
涓轰簡璁MySQL涓庨儴鍒ODBC搴旂敤杞欢鐩稿吋瀹癸紝鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬫煡璇㈡柟娉曟壘鍒版渶鍚庝竴涓彃鍏ヨ鐨AUTO_INCREMENT鍊硷細
SELECT * FROM tbl_name WHERE auto_col IS NULL
路 瀛楃鍒楃殑瀹氫箟鍙互鍖呮嫭涓涓CHARACTER SET灞炴э紝鐢ㄦ潵鎸囧畾瀛楃闆嗭紝涔熷彲浠ユ寚瀹氬垪鐨勬暣搴忋傝浜嗚В璇︾粏鎯呭喌锛岃鍙傝绗10绔狅細瀛楃闆嗘敮鎸銆CHARSET鏄CHARACTER SET鐨勫悓涔夎瘝銆
路 CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);
MySQL 5.1鐞嗚В锛屽湪瀛楃鍒楀畾涔変腑鐨勯暱搴﹁绾︿互瀛楃涓哄崟浣嶃傦紙鏈変簺鏃╂湡鐗堟湰浠ュ瓧鑺備负鍗曚綅銆傦級
路 DEFAULT瀛愬彞鐢ㄤ簬涓哄垪鎸囧畾涓涓粯璁ゅ笺傞粯璁ゅ煎繀椤讳负涓涓父鏁帮紝涓嶈兘涓轰竴涓嚱鏁版垨涓涓〃杈惧紡锛屾湁涓绉嶆儏鍐典緥澶栥備緥濡傦紝涓涓棩鏈熷垪鐨勯粯璁ゅ间笉鑳借璁剧疆涓轰竴涓嚱鏁帮紝濡NOW()鎴CURRENT_DATE銆備笉杩囷紝鏈変竴绉嶄緥澶栵紝鎮ㄥ彲浠ュTIMESTAMP鍒楁寚瀹CURRENT_TIMESTAMP涓洪粯璁ゅ笺傝鍙傝11.3.1.1鑺傦紝鈥淢ySQL 4.1涓殑TIMESTAMP灞炴р銆
BLOB鍜TEXT鍒椾笉鑳借璧嬩簣榛樿鍊笺
濡傛灉鍦ㄥ垪瀹氫箟涓病鏈夋槑纭殑DEFAULT鍊硷紝鍒MySQL鎸夌収濡備笅瑙勫垯纭畾榛樿鍊硷細
濡傛灉鍒楀彲浠ヤ娇鐢NULL浣滀负鍊硷紝鍒欎娇鐢DEFAULT NULL瀛愬彞瀵瑰垪杩涜瀹氫箟銆傦紙鍦MySQL鐨勬棭鏈熺増鏈腑涔熷姝ゃ傦級
濡傛灉鍒椾笉鑳戒娇鐢NULL浣滀负鍊硷紝鍒MySQL瀵瑰垪杩涜瀹氫箟鏃朵笉浣跨敤DEFAULT瀛愬彞銆傝緭鍏ユ暟鎹椂锛屽鏋INSERT鎴REPLACE璇彞涓嶅寘鎷垪鐨勫硷紝鍒MySQL渚濇嵁褰撴椂鐨勬湁鏁堢殑SQL妯″紡鎿嶄綔鍒楋細
o 濡傛灉涓ユ牸妯″紡娌℃湁琚惎鐢紝鍒MySQL浼氭牴鎹垪鏁版嵁绫诲瀷锛屾妸鍒楄缃负鏄庣‘鐨勯粯璁ゅ笺
o 濡傛灉涓ユ牸妯″紡宸茶鍚敤锛屽垯浜嬪姟琛ㄤ細鍑虹幇閿欒锛岃鍙ヨ鍥炴粴銆傚浜庨潪浜嬪姟琛紝浼氬嚭鐜伴敊璇紝涓嶈繃锛屽鏋滈敊璇嚭鐜板湪涓涓琛岃鍙ヤ腑鐨勭浜岃鎴栧悗缁锛屽垯浠ュ墠鐨勫悇琛屽皢琚彃鍏ャ
鍋囪琛t鎸変笅闈㈢殑鏂规硶杩涜瀹氫箟锛
CREATE TABLE t (i INT NOT NULL);
鍦ㄨ繖绉嶆儏鍐典笅锛i娌℃湁鏄庣‘鐨勯粯璁ゅ硷紝鎵浠ュ湪涓ユ牸妯″紡涓紝姣忎釜鍚庣画璇彞閮戒細浜х敓涓涓敊璇紝骞朵笖娌℃湁琛岃鎻掑叆銆傚綋鏈娇鐢ㄤ弗鏍兼ā寮忔椂锛屽彧鏈夌涓変釜璇彞浜х敓閿欒锛涙槑纭殑榛樿鍊艰鎻掑叆鍒板墠涓や釜璇彞涓紝浣嗘槸绗笁涓鍙ヤ細鍑虹幇閿欒锛屽洜涓DEFAULT(i)涓嶄細浜х敓涓涓硷細
INSERT INTO t VALUES();
INSERT INTO t VALUES(DEFAULT);
INSERT INTO t VALUES(DEFAULT(i));
瀵逛簬涓涓粰瀹氱殑琛紝鎮ㄥ彲浠ヤ娇鐢SHOW CREATE TABLE璇彞鏉ユ煡鐪嬮偅浜涘垪鏈夋槑纭殑DEFAULT瀛愬彞銆
路 瀵逛簬鍒楃殑璇勬敞鍙互浣跨敤COMMENT閫夐」鏉ヨ繘琛屾寚瀹氥傝瘎娉ㄩ氳繃SHOW CREATE TABLE鍜SHOW FULL COLUMNS璇彞鏄剧ず銆
路 灞炴SERIAL鍙互鐢ㄤ綔BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE鐨勫埆鍚嶃
路 KEY閫氬父鏄INDEX鍚屼箟璇嶃傚鏋滃叧閿瓧灞炴PRIMARY KEY鍦ㄥ垪瀹氫箟涓凡缁欏畾锛屽垯PRIMARY KEY涔熷彲浠ュ彧鎸囧畾涓KEY銆傝繖涔堝仛鐨勭洰鐨勬槸涓庡叾瀹冩暟鎹簱绯荤粺鍏煎銆
路 鍦UNIQUE绱㈠紩涓紝鎵鏈夌殑鍊煎繀椤讳簰涓嶇浉鍚屻傚鏋滄偍鍦ㄦ坊鍔犳柊琛屾椂浣跨敤鐨勫叧閿瓧涓庡師鏈夎鐨勫叧閿瓧鐩稿悓锛屽垯浼氬嚭鐜伴敊璇備緥澶栨儏鍐垫槸锛屽鏋滅储寮曚腑鐨勪竴涓垪鍏佽鍖呭惈NULL鍊硷紝鍒欐鍒楀彲浠ュ寘鍚涓NULL鍊笺傛渚嬪鎯呭喌涓嶉傜敤浜BDB琛ㄣ傚湪BDB涓紝甯︾储寮曠殑鍒楀彧鍏佽涓涓崟涓NULL銆
路 PRIMARY KEY鏄竴涓敮涓KEY锛屾鏃讹紝鎵鏈夌殑鍏抽敭瀛楀垪蹇呴』瀹氫箟涓NOT NULL銆傚鏋滆繖浜涘垪娌℃湁琚槑纭湴瀹氫箟涓NOT NULL锛MySQL搴旈殣鍚湴瀹氫箟杩欎簺鍒椼備竴涓〃鍙湁涓涓PRIMARY KEY銆傚鏋滄偍娌℃湁PRIMARY KEY骞朵笖涓涓簲鐢ㄧ▼搴忚姹傚湪琛ㄤ腑浣跨敤PRIMARY KEY锛屽垯MySQL杩斿洖绗竴涓UNIQUE绱㈠紩锛屾绱㈠紩娌℃湁浣滀负PRIMARY KEY鐨NULL鍒椼
路 鍦ㄥ凡鍒涘缓鐨勮〃涓紝PRIMARY KEY鐨勪綅缃渶闈犲墠锛岀劧鍚庢槸鎵鏈夌殑UNIQUE绱㈠紩锛岀劧鍚庢槸闈炲敮涓绱㈠紩銆傝繖鍙互甯姪MySQL浼樺寲绋嬪簭閫夋嫨浼樺厛浣跨敤鍝釜绱㈠紩锛屽苟涓旀洿蹇熺殑妫娴嬪嚭閲嶅鐨UNIQUE鍏抽敭瀛椼
路 PRIMARY KEY鍙互鏄竴涓鍒楃储寮曘備絾鏄紝鍦ㄥ垪瑙勭害涓娇鐢PRIMARY KEY鍏抽敭瀛楀睘鎬ф棤娉曞垱寤哄鍒楃储寮曘傝繖涔堝仛鍙兘鎶婁竴涓垪鏍囪涓轰富鍒椼傛偍蹇呴』浣跨敤涓涓崟鐙殑PRIMARY KEY锛index_col_name, ...锛夊瓙鍙ャ
路 濡傛灉PRIMARY KEY鎴UNIQUE绱㈠紩鍙寘鎷竴涓垪锛屽苟涓旀鍒椾负鏁存暟绫诲瀷锛屽垯鎮ㄤ篃鍙互鍦SELECT璇彞涓妸姝ゅ垪浣滀负_rowid寮曠敤銆
路 鍦MySQL涓紝PRIMARY KEY鐨勫悕绉颁负PRIMARY銆傚浜庡叾瀹冪储寮曪紝濡傛灉鎮ㄦ病鏈夎祴浜堝悕绉帮紝鍒欑储寮曡璧嬩簣鐨勫悕绉颁笌绗竴涓凡缂栧叆绱㈠紩鐨勫垪鐨勫悕绉扮浉鍚岋紝骞惰嚜閫夋坊鍔犲悗缂锛_2, _3,...锛夛紝浣垮悕绉颁负鍞竴鍚嶇О銆傛偍鍙互浣跨敤SHOW INDEX FROM tbl_name鏉ユ煡鐪嬭〃鐨勭储寮曞悕绉般傝鍙傝13.5.4.11鑺傦紝鈥淪HOW INDEX璇硶鈥銆
路 閮ㄥ垎瀛樺偍寮曟搸鍏佽鎮ㄥ湪鍒涘缓绱㈠紩鏃舵寚瀹氱储寮曠被鍨嬨index_type鎸囩ず璇彞鐨勮娉曟槸USING type_name銆
绀轰緥锛
CREATE TABLE lookup
(id INT, INDEX USING BTREE (id))
ENGINE = MEMORY;
瑕佷簡瑙f湁鍏USING鐨勮缁嗚鏄庯紝璇峰弬瑙13.1.4鑺傦紝鈥淐REATE INDEX璇硶鈥銆
瑕佷簡瑙f湁鍏MySQL濡備綍浣跨敤绱㈠紩鐨勬洿澶氫俊鎭紝璇峰弬瑙7.4.5鑺傦紝鈥淢ySQL濡備綍浣跨敤绱㈠紩鈥銆
路 鍦MySQL 5.1涓紝鍙湁MyISAM锛InnoDB, BDB鍜MEMORY瀛樺偍寮曟搸鏀寔鍦ㄥ惈鏈NULL鍊肩殑鍒椾腑缂栫储寮曘傚湪鍏跺畠鎯呭喌涓嬶紝鎮ㄥ繀椤诲畾涔夊凡缂栫储寮曠殑鍒椾负NOT NULL锛屽惁鍒欎細鍑虹幇閿欒銆
路 鍦ㄤ竴涓储寮曡绾︿腑浣跨敤col_name(length)璇硶锛屾偍鍙互鍒涘缓涓涓储寮曪紝姝ょ储寮曞彧浣跨敤涓涓CHAR鎴VARCHAR鍒楃殑绗竴涓length瀛楃銆傚彧瀵瑰垪鍊肩殑鍓嶇紑缂栧埗绱㈠紩鍙互浣跨储寮曟枃浠跺ぇ澶у噺灏忋傝鍙傝7.4.3鑺傦紝鈥滃垪绱㈠紩鈥銆
MyISAM鍜InnoDB瀛樺偍寮曟搸涔熸敮鎸佸BLOB鍜TEXT鍒楃紪绱㈠紩銆傚綋瀵BLOB鎴TEXT鍒楃紪绱㈠紩鏃讹紝鎮ㄥ繀椤讳负绱㈠紩鎸囧畾涓涓墠缂闀垮害銆備緥濡傦細
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
瀵逛簬MyISAM鍜InnoDB琛紝鍓嶇紑鏈闀垮彲浠ヤ负1000瀛楄妭锛屽浜庡叾瀹冭〃鏍肩被鍨嬶紝鏈闀垮彲浠ヤ负255瀛楄妭銆傛敞鎰忓墠缂闀垮害闄愬间互瀛楄妭涓哄崟浣嶏紝鑰屽湪CREATE TABLE璇彞涓殑鍓嶇紑闀垮害鐢ㄥ瓧绗︽暟鐩潵琛ㄨ堪銆傚綋涓轰竴涓娇鐢ㄥ瀛楄妭瀛楃闆嗙殑鍒楁寚瀹氬墠缂闀垮害鏃讹紝涓瀹氳鑰冭檻鍒拌繖涓鐐广
路 涓涓index_col_name瑙勭害鍙互浠ASC鎴DESC缁撳熬銆傝繖浜涘叧閿瘝鍙互鍦ㄥ皢鏉ヨ繘琛屾墿灞曪紝鐢ㄤ簬鎸囧畾鍗囧簭鎴栭檷搴忕殑绱㈠紩鍊煎瓨鍌ㄣ傚綋鍓嶏紝杩欎簺鍏抽敭璇嶈鍒嗘瀽浣嗘槸琚拷鐣ワ紱绱㈠紩鍊煎潎浠ュ崌搴忓偍瀛樸
路
褰撴偍鍦SELECT涓殑TEXT鍒楁垨BLOB鍒椾腑浣跨敤ORDER
BY鎴GROUP BY鏃讹紝鏈嶅姟鍣ㄥ彧浣跨敤鍒濆鐨勫瓧鑺傛暟鐩鍊艰繘琛屽垎绫汇傚瓧鑺傛暟鐩敱max_sort_length绯荤粺鍙橀噺杩涜鎸囩ず銆傝鍙傝11.4.3鑺傦紝鈥淏LOB鍜孴EXT绫诲瀷鈥銆
路 鎮ㄥ彲浠ュ垱寤虹壒娈婄殑FULLTEXT绱㈠紩锛岀敤浜庡叏鏂囨悳绱€傚彧鏈MyISAM琛ㄧ被鍨嬫敮鎸FULLTEXT绱㈠紩銆FULLTEXT绱㈠紩鍙彲浠ヤ粠CHAR, VARCHAR鍜TEXT鍒椾腑鍒涘缓銆傛暣涓垪閮戒細琚紪鍏ョ储寮曪紱涓嶆敮鎸佸閮ㄥ垎鍒楃紪绱㈠紩銆傚鏋滃凡鎸囧畾锛屽墠缂闀垮害浼氳蹇界暐銆傝浜嗚В杩愯鐨勮缁嗚鏄庯紝璇峰弬瑙12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解銆
路 鎮ㄥ彲浠ヤ负绌洪棿鍒楃被鍨嬪垱寤SPATIAL绱㈠紩銆傚彧鏈MyISAM琛ㄦ敮鎸佺┖闂寸被鍨嬶紝宸茬紪绱㈠紩鐨勫垪蹇呴』澹版槑涓NOT NULL銆傝鍙傝绗19绔狅細MySQL涓殑绌洪棿鎵╁睍銆
路 InnoDB琛ㄦ敮鎸佸澶栭敭闄愬埗鏉′欢杩涜妫鏌ャ傝鍙傝15.2鑺傦紝鈥淚nnoDB瀛樺偍寮曟搸鈥銆傛敞鎰忥紝鍦InnoDB涓紝FOREIGN KEY璇硶姣旀湰鑺傚紑濮嬫椂浠嬬粛鐨CREATE TABLE璇彞鐨勮娉曟洿涓ユ牸锛氳寮曠敤鐨勮〃涓殑鍒楀繀椤绘湁鏄庣‘鐨勫懡鍚嶃InnoDB鏀寔澶栭敭鐨ON DELETE鍜ON UPDATE涓ょ鎿嶄綔銆傛湁鍏崇簿纭娉曠殑璇存槑锛岃鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥銆
瀵逛簬鍏跺畠瀛樺偍寮曟搸锛MySQL鏈嶅姟鍣ㄥCREATE TABLE璇彞涓殑FOREIGN KEY鍜REFERENCES璇硶杩涜鍒嗘瀽锛屼絾涓嶉噰鍙栬繘涓姝ョ殑琛屽姩銆傛墍鏈夌殑瀛樺偍寮曟搸鍧囧CHECK瀛愬彞杩涜鍒嗘瀽锛屼絾鏄拷鐣CHECK瀛愬彞銆傝鍙傝1.8.5.5鑺傦紝鈥滃閿銆
路 瀵逛簬MyISAM琛紝姣忎釜NULL鍒楄澶氬崰鐢ㄤ竴浣嶏紝杩涗綅鍒拌窛绂绘渶杩戠殑瀛楄妭銆傛渶澶ц褰曢暱搴︼紙浠ュ瓧鑺備负鍗曚綅锛夋寜鐓у涓嬫柟娉曡绠楋細
路 row length = 1
路 + (sum of column lengths)
路 + (number of NULL columns + delete_flag + 7)/8
路 + (number of variable-length columns)
瀵逛簬閲囩敤闈欐佽褰曟牸寮忕殑琛紝delete_flag涓1銆傞潤鎬佽〃鍦ㄨ璁板綍涓娇鐢ㄤ竴浣嶇敤浣滀綅鏍囪銆備綅鏍囪鎸囩ず璇ヨ鏄惁宸茶鍒犻櫎銆傚浜庡姩鎬佽〃锛delete_flag涓0锛屽洜涓哄湪鍔ㄦ佽鏍囬涓凡瀛樺偍浜嗕綅鏍囪銆
杩欎簺璁$畻鏂规硶涓嶉傜敤浜InnoDB琛ㄣ傚浜InnoDB琛紝NULL鍒楃殑瀛樺偍閲忎笌NOT NULL鍒楃殑瀛樺偍閲忔病鏈夊尯鍒
ENGINE鍜TYPE閫夐」鐢ㄤ簬涓鸿〃鎸囧畾瀛樺偍寮曟搸銆ENGINE鏄閫夌殑閫夐」鍚嶇О銆
ENGINE鍜TYPE閫夐」閲囩敤浠ヤ笅鍊硷細
瀛樺偍寮曟搸 |
璇存槑 |
ARCHIVE |
妗f瀛樺偍寮曟搸銆傝鍙傝15.8鑺傦紝鈥淎RCHIVE瀛樺偍寮曟搸鈥銆 |
BDB |
甯﹂〉闈㈤攣瀹氱殑浜嬪姟瀹夊叏琛ㄣ備篃绉颁负BerkeleyDB銆傝鍙傝15.5鑺傦紝鈥淏DB (BerkeleyDB)瀛樺偍寮曟搸鈥銆 |
CSV |
鍊间箣闂寸敤閫楀彿闅斿紑鐨勮〃銆傝鍙傝15.9鑺傦紝鈥淐SV瀛樺偍寮曟搸銆 |
EXAMPLE |
绀轰緥寮曟搸銆傝鍙傝15.6鑺傦紝鈥淓XAMPLE瀛樺偍寮曟搸鈥銆 |
FEDERATED |
鍙互璁块棶杩滅▼琛ㄧ殑瀛樺偍寮曟搸銆傝鍙傝15.7鑺傦紝鈥淔EDERATED瀛樺偍寮曟搸鈥銆 |
HEAP |
|
(OBSOLETE) ISAM |
鍦MySQL 5.1涓病鏈夋寮曟搸銆傚鏋滄偍瑕佷粠浠ュ墠鐨勭増鏈崌绾у埌MySQL 5.1锛屾偍搴旇鍦ㄨ繘琛屽崌绾у墠鎶婂師鏈夌殑ISAM琛ㄨ浆鎹负MyISAM琛ㄣ傝鍙傝绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷銆 |
InnoDB |
甯﹁閿佸畾鍜屽閿殑浜嬪姟瀹夊叏琛ㄣ傝鍙傝15.2鑺傦紝鈥淚nnoDB瀛樺偍寮曟搸鈥銆 |
MEMORY |
鏈〃绫诲瀷鐨勬暟鎹彧淇濆瓨鍦ㄥ瓨鍌ㄥ櫒閲屻傦紙鍦ㄦ棭鏈MySQL鐗堟湰涓绉颁负HEAP銆傦級 |
MERGE |
MyISAM琛ㄧ殑闆嗗悎锛屼綔涓轰竴涓〃浣跨敤銆備篃绉颁负MRG_MyISAM銆傝鍙傝15.3鑺傦紝鈥淢ERGE瀛樺偍寮曟搸鈥銆 |
MyISAM |
浜岃繘鍒惰交渚垮紡瀛樺偍寮曟搸锛屾寮曟搸鏄MySQL鎵鐢ㄧ殑榛樿瀛樺偍寮曟搸銆傝鍙傝15.1鑺傦紝鈥淢yISAM瀛樺偍寮曟搸鈥銆 |
NDBCLUSTER |
瑕佷簡瑙f湁鍏MySQL瀛樺偍寮曟搸鐨勬洿澶氫俊鎭紝璇峰弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷銆
濡傛灉琚寚瀹氱殑瀛樺偍寮曟搸鏃犳硶鍒╃敤锛屽垯MySQL浣跨敤MyISAM浠f浛銆備緥濡傦紝涓涓〃瀹氫箟鍖呮嫭ENGINE=BDB閫夐」锛屼絾鏄MySQL鏈嶅姟鍣ㄤ笉鏀寔BDB琛紝鍒欒〃琚垱寤轰负MyISAM琛ㄣ傝繖鏍凤紝濡傛灉鎮ㄥ湪涓绘満涓婃湁浜嬪姟琛紝浣嗗湪浠庡睘鏈轰笂鍒涘缓鐨勬槸闈炰氦浜掑紡琛紙浠ュ姞蹇熷害锛夋椂锛屽彲浠ヨ繘琛屽鍒惰缃傚湪MySQL 5.1涓紝濡傛灉娌℃湁閬靛畧瀛樺偍寮曟搸瑙勭害锛屽垯浼氬嚭鐜拌鍛娿
鍏跺畠琛ㄩ夐」鐢ㄤ簬浼樺寲琛ㄧ殑鎬ц川銆傚湪澶氭暟鎯呭喌涓嬶紝鎮ㄤ笉蹇呮寚瀹氳〃閫夐」銆傝繖浜涢夐」閫傜敤浜庢墍鏈夊瓨鍌ㄥ紩鎿庯紝鍙︽湁璇存槑闄ゅ锛
路 AUTO_INCREMENT
琛ㄧ殑鍒濆AUTO_INCREMENT鍊笺傚湪MySQL 5.1涓紝鏈夐」鍙傜敤浜MyISAM鍜MEMORY琛ㄣInnoDB涔熸敮鎸佹湰閫夐」銆傚鏋滃紩鎿庝笉鏀寔AUTO_INCREMENT琛ㄩ夐」锛屽垯瑕佽缃紩鎿庣殑绗竴涓auto-increment鍊硷紝闇鎻掑叆涓涓滃亣鈥濊銆傝琛岀殑鍊兼瘮鍒涘缓琛ㄥ悗鐨勫煎皬涓锛岀劧鍚庡垹闄よ鍋囪銆
瀵逛簬鍦CREATE TABLE璇彞涓敮鎸AUTO_INCREMENT琛ㄩ夐」鐨勫紩鎿庯紝鎮ㄤ篃鍙互浣跨敤ALTER TABLE tbl_name AUTO_INCREMENT = n鏉ラ噸鏂拌缃AUTO_INCREMENT鍊笺
路 AVG_ROW_LENGTH
琛ㄤ腑骞冲潎琛岄暱搴︾殑杩戜技鍊笺傚彧闇瑕佸鍚昂瀵稿彲鍙樼殑璁板綍鐨勫ぇ鍨嬭〃杩涜姝ら」璁剧疆銆
褰撳垱寤轰竴涓MyISAM琛ㄦ椂锛MySQL浣跨敤MAX_ROWS鍜AVG_ROW_LENGTH閫夐」鐨勪箻绉潵纭畾寰楀嚭鐨勮〃鏈夊澶с傚鏋滄湁涓涓夐」鏈寚瀹氾紝鍒欒〃鐨勬渶澶у昂瀵镐负65,536TB鏁版嵁銆傦紙濡傛灉鎿嶄綔绯荤粺涓嶆敮鎸佽繖涔堝ぇ鐨勬枃浠讹紝鍒欒〃鐨勫昂瀵歌闄愬畾鍦ㄦ搷浣滅郴缁熺殑闄愬煎銆傦級濡傛灉鎮ㄦ兂缂╁皬鎸囬拡灏哄浣跨储寮曟洿灏忥紝閫熷害鏇村揩锛屽苟涓旀偍涓嶉渶瑕佸ぇ鏂囦欢锛屽垯鎮ㄥ彲浠ラ氳繃璁剧疆myisam_data_pointer_size绯荤粺鍙橀噺鏉ュ噺灏戦粯璁ゆ寚閽堢殑灏哄銆傦紙瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆傦級濡傛灉鎮ㄥ笇鏈涙墍鏈夌殑琛ㄥ彲浠ユ墿澶э紝瓒呰繃榛樿闄愬硷紝骞朵笖鎰挎剰璁╄〃绋嶅井鎱㈢偣锛屽苟绋嶅井澶х偣锛屽垯鎮ㄥ彲浠ラ氳繃璁剧疆姝ゅ彉閲忓鍔犻粯璁ゆ寚閽堢殑灏哄銆
路 [DEFAULT] CHARACTER SET
鐢ㄤ簬涓鸿〃鎸囧畾涓涓粯璁ゅ瓧绗﹂泦銆CHARSET鏄CHARACTER SET鐨勫悓涔夎瘝銆
瀵逛簬CHARACTER SET.
路 COLLATE
鐢ㄤ簬涓鸿〃鎸囧畾涓涓粯璁ゆ暣搴忋
路 CHECKSUM
濡傛灉鎮ㄥ笇鏈MySQL闅忔椂瀵规墍鏈夎杩涜瀹炴椂妫楠屾眰鍜岋紙涔熷氨鏄紝琛ㄥ彉鏇村悗锛MySQL鑷姩鏇存柊妫楠屾眰鍜岋級锛屽垯搴旀妸姝ら」璁剧疆涓1銆傝繖鏍峰仛锛岃〃鐨勬洿鏂伴熷害浼氱暐寰參浜涳紝浣嗘槸鏇村鏄撳鎵惧埌鍙楁崯鐨勮〃銆CHECKSUM TABLE璇彞鐢ㄤ簬鎶ュ憡妫楠屾眰鍜岋紙浠呴檺浜MyISAM锛夈
路 COMMENT
琛ㄧ殑娉ㄩ噴锛屾渶闀60涓瓧绗︺
路 CONNECTION
FEDERATED琛ㄧ殑杩炴帴瀛楃涓层傦紙 娉ㄩ噴锛氳緝鏃╃増鏈殑MySQL浣跨敤COMMENT閫夐」鐢ㄤ簬杩炴帴瀛楃涓层
路 MAX_ROWS
鎮ㄦ墦绠楀偍瀛樺湪琛ㄤ腑鐨勮鏁扮洰鐨勬渶澶у笺傝繖涓嶆槸涓涓‖鎬ч檺鍊硷紝鑰屾洿鍍忎竴涓寚绀鸿鍙ワ紝鎸囩ず鍑鸿〃蹇呴』鑳藉瓨鍌ㄨ嚦灏戣繖涔堝琛屻
路 MIN_ROWS
鎮ㄦ墦绠楀瓨鍌ㄥ湪琛ㄤ腑鐨勮鏁扮洰鐨勬渶灏忓笺
路 PACK_KEYS
濡傛灉鎮ㄥ笇鏈涚储寮曟洿灏忥紝鍒欐妸姝ら夐」璁剧疆涓1銆傝繖鏍峰仛閫氬父浣挎洿鏂伴熷害鍙樻參锛屽悓鏃堕槄璇婚熷害鍔犲揩銆傛妸閫夐」璁剧疆涓0鍙互鍙栨秷鎵鏈夌殑鍏抽敭瀛楀帇缂┿傛妸姝ら夐」璁剧疆涓DEFAULT鏃讹紝瀛樺偍寮曟搸鍙帇缂╅暱鐨CHAR鎴VARCHAR鍒楋紙浠呴檺浜MyISAM锛夈
濡傛灉鎮ㄤ笉浣跨敤PACK_KEYS锛屽垯榛樿鎿嶄綔鏄彧鍘嬬缉瀛楃涓诧紝浣嗕笉鍘嬬缉鏁板瓧銆傚鏋滄偍浣跨敤PACK_KEYS=1锛屽垯瀵规暟瀛椾篃杩涜鍘嬬缉銆
鍦ㄥ浜岃繘鍒舵暟瀛楀叧閿瓧杩涜鍘嬬缉鏃讹紝MySQL閲囩敤鍓嶇紑鍘嬬缉锛
o 姣忎釜鍏抽敭瀛楅渶瑕佷竴涓澶栫殑瀛楄妭鏉ユ寚绀哄墠涓涓叧閿瓧涓湁澶氬皯瀛楄妭涓庝笅涓涓叧閿瓧鐩稿悓銆
o 鎸囧悜琛岀殑鎸囬拡浠ラ珮浣嶅瓧鑺備紭鍏堢殑椤哄簭瀛樺偍鍦ㄥ叧閿瓧鐨勫悗闈紝鐢ㄤ簬鏀硅繘鍘嬬缉鏁堟灉銆
杩欐剰鍛崇潃锛屽鏋滀袱涓繛缁涓湁璁稿鐩稿悓鐨勫叧閿瓧锛屽垯鍚庣画鐨勨滅浉鍚屸濈殑鍏抽敭瀛楅氬父鍙崰鐢ㄤ袱涓瓧鑺傦紙鍖呮嫭鎸囧悜琛岀殑鎸囬拡锛夈備笌姝ょ浉姣旓紝甯歌鎯呭喌涓嬶紝鍚庣画鐨勫叧閿瓧鍗犵敤storage_size_for_key + pointer_size锛堟寚閽堝昂瀵搁氬父涓4锛夈備絾鏄紝鍙湁鍦ㄨ澶氭暟瀛楃浉鍚岀殑鎯呭喌涓嬶紝鍓嶇紑鍘嬬缉鎵嶆湁濂藉銆傚鏋滄墍鏈夌殑鍏抽敭瀛楀畬鍏ㄤ笉鍚岋紝骞朵笖鍏抽敭瀛椾笉鑳藉惈鏈NULL鍊硷紝鍒欐瘡涓叧閿瓧瑕佸浣跨敤涓涓瓧鑺傘傦紙鍦ㄨ繖绉嶆儏鍐典腑锛屽偍瀛樺帇缂╁悗鐨勫叧閿瓧鐨勯暱搴︾殑瀛楄妭涓庣敤浜庢爣璁板叧閿瓧鏄惁涓NULL鐨勫瓧鑺傛槸鍚屼竴瀛楄妭銆傦級
路 PASSWORD
浣跨敤瀵嗙爜瀵.frm鏂囦欢鍔犲瘑銆傚湪鏍囧噯MySQL鐗堟湰涓紝鏈夐」涓嶈捣浠讳綍浣滅敤銆
路 DELAY_KEY_WRITE
濡傛灉鎮ㄦ兂瑕佸欢杩熷鍏抽敭瀛楃殑鏇存柊锛岀瓑鍒拌〃鍏抽棴鍚庡啀鏇存柊锛屽垯鎶婃椤硅缃负1锛堜粎闄愪簬MyISAM锛夈
路 ROW_FORMAT
瀹氫箟鍚勮搴斿浣曞偍瀛樸傚綋鍓嶏紝姝ら夐」鍙傜敤浜MyISAM琛ㄣ傚浜庨潤鎬佽鎴栭暱搴﹀彲鍙樿锛屾閫夐」鍊煎彲浠ヤ负FIXED鎴DYNAMIC銆myisampack鐢ㄤ簬鎶婄被鍨嬭缃负COMPRESSED銆傝鍙傝15.1.3鑺傦紝鈥淢yISAM琛ㄧ殑瀛樺偍鏍煎紡鈥銆
鍦ㄩ粯璁ゆ儏鍐典笅锛InnoDB璁板綍浠ュ帇缂╂牸寮忓瓨鍌紙ROW_FORMAT=COMPACT锛夈傞氳繃鎸囧畾ROW_FORMAT=REDUNDANT锛屼粛鐒跺彲浠ョ敵璇风敤浜庤緝鏃╃増鏈殑MySQL涓殑闈炲帇缂╂牸寮忋
路 RAID_TYPE
鍦MySQL 5.0涓,RAID鏀寔琚垹闄や簡銆傝浜嗚В鏈夊叧RAID鐨勮鏄庯紝璇峰弬瑙http://dev.mysql.com/doc/refman/4.1/en/create-table.html銆
路 UNION
褰撴偍鎯宠鎶婁竴缁勭浉鍚岀殑琛ㄥ綋浣滀竴涓〃浣跨敤鏃讹紝閲囩敤UNION銆UNION浠呴傜敤浜MERGE琛ㄣ傝鍙傝15.3鑺傦紝鈥淢ERGE瀛樺偍寮曟搸鈥銆
瀵逛簬鎮ㄦ槧灏勫埌涓涓MERGE琛ㄤ笂鐨勮〃锛屾偍蹇呴』鎷ユ湁SELECT, UPDATE鍜DELETE鏉冮檺銆傦紙娉ㄩ噴锛氫互鍓嶏紝鎵鏈夎浣跨敤鐨勮〃蹇呴』浣嶄簬鍚屼竴涓暟鎹簱涓紝骞朵綔涓MERGE琛ㄣ傝繖浜涢檺鍒朵笉鍐嶉傜敤銆傦級
路 INSERT_METHOD
濡傛灉鎮ㄥ笇鏈涘湪MERGE琛ㄤ腑鎻掑叆鏁版嵁锛屾偍蹇呴』鐢INSERT_METHOD鎸囧畾搴旀彃鍏ヨ鐨勮〃銆INSERT_METHOD閫夐」浠呯敤浜MERGE琛ㄣ備娇鐢FIRST鎴LAST鎶婅鎻掑叆鍒扮涓涓垨鏈鍚庝竴涓〃涓紱鎴栬呬娇鐢NO锛岄樆姝㈡彃鍏ヨ銆傝鍙傝15.3鑺傦紝鈥淢ERGE瀛樺偍寮曟搸鈥銆
路 DATA DIRECTORY, INDEX DIRECTORY
閫氳繃浣跨敤DATA DIRECTORY='directory'鎴INDEX DIRECTORY='directory'锛屾偍鍙互鎸囧畾MyISAM瀛樺偍寮曟搸鏀剧疆琛ㄦ牸鏁版嵁鏂囦欢鍜岀储寮曟枃浠剁殑浣嶇疆銆傛敞鎰忥紝鐩綍搴旀槸閫氬悜鐩綍鐨勫畬鏁磋矾寰勶紙涓嶆槸鐩稿璺緞锛夈
浠呭綋鎮ㄦ病鏈変娇鐢--skip-symbolic-links閫夐」鏃讹紝DATA DIRECTORY, INDEX DIRECTORY鎵嶈兘浣跨敤銆傛搷浣滅郴缁熷繀椤绘湁涓涓鍦ㄥ伐浣滅殑銆佺嚎绋嬪畨鍏ㄧ殑realpath()璋冪敤銆傝浜嗚В鍏ㄩ潰淇℃伅锛岃鍙傝7.6.1.2鑺傦紝鈥滃湪Unix骞冲彴涓婁娇鐢ㄨ〃鐨勭鍙烽摼鎺鈥濄
路 瀵逛簬鐢CREATE TABLE鍒涘缓鐨勮〃锛屽彲浠ヤ娇鐢partition_options鎺у埗鍒嗗尯銆傚鏋滀娇鐢ㄤ簡partition_options锛屽垯鍏朵腑蹇呴』鍖呭惈鑷冲皯涓涓PARTITION BY瀛愬彞銆傛湰瀛愬彞鍖呭惈鐢ㄤ簬纭畾鍒嗗尯鐨勫嚱鏁帮紱璇ュ嚱鏁颁細杩斿洖涓涓暣鍊硷紝鑼冨洿浠1鍒num銆傛澶num涓哄垎鍖虹殑鏁扮洰銆傛鍑芥暟涓彲浠ヤ娇鐢ㄧ殑閫夐」鏄剧ず鍦ㄤ笅闈㈢殑娓呭崟涓 瑕佺偣锛氬湪鏈妭寮濮嬫椂浠嬬粛鐨勭敤浜partition_options鐨勮娉曚腑鏄剧ず鐨勯夐」锛屽苟涓嶆槸閮借兘鐢ㄤ簬鎵鏈夊垎鍖虹被鍨嬨傝浜嗚В鍚勭绫诲瀷鍏蜂綋鐨勪俊鎭 锛岃鍙傝浠ヤ笅鍚勭被鍨嬬殑娓呭崟銆傝浜嗚В鏈夊叧鍦MySQL涓殑鍒嗗尯鐨勬搷浣滃拰浣跨敤鎯呭喌鐨勫叏闈㈣鏄庯紝浠ュ強瑕佷簡瑙h〃鍒涘缓鐨勭ず渚嬪拰涓MySQL鍒嗗尯鏈夊叧鐨勫叾瀹冨懡浠わ紝璇峰弬瑙绗18绔狅細鍒嗗尯銆
o HASH锛expr锛夛細鐢ㄤ簬娣风紪涓涓垨澶氫釜鍒楋紝鍒涘缓涓涓叧閿瓧锛岀敤浜庢斁缃锛屽苟纭畾琛岀殑浣嶇疆銆expr鏄竴涓〃杈惧紡锛屼娇鐢ㄤ竴涓垨澶氫釜琛ㄤ腑鐨勫垪銆傝琛ㄨ揪寮忓彲浠ユ槸浠讳綍鑳藉鐢熸垚鍗曚竴鏁村肩殑鍚堟硶鐨MySQL琛ㄨ揪寮忥紙鍖呮嫭MySQL鍑芥暟锛夈備緥濡傦紝杩欎簺閮芥槸鏈夋晥鐨CREATE TABLE璇彞锛岃鍙ヤ腑浣跨敤浜PARTITION BY HASH锛
o CREATE TABLE t1 (col1 INT, col2 CHAR(5))
o PARTITION BY HASH(col1);
o
o CREATE TABLE t1 (col1 INT, col2 CHAR(5))
o PARTITION BY HASH( ORD(col2) );
o
o CREATE TABLE t1 (col1 INT, col2 CHAR(5), col3 DATETIME)
o PARTITION BY HASH ( YEAR(col3) );
VALUES LESS THAN鎴VALUES IN瀛愬彞涓嶈兘鍜PARTITION BY HASH涓璧蜂娇鐢ㄣ
PARTITION BY HASH浣跨敤expr琚垎鍖烘暟鐩墍闄ゅ悗鐨勪綑鏁帮紙涔熷氨鏄ā鏁帮級銆傝浜嗚В绀轰緥鍜屽叾瀹冧俊鎭紝璇峰弬瑙18.2.3鑺傦紝鈥淗ASH鍒嗗尯鈥銆
LENEAR鍏抽敭璇嶉渶瑕佷竴绉嶄笉鍚岀殑绠楁硶銆傚湪杩欑鎯呭喌涓嬶紝閫氳繃涓娆℃垨澶氭閫昏緫AND杩愮畻寰楀嚭鐨勭粨鏋滐紝璁$畻鍑哄瓨鍌ㄨ褰曠殑鍒嗗尯鐨勬暟鐩傝浜嗚В绾垮舰娣风紪鐨勮璁哄拰绀轰緥锛岃鍙傝18.2.3.1鑺傦紝鈥淟INEAR HASH鍒嗗尯鈥銆
o KEY(column_list)锛氫笌HASH杩戜技锛岄櫎浜嗘湁涓鐐逛笉涓鏍凤紝鍗MySQL鎻愪緵浜嗘贩缂栧嚱鏁帮紝浠ヤ繚璇佸潎鍖鐨勬暟鎹垎甯冦column_list鑷彉閲忓彧鏄悇鍒楃殑涓涓竻鍗曘傛湰绀轰緥鏄剧ず浜嗙敱鍏抽敭瀛楄繘琛屽垎鍖虹殑涓涓畝鍗曠殑琛紝鍒嗕负4涓垎鍖猴細
o CREATE TABLE tk (col1 INT, col2 CHAR(5), col3 DATE)
o PARTITION BY KEY(col3)
o PARTITIONS 4;
閲囩敤LINEAR鍏抽敭璇嶏紝鎮ㄥ彲浠ュ鐢卞叧閿瓧鍒嗗尯鐨勮〃杩涜绾垮舰鍒嗗尯銆傝繖涓庣敱HASH杩涜鍒嗗尯鐨勮〃鏍兼湁鍚屾牱鐨勬晥鏋滐紱涔熷氨鏄锛屼娇鐢&鎿嶄綔绗︽煡鎵惧垎鍖烘暟鐩紝鑰屼笉鏄娇鐢ㄦā鏁帮紙璇︾粏璇存槑瑙18.2.3.1鑺傦紝鈥淟INEAR HASH鍒嗗尯鈥鍜18.2.4鑺傦紝鈥淜EY鍒嗗尯鈥锛夈傛湰绀轰緥閲囩敤浜嗗叧閿瓧绾垮舰鍒嗗尯锛岀敤鏉ュ湪5涓垎鍖轰箣闂村垎閰嶆暟鎹細
CREATE TABLE tk (col1 INT, col2 CHAR(5), col3 DATE)
PARTITION BY LINEAR KEY(col3)
PARTITIONS 5;
VALUES LESS THAN鎴VALUES IN瀛愬彞涓嶈兘鍜PARTITION BY KEY涓璧蜂娇鐢ㄣ
o RANGE锛氬湪姝ゆ儏鍐典笅锛expr浣跨敤涓濂VALUES LESS THAN鎿嶄綔绗鏄剧ず浜嗘煇涓鑼冨洿鍐呯殑鍊笺傚綋浣跨敤鑼冨洿鍒嗗尯鏃讹紝鎮ㄥ繀椤讳娇鐢VALUES LESS THAN瀹氫箟鑷冲皯涓涓垎鍖恒VALUES IN涓嶈兘鍜岃寖鍥村垎鍖轰竴璧蜂娇鐢ㄣ
VALUES LESS THAN鍙互涓庝竴涓枃瀛楀煎悓鏃朵娇鐢紝鎴栬呬笌涓涓彲浠ユ眰绠楀崟涓鍊肩殑琛ㄨ揪寮忓悓鏃朵娇鐢ㄣ
涓句緥璇存槑锛屽亣璁炬偍鏈変竴涓〃锛屾偍甯屾湜閲囩敤浠ヤ笅鏂规硶瀵瑰寘鍚勾浠藉肩殑涓鍒楄繘琛屽垎鍖猴細
鍒嗗尯缂栧彿锛 |
骞翠唤鑼冨洿锛 |
0 |
1990浠ュ墠 |
1 |
1991 - 1994 |
2 |
1995 - 1998 |
3 |
1999 - 2002 |
4 |
2003 - 2005 |
5 |
2006骞翠互鍚 |
閲囩敤杩欑鍒嗗尯鏂规硶鐨勮〃鍙互閫氳繃濡備笅CREATE TABLE璇彞瀹炵幇锛
CREATE TABLE t1 (
year_col INT,
some_data INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999),
PARTITION p3 VALUES LESS THAN (2002),
PARTITION p4 VALUES LESS THAN (2006),
PARTITION p5 VALUES LESS THAN MAXVALUE
);
PARTITION ... VALUES LESS THAN ...璇彞鎸夐『搴忔墽琛屻VALUES LESS THAN MAXVALUE鐨勪綔鐢ㄦ槸鎸囧畾澶т簬鏈澶у肩殑鈥滃叾浣欌濈殑鍊笺
娉ㄦ剰锛VALUES LESS THAN瀛愬彞鎸夐『搴忔墽琛岋紝鎵ц鏂瑰紡绫讳技浜switch ... case璇鐨勪竴閮ㄥ垎锛堣澶氱紪绋嬭瑷锛屽C, Java鍜PHP涔熷姝わ級銆備篃灏辨槸璇达紝瀛愬彞蹇呴』鎸夌収杩欐牱涓绉嶆柟娉曟帓鍒楋紝姣忎竴涓悗缁殑VALUES LESS THAN涓寚瀹氱殑涓婇檺鍊煎ぇ浜庡墠涓涓VALUES LESS THAN涓寚瀹氱殑涓婇檺鍊硷紝骞跺湪娓呭崟鐨勬渶鍚庡姞涓涓弬鐓фх殑MAXVALUE銆
VALUES IN涓庝竴绯诲垪鐨勫煎悓鏃朵娇鐢ㄣ備妇渚嬭鏄庯紝鎮ㄥ彲浠ュ垱寤哄涓嬬殑鍒嗗尯鏂规硶锛
CREATE TABLE client_firms (
id INT,
name VARCHAR(35)
)
PARTITION BY RANGE (id) (
PARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21),
PARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22),
PARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23),
PARTITION r3 VALUES IN (4, 8, 12, 16, 20, 24)
);
褰撳墠锛屼笌VALUES IN...鍚屾椂浣跨敤鐨勫煎繀椤诲彧鍖呭惈鏁存暟鍊笺
锛堝洜涓烘琛ㄥ彧浣跨敤VALUES IN琛ㄨ揪寮忚繘琛屽垎鍖猴紝鎮ㄤ篃鍙互鐢PARTITION BY LIST浠f浛锛岃屼笉鏄娇鐢PARTITION BY RANGE銆傝鍙傝涓嬩竴鏉°傦級
鍦ㄤ娇鐢VALUES LESS THAN鎴VALUES IN鎯呭喌涓嬶紝姣忎釜鍒嗗尯浣跨敤PARTITION name瀹氫箟锛屾澶name鏄垎鍖虹殑鏍囪瘑鍚嶏紝鍚庨潰鎺VALUES...瀛愬彞銆
o LIST(expr)锛氬綋鏍规嵁鍚湁涓绯诲垪闄愬畾鎬у硷紙渚嬪宸炰唬鐮佹垨鍥藉浠g爜锛夌殑鍒楄繘琛屽垎鍖烘椂浣跨敤銆傚湪杩欑鎯呭喌涓嬶紝鎵鏈変笌鐗瑰畾鐨勫窞鎴栧浗瀹舵湁鍏崇殑璁板綍閮借鍒嗛厤鍒颁竴涓崟涓鍒嗗尯涓紝鎴栬呭彲浠ラ鐣欏嚭涓涓垎鍖猴紝鐢ㄤ簬涓绯诲垪鐗瑰畾鐨勫窞鎴栧浗瀹躲LIST(expr)涓RANGE绫讳技锛岄櫎浜嗕竴鐐逛互澶栵紝鍗冲彧鏈VALUES IN鍙互琚敤浜庝负姣忎釜鍒嗗尯鎸囧畾鍊笺
褰撲娇鐢ㄦ竻鍗曞垎鍖烘椂锛屾偍蹇呴』浣跨敤VALUES IN瀹氫箟鑷冲皯涓涓垎鍖恒VALUES LESS THAN涓嶈兘涓PARTITION BY LIST涓璧蜂娇鐢ㄣ
o 鍒嗗尯鏁扮洰鍙互浣跨敤PARTITION num瀛愬彞锛岃嚜閫夎繘琛屾寚瀹氾紝姝ゅ锛num鏄垎鍖虹殑鏁扮洰銆傚鏋滄湰瀛愬彞鍜屽叾瀹PARTITION瀛愬彞鍚屾椂浣跨敤锛屽垯num蹇呴』涓庝娇鐢PARTITION瀛愬彞璇存槑鐨勫垎鍖虹殑鎬绘暟鐩哥瓑銆
娉ㄩ噴锛氫笉璁烘偍鍦ㄥ垱寤轰竴涓敱RANGE鎴LIST杩涜鍒嗗尯鐨勮〃鏃舵槸鍚︿娇鐢ㄤ簡PARTITIONS瀛愬彞锛屾偍蹇呴』鍦ㄨ〃瀹氫箟涓寘鎷嚦灏戜竴涓PARTITION VALUES锛堣鍚庯級銆
o 涓涓垎鍖哄彲浠ヨ嚜閫夊垎闅旀垚澶氫釜瀛愬垎鍖恒備娇鐢ㄨ嚜閫夌殑SUBPARTITION BY瀛愬彞鍙互鎸囩ず銆傚瓙鍒嗗尯鍙互鐢HASH鎴KEY杩涜鍒嗛殧銆備袱绉嶆柟娉曞缓绔嬬殑瀛愬垎鍖哄潎涓LINEAR銆傚垎闅斿瓙鍒嗗尯鏃剁殑鎿嶄綔鏂瑰紡涓庝互鍓嶆弿杩扮殑鍒嗗尯绫诲瀷鐨勬搷浣滄柟寮忎竴鏍枫傦紙鏃犳硶鐢LIST鎴RANGE杩涜瀛愬垎鍖哄垎闅斻傦級
浣跨敤SUBPARTITIONS鍏抽敭璇嶏紝鍚庨潰鎺ヤ竴涓暣鍊硷紝鍙互瀵瑰瓙鍒嗗尯鐨勬暟鐩繘琛屾寚绀恒
路 浣跨敤涓涓partition_definition瀛愬彞鍙互瀵规瘡涓垎鍖哄垎鍒繘琛屽畾涔夈備笅闈㈡槸缁勬垚杩欎釜瀛愬彞鐨勫悇涓儴鍒嗭細
o PARTITION partition_name锛氱敤浜庝负鍒嗗尯鎸囧畾涓涓昏緫鍚嶇О銆
o VALUE瀛愬彞锛氬浜庤寖鍥村垎鍖猴紝姣忎釜鍒嗗尯蹇呴』鍖呮嫭涓涓VALUES LESS THAN瀛愬彞锛涘浜庢竻鍗曞垎鍖猴紝鎮ㄥ繀椤讳负姣忎釜鍒嗗尯鎸囧畾涓涓VALUES IN瀛愬彞銆傛湰瀛愬彞鐢ㄤ簬纭畾鍝簺琛屽皢琚瓨鍌ㄥ埌姝ゅ垎鍖轰腑銆傝浜嗚В璇硶绀轰緥锛岃鍙傝绗18绔狅細鍒嗗尯涓鍒嗗尯绫诲瀷鐨勮璁恒
o 鑷夌殑COMMENT瀛愬彞鍙互鐢ㄤ簬鎻忚堪鍒嗗尯銆傛敞閲婂繀椤诲姞鍗曞紩鍙枫備妇渚嬭鏄庯細
o COMMENT = 'Data for the years previous to 1999'
o DATA DIRECTORY鍜INDEX DIRECTORY鍙互琚敤浜庢寚绀烘湰鍒嗗尯鐨勬暟鎹拰绱㈠紩鍚勮嚜鐨勫瓨鍌ㄤ綅缃殑鐩綍銆data_dir鍜index_dir閮藉繀椤绘槸缁濆绯荤粺璺緞銆備緥濡傦細
o CREATE TABLE th (id INT, name VARCHAR(30), adate DATE)
o PARTITION BY LIST(YEAR(adate))
o (
o PARTITION p1999 VALUES IN (1995, 1999, 2003) DATA DIRECTORY = '/var/appdata/95/data' INDEX DIRECTORY = '/var/appdata/95/idx',
o PARTITION p2000 VALUES IN (1996, 2000, 2004) DATA DIRECTORY = '/var/appdata/96/data' INDEX DIRECTORY = '/var/appdata/96/idx',
o PARTITION p2001 VALUES IN (1997, 2001, 2005) DATA DIRECTORY = '/var/appdata/97/data' INDEX DIRECTORY = '/var/appdata/97/idx',
o PARTITION p2000 VALUES IN (1998, 2002, 2006) DATA DIRECTORY = '/var/appdata/98/data' INDEX DIRECTORY = '/var/appdata/98/idx'
);
DATA DIRECTORY鍜INDEX DIRECTORY鐨勬搷浣滄柟娉曚笌CREATE TABLE璇彞涓殑table_option瀛愬彞鐨勬搷浣滄柟娉曚竴鏍枫傛table_option瀛愬彞鐢ㄤ簬浣嶄簬MyISAM琛ㄧ鐞嗙▼搴忎笅鐨勫悇琛ㄣ
鍙互涓烘瘡涓垎鍖烘寚瀹氫竴涓暟鎹洰褰曞拰涓涓储寮曠洰褰曘傚鏋滀笉鎸囧畾锛屽垯鏁版嵁鍜岀储寮曡瀛樺偍鍦ㄩ粯璁ょ殑MySQL鏁版嵁鐩綍涓
o MAX_ROWS鍜MIN_ROWS鍒嗗埆鐢ㄤ簬灏嗚瀛樺偍鍦ㄥ垎鍖轰腑鐨勮鏁扮洰鏈澶у煎拰琛屾暟鐩渶灏忓笺max_number_of_rows鍜min_number_of_rows鐨勫蹇呴』涓烘鏁存暟銆傚拰鍏锋湁鍚屾牱鍚嶇О鐨勬闈㈤夐」涓鏍凤紝max_number_of_rows鍜min_number_of_rows鍙綔涓哄鏈嶅姟鍣ㄧ殑鈥滃缓璁濆硷紝骞朵笉鏄‖鎬ч檺鍊笺
o 鑷夌殑TABLESPACE瀛愬彞鍙互鐢ㄤ簬涓哄垎鍖烘寚瀹氫竴涓闈㈢┖闂淬備粎鐢ㄤ簬MySQL Cluster銆
o 鑷夌殑[STORAGE] ENGINE瀛愬彞鍙互鎶婃湰鍒嗗尯涓〃鐨勭被鍨嬫敼涓烘寚瀹氱殑绫诲瀷銆傝〃鐨勭被鍨嬪彲浠ユ槸鏈MySQL鏈嶅姟鍣ㄦ敮鎸佺殑鎵鏈夌被鍨嬨STORAGE鍏抽敭瀛楀拰绛夊彿(=)鍧囦负鑷夐」銆傚鏋滄病鏈変娇鐢ㄦ閫夐」璁剧疆鍒嗗尯瀛樺偍寮曟搸锛屽垯閫傜敤浜庢暣涓〃鐨勫紩鎿庡彲浠ョ敤浜庢鍒嗗尯銆
娉ㄩ噴锛氬垎鍖虹鐞嗙▼搴忓浜PARTITION鍜SUBPARTITION鍧囨帴鍙[STORAGE] ENGINE閫夐」銆傜洰鍓嶏紝姝ゅ瓙鍙ョ殑浣跨敤鏂瑰紡浠呴檺浜庡鎵鏈夌殑鍒嗗尯鎴栧瓙鍒嗗尯璁剧疆鍚屼竴涓瓨鍌ㄥ紩鎿庯紝濡傛灉璇曞浘鍦ㄥ悓涓涓〃鍐呭涓嶅悓鐨勫垎鍖烘垨瀛愬垎鍖鸿缃笉鍚岀殑瀛樺偍寮曟搸锛屽垯浼氬嚭鐜伴敊璇ERROR 1469 (HY000)锛氬湪鏈増鏈殑MySQL涓紝涓嶅厑璁稿湪鍚勫垎鍖轰腑娣风敤绠$悊绋嬪簭銆傛垜浠墦绠楀湪灏嗘潵鐨MySQL 5.1鐗堟湰涓姞鍏ヨ繖绉嶅鍒嗗尯鐨勯檺瀹氥
o NODEGROUP閫夐」鍙互鐢ㄤ簬浣挎湰鍒嗗尯鍙互浣滀负鑺傜偣缁勭殑涓閮ㄥ垎锛岃妭鐐圭粍浣跨敤node_group_id璇嗗埆銆傛湰閫夐」浠呴傜敤浜MySQL Cluster銆
o 鍒嗗尯瀹氫箟鍙互鑷夊湴鍖呭惈涓涓垨澶氫釜subpartition_definition瀛愬彞銆傛瘡涓繖绉嶅瓙鍙ヨ嚦灏戝寘鎷SUBPARTITION name锛屾澶勶紝name鏄瓙鍒嗗尯鐨勮瘑鍒悕绉般傞櫎浜嗙敤SUBPARTITION浠f浛PARTITION鍏抽敭璇嶅锛岀敤浜庡瓙鍒嗗尯瀹氫箟鐨勮娉曚笌鐢ㄤ簬鍒嗗尯瀹氫箟鐨勮娉曚竴鏍枫
瀛愬垎鍖哄繀椤荤敱HASH鎴KEY瀹屾垚锛屽苟涓斿彧鑳藉RANGE鎴LIST鍒嗗尯杩涜瀛愬垎鍖恒傝鍙傝18.2.5鑺傦紝鈥滃瓙鍒嗗尯鈥銆
路 鍒嗗尯鍙互淇敼銆佸悎骞躲佹坊鍔犲埌琛ㄤ腑锛屾垨浠庤〃涓垹鍘汇傝浜嗚В鏈夊叧瀹屾垚杩欎簺浠诲姟鐨MySQL鍛戒护鐨勫熀鏈鏄庯紝璇峰弬瑙13.1.2鑺傦紝鈥淎LTER TABLE璇硶鈥銆傝浜嗚В璇︾粏鐨勮鏄庡拰绀轰緥锛岃鍙傝18.3鑺傦紝鈥滃垎鍖虹鐞嗏銆
鎮ㄥ彲浠ュ湪CREATE TABLE璇彞鐨勬湯灏炬坊鍔犱竴涓SELECT璇彞锛屽湪涓涓〃鐨勫熀纭涓婂垱寤鸿〃銆
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
MySQL浼氬SELECT涓殑鎵鏈夐」鍒涘缓鏂板垪銆備妇渚嬭鏄庯細
mysql> CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> TYPE=MyISAM SELECT b,c FROM test2;
鏈鍙ョ敤浜庡垱寤哄惈涓変釜鍒楋紙a, b, c锛夌殑MyISAM琛ㄣ傛敞鎰忥紝鐢SELECT璇彞鍒涘缓鐨勫垪闄勫湪琛ㄧ殑鍙充晶锛岃屼笉鏄鐩栧湪琛ㄤ笂銆傚弬鑰冧互涓嬬ず渚嬶細
mysql> SELECT * FROM foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM bar;
+------+---+
| m | n |
+------+---+
| NULL | 1 |
+------+---+
1 row in set (0.00 sec)
瀵瑰簲浜庤〃foo涓殑姣忎竴琛岋紝鍦ㄨ〃bar涓彃鍏ヤ竴琛岋紝鍚湁琛foo涓殑鍊间互鍙婃柊鍒椾腑鐨勯粯璁ゅ笺
鍦ㄧ敱CREATE TABLE...SELECT鐢熸垚鐨勮〃涓紝鍙湪CREATE TABLE閮ㄥ垎涓懡鍚嶇殑鍒楅鍏堝嚭鐜般傚湪涓や釜閮ㄥ垎涓兘鍛藉悕鐨勫垪鍜屽彧鍦SELECT閮ㄥ垎涓懡鍚嶇殑鍒楅殢鍚庡嚭鐜般備篃鍙互閫氳繃鎸囧畾CREATE TABLE閮ㄥ垎涓殑鍒楄鐩SELECT鍒椾腑鐨勬暟鎹被鍨嬨
濡傛灉鍦ㄦ妸鏁版嵁澶嶅埗鍒拌〃涓椂鍑虹幇閿欒锛屽垯琛ㄤ細鑷姩琚彇娑堬紝涓嶄細琚垱寤恒
CREATE TABLE...SELECT涓嶄細鑷姩鍒涘缓浠讳綍绱㈠紩銆傜储寮曢渶瑕佷笓闂ㄥ垱寤猴紝浠ヤ究浣胯鍙ョ殑鐏垫椿鎬ф洿寮恒傚鏋滄偍甯屾湜涓哄凡鍒涘缓鐨勮〃寤虹珛绱㈠紩锛屾偍搴斿湪SELECT璇彞鍓嶆寚瀹氱储寮曘
mysql> CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;
鍒楃殑绫诲瀷浼氬彂鐢熼儴鍒嗚浆鍖栥備緥濡傦紝AUTO_INCREAMENT灞炴т笉浼氳淇濈暀锛VARCHAR鍒椾細鍙樻垚CHAR鍒椼
褰撲娇鐢CREATE...SELECT鍒涘缓琛ㄦ椂锛屽湪鏌ヨ鏃朵竴瀹氳瀵瑰姛鑳借皟鐢ㄥ拰琛ㄨ揪寮忚捣鍒悕銆傚鏋滀笉璧峰埆鍚嶏紝鍒CREATE璇彞浼氬嚭鐜伴敊璇垨鑰呯敓鎴愪笉绗﹀悎闇瑕佺殑鍒楀悕绉般
CREATE TABLE artists_and_works
SELECT artist.name, COUNT(work.artist_id) AS number_of_works
FROM artist LEFT JOIN work ON artist.id = work.artist_id
GROUP BY artist.id;
鎮ㄤ篃鍙互鏄庣‘鍦颁负涓涓凡鐢熸垚鐨勫垪鎸囧畾绫诲瀷锛
CREATE TABLE foo (a TINYINT NOT NULL) SELECT b+1 AS a FROM bar;
鏍规嵁鍏跺畠琛ㄧ殑瀹氫箟锛堝寘鎷湪鍘熻〃涓畾涔夌殑鎵鏈夌殑鍒楀睘鎬у拰绱㈠紩锛夛紝浣跨敤LIKE鍒涘缓涓涓┖琛:
CREATE TABLE new_tbl LIKE orig_tbl;
CREATE TABLE...LIKE涓嶄細澶嶅埗瀵瑰師琛ㄦ垨澶栭敭瀹氫箟鎸囧畾鐨DATA DIRECTORY鎴INDEX DIRECTORY琛ㄩ夐」銆
鎮ㄥ彲浠ュ湪SELECT鍓嶅鍔IGNORE鎴REPLACE锛屾寚绀哄浣曞澶嶅埗鍞竴鍏抽敭瀛楀肩殑璁板綍杩涜鎿嶇旱銆備娇鐢IGNORE鍚庯紝濡傛灉鏂拌褰曞鍒朵簡鍘熸湁鐨勫敮涓鍏抽敭瀛楀肩殑璁板綍锛屽垯鏂拌褰曡涓㈠純銆備娇鐢REPLACE鍚庯紝鏂拌褰曟浛鎹㈠叿鏈夌浉鍚岀殑鍞竴鍏抽敭瀛楀肩殑璁板綍銆傚鏋滄病鏈夋寚瀹IGNORE鎴REPLACE锛屽垯鍑虹幇澶氶噸鍞竴鍏抽敭瀛楀兼椂浼氬鑷村彂鐢熼敊璇
涓轰簡纭繚鏇存柊鏃ュ織/浜岃繘浣嶆棩蹇楀彲浠ヨ鐢ㄤ簬鍐嶆鍒涘缓鍘熻〃锛MySQL涓嶅厑璁稿湪CREATE TABLE...SELECT杩囩▼涓繘琛岃仈鍚堟彃鍏ャ
鍦ㄦ湁浜涙儏鍐典笅锛岃緝鏃╃増鏈殑MySQL浼氶潤榛樺湴鏇存敼鍦CREATE TABLE鎴ALTER TABLE璇彞涓粰瀹氱殑鍒楄绾︺傚湪MySQL 5.1涓笉浼氳繘琛岃繖绫诲彉鏇淬傚鏋滀娇鐢ㄦ寚瀹氱殑鏁版嵁绫诲瀷鏃犳硶鍒涘缓鍒楋紝鍒欎細鍑虹幇閿欒銆
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
DROP DATABASE鐢ㄤ簬鍙栨秷鏁版嵁搴撲腑鐨勬墍鐢ㄨ〃鏍煎拰鍙栨秷鏁版嵁搴撱備娇鐢ㄦ璇彞鏃惰闈炲父灏忓績锛佸鏋滆浣跨敤DROP DATABASE锛屾偍闇瑕佽幏寰楁暟鎹簱DROP鏉冮檺銆
IF EXISTS鐢ㄤ簬闃叉褰撴暟鎹簱涓嶅瓨鍦ㄦ椂鍙戠敓閿欒銆
涔熷彲浠ヤ娇鐢DROP SCHEMA銆
濡傛灉鎮ㄥ涓涓甫鏈夌鍙烽摼鎺ョ殑鏁版嵁搴撲娇鐢DROP DATABASE锛屽垯閾炬帴鍜屽師鏁版嵁搴撻兘琚彇娑堛
DROP DATABASE浼氳繑鍥炲凡琚彇娑堢殑琛ㄧ殑鏁扮洰銆傛鏁扮洰鐩稿綋浜庤鍙栨秷鐨.frm鏂囦欢鐨勬暟鐩
鍦ㄦ甯告搷浣滀腑MySQL鑷韩浼氬垱寤哄嚭涓浜涙枃浠跺拰鐩綍銆DROP DATABASE璇彞浼氫粠缁欏畾鐨勬暟鎹簱鐩綍涓彇娑堣繖浜涙枃浠跺拰鐩綍锛
路 鎵鏈夊甫杩欎簺鎵╁睍鍚嶇殑鏂囦欢锛
.BAK |
.DAT |
.HSH |
|
.MRG |
.MYD |
.ISD |
|
.MYI |
.db |
.frm |
|
路 鍚嶇О涓寘鍚袱浣16杩涘埗鏁00-ff鐨勬墍鏈夊瓙鐩綍銆傝繖浜涘瓙鐩綍鐢ㄤ簬RAID琛ㄣ傦紙褰撳RAID琛ㄧ殑鏀寔琚彇娑堟椂锛屽湪MySQL 5.0涓紝杩欎簺鐩綍涓嶄細琚彇娑堛傛偍搴旇鍦ㄥ崌绾у埌MySQL 5.0鎴栨洿鏂扮殑鐗堟湰鍓嶈浆鍖栧師鏈夌殑RAID琛紝骞朵汉宸ュ彇娑堣繖浜涚洰褰曘傝鍙傝MySQL 5.0鍙傝冩墜鍐屼腑鏈夊叧浠庤緝鏃╃増鏈崌绾у埌MySQL 5.0鐨勭珷鑺傘MySQL 5.0鍙傝冩墜鍐屽彲浠ヤ粠MySQL缃戠珯涓幏鍙栥傦級
路 db.opt鏂囦欢
濡傛灉鍦MySQL鍙栨秷浜嗕笂杩拌繖浜涙枃浠朵箣鍚庯紝鍦ㄦ暟鎹簱鐩綍涓粛淇濈暀鏈夊叾瀹冩枃浠跺拰鐩綍锛屽垯鏁版嵁搴撶洰褰曚笉鑳借鍙栨秷銆傚湪杩欑鎯呭喌涓嬶紝鎮ㄥ繀椤讳汉宸ュ彇娑堟墍鏈変繚鐣欎笅鐨勬枃浠舵垨鐩綍锛屽苟鍐嶆鍙戦DROP DATABASE璇彞銆
鎮ㄨ繕鍙互浣跨敤mysqladmin鏉ュ彇娑堟枃浠躲傝鍙傝8.5鑺傦紝鈥渕ysqladmin锛氱敤浜庣鐞哅ySQL鏈嶅姟鍣ㄧ殑瀹㈡埛绔銆
DROP INDEX index_name ON tbl_name
DROP INDEX鐢ㄤ簬浠庤〃tbl_name涓彇娑堝悕绉颁负index_name鐨勭储寮曘傛湰璇彞琚槧灏勫埌涓涓ALTER TABLE璇彞涓紝鐢ㄤ簬鍙栨秷绱㈠紩銆傝鍙傝13.1.2鑺傦紝鈥淎LTER TABLE璇硶鈥銆
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
DROP TABLE鐢ㄤ簬鍙栨秷涓涓垨澶氫釜琛ㄣ傛偍蹇呴』鏈夋瘡涓〃鐨DROP鏉冮檺銆傛墍鏈夌殑琛ㄦ暟鎹拰琛ㄥ畾涔変細琚彇娑堬紝鎵浠ヤ娇鐢ㄦ湰璇彞瑕佸皬蹇冿紒
娉ㄦ剰锛屽浜庝竴涓甫鍒嗗尯鐨勮〃锛DROP TABLE浼氭案涔呮у湴鍙栨秷琛ㄥ畾涔夛紝鍙栨秷鍚勫垎鍖猴紝骞跺彇娑堝偍瀛樺湪杩欎簺鍒嗗尯涓殑鎵鏈夋暟鎹DROP TABLE杩樹細鍙栨秷涓庤鍙栨秷鐨勮〃鏈夊叧鑱旂殑鍒嗗尯瀹氫箟锛.par锛夋枃浠躲
瀵逛笌涓嶅瓨鍦ㄧ殑琛紝浣跨敤IF EXISTS鐢ㄤ簬闃叉閿欒鍙戠敓銆傚綋浣跨敤IF EXISTS鏃讹紝瀵逛簬姣忎釜涓嶅瓨鍦ㄧ殑琛紝浼氱敓鎴愪竴涓NOTE銆傝鍙傝13.5.4.22鑺傦紝鈥淪HOW WARNINGS璇硶鈥銆
RESTRICT鍜CASCADE鍙互浣垮垎鍖烘洿瀹规槗銆傜洰鍓嶏紝RESTRICT鍜CASCADE涓嶈捣浣滅敤銆
娉ㄩ噴锛氶櫎闈炴偍浣跨敤TEMPORARY鍏抽敭璇嶏紝DROP TABLE浼氳嚜鍔ㄦ彁浜ゅ綋鍓嶇殑鏈夋晥鐨勪簨鍔°
TEMPORARY鍏抽敭璇嶅叿鏈変互涓嬩綔鐢細
路 璇彞鍙彇娑TEMPORARY琛ㄣ
路 璇彞涓嶄細缁堟姝e湪杩涜涓殑浜嬪姟銆
路 涓嶄細鏌ラ獙瀛樺彇鏉冦傦紙TEMPORARY琛ㄤ粎瀵逛簬鍒涘缓璇ヨ〃鐨勫鎴风鏄彲瑙佺殑锛屾墍浠ユ煡楠屾槸涓嶅繀瑕佺殑銆傦級
浣跨敤TEMPORARY鏄‘淇濇偍涓嶄細鎰忓鍙栨秷涓涓潪TEMPORARY琛ㄧ殑鑹ソ鏂规硶銆
RENAME TABLE tbl_name TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...
鏈鍙ョ敤浜庡涓涓垨澶氫釜琛ㄨ繘琛岄噸鍛藉悕銆
閲嶅懡鍚嶆搷浣滆嚜鍔ㄨ繘琛岋紝杩欐剰鍛崇潃褰撻噸鍛藉悕姝e湪杩愯鏃讹紝鍏跺畠绾跨▼涓嶈兘璇诲彇浠讳綍琛ㄣ備緥濡傦紝濡傛灉鎮ㄦ湁涓涓師鏈夌殑琛old_table锛屾偍鍙互鍒涘缓鍙︿竴涓叿鏈夌浉鍚岀粨鏋勭殑绌鸿〃new_table锛岀劧鍚庣敤姝ょ┖琛ㄦ浛鎹㈠師鏈夌殑琛細
CREATE TABLE new_table (...);
RENAME TABLE old_table TO backup_table, new_table TO old_table;
濡傛灉姝よ鍙ョ敤浜庡澶氫釜琛ㄨ繘琛岄噸鍛藉悕锛屽垯閲嶅懡鍚嶆搷浣滀粠宸﹁嚦鍙宠繘琛屻傚鏋滄偍鎯宠浜ゆ崲涓や釜琛ㄧ殑鍚嶇О锛屾偍鍙互杩欐牱鍋氾紙鍋囪涓嶅瓨鍦ㄥ悕绉颁负tmp_table鐨勮〃锛夛細
RENAME TABLE old_table TO tmp_table,
new_table TO old_table,
tmp_table TO new_table;
鍙涓や釜鏁版嵁搴撲綅浜庡悓涓鏂囦欢绯荤粺涓紝鎮ㄨ繕鍙互瀵硅〃杩涜閲嶅懡鍚嶏紝鎶婅〃浠庝竴涓暟鎹簱涓Щ鍔ㄥ埌鍙︿竴涓暟鎹簱涓細
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
褰撴偍鎵цRENAME鏃讹紝鎮ㄤ笉鑳芥湁琚攣瀹氱殑琛紝涔熶笉鑳芥湁澶勪簬娲绘х姸鎬佺殑浜嬪姟銆傛偍杩樺繀椤绘嫢鏈夊師琛ㄧ殑ALTER鍜DROP鏉冮檺锛屼互鍙婃柊琛ㄧ殑CREATE鍜INSERT鏉冮檺銆
濡傛灉MySQL瀵瑰涓〃杩涜閲嶅懡鍚嶆椂閬囧埌浜嗛敊璇紝MySQL浼氬鎵鏈夊凡琚噸鍛藉悕鐨勮〃杩涜鍙嶅悜閲嶅懡鍚嶏紝杩斿洖鍒板師鏉ョ殑鐘舵併
鍙鎮ㄤ笉灏濊瘯閫氳繃閲嶅懡鍚嶆妸瑙嗗浘鍔犲叆鍙︿竴涓暟鎹簱涓紝鍒RENAME TABLE涔熷彲浠ョ敤浜庤鍥俱
鍗曡〃璇硶锛
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_definition]
[ORDER BY ...]
[LIMIT row_count]
澶氳〃璇硶锛
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*] ...]
FROM table_references
[WHERE where_definition]
鎴栵細
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*] ...]
USING table_references
[WHERE where_definition]
tbl_name涓湁浜涜婊¤冻鐢where_definition缁欏畾鐨勬潯浠躲DELETE鐢ㄤ簬鍒犻櫎杩欎簺琛岋紝骞惰繑鍥炶鍒犻櫎鐨勮褰曠殑鏁扮洰銆
濡傛灉鎮ㄧ紪鍐欑殑DELETE璇彞涓病鏈WHERE瀛愬彞锛屽垯鎵鏈夌殑琛岄兘琚垹闄ゃ傚綋鎮ㄤ笉鎯崇煡閬撹鍒犻櫎鐨勮鐨勬暟鐩椂锛屾湁涓涓洿蹇殑鏂规硶锛屽嵆浣跨敤TRUNCATE TABLE銆傝鍙傝13.2.9鑺傦紝鈥淭RUNCATE璇硶鈥銆
濡傛灉鎮ㄥ垹闄ょ殑琛屼腑鍖呮嫭鐢ㄤ簬AUTO_INCREMENT鍒楃殑鏈澶у硷紝鍒欒鍊艰閲嶆柊鐢ㄤ簬BDB琛紝浣嗘槸涓嶄細琚敤浜MyISAM琛ㄦ垨InnoDB琛ㄣ傚鏋滄偍鍦AUTOCOMMIT妯″紡涓嬩娇鐢DELETE FROM tbl_name锛堜笉鍚WHERE瀛愬彞锛夊垹闄よ〃涓殑鎵鏈夎锛屽垯瀵逛簬鎵鏈夌殑琛ㄧ被鍨嬶紙闄InnoDB鍜MyISAM澶栵級锛屽簭鍒楅噸鏂扮紪鎺掋傚浜InnoDB琛紝姝ら」鎿嶄綔鏈変竴浜涗緥澶栵紝鍦15.2.6.3鑺傦紝鈥淎UTO_INCREMENT鍒楀浣曞湪InnoDB涓繍琛屸涓繘琛屼簡璁ㄨ銆
瀵逛簬MyISAM鍜BDB琛紝鎮ㄥ彲浠ユ妸AUTO_INCREMENT娆$骇鍒楁寚瀹氬埌涓涓鍒楀叧閿瓧涓傚湪杩欑鎯呭喌涓嬶紝浠庡簭鍒楃殑椤剁琚垹闄ょ殑鍊艰鍐嶆浣跨敤锛岀敋鑷冲浜MyISAM琛ㄤ篃濡傛銆傝鍙傝3.6.9鑺傦紝鈥滀娇鐢ˋUTO_INCREMENT鈥銆
DELETE璇彞鏀寔浠ヤ笅淇グ绗︼細
路 濡傛灉鎮ㄦ寚瀹LOW_PRIORITY锛屽垯DELETE鐨勬墽琛岃寤惰繜锛岀洿鍒版病鏈夊叾瀹冨鎴风璇诲彇鏈〃鏃跺啀鎵ц銆
路 瀵逛簬MyISAM琛紝濡傛灉鎮ㄤ娇鐢QUICK鍏抽敭璇嶏紝鍒欏湪鍒犻櫎杩囩▼涓紝瀛樺偍寮曟搸涓嶄細鍚堝苟绱㈠紩绔粨鐐癸紝杩欐牱鍙互鍔犲揩閮ㄥ垎绉嶇被鐨勫垹闄ゆ搷浣滅殑閫熷害銆
路 鍦ㄥ垹闄よ鐨勮繃绋嬩腑锛IGNORE鍏抽敭璇嶄細浣MySQL蹇界暐鎵鏈夌殑閿欒銆傦紙鍦ㄥ垎鏋愰樁娈甸亣鍒扮殑閿欒浼氫互甯歌鏂瑰紡澶勭悊銆傦級鐢变簬浣跨敤鏈夐」鑰岃蹇界暐鐨勯敊璇細浣滀负璀﹀憡杩斿洖銆
鍒犻櫎鎿嶄綔鐨勯熷害浼氬彈鍒颁竴浜涘洜绱犵殑褰卞搷锛岃繖浜涘洜绱犲湪7.2.18鑺傦紝鈥淒ELETE璇彞鐨勯熷害鈥涓繘琛屼簡璁ㄨ銆
鍦MyISAM琛ㄤ腑锛岃鍒犻櫎鐨勮褰曡淇濈暀鍦ㄤ竴涓甫閾炬帴鐨勬竻鍗曚腑锛屽悗缁殑INSERT鎿嶄綔浼氶噸鏂颁娇鐢ㄦ棫鐨勮褰曚綅缃傝閲嶆柊浣跨敤鏈娇鐢ㄧ殑绌洪棿骞跺噺灏忔枃浠剁殑灏哄锛屽垯浣跨敤OPTIMIZE TABLE璇彞鎴myisamchk搴旂敤绋嬪簭閲嶆柊缂栨帓琛ㄣOPTIMIZE TABLE鏇寸畝渚匡紝浣嗘槸myisamchk閫熷害鏇村揩銆傝鍙傝13.5.2.5鑺傦紝鈥淥PTIMIZE TABLE璇硶鈥鍜绗7绔狅細浼樺寲銆
QUICK淇グ绗︿細褰卞搷鍒板湪鍒犻櫎鎿嶄綔涓储寮曠缁撶偣鏄惁鍚堝苟銆傚綋鐢ㄤ簬琚垹闄ょ殑琛岀殑绱㈠紩鍊艰鏉ヨ嚜鍚庢彃鍏ョ殑琛岀殑鐩歌繎鐨勭储寮曞间唬鏇挎椂锛DELETE QUICK鏈涓洪傜敤銆傚湪姝ゆ儏鍐典笅锛岃鍒犻櫎鐨勫肩暀涓嬫潵鐨勭┖绌磋閲嶆柊浣跨敤銆
鏈厖婊$殑绱㈠紩鍧楄法瓒婃煇涓涓寖鍥寸殑绱㈠紩鍊硷紝浼氬啀娆″彂鐢熸柊鐨勬彃鍏ャ傚綋琚垹闄ょ殑鍊煎鑷村嚭鐜版湭鍏呮弧鐨勭储寮曞潡鏃讹紝DELETE QUICK娌℃湁浣滅敤銆傚湪姝ゆ儏鍐典笅锛屼娇鐢QUICK浼氬鑷存湭鍒╃敤鐨勭储寮曚腑鍑虹幇搴熷純绌洪棿銆備笅闈㈡槸姝ょ鎯呭喌鐨勪妇渚嬭鏄庯細
1. 鍒涘缓涓涓〃锛岃〃涓寘鍚凡缂栫储寮曠殑AUTO_INCREMENT鍒椼
2. 鍦ㄨ〃涓彃鍏ュ緢澶氳褰曘傛瘡娆℃彃鍏ヤ細浜х敓涓涓储寮曞硷紝姝ょ储寮曞艰娣诲姞鍒扮储寮曠殑楂樼澶勩
3. 浣跨敤DELETE QUICK浠庡垪鐨勪綆绔鍒犻櫎涓缁勮褰曘
鍦ㄦ鎯呭喌涓嬶紝涓庤鍒犻櫎鐨勭储寮曞肩浉鍏崇殑绱㈠紩鍧楀彉鎴愭湭鍏呮弧鐨勭姸鎬侊紝浣嗘槸锛岀敱浜庝娇鐢ㄤ簡QUICK锛岃繖浜涚储寮曞潡涓嶄細涓庡叾瀹冪储寮曞潡鍚堝苟銆傚綋鎻掑叆鏂板兼椂锛岃繖浜涚储寮曞潡浠嶄负鏈厖婊$殑鐘舵侊紝鍘熷洜鏄柊璁板綍涓嶅惈鏈夊湪琚垹闄ょ殑鑼冨洿鍐呯殑绱㈠紩鍊笺傚彟澶栵紝鍗充娇鎮ㄦ鍚庝娇鐢DELETE鏃朵笉鍖呭惈QUICK锛岃繖浜涚储寮曞潡涔熶粛鏄湭鍏呮弧鐨勶紝闄ら潪琚垹闄ょ殑绱㈠紩鍊间腑鏈変竴閮ㄥ垎纰板阀浣嶄簬杩欎簺鏈厖婊$殑鍧楃殑涔嬩腑锛屾垨涓庤繖浜涘潡鐩搁偦銆傚湪杩欎簺鎯呭喌涓嬶紝濡傛灉瑕侀噸鏂板埄鐢ㄦ湭浣跨敤鐨勭储寮曠┖闂达紝闇浣跨敤OPTIMIZE TABLE銆
濡傛灉鎮ㄦ墦绠椾粠涓涓〃涓垹闄よ澶氳锛屼娇鐢DELETE QUICK鍐嶅姞涓OPTIMIZE TABLE鍙互鍔犲揩閫熷害銆傝繖鏍峰仛鍙互閲嶆柊寤虹珛绱㈠紩锛岃屼笉鏄繘琛屽ぇ閲忕殑绱㈠紩鍧楀悎骞舵搷浣溿
鐢ㄤ簬DELETE鐨MySQL鍞竴鐨LIMIT row_count閫夐」鐢ㄤ簬鍛婄煡鏈嶅姟鍣ㄥ湪鎺у埗鍛戒护琚繑鍥炲埌瀹㈡埛绔墠琚垹闄ょ殑琛岀殑鏈澶у笺傛湰閫夐」鐢ㄤ簬纭繚涓涓DELETE璇彞涓嶄細鍗犵敤杩囧鐨勬椂闂淬傛偍鍙互鍙噸澶DELETE璇彞锛岀洿鍒扮浉鍏宠鐨勬暟鐩皯浜LIMIT鍊间负姝€
濡傛灉DELETE璇彞鍖呮嫭涓涓ORDER BY瀛愬彞锛屽垯鍚勮鎸夌収瀛愬彞涓寚瀹氱殑椤哄簭杩涜鍒犻櫎銆傛瀛愬彞鍙湪涓LIMIT鑱旂敤鏄墠璧蜂綔鐢ㄣ備緥濡傦紝浠ヤ笅瀛愬彞鐢ㄤ簬鏌ユ壘涓WHERE瀛愬彞瀵瑰簲鐨勮锛屼娇鐢timestamp_column杩涜鍒嗙被锛屽苟鍒犻櫎绗竴锛堟渶鏃х殑锛夎锛
DELETE FROM somelog
WHERE user = 'jcole'
ORDER BY timestamp_column
LIMIT 1;
鎮ㄥ彲浠ュ湪涓涓DELETE璇彞涓寚瀹氬涓〃锛屾牴鎹涓〃涓殑鐗瑰畾鏉′欢锛屼粠涓涓〃鎴栧涓〃涓垹闄よ銆備笉杩囷紝鎮ㄤ笉鑳藉湪涓涓琛DELETE璇彞涓娇鐢ORDER BY鎴LIMIT銆
table_references閮ㄥ垎鍒楀嚭浜嗗寘鍚湪鑱斿悎涓殑琛ㄣ傛璇硶鍦13.2.7.1鑺傦紝鈥淛OIN璇硶鈥涓繘琛屼簡璇存槑銆
瀵逛簬绗竴涓娉曪紝鍙垹闄ゅ垪浜FROM瀛愬彞涔嬪墠鐨勮〃涓殑瀵瑰簲鐨勮銆傚浜庣浜屼釜璇硶锛屽彧鍒犻櫎鍒椾簬FROM瀛愬彞涔嬩腑锛堝湪USING瀛愬彞涔嬪墠锛夌殑琛ㄤ腑鐨勫搴旂殑琛屻備綔鐢ㄦ槸锛屾偍鍙互鍚屾椂鍒犻櫎璁稿涓〃涓殑琛岋紝骞朵娇鐢ㄥ叾瀹冪殑琛ㄨ繘琛屾悳绱細
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
鎴栵細
DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
褰撴悳绱㈠緟鍒犻櫎鐨勮鏃讹紝杩欎簺璇彞浣跨敤鎵鏈変笁涓〃锛屼絾鏄彧浠庤〃t1鍜岃〃t2涓垹闄ゅ搴旂殑琛屻
浠ヤ笂渚嬪瓙鏄剧ず浜嗕娇鐢ㄩ楀彿鎿嶄綔绗︾殑鍐呴儴鑱斿悎锛屼絾鏄琛DELETE璇彞鍙互浣跨敤SELECT璇彞涓厑璁哥殑鎵鏈夌被鍨嬬殑鑱斿悎锛屾瘮濡LEFT JOIN銆
鏈娉曞厑璁稿湪鍚嶇О鍚庨潰鍔.*锛屼互渚夸笌Access鐩稿銆
濡傛灉鎮ㄤ娇鐢ㄧ殑澶氳〃DELETE璇彞鍖呮嫭InnoDB琛紝骞朵笖杩欎簺琛ㄥ彈澶栭敭鐨勯檺鍒讹紝鍒MySQL浼樺寲绋嬪簭浼氬琛ㄨ繘琛屽鐞嗭紝鏀瑰彉鍘熸潵鐨勪粠灞炲叧绯汇傚湪杩欑鎯呭喌涓嬶紝璇ヨ鍙ュ嚭鐜伴敊璇苟杩斿洖鍒板墠闈㈢殑姝ラ銆傝閬垮厤姝ら敊璇紝鎮ㄥ簲璇ヤ粠鍗曚竴琛ㄤ腑鍒犻櫎锛屽苟渚濋潬InnoDB鎻愪緵鐨ON DELETE鍔熻兘锛屽鍏跺畠琛ㄨ繘琛岀浉搴旂殑淇敼銆
娉ㄩ噴锛氬綋寮曠敤琛ㄥ悕绉版椂锛屾偍蹇呴』浣跨敤鍒悕锛堝鏋滃凡缁欏畾锛夛細
DELETE t1 FROM test AS t1, test2 WHERE ...
杩涜澶氳〃鍒犻櫎鏃舵敮鎸佽法鏁版嵁搴撳垹闄わ紝浣嗘槸鍦ㄦ鎯呭喌涓嬶紝鎮ㄥ湪寮曠敤琛ㄦ椂涓嶈兘浣跨敤鍒悕銆備妇渚嬭鏄庯細
DELETE test1.tmp1, test2.tmp2 FROM test1.tmp1, test2.tmp2 WHERE ...
鐩墠锛屾偍涓嶈兘浠庝竴涓〃涓垹闄わ紝鍚屾椂鍙堝湪瀛愭煡璇腑浠庡悓涓涓〃涓夋嫨銆
HANDLER tbl_name OPEN [ AS alias ] HANDLER tbl_name READ index_name { = | >= | <= | < } (value1,value2,...) [ WHERE where_condition ] [LIMIT ... ] HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST } [ WHERE where_condition ] [LIMIT ... ] HANDLER tbl_name READ { FIRST | NEXT } [ WHERE where_condition ] [LIMIT ... ] HANDLER tbl_name CLOSE
HANDLER璇彞鎻愪緵閫氬線琛ㄥ瓨鍌ㄥ紩鎿庢帴鍙g殑鐩存帴閫氶亾銆HANDLER鍙互鐢ㄤ簬MyISAM鍜InnoDB琛ㄣ
HANDLER...OPEN璇彞鐢ㄤ簬鎵撳紑涓涓〃锛岄氳繃鍚庣画鐨HANDLER...READ璇彞寤虹珛璇诲彇琛ㄧ殑閫氶亾銆傛湰琛ㄧ洰鏍囦笉浼氳鍏跺畠绾跨▼鍏变韩锛屼篃涓嶄細鍏抽棴锛岀洿鍒扮嚎绋嬭皟鐢HANDLER...CLOSE鎴栫嚎绋嬩腑姝㈡椂涓烘銆傚鏋滄偍浣跨敤涓涓埆鍚嶆墦寮琛紝鍒欎娇鐢ㄥ叾瀹HANDLER璇彞杩涗竴姝ュ弬闃呰〃鏄繀椤讳娇鐢ㄦ鍒悕锛岃屼笉鑳戒娇鐢ㄨ〃鍚嶃
濡傛灉琚寚瀹氱殑绱㈠紩婊¤冻缁欏畾鐨勫煎苟涓旂鍚堜簡WHERE鏉′欢锛屽垯绗竴涓HANDLER...READ璇硶鍙栧嚭涓琛屻傚鏋滄偍鏈変竴涓鍒楃储寮曪紝鍒欐寚瀹氱储寮曞垪鍊间负涓涓敤閫楀彿闅斿紑鐨勬竻鍗曘傛棦鍙互涓虹储寮曚腑鐨勬墍鏈夊垪鎸囧畾鍊硷紝涔熷彲浠ヤ负绱㈠紩鍒楃殑鏈宸﹁竟鐨勫墠缂鎸囧畾鍊笺傚亣璁句竴涓储寮曞寘鎷笁涓垪锛屽悕绉颁负col_a, col_b,鍜col_c锛屽苟鎸夋椤哄簭鎺掑垪銆HANDLER璇彞鍙互涓虹储寮曚腑鐨勬墍鏈変笁涓垪鎸囧畾鍊硷紝鎴栬呬负涓涓渶宸﹁竟鍓嶇紑涓殑鍚勫垪鎸囧畾鍊笺備妇渚嬭鏄庯細
HANDLER ... index_name = (col_a_val,col_b_val,col_c_val) ...
HANDLER ... index_name = (col_a_val,col_b_val) ...
HANDLER ... index_name = (col_a_val) ...
绗簩涓HANDLER...READ璇硶鎸夌储寮曠殑椤哄簭浠庤〃涓彇鍑轰竴琛屻傜储寮曠殑椤哄簭绗﹀悎WHERE鏉′欢銆
绗笁涓HANDLER...READ璇硶鎸夎嚜鐒惰鐨勯『搴忎粠琛ㄤ腑鍙栧嚭涓琛屻傝嚜鐒惰鐨勯『搴忕鍚WHERE鏉′欢銆傚綋鎯宠瀵规暣涓〃杩涜鎵弿鏃讹紝姝よ鍙ユ瘮HANDLER tbl_name READ index_name鏇村揩銆傝嚜鐒惰鐨勯『搴忔寚鐨勬槸琛屽瓨鍌ㄥ湪MyISAM琛ㄦ暟鎹枃浠剁殑椤哄簭銆傛湰璇彞涔熼傜敤浜InnoDB琛紝浣嗘槸鍥犱负娌℃湁鐙珛鐨勬暟鎹枃浠讹紝鎵浠ユ病鏈夎繖绫绘蹇点
涓嶄娇鐢LIMIT瀛愬彞鏃讹紝鎵鏈夊舰寮忕殑HANDLER...READ璇彞鍧囧彧鍙栧嚭涓琛屻 濡傛灉瑕佽繑鍥炲涓锛屽簲鍔犲叆涓涓LIMIT瀛愬彞銆傛湰璇彞浜SELECT璇彞鐨勮娉曚竴鏍枫傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆
HANDLER...CLOSE鐢ㄤ簬鍏抽棴浣跨敤HANDLER...OPEN鎵撳紑鐨勮〃銆
娉ㄩ噴锛氳浣跨敤HANDLER鎺ュ彛鏉ユ煡闃呬竴涓〃鐨PRIMARY KEY锛屽簲浣跨敤甯﹀紩鍙风殑璇嗗埆绗`PRIMARY`锛
HANDLER tbl_name READ `PRIMARY` > (...);
HANDLER鏄瘮杈冧綆绾у埆鐨勮鍙ャ備緥濡傦紝瀹冧笉鑳芥彁渚涗竴鑷存с備篃灏辨槸璇达紝HANDLER...OPEN涓嶈兘涓鸿〃鍋氬揩鐓э紝涔熶笉鑳介攣瀹氳〃銆傝繖鎰忓懗鐫锛屽綋涓涓HANDLER...OPEN璇彞琚紪鍐欏悗锛岃〃鏁版嵁鍙互琚洿鏀癸紙鐢ㄦ绾跨▼鎴栫敤鍏跺畠绾跨▼锛夛紝骞朵笖杩欎簺鏇存敼鍙細閮ㄥ垎鍦板嚭鐜板湪HANDLER...NEXT鎴HANDLER...PREV鎵弿涓
浣跨敤HANDLER鎺ュ彛浠f浛甯歌鐨SELECT璇彞鏈夊涓師鍥狅細
路 HANDLER姣SELECT鏇村揩锛
o 涓涓寚瀹氱殑瀛樺偍寮曟搸绠$悊绋嬪簭鐩爣涓轰簡HANDLER...OPEN杩涜鏁村簭銆傝鐩爣琚噸鏂扮敤浜庤琛ㄧ殑鍚庣画鐨HANDLER璇彞锛涗笉闇瑕佸姣忎釜璇彞杩涜閲嶆柊鍒濆鍖栥
o 娑夊強鐨勫垎鏋愯緝灏戙
o 娌℃湁浼樺寲绋嬪簭鎴栨煡璇㈡牎楠屽紑閿銆
o 鍦ㄤ袱涓鐞嗙▼搴忚姹備箣闂达紝涓嶉渶瑕侀攣瀹氳〃銆
o 绠$悊绋嬪簭鎺ュ彛涓嶉渶瑕佹彁渚涘瑙備竴鑷寸殑鏁版嵁锛堜緥濡傦紝鍏佽鏃犳潯鐞嗙殑璇诲彇锛夛紝鎵浠ュ瓨鍌ㄥ紩鎿庡彲浠ヤ娇鐢ㄤ紭鍖栵紝鑰SELECT閫氬父涓嶅厑璁镐娇鐢ㄤ紭鍖栥
路 鏈変簺搴旂敤绋嬪簭浣跨敤涓ISAM杩戜技鐨勬帴鍙d笌MySQL杩炴帴銆備娇鐢HANDLER鍙互鏇村鏄撳湴涓庤繖浜涘簲鐢ㄧ▼搴忚繛鎺ャ
路 HANDLER鍏佽鎮ㄩ噰鐢ㄤ竴绉嶇壒娈婄殑鏂瑰紡杩涘嚭鏁版嵁搴撱傝屼娇鐢SELECT鏃堕毦浠ラ噰鐢紙鎴栦笉鍙兘閲囩敤锛夎繖绉嶆柟寮忋傛湁浜涘簲鐢ㄧ▼搴忓彲浠ユ彁渚涗竴涓氦浜掑紡鐨勭敤鎴锋帴鍙d笌鏁版嵁搴撹繛鎺ャ傚綋涓庤繖浜涘簲鐢ㄧ▼搴忓悓鏃朵娇鐢ㄦ椂锛岀敤HANDLER鎺ュ彛瑙傜湅鏁版嵁鏇村姞鑷劧銆
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
鎴栵細
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
鎴栵細
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
INSERT鐢ㄤ簬鍚戜竴涓凡鏈夌殑琛ㄤ腑鎻掑叆鏂拌銆INSERT...VALUES鍜INSERT...SET褰㈠紡鐨勮鍙ユ牴鎹槑纭寚瀹氱殑鍊兼彃鍏ヨ銆INSERT...SELECT褰㈠紡鐨勮鍙ユ彃鍏ヤ粠鍏跺畠琛ㄤ腑閫夊嚭鐨勮銆傚湪13.2.4.1鑺傦紝鈥淚NSERT ... SELECT璇硶鈥涓INSERT...SELECT杩涜浜嗚繘涓姝ョ殑璁ㄨ銆
琛屽簲琚彃鍏ュ埌tbl_name琛ㄤ腑銆傚彲浠ユ寜浠ヤ笅鏂规硶鎸囧畾鍒椼傛湰璇彞鍚戣繖浜涘垪鎻愪緵鍊笺
路 鍒楀悕绉版竻鍗曟垨SET瀛愬彞鏄庣‘鐨勬寚绀轰簡鍒椼
路 濡傛灉鎮ㄤ笉涓INSERT...VALUES鎴INSERT...SELECT鎸囧畾鍒楃殑娓呭崟锛屽垯琛ㄤ腑姣忓垪鐨勫煎繀椤诲湪VALUES娓呭崟涓彁渚涳紝鎴栫敱SELECT鎻愪緵銆傚鏋滄偍涓嶇煡閬撹〃涓悇鍒楃殑椤哄簭锛屽垯浣跨敤DESCRIBE tbl_name鏌ヨ銆
鍒楀煎彲浠ラ噰鐢ㄥ绉嶆柟娉曠粰瀹氾細
路 濡傛灉涓嶆槸鍦ㄤ弗鏍兼ā寮忎笅杩愯锛屽垯鎵鏈夋病鏈夋槑纭粰瀹氬肩殑鍒楅兘琚缃负榛樿鍊硷紙鏄庣‘鐨勬垨闅愬惈鐨勶級銆備緥濡傦紝濡傛灉鎮ㄦ寚瀹氫簡涓涓垪娓呭崟锛屼絾姝ゆ竻鍗曟病鏈夊琛ㄤ腑鎵鏈夌殑鍒楄繘琛屽懡鍚嶏紝鍒欐湭鍛藉悕鐨勫悇鍒楄璁剧疆涓洪粯璁ゅ笺傞粯璁ゅ肩殑璧嬪煎湪13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥涓繘琛屼簡璇存槑銆備篃鍙弬瑙1.8.6.2鑺傦紝鈥滃鏃犳晥鏁版嵁鐨勭害鏉熲銆
鏈夋椂鍊欙紝鎮ㄩ渶瑕佸鎵鏈夋病鏈夐粯璁ゅ肩殑鍒楁槑纭湴鎸囧畾鍊笺傚鏋滄偍甯屾湜锛屽湪娌℃湁鏄庣‘鎸囧畾鍊兼椂锛INSERT璇彞鍙互鐢熸垚閿欒淇℃伅锛屽垯鎮ㄥ簲璇ヤ娇鐢STRICT妯″紡銆傝鍙傝5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊銆
路 浣跨敤鍏抽敭璇DEFAULT锛屾槑纭湴鎶婂垪璁剧疆涓洪粯璁ゅ笺傝繖鏍凤紝缂栧啓鍚戞墍鏈夊垪璧嬪肩殑INSERT璇彞鏃跺彲浠ユ洿瀹规槗锛屽洜涓轰娇鐢DEFAULT鍙互閬垮厤缂栧啓鍑轰笉瀹屾暣鐨勩佹湭鍖呭惈鍏ㄩ儴鍒楀肩殑VALUES娓呭崟銆傚鏋滀笉浣跨敤DEFUALT锛屾偍蹇呴』缂栧啓涓涓垪鍚嶇О娓呭崟锛屼笌VALUES娓呭崟涓殑姣忎釜鍊煎搴斻
鎮ㄨ繕鍙互浣跨敤DEFAULT(col_name)浣滀负涓绉嶆洿閫氱敤鐨勫舰寮忥紝鍦ㄨ〃杈惧紡涓娇鐢紝鐢ㄤ簬鐢熸垚涓涓垪鐨勯粯璁ゅ笺
路 濡傛灉鍒楁竻鍗曞拰VALUES娓呭崟鍧囦负绌烘竻鍗曪紝鍒INSERT浼氬垱寤轰竴涓锛屾瘡涓垪閮借璁剧疆涓洪粯璁ゅ硷細
路 mysql> INSERT INTO tbl_name () VALUES();
鍦STRICT妯″紡涓紝濡傛灉鏈変竴鍒楁病鏈夐粯璁ゅ硷紝鍒欎細鍑虹幇閿欒銆傛垨鑰咃紝MySQL浼氬鎵鏈夋病鏈夋槑纭畾涔夐粯璁ゅ肩殑鍒椾娇鐢ㄩ殣鍚殑榛樿鍊笺
路 鎮ㄥ彲浠ユ寚瀹氫竴涓〃杈惧紡expr鏉ユ彁渚涗竴涓垪鍊笺傚鏋滆〃杈惧紡鐨勭被鍨嬩笌鍒楀间笉鍖归厤锛岃繖鏍峰仛浼氶犳垚绫诲瀷杞寲銆傚苟涓旓紝缁欏畾鍊肩殑杞寲浼氬鑷翠笉鍚岀殑鎻掑叆鍊硷紝鎻掑叆浣曞肩敱鍒楃被鍨嬭屽畾銆備緥濡傦紝鍚戜竴涓INT, FLOAT, DECIMAL(10,6)鎴YEAR鍒楁彃鍏ュ瓧绗︿覆'1999.0e-2'锛屾彃鍏ュ煎垎鍒槸1999锛19.9921锛19.992100鍜1999銆傚瓨鍌ㄥ湪INT鍜YEAR鍒椾腑鐨勫间负1999鐨勫師鍥犳槸锛屽湪浠庡瓧绗︿覆鍒版暣鏁扮殑杞寲涓紝鍙妸瀛楃涓茬殑鍓嶉潰閮ㄥ垎鐪嬩綔鏈夋晥鐨勬暣鏁版垨骞翠唤銆傚浜庢诞鐐瑰垪鍜屽浐瀹氱偣鍒楋紝鍦ㄤ粠瀛楃涓插埌娴偣鐨勮浆鍖栦腑锛屾妸鏁翠釜瀛楃涓插潎鐪嬩綔鏈夋晥鐨勬诞鐐瑰笺
琛ㄨ揪寮expr鍙互寮曠敤鍦ㄥ兼竻鍗曚腑宸茶缃殑鎵鏈夊垪銆備緥濡傦紝鎮ㄥ彲浠ヨ繖涔堟搷浣滐紝鍥犱负鐢ㄤ簬col2鐨勫煎紩鐢ㄤ簡col1锛岃col1宸茬粡琚祴鍊硷細
mysql> INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
浣嗘槸浠ヤ笅璇彞涓嶅悎娉曪紝鍥犱负鐢ㄤ簬col1鐨勫煎紩鐢ㄤ簡col2锛岃col2鍦col1涔嬪悗琚祴鍊硷細
mysql> INSERT INTO tbl_name (col1,col2) VALUES(col2*2,15);
鏈変竴绉嶄緥澶栨儏鍐碉紝閭e氨鏄惈鏈AUTO_INCREMENT鍊肩殑鍒椼傚洜涓AUTO_INCREMENT鍊煎湪鍏跺畠鍊艰祴鍊间箣鍚庤鐢熸垚锛屾墍浠ヤ换浣曞湪璧嬪兼椂瀵AUTO_INCREMENT鍒楃殑寮曠敤閮戒細杩斿洖0銆
INSERT璇彞鏀寔涓嬪垪淇敼绗︼細
路 濡傛灉鎮ㄤ娇鐢DELAYED鍏抽敭瀛楋紝鍒欐湇鍔″櫒浼氭妸寰呮彃鍏ョ殑琛屾斁鍒颁竴涓紦鍐插櫒涓紝鑰屽彂閫INSERT DELAYED璇彞鐨勫鎴风浼氱户缁繍琛屻傚鏋滆〃姝e湪琚娇鐢紝鍒欐湇鍔″櫒浼氫繚鐣欒繖浜涜銆傚綋琛ㄧ┖闂叉椂锛屾湇鍔″櫒寮濮嬫彃鍏ヨ锛屽苟瀹氭湡妫鏌ユ槸鍚︽湁鏂扮殑璇诲彇璇锋眰銆傚鏋滄湁鏂扮殑璇诲彇璇锋眰锛屽垯琚欢杩熺殑琛岃寤剁紦鎵ц锛岀洿鍒拌〃鍐嶆绌洪棽鏃朵负姝€傝鍙傝13.2.4.2鑺傦紝鈥淚NSERT DELAYED璇硶鈥銆
路 濡傛灉鎮ㄤ娇鐢LOW_PRIORITY鍏抽敭璇嶏紝鍒INSERT鐨勬墽琛岃寤惰繜锛岀洿鍒版病鏈夊叾瀹冨鎴风浠庤〃涓鍙栦负姝€傚綋鍘熸湁瀹㈡埛绔鍦ㄨ鍙栨椂锛屾湁浜涘鎴风鍒氬紑濮嬭鍙栥傝繖浜涘鎴风涔熻鍖呮嫭鍦ㄥ唴銆傛鏃讹紝INSERT LOW_PRIORITY璇彞绛夊欍傚洜姝わ紝鍦ㄨ鍙栭噺寰堝ぇ鐨勬儏鍐典笅锛屽彂鍑INSERT LOW_PRIORITY璇彞鐨勫鎴风鏈夊彲鑳介渶瑕佺瓑寰呭緢闀夸竴娈垫椂闂达紙鐢氳嚦鏄案杩滅瓑寰呬笅鍘伙級銆傦紙杩欎笌INSERT DELAYED褰㈡垚瀵规瘮锛INSERT DELAYED绔嬪埢璁╁鎴风缁х画鎵ц銆傝鍙傝13.2.4.2鑺傦紝鈥淚NSERT DELAYED璇硶鈥銆傦級娉ㄦ剰LOW_PRIORITY閫氬父涓嶅簲鐢ㄤ簬MyISAM琛紝鍥犱负杩欎箞鍋氫細鍙栨秷鍚屾椂杩涜鐨勬彃鍏ャ傝鍙傝15.1鑺傦紝鈥淢yISAM瀛樺偍寮曟搸鈥銆
路 濡傛灉鎮ㄦ寚瀹氫簡HIGH_PRIORITY锛屽悓鏃舵湇鍔″櫒閲囩敤--low-priority-updates閫夐」鍚姩锛屽垯HIGH_PRIORITY灏嗚鐩--low-priority-updates閫夐」銆傝繖涔堝仛杩樹細瀵艰嚧鍚屾椂杩涜鐨勬彃鍏ヨ鍙栨秷銆
路 浣跨敤mysql_affected_rows() C API鍑芥暟锛屽彲浠ヨ幏寰楃敤浜INSERT鐨勫彈褰卞搷琛岀殑鍊笺傝鍙傝25.2.3.1鑺傦紝鈥渕ysql_affected_rows()鈥銆
路 濡傛灉鎮ㄥ湪涓涓INSERT璇彞涓娇鐢IGNORE鍏抽敭璇嶏紝鍦ㄦ墽琛岃鍙ユ椂鍑虹幇鐨勯敊璇褰撲綔璀﹀憡澶勭悊銆備緥濡傦紝娌℃湁浣跨敤IGNORE鏃讹紝濡傛灉涓涓澶嶅埗浜嗗師鏈夌殑UNIQUE绱㈠紩鎴PRIMARY KEY鍊硷紝浼氬鑷村嚭鐜伴噸澶嶅叧閿瓧閿欒锛岃鍙ユ墽琛屽け璐ャ備娇鐢IGNORE鏃讹紝璇ヨ浠嶇劧鏈鎻掑叆锛屼絾鏄笉浼氬嚭鐜伴敊璇IGNORE鏈鎸囧畾鏃讹紝濡傛灉鏁版嵁杞寲寮曞彂閿欒锛屽垯浼氫娇璇彞鎵ц澶辫触銆備娇鐢IGNORE鍚庯紝鏃犳晥鏁版嵁琚皟鏁村埌鏈鎺ヨ繎鐨勫硷紝骞惰鎻掑叆锛涙鏃讹紝鐢熸垚璀﹀憡锛屼絾鏄鍙ユ墽琛屼笉浼氬け璐ャ傛偍鍙互浣跨敤mysql_info() C API鍑芥暟娴嬪畾鏈夊灏戣琚彃鍏ュ埌琛ㄤ腑銆
濡傛灉鎮ㄦ寚瀹氫簡ON DUPLICATE KEY UPDATE锛屽苟涓旀彃鍏ヨ鍚庝細瀵艰嚧鍦ㄤ竴涓UNIQUE绱㈠紩鎴PRIMARY KEY涓嚭鐜伴噸澶嶅硷紝鍒欐墽琛屾棫琛UPDATE銆備緥濡傦紝濡傛灉鍒a琚畾涔変负UNIQUE锛屽苟涓斿寘鍚1锛屽垯浠ヤ笅涓や釜璇彞鍏锋湁鐩稿悓鐨勬晥鏋滐細
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
-> ON DUPLICATE KEY UPDATE c=c+1;
mysql> UPDATE table SET c=c+1 WHERE a=1;
濡傛灉琛屼綔涓烘柊璁板綍琚彃鍏ワ紝鍒欏彈褰卞搷琛岀殑鍊间负1锛涘鏋滃師鏈夌殑璁板綍琚洿鏂帮紝鍒欏彈褰卞搷琛岀殑鍊间负2銆
娉ㄩ噴锛氬鏋滃垪b涔熸槸鍞竴鍒楋紝鍒INSERT涓庢UPDATE璇彞鐩稿綋锛
mysql> UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
濡傛灉a=1 OR b=2涓庡涓鍚戝尮閰嶏紝鍒欏彧鏈変竴涓琚洿鏂般傞氬父锛屾偍搴旇灏介噺閬垮厤瀵瑰甫鏈夊涓敮涓鍏抽敭瀛楃殑琛ㄤ娇鐢ON DUPLICATE KEY瀛愬彞銆
鎮ㄥ彲浠ュ湪UPDATE瀛愬彞涓娇鐢VALUES(col_name)鍑芥暟浠INSERT...UPDATE璇彞鐨INSERT閮ㄥ垎寮曠敤鍒楀笺傛崲鍙ヨ瘽璇达紝濡傛灉娌℃湁鍙戠敓閲嶅鍏抽敭瀛楀啿绐侊紝鍒UPDATE瀛愬彞涓殑VALUES(col_name)鍙互寮曠敤琚彃鍏ョ殑col_name鐨勫笺傛湰鍑芥暟鐗瑰埆閫傜敤浜庡琛屾彃鍏ャVALUES()鍑芥暟鍙湪INSERT...UPDATE璇彞涓湁鎰忎箟锛屽叾瀹冩椂鍊欎細杩斿洖NULL銆
绀轰緥锛
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
-> ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
鏈鍙ヤ笌浠ヤ笅涓や釜璇彞浣滅敤鐩稿悓锛
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
-> ON DUPLICATE KEY UPDATE c=3;
mysql> INSERT INTO table (a,b,c) VALUES (4,5,6)
-> ON DUPLICATE KEY UPDATE c=9;
褰撴偍浣跨敤ON DUPLICATE KEY UPDATE鏃讹紝DELAYED閫夐」琚拷鐣ャ
鎮ㄥ彲浠ヤ娇鐢SQL LAST_INSERT_ID()鍑芥暟鏌ユ壘鐢ㄤ簬AUTO_INCREMENT鍒楃殑鍊笺備粠C API鐨勫唴閮紝浣跨敤mysql_insert_id()鍑芥暟銆備笉杩囷紝鎮ㄥ簲璇ユ敞鎰忥紝涓や釜鍑芥暟鐨勪綔鐢ㄥ苟涓嶆绘槸鐩稿悓鐨勩傚湪12.9.3鑺傦紝鈥滀俊鎭嚱鏁扳鍜25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥涓繘涓姝ヨ璁轰簡涓AUTO_INCREMENT鍒楁湁鍏崇殑INSERT璇彞鐨勪綔鐢ㄣ
濡傛灉鎮ㄤ娇鐢INSERT...VALUES璇彞鏃堕噰鐢ㄤ簡澶氫釜鍊兼竻鍗曟垨INSERT...SELECT锛屽垯璇ヨ鍙ユ寜浠ヤ笅鏍煎紡杩斿洖涓涓俊鎭瓧绗︿覆锛
Records: 100 Duplicates: 0 Warnings: 0
璁板綍鎸囩ず浜嗙粡杩囪鍙ュ鐞嗙殑琛岀殑鏁扮洰銆傦紙鍥犱负閲嶅鏁扮洰鍙互涓嶆槸闆讹紝鎵浠ヨ鏁扮洰涓嶄竴瀹氭槸瀹為檯琚彃鍏ョ殑琛岀殑鏁扮洰銆傦級閲嶅鏁扮洰鎸囩殑鏄笉鑳借鎻掑叆鐨勮鐨勬暟鐩紝鍥犱负杩欎簺琛屼細澶嶅埗閮ㄥ垎鍘熸湁鐨勫敮涓绱㈠紩鍊笺傝鍛婃寚鐨勬槸鎻掑叆鏈夐敊璇垨鏈夐棶棰樼殑鍒楀肩殑娆℃暟銆傚湪浠ヤ笅鎯呭喌涓嬩細鍑虹幇璀﹀憡锛
路 鍚戜竴涓凡瀹氫箟涓NOT NULL鐨勫垪涓彃鍏NULL銆傚浜庝竴涓琛INSERT璇彞鎴INSERT INTO...SELECT璇彞锛屾牴鎹垪鏁版嵁鐨勭被鍨嬶紝鍒楄璁剧疆涓洪殣鍚殑榛樿鍊笺傚浜庢暟瀛楃被鍨嬶紝榛樿鍊间负0锛涘浜庡瓧绗︿覆绫诲瀷锛岄粯璁ゅ间负绌哄瓧绗︿覆('')锛涘浜庢棩鏈熷拰鏃堕棿绫诲瀷锛岄粯璁ゅ间负鈥zero鈥濆笺傚INSERT INTO...SELECT璇彞鐨勫鐞嗘柟娉曚笌瀵瑰琛屾彃鍏ョ殑澶勭悊鏂规硶涓鏍凤紝鍥犱负鏈嶅姟鍣ㄤ笉鑳芥娴嬫潵鑷SELECT鐨勭粨鏋滐紝涓嶈兘鍒ゆ柇鏄惁杩斿洖鍗曚竴琛屻傦紙瀵逛簬鍗曚竴琛INSERT锛屽綋NULL琚彃鍏ヤ竴涓NOT NULL鍒楁椂锛屼笉浼氬嚭鐜拌鍛婏紝鑰屾槸鍑虹幇閿欒锛屽苟涓旇鍙ヨ繍琛屽け璐ャ傦級
路 鏁板瓧鍒楃殑鍊艰璁剧疆鍦ㄥ垪鐨勫艰寖鍥翠箣澶栥傛鍊艰淇敼涓烘湭鏈鎺ヨ繎鐨勫艰寖鍥寸鐐广
路 鍚戜竴涓暟瀛楀垪璧嬩簣涓涓緥濡'10.34 a'鐨勫笺傚熬閮ㄧ殑闈炴暟瀛楁枃鏈鍒犺妭锛屽叾浣欑殑鏁板瓧閮ㄥ垎琚彃鍏ワ紝濡傛灉瀛楃涓插兼病鏈夊墠瀵肩殑鏁板瓧閮ㄥ垎锛屽垯璇ュ垪琚缃负0銆
路 鍚戜竴涓瓧绗︿覆鍒楋紙CHAR, VARCHAR, TEXT鎴BLOB锛変腑鎻掑叆鐨勫瓧绗︿覆瓒呰繃浜嗗垪鐨勬渶澶ч暱搴︺傛鍊艰鍒犺妭鍒板垪鐨勬渶澶ч暱搴︺
路 鍚戞棩鏈熸垨鏃堕棿鍒椾腑鎻掑叆鐨勫煎浜庤鍒楃殑绫诲瀷鏄笉鍚堟硶鐨勩傛牴鎹垪鐨勭被鍨嬶紝璇ュ垪琚缃埌鐩稿簲鐨勯浂鍊笺
濡傛灉鎮ㄦ鍦ㄤ娇鐢C API锛屽垯鍙互閫氳繃璋冪敤mysql_info()鍑芥暟鑾峰彇淇℃伅瀛楃涓层傝鍙傝25.2.3.34鑺傦紝鈥渕ysql_info()鈥銆
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
浣跨敤INSERT...SELECT锛屾偍鍙互蹇熷湴浠庝竴涓垨澶氫釜琛ㄤ腑鍚戜竴涓〃涓彃鍏ュ涓銆
绀轰緥锛
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
浣跨敤INSERT...SELECT璇彞鏃朵細鍑虹幇浠ヤ笅鎯呭喌锛
路 鏄庣‘鍦版寚瀹IGNORE锛岀敤浜庡拷鐣ヤ細瀵艰嚧閲嶅鍏抽敭瀛楅敊璇殑璁板綍銆
路 涓嶈鍚屾椂浣跨敤DELAYED鍜INSERT...SELECT銆
路 INSERT璇彞鐨勭洰鏍囪〃浼氭樉绀哄湪鏌ヨ鐨SELECT閮ㄥ垎鐨FROM瀛愬彞涓傦紙鍦ㄦ湁浜涙棫鐗堟湰鐨MySQL涓笉浼氬嚭鐜拌繖绉嶆儏鍐点傦級
路 AUTO_INCREMENT鍒楃収甯歌繍琛屻
路 涓轰簡纭繚浜岃繘鍒舵棩蹇楀彲浠ヨ鐢ㄤ簬鍐嶆鍒涘缓鍘熻〃锛MySQL涓嶅厑璁稿湪INSERT...SELECT杩愯鏈熼棿鍚屾椂杩涜鎻掑叆鎿嶄綔銆
路 鐩墠锛屾偍涓嶈兘鍦ㄥ悜涓涓〃鎻掑叆鐨勫悓鏃讹紝鍙堝湪涓涓瓙鏌ヨ涓粠鍚屼竴涓〃涓夋嫨銆
鍦ON DUPLICATE KEY UPDATE鐨勫奸儴鍒嗕腑锛屽彧瑕佹偍涓嶄娇鐢SELECT閮ㄥ垎涓殑GROUP BY锛屾偍灏卞彲浠ュ紩鐢ㄥ湪鍏跺畠琛ㄤ腑鐨勫垪銆傛湁涓涓壇浣滅敤鏄紝鎮ㄥ繀椤讳娇鍊奸儴鍒嗕腑鐨勯潪鍞竴鍒楃殑鍚嶇О绗﹀悎瑕佹眰銆
鎮ㄥ彲浠ヤ娇鐢REPLACE鏇夸唬INSERT锛屾潵瑕嗙洊鏃ц銆傚浜庡寘鍚敮涓鍏抽敭瀛楀硷紝骞跺鍒朵簡鏃ц鐨勬柊琛岋紝鍦ㄨ繘琛屽鐞嗘椂锛REPLACE鍙互浣滀负INSERT IGNORE鐨勫悓绫诲瓙鍙ワ細鏂拌琚敤浜庢浛鎹㈡棫琛岋紝鑰屼笉鏄涓㈠純銆
INSERT DELAYED ...
鐢ㄤ簬INSERT璇彞鐨DELAYED閫夐」鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚鏋滄偍鐨勫鎴风涓嶈兘绛夊緟INSERT瀹屾垚锛屽垯杩欎釜閫夐」鏄潪甯告湁鐢ㄧ殑銆傚綋鎮ㄤ娇鐢MySQL杩涜鏃ュ織缂栧啓鏃讹紝杩欐槸闈炲父甯歌鐨勯棶棰樸傛偍涔熷彲浠ュ畾鏈熻繍琛SELECT鍜UPDATE璇彞锛岃繖浜涜鍙ヨ姳璐圭殑鏃堕棿杈冮暱銆
褰撲竴涓鎴风浣跨敤INSERT DELAYED鏃讹紝浼氱珛鍒讳粠鏈嶅姟鍣ㄥ寰楀埌涓涓‘瀹氥傚苟涓旇琚帓鍏ラ槦鍒楋紝褰撹〃娌℃湁琚叾瀹冪嚎绋嬩娇鐢ㄦ椂锛屾琛岃鎻掑叆銆
浣跨敤INSERT DELAYED鐨勫彟涓涓噸瑕佺殑濂藉鏄紝鏉ヨ嚜璁稿瀹㈡埛绔殑鎻掑叆琚泦涓湪涓璧凤紝骞惰缂栧啓鍏ヤ竴涓潡銆傝繖姣旀墽琛岃澶氱嫭绔嬬殑鎻掑叆瑕佸揩寰堝銆
浣跨敤DELAYED鏃舵湁涓浜涢檺鍒讹細
路 INSERT DELAYED浠呴傜敤浜MyISAM, MEMORY鍜ARCHIVE琛ㄣ傚浜MyISAM琛紝濡傛灉鍦ㄦ暟鎹枃浠剁殑涓棿娌℃湁绌洪棽鐨勫潡锛屽垯鏀寔鍚屾椂閲囩敤SELECT鍜INSERT璇彞銆傚湪杩欎簺鎯呭喌涓嬶紝鍩烘湰涓嶉渶瑕佸MyISAM浣跨敤INSERT DELAYED銆傝鍙傝15.1鑺傦紝鈥淢yISAM瀛樺偍寮曟搸鈥, 15.4鑺傦紝鈥淢EMORY (HEAP)瀛樺偍寮曟搸鈥鍜15.8鑺傦紝鈥淎RCHIVE瀛樺偍寮曟搸鈥銆
路 INSERT DELAYED搴旇浠呯敤浜庢寚瀹氬兼竻鍗曠殑INSERT璇彞銆傛湇鍔″櫒蹇界暐鐢ㄤ簬INSERT DELAYED...SELECT璇彞鐨DELAYED銆
路 鏈嶅姟鍣ㄥ拷鐣ョ敤浜INSERT DELAYED...ON DUPLICATE UPDATE璇彞鐨DELAYED銆
路 鍥犱负鍦ㄨ琚彃鍏ュ墠锛岃鍙ョ珛鍒昏繑鍥烇紝鎵浠ユ偍涓嶈兘浣跨敤LAST_INSERT_ID()鏉ヨ幏鍙AUTO_INCREMENT鍊笺AUTO_INCREMENT鍊煎彲鑳界敱璇彞鐢熸垚銆
路 瀵逛簬SELECT璇彞锛DELAYED琛屼笉鍙锛岀洿鍒拌繖浜涜纭疄琚彃鍏ヤ簡涓烘銆
路 DELAYED鍦ㄤ粠灞炲鍒舵湇鍔″櫒涓蹇界暐浜嗭紝鍥犱负DELAYED涓嶄細鍦ㄤ粠灞炴湇鍔″櫒涓骇鐢熶笌涓绘湇鍔″櫒涓嶄竴鏍风殑鏁版嵁銆
娉ㄦ剰锛岀洰鍓嶅湪闃熷垪涓殑鍚勮鍙繚瀛樺湪瀛樺偍鍣ㄤ腑锛岀洿鍒板畠浠鎻掑叆鍒拌〃涓负姝€傝繖鎰忓懗鐫锛屽鏋滄偍寮鸿涓浜mysqld锛堜緥濡傦紝浣跨敤kill -9锛夋垨鑰呭鏋mysqld鎰忓鍋滄锛屽垯鎵鏈夋病鏈夎鍐欏叆纾佺洏鐨勮閮戒細涓㈠け銆
浠ヤ笅璇︾粏鎻忚堪浜嗗綋鎮ㄥINSERT鎴REPLACE浣跨敤DELAYED閫夐」鏃朵細鍙戠敓浠涔堟儏鍐点傚湪杩欎簺鎻忚堪涓紝鈥滅嚎绋嬧濇寚鐨勬槸宸叉帴鍙椾簡涓涓INSERT DELAYED璇彞鐨勭嚎绋嬶紝鈥滅鐞嗙▼搴忊濇寚鐨勬槸涓烘煇涓壒瀹氱殑琛ㄥ鐞嗘墍鏈INSERT DELAYED璇彞鐨勭嚎绋嬨
路 褰撲竴涓嚎绋嬪涓涓〃鎵цDELAYED璇彞鏃讹紝浼氬垱寤哄嚭涓涓鐞嗙▼搴忕嚎绋嬶紙濡傛灉鍘熸潵涓嶅瓨鍦級锛屽鐢ㄤ簬鏈〃鐨勬墍鏈DELAYED璇彞杩涜澶勭悊銆
路 绾跨▼浼氭鏌ユ槸鍚︾鐞嗙▼搴忎互鍓嶅凡鑾峰彇浜DELAYED閿佸畾锛涘鏋滄病鏈夎幏鍙栵紝鍒欏憡鐭ョ鐞嗙▼搴忕嚎绋嬭繘琛屾椤规搷浣溿傚嵆浣垮叾瀹冪嚎绋嬪琛ㄦ湁READ鎴WRITE閿佸畾锛屼篃鍙互鑾峰緱DELAYED閿佸畾銆備絾鏄鐞嗙▼搴忎細绛夊緟鎵鏈夌殑ALTER TABLE閿佸畾鎴FLUSH TABLE閿佸畾锛屼互纭繚琛ㄧ殑缁撴瀯鏄渶鏂扮殑銆
路 绾跨▼鎵цINSERT璇彞锛屼絾涓嶆槸鎶婅鍐欏叆琛ㄤ腑锛岃屾槸鎶婃渶缁堣鐨勬嫹璐濇斁鍏ヤ竴涓敱绠$悊绋嬪簭绾跨▼绠$悊鐨勯槦鍒椾腑銆傜嚎绋嬩細鎻愮ず鍑虹幇璇硶閿欒锛岃繖浜涢敊璇細琚姤鍛婂埌瀹㈡埛绔腑銆
路 鍥犱负鍦ㄦ彃鍏ユ搷浣滀箣鍓嶏紝INSERT杩斿洖宸茬粡瀹屾垚锛屾墍浠ュ鎴风涓嶈兘浠庢湇鍔″櫒澶勮幏鍙栭噸澶嶈褰曠殑鏁扮洰锛屼篃涓嶈兘鑾峰彇鐢熸垚鐨勮鐨AUTO_INCREMENT鍊笺傦紙濡傛灉鎮ㄤ娇鐢C API锛屽垯鍑轰簬鍚屾牱鐨勫師鍥狅紝mysql_info()鍑芥暟涓嶄細杩斿洖浠讳綍鏈夋剰涔夌殑涓滆タ銆傦級
路 褰撹琚彃鍏ヨ〃涓椂锛屼簩杩涘埗鏃ュ織琚鐞嗙▼搴忕嚎绋嬫洿鏂般傚湪澶氳鎻掑叆鎯呭喌涓嬶紝褰撶涓琛岃鎻掑叆鏃讹紝浜岃繘鍒舵棩蹇楄鏇存柊銆
路 姣忔delayed_insert_limit琛岃缂栧啓鏃讹紝绠$悊绋嬪簭浼氭鏌ユ槸鍚︽湁SELECT璇彞浠嶇劧鏈墽琛屻傚鏋滄湁锛屽垯浼氬湪缁х画杩愯鍓嶏紝璁╄繖浜涜鍙ュ厛鎵ц銆
路 褰撶鐞嗙▼搴忕殑闃熷垪涓病鏈夊浣欑殑琛屾椂锛岃〃琚В閿併傚鏋滃湪delayed_insert_timeout鏃堕棿鍐咃紝娌℃湁鎺ユ敹鍒版柊鐨INSERT DELAYED璇彞锛屽垯绠$悊绋嬪簭涓銆
路 濡傛灉鍦ㄦ煇涓壒瀹氱殑绠$悊绋嬪簭闃熷垪涓紝鏈夎秴杩delayed_queue_size鐨勮鏈鎵ц锛屽垯鐢宠INSERT DELAYED鐨勭嚎绋嬩細绛夊緟锛岀洿鍒伴槦鍒椾腑鍑虹幇绌洪棿涓烘銆傝繖涔堝仛鍙互纭繚mysqld涓嶄細鎶婃墍鏈夌殑瀛樺偍鍣ㄩ兘鐢ㄤ簬琚欢杩熺殑瀛樺偍闃熷垪銆
路 绠$悊绋嬪簭绾跨▼浼氭樉绀哄湪MySQL杩涚▼娓呭崟涓紝鍏跺懡浠ゅ垪涓寘鍚delayed_insert銆傚鏋滄偍鎵ц涓涓FLUSH TABLES璇彞鎴栦娇鐢KILL thread_id杩涜鍒犻櫎锛屽垯浼氬垹闄ゆ绾跨▼銆備笉杩囷紝鍦ㄩ鍑哄墠锛岀嚎绋嬩細棣栧厛鎶婃墍鏈夋帓鍏ラ槦鍒楃殑琛屽瓨鍌ㄥ埌琛ㄤ腑銆傚湪杩欐湡闂达紝璇ョ嚎绋嬩笉浼氫粠鍏跺畠绾跨▼澶勬帴鍙椾换浣曟柊鐨INSERT璇彞銆傚鏋滄偍鍦ㄦ涔嬪悗鎵ц涓涓INSERT DELAYED璇彞锛屽垯浼氬垱寤哄嚭涓涓柊鐨勭鐞嗙▼搴忕嚎绋嬨
娉ㄦ剰锛屽鏋滄湁涓涓INSERT DELAYED绠$悊绋嬪簭姝e湪杩愯锛屽垯杩欐剰鍛崇潃INSERT DELAYED璇彞姣斿父瑙勭殑INSERT璇彞鍏锋湁鏇撮珮鐨勪紭鍏堟潈銆傚叾瀹冩洿鏂拌鍙ュ繀椤荤瓑寰咃紝鐩村埌INSERT DELAYED璇彞闃熷垪閮借繍琛屽畬姣曪紝鎴栬呯鐞嗙▼搴忕嚎绋嬭涓锛堜娇鐢KILL thread_id锛夛紝鎴栬呮墽琛屼簡涓涓FLUSH TABLES鏃朵负姝€
路 浠ヤ笅鐘舵佸彉閲忔彁渚涗簡鏈夊叧INSERT DELAYED璇彞鐨勪俊鎭細
鐘舵佸彉閲 |
鎰忎箟 |
Delayed_insert_threads |
绠$悊绋嬪簭绾跨▼鐨勬暟鐩 |
Delayed_writes |
浣跨敤INSERT DELAYED鍐欏叆鐨勮鐨勬暟鐩 |
Not_flushed_delayed_rows |
绛夊緟琚啓鍏ョ殑琛岀殑鏁扮洰 |
路 鎮ㄥ彲浠ラ氳繃鍙戦佷竴涓SHOW STATUS璇彞锛屾垨鑰呮墽琛屼竴涓mysqladmin extended-status鍛戒护锛屾潵闃呰杩欎簺鍙橀噺銆
娉ㄦ剰锛屽綋娌℃湁浣跨敤琛ㄦ椂锛INSERT DELAYED姣斿父瑙勭殑INSERT瑕佹參銆傚浜庢湇鍔″櫒鏉ヨ锛屼负姣忎釜鍚湁寤惰繜琛岀殑琛ㄦ搷绾典竴涓嫭绔嬬殑绾跨▼锛屼篃鏄竴涓澶栫殑绯荤粺寮閿銆傝繖鎰忓懗鐫鍙湁褰撴偍纭鎮ㄩ渶瑕佹椂锛屾墠搴斾娇鐢INSERT DELAYED銆
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char' ]
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...)]
LOAD DATA INFILE璇彞鐢ㄤ簬楂橀熷湴浠庝竴涓枃鏈枃浠朵腑璇诲彇琛岋紝骞惰鍏ヤ竴涓〃涓傛枃浠跺悕绉板繀椤讳负涓涓枃瀛楀瓧绗︿覆銆
瑕佷簡瑙f湁鍏INSERT鍜LOAD DATA INFILE鐨勬晥鐜囩殑瀵规瘮鍜屾湁鍏LOAD DATA INFILE鍔犻熺殑鏇村淇℃伅锛岃鍙傝7.2.16鑺傦紝鈥淚NSERT璇彞鐨勯熷害鈥銆
鐢character_set_database绯荤粺鍙橀噺鎸囩ず鐨勫瓧绗﹂泦琚敤浜庤В閲婃枃浠朵腑鐨勪俊鎭SET NAMES鍜character_set_client鐨勮缃笉浼氬奖鍝嶅杈撳叆鐨勮В閲娿
娉ㄦ剰锛岀洰鍓嶄笉鑳借浇鍏UCS2鏁版嵁鏂囦欢銆
鎮ㄤ篃鍙互閫氳繃浣跨敤mysqlimport搴旂敤绋嬪簭杞藉叆鏁版嵁鏂囦欢锛涢氳繃鍚戞湇鍔″櫒鍙戦佷竴涓LOAD DATA INFILE璇彞瀹炵幇姝ゅ姛鑳姐--local閫夐」鐢ㄤ簬浣mysqlimport浠庡鎴蜂富鏈轰腑璇诲彇鏁版嵁鏂囦欢銆傚鏋滃鎴风鍜屾湇鍔″櫒鏀寔鍘嬬缉鍗忚锛屽垯鎮ㄥ彲浠ユ寚瀹氣compress閫夐」鎻愰珮鍦ㄦ參閫熺綉缁滀腑鐨勬ц兘銆傝鍙傝8.10鑺傦紝鈥渕ysqlimport锛氭暟鎹鍏ョ▼搴銆
濡傛灉鎮ㄤ娇鐢LOW_PRIORITY锛屽垯LOAD DATA璇彞鐨勬墽琛岃寤惰繜锛岀洿鍒版病鏈夊叾瀹冪殑瀹㈡埛绔粠琛ㄤ腑璇诲彇涓烘銆
濡傛灉涓涓MyISAM琛ㄦ弧瓒冲悓鏃舵彃鍏ョ殑鏉′欢锛堝嵆璇ヨ〃鍦ㄤ腑闂存湁绌洪棽鍧楋級锛屽苟涓旀偍瀵硅繖涓MyISAM琛ㄦ寚瀹氫簡CONCURRENT锛屽垯褰LOAD DATA姝e湪鎵ц鏃讹紝鍏跺畠绾跨▼浼氫粠琛ㄤ腑閲嶆柊鑾峰彇鏁版嵁銆傚嵆浣挎病鏈夊叾瀹冪嚎绋嬪湪鍚屾椂浣跨敤鏈〃鏍硷紝浣跨敤鏈夐」涔熶細鐣ュ井褰卞搷LOAD DATA鐨勬ц兘銆
濡傛灉鎸囧畾浜LOCAL锛屽垯琚涓轰笌杩炴帴鐨勫鎴风鏈夊叧锛
路 濡傛灉鎸囧畾浜LOCAL锛屽垯鏂囦欢浼氳瀹㈡埛涓绘満涓婄殑瀹㈡埛绔鍙栵紝骞惰鍙戦佸埌鏈嶅姟鍣ㄣ傛枃浠朵細琚粰浜堜竴涓畬鏁寸殑璺緞鍚嶇О锛屼互鎸囧畾纭垏鐨勪綅缃傚鏋滅粰瀹氱殑鏄竴涓浉瀵圭殑璺緞鍚嶇О锛屽垯姝ゅ悕绉颁細琚悊瑙d负鐩稿浜庡惎鍔ㄥ鎴风鏃舵墍鍦ㄧ殑鐩綍銆
路 濡傛灉LOCAL娌℃湁琚寚瀹氾紝鍒欐枃浠跺繀椤讳綅浜庢湇鍔″櫒涓绘満涓婏紝骞朵笖琚湇鍔″櫒鐩存帴璇诲彇銆
褰撳湪鏈嶅姟鍣ㄤ富鏈轰笂涓烘枃浠跺畾浣嶆椂锛屾湇鍔″櫒浣跨敤浠ヤ笅瑙勫垯锛
路 濡傛灉缁欏畾浜嗕竴涓粷瀵圭殑璺緞鍚嶇О锛屽垯鏈嶅姟鍣ㄤ娇鐢ㄦ璺緞鍚嶇О銆
路 濡傛灉缁欏畾浜嗗甫鏈変竴涓垨澶氫釜寮曞缁勪欢鐨勭浉瀵硅矾寰勫悕绉帮紝鍒欐湇鍔″櫒浼氭悳绱㈢浉瀵逛簬鏈嶅姟鍣ㄦ暟鎹洰褰曠殑鏂囦欢銆
路 濡傛灉缁欏畾浜嗕竴涓笉甯﹀紩瀵肩粍浠剁殑鏂囦欢鍚嶇О锛屽垯鏈嶅姟鍣ㄤ細鍦ㄩ粯璁ゆ暟鎹簱鐨勬暟鎹簱鐩綍涓鎵炬枃浠躲
娉ㄦ剰锛岃繖浜涜鍒欐剰鍛崇潃鍚嶄负./myfile.txt鐨勬枃浠朵細浠庢湇鍔″櫒鏁版嵁鐩綍涓璇诲彇锛岃屽悕涓myfile.txt鐨勫悓鏍风殑鏂囦欢浼氫粠榛樿鏁版嵁搴撶殑鏁版嵁搴撶洰褰曚腑璇诲彇銆備緥濡傦紝涓嬮潰鐨LOAD DATA璇彞浼氫粠db1鏁版嵁搴撶洰褰曚腑璇诲彇鏂囦欢data.txt锛屽洜涓db1鏄綋鍓嶆暟鎹簱銆傚嵆浣胯鍙ユ槑纭妸鏂囦欢杞藉叆鍒db2鏁版嵁搴撲腑鐨勮〃閲岋紝涔熶細浠db1鐩綍涓鍙栥
mysql> USE db1;
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
娉ㄦ剰锛屼娇鐢ㄦ鏂滄潬鎸囧畾Windows璺緞鍚嶇О锛岃屼笉鏄娇鐢ㄥ弽鏂滄潬銆傚鏋滄偍浣跨敤鍙嶆枩鏉狅紝鎮ㄥ繀椤讳娇鐢ㄤ袱涓
鍑轰簬瀹夊叏鍘熷洜锛屽綋璇诲彇浣嶄簬鏈嶅姟鍣ㄤ腑鐨勬枃鏈枃浠舵椂锛屾枃浠跺繀椤讳綅浜庢暟鎹簱鐩綍涓紝鎴栬呮槸鍏ㄤ綋鍙鐨勩傚彟澶栵紝瑕佸鏈嶅姟鍣ㄦ枃浠朵娇鐢LOAD DATA INFILE锛屾偍蹇呴』鎷ユ湁FILE鏉冮檺銆
涓庤鏈嶅姟鍣ㄧ洿鎺ヨ鍙栨枃浠剁浉姣旓紝浣跨敤LOCAL閫熷害鐣ユ參锛岃繖鏄洜涓烘枃浠剁殑鍐呭蹇呴』閫氳繃瀹㈡埛绔彂閫佸埌鏈嶅姟鍣ㄤ笂銆備笉杩囷紝鎮ㄤ笉闇瑕FILE鏉冮檺鏉ヨ浇鍏ユ湰鍦版枃浠躲
鍙湁褰撴偍鐨勬湇鍔″櫒鍜屾偍鐨勫鎴风閮借鍙椂锛LOCAL鎵嶅彲杩愯銆備緥濡傦紝濡傛灉浣跨敤鈥local-infile=0鍚姩mysqld锛屽垯LOCAL涓嶈繍琛屻傝鍙傝5.6.4鑺傦紝鈥淟OAD DATA LOCAL瀹夊叏闂鈥銆
濡傛灉鎮ㄩ渶瑕LOAD DATA鏉ヤ粠涓涓閬撲腑璇诲彇锛屾偍鍙互浣跨敤浠ヤ笅鏂规硶锛堟澶勬垜浠妸/鐩綍娓呭崟杞藉叆涓涓〃鏍硷級锛
mkfifo /mysql/db/x/x
chmod 666 /mysql/db/x/x
find / -ls > /mysql/db/x/x
mysql -e "LOAD DATA INFILE 'x' INTO TABLE x" x
鏈変簺杈撳叆璁板綍鎶婂師鏈夌殑璁板綍澶嶅埗鍒板敮涓鍏抽敭瀛楀间笂銆REPLACE鍜IGNORE鍏抽敭瀛楃敤浜庢帶鍒惰繖浜涜緭鍏ヨ褰曠殑鎿嶄綔銆
濡傛灉鎮ㄦ寚瀹氫簡REPLACE锛屽垯杈撳叆琛屼細鏇挎崲鍘熸湁琛岋紙鎹㈠彞璇濊锛屼笌鍘熸湁琛屼竴鏍凤紝瀵逛竴涓富绱㈠紩鎴栧敮涓绱㈠紩鍏锋湁鐩稿悓鍊肩殑琛岋級銆傝鍙傝13.2.6鑺傦紝鈥淩EPLACE璇硶鈥銆
濡傛灉鎮ㄦ寚瀹IGNORE锛屽垯鎶婂師鏈夎澶嶅埗鍒板敮涓鍏抽敭瀛楀肩殑杈撳叆琛岃璺宠繃銆傚鏋滄偍杩欎袱涓夐」閮戒笉鎸囧畾锛屽垯杩愯鎯呭喌鏍规嵁LOCAL鍏抽敭璇嶆槸鍚﹁鎸囧畾鑰屽畾銆備笉浣跨敤LOCAL鏃讹紝褰撳嚭鐜伴噸澶嶅叧閿瓧鍊兼椂锛屼細鍙戠敓閿欒锛屽苟涓斿墿涓嬬殑鏂囨湰鏂囦欢琚拷鐣ャ備娇鐢LOCAL鏃讹紝榛樿鐨勮繍琛屾儏鍐靛拰IGNORE琚寚瀹氭椂鐨勬儏鍐电浉鍚岋紱杩欐槸鍥犱负鍦ㄨ繍琛屼腑闂达紝鏈嶅姟鍣ㄦ病鏈夊姙娉曚腑姝㈡枃浠剁殑浼犺緭銆
濡傛灉鎮ㄥ笇鏈涘湪杞藉叆杩愯杩囩▼涓拷鐣ュ閿殑闄愬埗锛屾偍鍙互鍦ㄦ墽琛LOAD DATA鍓嶅彂閫佷竴涓SET FOREIGN_KEY_CHECKS=0璇彞銆
濡傛灉鎮ㄥ涓涓┖鐨MyISAM琛ㄤ娇鐢LOAD DATA INFILE锛屽垯鎵鏈夌殑闈炲敮涓绱㈠紩浼氳鍒涘缓鍦ㄤ竴涓嫭绔嬫壒涓紙瀵逛簬REPAIR TABLE锛夈傚綋鎮ㄦ湁璁稿绱㈠紩鏃讹紝杩欓氬父浼氫娇LOAD DATA INFILE澶уぇ鍔犲揩銆傞氬父锛LOAD DATA INFILE鐨勯熷害浼氶潪甯稿揩锛屼絾鏄湪鏌愪簺鏋佺鎯呭喌涓嬶紝鎮ㄥ彲浠ュ湪鎶婃枃浠惰浇鍏ュ埌琛ㄤ腑涔嬪墠浣跨敤ALTER TABLE...DISABLE KEYS鍏抽棴LOAD DATA INFILE锛屾垨鑰呭湪杞藉叆鏂囦欢涔嬪悗浣跨敤ALTER TABLE...ENABLE KEYS鍐嶆鍒涘缓绱㈠紩锛屼娇鍒涘缓绱㈠紩鐨勯熷害鏇村揩銆傝鍙傝7.2.16鑺傦紝鈥淚NSERT璇彞鐨勯熷害鈥銆
LOAD DATA INFILE鏄SELECT...INTO OUTFILE鐨勮ˉ璇傦紙瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆傦級瑕佷粠涓涓〃涓妸鏁版嵁鍐欏叆涓涓枃浠朵腑锛屽簲浣跨敤SELECT...INTO OUTFILE銆傝璇诲彇鏂囦欢锛屾斁鍥炲埌琛ㄤ腑锛屽簲浣跨敤LOAD DATA INFILE銆FIELDS鍜LINES瀛愬彞鐨勮娉曞浜庝袱涓鍙ユ槸涓鏍风殑銆備袱涓瓙鍙ラ兘鏄嚜閫夌殑锛屼絾鏄鏋滀袱涓兘琚寚瀹氫簡锛FIELDS蹇呴』浣嶄簬LINES鐨勫墠闈€
濡傛灉鎮ㄦ寚瀹氫簡涓涓FIELDS瀛愬彞锛屽垯姣忎釜浜氬瓙鍙ワ紙TERMINATED BY, [OPTIONALLY] ENCLOSED BY鍜ESCAPED BY锛変篃鏄嚜閫夌殑銆備笉杩囷紝鎮ㄥ繀椤绘寚瀹氬叾涓嚦灏戜竴涓
濡傛灉鎮ㄤ笉鎸囧畾FIELDS瀛愬彞锛屽垯榛樿鍊间负鍋囪鎮ㄥ啓涓嬪涓嬭鍙ユ椂鐨勫硷細
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
濡傛灉鎮ㄤ笉鎸囧畾LINES瀛愬彞锛屽垯榛樿鍊间负鍋囪鎮ㄥ啓涓嬪涓嬭鍙ユ椂鐨勫硷細
LINES TERMINATED BY '\n' STARTING BY ''
鎹㈠彞璇濊锛屽綋璇诲彇杈撳叆鍊兼椂锛岄粯璁ゅ间細浣LOAD DATA INFILE鎸夊涓嬫柟寮忚繍琛岋細
路 鍦ㄦ柊琛屽瀵绘壘琛岀殑杈圭晫銆
路 涓嶄細璺宠繃浠讳綍琛屽墠缂銆
路 鍦ㄥ埗琛ㄧ澶勬妸琛屽垎瑙d负瀛楁銆
路 涓嶅笇鏈涘瓧娈佃鍖呭惈鍦ㄤ换浣曞紩鍙峰瓧绗︿箣涓
路 鍑虹幇鍒惰〃绗︺佹柊琛屻佹垨鍦ㄢ\鈥欏墠鏈夆\鈥欐椂锛岀悊瑙d负浣滀负瀛楁鍊间竴閮ㄥ垎鐨勬枃瀛楀瓧绗︺
鐩稿弽鐨勶紝褰撶紪鍐欒緭鍑哄兼椂锛岄粯璁ゅ间細浣SELECT...INTO OUTFILE鎸夊涓嬫柟寮忚繍琛岋細
路 鍦ㄥ瓧娈典箣闂村啓鍏ュ埗琛ㄧ銆
路 涓嶆妸瀛楁鍖呭惈鍦ㄤ换浣曞紩鍙峰瓧绗︿腑銆
路 褰撳瓧娈靛间腑鍑虹幇鍒惰〃绗︺佹柊琛屾垨鈥\鈥欐椂锛屼娇鐢ㄢ\鈥欒繘琛岃浆涔夈
路 鍦ㄨ鐨勬湯绔啓鍏ユ柊琛屻
娉ㄦ剰锛岃鍐欏叆FIELDS ESCAPED BY 鈥\\鈥欙紝鎮ㄥ繀椤讳负寰呰鍙栫殑鍊兼寚瀹氫袱涓弽鏂滄潬锛屼綔涓轰竴涓崟鍙嶆枩鏉犱娇鐢ㄣ
娉ㄩ噴锛氬鏋滄偍宸茬粡鍦Windows绯荤粺涓敓鎴愪簡鏂囨湰鏂囦欢锛屾偍鍙兘蹇呴』浣跨敤LINES TERMINATED BY 鈥\r\n鈥欐潵姝g‘鍦拌鍙栨枃浠讹紝鍥犱负Windows绋嬪簭閫氬父浣跨敤涓や釜瀛楃浣滀负涓涓缁堟绗︺傞儴鍒嗙▼搴忥紝姣斿WordPad锛屽綋缂栧啓鏂囦欢鏃讹紝鍙兘浼氫娇鐢\r浣滀负琛岀粓姝㈢銆傝璇诲彇杩欐牱鐨勬枃浠讹紝搴斾娇鐢LINES TERMINATED BY 鈥\r鈥欍
濡傛灉鎵鏈夋偍甯屾湜璇诲叆鐨勮閮藉惈鏈変竴涓偍甯屾湜蹇界暐鐨勫叡鐢ㄥ墠缂锛屽垯鎮ㄥ彲浠ヤ娇鐢'prefix_string'鏉ヨ烦杩囧墠缂锛堝拰鍓嶇紑鍓嶇殑瀛楃锛夈傚鏋滄煇琛屼笉鍖呮嫭鍓嶇紑锛屽垯鏁翠釜琛岃璺宠繃銆傛敞閲婏細prefix_string浼氬嚭鐜板湪涓琛岀殑涓棿銆
绀轰緥锛
mysql> LOAD DATA INFILE '/tmp/test.txt'
-> INTO TABLE test LINES STARTING BY "xxx";
浣跨敤姝よ鍙ワ紝鎮ㄥ彲浠ヨ鍏ュ寘鍚湁濡備笅鍐呭鐨勬枃浠讹細
xxx"row",1
something xxx"row",2
骞跺彧寰楀埌鏁版嵁("row",1)鍜("row",2)銆
IGNORE number LINES閫夐」鍙互琚敤浜庡湪鏂囦欢鐨勫紑濮嬪蹇界暐琛屻備緥濡傦紝鎮ㄥ彲浠ヤ娇鐢IGNORE 1 LINES鏉ヨ烦杩囦竴涓寘鍚垪鍚嶇О鐨勮捣濮嬫爣棰樿锛
mysql> LOAD DATA INFILE '/tmp/test.txt'
-> INTO TABLE test IGNORE 1 LINES;
褰撴偍鑱斿悎浣跨敤SELECT...INTO OUTFILE鍜LOAD DATA INFILE鏉ヤ粠涓涓暟鎹簱涓妸鏁版嵁鍐欏叆涓涓枃浠朵腑锛岀劧鍚庡啀璇诲彇鏂囦欢锛岃繑鍥炲埌鏁版嵁搴撲腑鏃讹紝鐢ㄤ簬涓や釜璇彞鐨field-鍜line-handling閫夐」蹇呴』鍖归厤銆傚惁鍒欙紝LOAD DATA INFILE涓嶄細姝g‘鍦扮悊瑙f枃浠剁殑鍐呭銆傚亣璁炬偍浣跨敤SELECT...INTO OUTFILE鏉ョ紪鍐欎竴涓殑鏂囦欢锛屽瓧娈电敱閫楀彿鍒嗛殧锛
mysql> SELECT * INTO OUTFILE 'data.txt'
-> FIELDS TERMINATED BY ','
-> FROM table2;
瑕佽鍙栫敱閫楀彿鍒嗛殧鐨勬枃浠跺苟杩斿洖锛屽垯姝g‘鐨勮鍙ュ簲璇ユ槸锛
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
-> FIELDS TERMINATED BY ',';
濡傛灉鎮ㄥ皾璇曚娇鐢ㄤ互涓嬫墍绀虹殑璇彞璇诲叆鏂囦欢锛屽垯涓嶄細杩愯锛屽洜涓鸿璇彞鍛戒护LOAD DATA INFILE瀵绘壘浣嶄簬瀛楁涔嬮棿鐨勫埗琛ㄧ锛
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
-> FIELDS TERMINATED BY '\t';
缁撴灉寰堝彲鑳芥槸锛屾瘡涓緭鍏ヨ琚悊瑙d负涓涓崟涓瀛楁銆
LOAD DATA INFILE涔熷彲浠ヨ鐢ㄤ簬璇诲彇浠庡婧愪腑鑾峰彇鐨勬枃浠躲備緥濡傦紝涓涓dBASE鏍煎紡鐨勬枃浠跺叿鏈変互閫楀彿鍒嗛殧骞朵笖鍖呭惈鍦ㄥ弻寮曞彿涓殑瀛楁銆傚鏋滄枃浠朵腑鐨勫悇琛屼互鏂拌涓虹粨灏撅紝鍒欐澶勬墍绀虹殑璇彞鎻忚堪浜嗘偍鍙互鐢ㄤ簬杞藉叆鏂囦欢鐨field-鍜line-handling閫夐」锛
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\n';
鎵鏈field-鎴line-handling閫夐」閮藉彲浠ユ寚瀹氫竴涓┖瀛楃涓('')銆傚鏋滃瓧绗︿覆涓嶆槸绌虹殑锛屽垯FIELDS [OPTIONALLY] ENCLOSED BY鍜FIELDS ESCAPED BY鍊煎繀椤讳负鍗曚竴瀛楃銆FIELDS TERMINATED BY, LINES STARTING BY鍜LINES TERMINATED BY鍊煎彲浠ヨ秴杩囦竴涓瓧绗︺備緥濡傦紝瑕佺紪鍐欑敱鍥炶溅/鎹㈣鎴愬瀛楃浣滀负缁撳熬鐨勮锛屾垨璇诲彇鍖呭惈杩欑被琛岀殑鏂囦欢锛屽垯搴旀寚瀹氫竴涓LINES TERMINATED BY 鈥\r\n鈥欏瓙鍙ャ
濡傛灉jokes琚敱%%缁勬垚鐨勮鍒嗛殧锛瑕佽鍙栧寘鍚jokes鐨勬枃浠讹紝鎮ㄥ彲浠ヨ繖涔堟搷浣滐細
mysql> CREATE TABLE jokes
-> (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> joke TEXT NOT NULL);
mysql> LOAD DATA INFILE '/tmp/jokes.txt' INTO TABLE jokes
-> FIELDS TERMINATED BY ''
-> LINES TERMINATED BY '\n%%\n' (joke);
FIELDS [OPTIONALLY] ENCLOSED BY鐢ㄤ簬鎺у埗瀛楁鐨勫紩鍙枫傚浜庯紙SELECT...INTO OUTFILE锛夛紝濡傛灉鎮ㄥ拷鐣ヤ簡璇嶈OPTIONALLY锛屽垯鎵鏈夌殑瀛楁閮借鍖呭惈鍦ENCLOSED BY瀛楃涓蹭腑銆傛澶勫睍绀轰簡姝ょ被杈撳嚭鐨勪竴涓ず渚嬶紙浣跨敤閫楀彿浣滀负瀛楁鍒嗛殧绗︼級锛
"1","a string","100.20"
"2","a string containing a , comma","102.20"
"3","a string containing a \" quote","102.20"
"4","a string containing a \", quote and comma","102.20"
濡傛灉鎮ㄦ寚瀹氫簡OPTINALLY锛屽垯ENCLOSED BY瀛楃鍙鐢ㄤ簬鍖呭惈鍏锋湁瀛楃涓叉暟鎹被鍨嬶紙姣斿CHAR, BINARY, TEXT鎴ENUM锛夌殑鍒椾腑鐨勫硷細
1,"a string",100.20
2,"a string containing a , comma",102.20
3,"a string containing a \" quote",102.20
4,"a string containing a \", quote and comma",102.20
娉ㄦ剰锛屽鏋滃湪瀛楁鍊煎唴鍑虹幇ENCLOSED BY瀛楃锛屽垯閫氳繃浣跨敤ESCAPED BY瀛楃浣滀负鍓嶇紑锛屽ENCLOSED BY瀛楃杩涜杞箟銆傚彟澶栵紝瑕佹敞鎰忥紝濡傛灉鎮ㄦ寚瀹氫簡涓涓┖鐨ESCAPED BY鍊硷紝鍒欏彲鑳戒細鐢熸垚涓嶈兘琚LOAD DATA INFILE姝g‘璇诲彇鐨勮緭鍑哄笺備緥濡傦紝濡傛灉杞箟绗︿负绌哄瓧绗︼紝鍒欏垰鏄剧ず鐨勫厛鍓嶈緭鍑哄煎簲鏄剧ず濡備笅銆傝瑙傚療锛岀鍥涜涓殑绗簩涓瓧娈靛湪寮曞彿鍚庨潰鍖呭惈涓涓楀彿锛岃寮曞彿锛堥敊璇湴锛夋樉绀哄嚭鏉ワ紝浣滀负瀛楁鐨勭粨灏撅細
1,"a string",100.20
2,"a string containing a , comma",102.20
3,"a string containing a " quote",102.20
4,"a string containing a ", quote and comma",102.20
瀵逛簬杈撳叆鍊硷紝ENCLOSED BY瀛楃琚粠瀛楁瀛楃殑鏈熬鍓ョ銆傦紙涓嶈OPTIONALLY鏄惁琚寚瀹氶兘浼氬墺绂伙紱OPTIONALLY瀵硅緭鍏ュ肩殑瑙i噴娌℃湁褰卞搷銆傦級濡傛灉ENCLOSED BY瀛楃鍓嶉潰甯︽湁ESCAPED BY瀛楃锛屽垯琚悊瑙d负褰撳墠瀛楁鍊肩殑涓閮ㄥ垎銆
濡傛灉瀛楁浠ENCLOSED BY瀛楃涓哄紑濮嬶紝褰撳嚭鐜拌繖绫诲瓧绗︽椂锛屽彧鏈夊悗闈㈡帴鐫瀛楁鎴栬TERMINATED BY搴忓垪鏃讹紝杩欑被瀛楃琚涓烘槸涓涓瓧娈靛肩殑缁撳熬銆備负浜嗛伩鍏嶆剰鎬濅笉鏄庣‘锛屽綋鍦ㄤ竴涓瓧娈靛间腑鍑虹幇ENCLOSED BY瀛楃鏃讹紝姝ゅ瓧绗﹀彲浠ラ噸澶嶄功鍐欙紝骞惰鐞嗚В涓哄崟涓鐨勫瓧绗︺備緥濡傦紝濡傛灉鎸囧畾浜ENCLOSED BY '"'锛屽垯鎸夌収浠ヤ笅鏂规硶鎿嶄綔寮曞彿锛
"The ""BIG"" boss" -> The "BIG" boss
The "BIG" boss -> The "BIG" boss
The ""BIG"" boss -> The ""BIG"" boss
FIELDS ESCAPED BY鐢ㄤ簬鎺у埗濡備綍鍐欏叆鎴栬鍙栫壒娈婂瓧绗︺傚鏋FIELDS ESCAPED BY瀛楃涓嶆槸绌哄瓧绗︼紝鍒欏彲浠ュ湪杈撳嚭涓敤浜庡浠ヤ笅瀛楃鍔犲墠缂锛
路 FIELDS ESCAPED BY瀛楃
路 FIELDS [OPTIONALLY] ENCLOSED BY瀛楃
路 FIELDS TERMINATED BY鍜LINES TERMINATED BY鍊肩殑绗竴涓瓧绗
路 ASCII 0锛堝湪杞箟绗︿箣鍚庣紪鍐欑殑瀛楃瀹為檯涓婃槸ASCII鈥0鈥欙紝鑰屼笉鏄竴涓间负0鐨勫瓧鑺傦級
濡傛灉FIELDS ESCAPED BY瀛楃涓虹┖瀛楃锛屽垯娌℃湁瀛楃琚浆涔夛紝骞朵笖NULL琚綔涓NULL杈撳嚭锛岃屼笉鏄\N銆傚幓鎸囧畾涓涓┖鐨勮浆涔夌涓嶆槸涓涓ソ鍔炴硶锛岀壒鍒槸濡傛灉鏁版嵁鐨勫瓧娈靛煎寘鍚换浣曞垰缁欏畾鐨勬竻鍗曚腑鐨勫瓧绗︽椂锛屾洿涓嶈兘杩欎箞鍋氥
瀵逛簬杈撳叆鍊硷紝濡傛灉FIELDS ESCAPED BY瀛楃涓嶆槸绌哄瓧绗︼紝鍒欏嚭鐜拌繖绉嶅瓧绗︽椂浼氳鍓ョ锛岀劧鍚庝互涓嬪瓧绗﹁浣滀负瀛楁鍊肩殑涓閮ㄥ垎銆備緥澶栨儏鍐垫槸锛岃杞箟鐨勨0鈥欐垨鈥N鈥欙紙渚嬪锛\0鎴\N锛屾鏃惰浆涔夌涓衡\鈥欙級銆傝繖浜涘簭鍒楄鐞嗚В涓ASCII NUL锛堜竴涓浂鍊煎瓧鑺傦級鍜NULL銆傜敤浜NULL澶勭悊鐨勮鍒欏湪鏈妭鐨勫悗閮ㄨ繘琛岃鏄庛
瑕佷簡瑙f湁鍏斥\鈥-escape璇硶鐨勬洿澶氫俊鎭紝璇峰弬瑙9.1鑺傦紝鈥滄枃瀛楀尖銆
鍦ㄧ壒瀹氭儏鍐典笅锛field-鍜line-handling閫夐」鐩镐簰褰卞搷锛
路 濡傛灉LINES TERMINATED BY鏄┖瀛楃涓诧紝骞朵笖FIELDS TERMINATED BY涓嶆槸绌哄瓧绗︿覆锛屽垯鍚勮浠FIELDS TERMINATED BY浣滀负缁撳熬銆
路 濡傛灉FIELDS TERMINATED BY鍜FIELDS ENCLOSED BY鍊煎潎涓虹┖鍊('')锛屽垯浣跨敤鍥哄畾琛岋紙鏃犲垎闅旓級鏍煎紡銆備娇鐢ㄥ浐瀹氳鏍煎紡鏃讹紝鍦ㄥ瓧娈典箣闂翠笉浣跨敤鍒嗛殧绗︼紙浣嗘槸鎮ㄤ粛鐒跺彲浠ユ湁琛岀粓姝㈢锛夈傚垪鍊间娇鐢ㄥ垪鐨勬樉绀哄搴﹁繘琛屽啓鍏ュ拰璇诲彇銆備緥濡傦紝濡傛灉鏌愬垪琚畾涔変负INT(7)锛屽垯浣跨敤7瀛楃瀛楁鍐欏叆鍒楀笺傝緭鍑烘椂锛岄氳繃璇诲彇7涓瓧绗﹁幏鍙栧垪鍊笺
LINES TERMINATED BY浠嶇劧鐢ㄤ簬鍒嗛殧琛屻傚鏋滄煇琛屼笉鍖呭惈鎵鏈夊瓧娈碉紝鍒欏叾浣欑殑鍚勫垪琚缃埌榛樿鍊笺傚鏋滄偍娌℃湁琛岀粓姝㈢锛屾偍搴旇鎶婄粓姝㈢璁剧疆涓''銆傚湪姝ゆ儏鍐典笅锛屾枃鏈枃浠跺繀椤诲寘鍚瘡琛岀殑鎵鏈夊瓧娈点
鍥哄畾琛屾牸寮忎篃浼氬奖鍝NULL鍊肩殑鎿嶄綔锛岃繖灏嗗湪浠ュ悗杩涜浠嬬粛銆傛敞鎰忥紝濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓瀛楄妭瀛楃闆嗭紝鍒欏浐瀹氳鏍兼牸寮忎笉浼氳繍琛屻
鏍规嵁姝e湪浣跨敤涓殑FIELDS鍜LINES閫夐」鐨勪笉鍚岋紝NULL鍊肩殑鎿嶄綔鏈夋墍鍙樺寲锛
路 瀵逛簬榛樿鐨FIELDS鍜LINES鍊硷紝NULL琚綔涓\N鐨勫瓧娈靛肩紪鍐欙紝鐢ㄤ簬杈撳嚭锛\N瀛楁鍊艰浣滀负NULL璇诲彇锛岀敤浜庤緭鍏ワ紙鍋囪ESCAPED BY瀛楃涓衡\鈥欙級銆
路 濡傛灉FIELDS ENCLOSED BY涓嶆槸绌哄硷紝鍒欏寘鍚互鏂囧瓧璇嶈NULL涓哄肩殑瀛楁琚綔涓NULL鍊艰鍙栥傝繖涓庤FIELDS ENCLOSED BY瀛楃鍖呭洿鐨勮瘝璇NULL涓嶅悓銆傝璇嶈琚綔涓哄瓧绗︿覆'NULL'璇诲彇銆
路 濡傛灉FIELDS ESCAPED BY鏄┖鍊硷紝鍒NULL琚綔涓鸿瘝璇NULL鍐欏叆銆
路 閲囩敤鍥哄畾琛屾牸寮忔椂锛堝綋FIELDS TERMINATED BY鍜FIELDS ENCLOSED BY鍧囦负绌哄兼椂閲囩敤锛夛紝NULL琚綔涓轰竴涓┖瀛楃涓插啓鍏ャ傛敞鎰忥紝杩欎細瀵艰嚧鍦ㄨ鍐欏叆鏂囦欢鏃讹紝琛ㄤ腑鐨NULL鍊煎拰绌哄瓧绗︿覆鍧囨棤娉曡鲸鍒紝杩欐槸鍥犱负涓よ呴兘琚綔涓虹┖瀛楃涓插啓鍏ャ傚鏋滄偍闇瑕佸湪璇诲彇鏂囦欢骞惰繑鍥炴椂鑳藉鍒嗚鲸涓よ咃紝鍒欐偍涓嶅簲浣跨敤鍥哄畾琛屾牸寮忋
LOAD DATA INFILE涓嶆敮鎸佹湁浜涙儏鍐碉細
路 鍥哄畾瑙勬牸琛岋紙FIELDS TERMINATED BY鍜FIELDS ENCLOSED BY鍧囦负绌哄硷級鍜BLOB鎴TEXT鍒椼
路 濡傛灉鎮ㄦ寚瀹氫簡涓涓垎闅旂锛屽苟涓旇鍒嗛殧绗︿笌鍏跺畠鐨勫墠缂涓鏍凤紝鍒LOAD DATA INFILE涓嶈兘姝g‘鍦扮悊瑙h緭鍏ュ笺備緥濡傦紝涓嬮潰鐨FIELDS瀛愬彞浼氬鑷撮棶棰橈細
路 FIELDS TERMINATED BY '"' ENCLOSED BY '"'
路 濡傛灉FIELDS ESCAPED BY涓虹┖鍊硷紝鍒欏寘鍚FIELDS ENCLOSED BY鎴LINES TERMINATED BY鐨勫瓧娈靛煎悗闈㈠啀鎺FIELDS TERMINATED BY鍊间細瀵艰嚧LOAD DATA INFILE杩囨棭鍦板仠姝㈣鍙栦竴涓瓧娈垫垨琛屻傚嚭鐜拌繖绉嶆儏鍐电殑鍘熷洜鏄LOAD DATA INFILE涓嶈兘姝g‘鍦板喅瀹氬瓧娈垫垨琛屽煎湪鍝噷缁撴潫銆
浠ヤ笅鐨勪緥瀛愯浇鍏ヤ簡persondata琛ㄤ腑鐨勬墍鏈夊垪锛
mysql> LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
榛樿鎯呭喌涓嬶紝濡傛灉鍦LOAD DATA INFILE璇彞鐨勬湯灏惧娌℃湁璁惧垪娓呭崟鏃讹紝鍒欒緭鍏ヨ棰勮浼氬寘鍚竴涓瓧娈碉紝鐢ㄤ簬琛ㄤ腑鐨勬瘡涓垪銆傚鏋滄偍鍙兂杞藉叆涓涓〃鐨勯儴鍒嗗垪锛屽垯搴旀寚瀹氫竴涓垪娓呭崟锛
mysql> LOAD DATA INFILE 'persondata.txt'
-> INTO TABLE persondata (col1,col2,...);
濡傛灉杈撳叆鏂囦欢涓悇瀛楁鐨勯『搴忎笌琛ㄤ腑鍚勫垪鐨勯『搴忎笉鍚岋紝鎮ㄤ篃蹇呴』鎸囧畾涓涓垪娓呭崟銆傚惁鍒欙紝MySQL涓嶈兘鎶婅緭鍏ュ瓧娈靛拰琛ㄤ腑鐨勫垪鍖归厤璧锋潵銆
鍒楁竻鍗曞彲浠ュ寘鍚垪鍚嶇О鎴栫敤鎴峰彉閲忋傛敮鎸SET瀛愬彞銆傝繖浣挎偍鍙互鎶婅緭鍏ュ艰祴浜堢敤鎴峰彉閲忥紝鐒跺悗鍦ㄦ妸缁撴灉璧嬩簣鍒椾箣鍓嶏紝瀵硅繖浜涘艰繘琛屽彉鎹€
SET瀛愬彞涓殑鐢ㄦ埛鍙橀噺鍙互閲囩敤澶氱鏂瑰紡浣跨敤銆備互涓嬩緥瀛愪娇鐢ㄦ暟鎹枃浠朵腑鐨勭涓鍒楋紝鐩存帴鐢ㄤ簬t1.column1鐨勫笺傚湪鐢ㄦ埛鍙橀噺琚敤浜t2.column2鍊间箣鍓嶏紝鎶婄浜屽垪璧嬩簣鐢ㄦ埛鍙橀噺銆傝鍙橀噺浠庡睘浜庝竴涓垎鍓茶繍琛屻
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, @var1)
SET column2 = @var1/100;
SET瀛愬彞鍙互琚敤浜庢彁渚涗笉鏄潵婧愪簬杈撳叆鏂囦欢鐨勫笺備互涓嬭鍙ユ妸column3璁剧疆涓哄綋鍓嶇殑鏃ユ湡鍜屾椂闂达細
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
鎮ㄤ篃鍙互閫氳繃鎶婅緭鍏ュ艰祴浜堜竴涓敤鎴峰彉閲忥紝鍚屾椂涓嶆妸鍙橀噺璧嬩簣琛ㄤ腑鐨勫垪锛屾潵涓㈠純姝よ緭鍏ュ硷細
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, @dummy, column2, @dummy, column3);
鍒/鍙橀噺娓呭崟鍜SET瀛愬彞鐨勪娇鐢ㄥ彈鍒颁互涓嬮檺瀹氾細
路 鍦SET瀛愬彞涓殑璧嬪煎簲鍙惈鏈変綅浜庤祴鍊兼搷浣滅鐨勫乏渚х殑鍒楀悕绉般
路 鎮ㄥ彲浠ュ湪SET璧嬪肩殑鍙充晶浣跨敤瀛愭煡璇€傚鏋滃瓙鏌ヨ鍙互杩斿洖涓涓硷紝骞朵笖姝ゅ煎皢琚祴浜堝埌涓涓垪涓紝鍒欐瀛愭煡璇㈠彧鑳芥槸鏍囬噺瀛愭煡璇€傚彟澶栵紝鎮ㄤ笉鑳戒娇鐢ㄥ瓙鏌ヨ浠庝竴涓鍦ㄨ杞藉叆鐨勮〃涓夋嫨銆
路 瀵逛簬浜庡垪/鍙橀噺娓呭崟鎴SET瀛愬彞锛岃IGNORE瀛愬彞蹇界暐鐨勮涓嶈澶勭悊銆
路 褰撹浇鍏ラ噰鐢ㄥ浐瀹氳鏍煎紡鐨勬暟鎹椂锛屼笉鑳戒娇鐢ㄧ敤鎴峰彉閲忥紝鍥犱负鐢ㄦ埛鍙橀噺娌℃湁鏄剧ず瀹藉害銆
褰撳鐞嗕竴涓緭鍏ヨ鏃讹紝LOAD DATA浼氫緷鎹垪/鍙橀噺娓呭崟鍜SET瀛愬彞锛屾妸琛屾媶鍒嗘垚瀛楁锛屽苟浣跨敤鍊笺傜劧鍚庯紝寰楀埌鐨勮琚彃鍏ヨ〃涓傚鏋滄湁鐢ㄤ簬琛ㄧ殑BEFORE INSERT鎴AFTER INSERT瑙﹀彂鍣紝鍒欏湪鎻掑叆琛屼箣鍓嶅拰鎻掑叆琛屼箣鍚庡垎鍒惎鍔ㄨЕ鍙戝櫒銆
濡傛灉涓涓緭鍏ヨ鍚湁杩囧鐨勫瓧娈碉紝鍒欏浣欑殑瀛楁琚拷鐣ワ紝骞朵笖璀﹀憡鐨勬暟閲忓鍔犮
濡傛灉涓涓緭鍏ヨ鍚湁鐨勫瓧娈佃繃灏戯紝鍒欒緭鍏ュ瓧娈电己澶辩殑琛ㄤ腑鐨勫垪琚缃负榛樿鍊笺傞粯璁ゅ艰祴鍊煎湪13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥涓繘琛屼簡璇存槑銆
濡傛灉瀛楁鍊肩己澶憋紝鍒欏涓涓┖瀛楁鍊间細琚寜涓嶅悓鏂瑰紡鐞嗚В锛
路 瀵逛簬瀛楃涓茬被鍨嬶紝鍒楄璁剧疆涓虹┖瀛楃涓层
路 瀵逛簬鏁板瓧绫诲瀷锛屽垪琚缃负0銆
路 瀵逛簬鏃ユ湡鍜屾椂闂寸被鍨嬶紝鍒楄璁剧疆涓鸿绫诲瀷鐩稿簲鐨勨zero鈥濄傝鍙傝11.3鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷鈥銆
濡傛灉鎮ㄦ槑纭湴鎶婁竴涓┖瀛楃涓茶祴浜堜竴涓INSERT鎴UPDATE璇彞涓殑瀛楃涓茬被鍨嬨佹暟瀛楃被鍨嬫垨鏃ユ湡鎴栨椂闂寸被鍨嬶紝鍒欎骇鐢熺殑杩欎簺鍊肩浉鍚屻
鍙湁鍦ㄤ袱绉嶆儏鍐典笅TIMESTAMP鍒楄璁剧疆涓哄綋鍓嶆棩鏈熷拰鏃堕棿銆備竴绉嶆儏鍐垫椂褰撳垪鏈変竴涓NULL鍊硷紙涔熷氨鏄\N锛夋椂锛涘彟涓绉嶆儏鍐垫槸锛堜粎瀵逛簬绗竴涓TIMESTAMP鍒楋級锛屽綋涓涓瓧娈垫竻鍗曡鎸囧畾鏃讹紝TIMESTAMP鍒椾細浠庡瓧娈垫竻鍗曚腑琚暐鍘汇
LOAD DATA INFILE鎶婃墍鏈夌殑杈撳叆鍊煎綋浣滃瓧绗︿覆锛屾墍浠ユ偍涓嶈兘鎸夌収浣跨敤INSERT璇彞鐨勬柟寮忎娇鐢ENUM鎴SET鍒楃殑鏁板瓧鍊笺傛墍鏈夌殑ENUM鍜SET鍊煎繀椤昏鎸囧畾涓哄瓧绗︿覆銆
褰LOAD DATA INFILE璇彞缁撴潫鏃讹紝浼氭寜浠ヤ笅鏍煎紡杩斿洖涓涓俊鎭瓧绗︿覆锛
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
濡傛灉鎮ㄦ鍦ㄤ娇鐢C API锛屾偍鍙互閫氳繃璋冪敤mysql_info()鍑芥暟鑾峰彇鏈夊叧璇彞鐨勪俊鎭傝鍙傝25.2.3.34鑺傦紝鈥渕ysql_info()鈥銆
褰撳奸氳繃INSERT璇彞琚彃鍏ユ椂鎴栧嚭鐜扮浉鍚屾儏鍐垫椂锛屼細鍙戠敓璀﹀憡锛堣13.2.4鑺傦紝鈥淚NSERT璇硶鈥锛夈備緥澶栨儏鍐垫槸锛屽綋杈撳叆琛屼腑瀛楁杩囧鎴栬繃灏戞椂锛LOAD DATA INFILE涔熺敓鎴愯鍛娿傝繖浜涜鍛婂苟涓嶅瓨鍌紱璀﹀憡鐨勬暟閲忓彧鐢ㄤ簬鎸囩ず杩愯鏄惁鑹ソ銆
鎮ㄥ彲浠ヤ娇鐢SHOW WARNINGS鏉ュ緱鍒扮涓鎵max_error_count璀﹀憡鐨勬竻鍗曪紝浣滀负鏈夊叧杩愯閿欒鐨勪俊鎭傝鍙傝13.5.4.22鑺傦紝鈥淪HOW WARNINGS璇硶鈥銆
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
鎴栵細
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
鎴栵細
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT ...
REPLACE鐨勮繍琛屼笌INSERT寰堢浉鍍忋傚彧鏈変竴鐐归櫎澶栵紝濡傛灉琛ㄤ腑鐨勪竴涓棫璁板綍涓庝竴涓敤浜PRIMARY KEY鎴栦竴涓UNIQUE绱㈠紩鐨勬柊璁板綍鍏锋湁鐩稿悓鐨勫硷紝鍒欏湪鏂拌褰曡鎻掑叆涔嬪墠锛屾棫璁板綍琚垹闄ゃ傝鍙傝13.2.4鑺傦紝鈥淚NSERT璇硶鈥銆
娉ㄦ剰锛岄櫎闈炶〃鏈変竴涓PRIMARY KEY鎴UNIQUE绱㈠紩锛屽惁鍒欙紝浣跨敤涓涓REPLACE璇彞娌℃湁鎰忎箟銆傝璇彞浼氫笌INSERT鐩稿悓锛屽洜涓烘病鏈夌储寮曡鐢ㄤ簬纭畾鏄惁鏂拌澶嶅埗浜嗗叾瀹冪殑琛屻
鎵鏈夊垪鐨勫煎潎鍙栬嚜鍦REPLACE璇彞涓鎸囧畾鐨勫笺傛墍鏈夌己澶辩殑鍒楄璁剧疆涓哄悇鑷殑榛樿鍊硷紝杩欏拰INSERT涓鏍枫傛偍涓嶈兘浠庡綋鍓嶈涓紩鐢ㄥ硷紝涔熶笉鑳藉湪鏂拌涓娇鐢ㄥ笺傚鏋滄偍浣跨敤涓涓緥濡傗SET col_name = col_name + 1鈥濈殑璧嬪硷紝鍒欏浣嶄簬鍙充晶鐨勫垪鍚嶇О鐨勫紩鐢ㄤ細琚綔涓DEFAULT(col_name)澶勭悊銆傚洜姝わ紝璇ヨ祴鍊肩浉褰撲簬SET col_name = DEFAULT(col_name) + 1銆
涓轰簡鑳藉浣跨敤REPLACE锛屾偍蹇呴』鍚屾椂鎷ユ湁琛ㄧ殑INSERT鍜DELETE鏉冮檺銆
REPLACE璇彞浼氳繑鍥炰竴涓暟锛屾潵鎸囩ず鍙楀奖鍝嶇殑琛岀殑鏁扮洰銆傝鏁版槸琚垹闄ゅ拰琚彃鍏ョ殑琛屾暟鐨勫拰銆傚鏋滃浜庝竴涓崟琛REPLACE璇ユ暟涓1锛屽垯涓琛岃鎻掑叆锛屽悓鏃舵病鏈夎琚垹闄ゃ傚鏋滆鏁板ぇ浜1锛屽垯鍦ㄦ柊琛岃鎻掑叆鍓嶏紝鏈変竴涓垨澶氫釜鏃ц琚垹闄ゃ傚鏋滆〃鍖呭惈澶氫釜鍞竴绱㈠紩锛屽苟涓旀柊琛屽鍒朵簡鍦ㄤ笉鍚岀殑鍞竴绱㈠紩涓殑涓嶅悓鏃ц鐨勫硷紝鍒欐湁鍙兘鏄竴涓崟涓琛屾浛鎹簡澶氫釜鏃ц銆
鍙楀奖鍝嶇殑琛屾暟鍙互瀹规槗鍦扮‘瀹氭槸鍚REPLACE鍙坊鍔犱簡涓琛岋紝鎴栬呮槸鍚REPLACE涔熸浛鎹簡鍏跺畠琛岋細妫鏌ヨ鏁版槸鍚︿负1锛堟坊鍔狅級鎴栨洿澶э紙鏇挎崲锛夈
濡傛灉鎮ㄦ鍦ㄤ娇鐢C API锛屽垯鍙互浣跨敤mysql_affected_rows()鍑芥暟鑾峰緱鍙楀奖鍝嶇殑琛屾暟銆
鐩墠锛屾偍涓嶈兘鍦ㄤ竴涓瓙鏌ヨ涓紝鍚戜竴涓〃涓洿鎹紝鍚屾椂浠庡悓涓涓〃涓夋嫨銆
浠ヤ笅鏄墍鐢ㄧ畻娉曠殑鏇磋缁嗙殑璇存槑锛堣绠楁硶涔熺敤浜LOAD DATA...REPLACE锛夛細
1. 灏濊瘯鎶婃柊琛屾彃鍏ュ埌琛ㄤ腑
2. 褰撳洜涓哄浜庝富閿垨鍞竴鍏抽敭瀛楀嚭鐜伴噸澶嶅叧閿瓧閿欒鑰岄犳垚鎻掑叆澶辫触鏃讹細
a. 浠庤〃涓垹闄ゅ惈鏈夐噸澶嶅叧閿瓧鍊肩殑鍐茬獊琛
b. 鍐嶆灏濊瘯鎶婃柊琛屾彃鍏ュ埌琛ㄤ腑
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr, ...
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name']
[FROM table_references
[WHERE where_definition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_definition]
[ORDER BY {col_name | expr | position}
[ASC | DESC] , ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]
SELECT鐢ㄤ簬鎭㈠浠庝竴涓垨澶氫釜琛ㄤ腑閫夋嫨鐨勮锛屽苟鍙互鍔犲叆UNION璇彞鍜屽瓙鏌ヨ銆傝鍙傝13.2.7.2鑺傦紝鈥淯NION璇硶
鈥鍜13.2.8鑺傦紝鈥淪ubquery璇硶鈥銆
路 姣忎釜select_expr閮芥寚绀轰竴涓偍鎯宠鎭㈠鐨勫垪銆
路 table_references鎸囩ず琛屼粠鍝釜琛ㄦ垨鍝簺琛ㄤ腑琚仮澶嶃傚湪13.2.7.1鑺傦紝鈥淛OIN璇硶鈥涓璇ヨ娉曡繘琛屼簡璇存槑銆
路 where_definition鍖呮嫭鍏抽敭璇WHERE锛屽悗闈㈡帴涓涓〃杈惧紡銆傝琛ㄨ揪寮忔寚绀鸿閫夋嫨鐨勮蹇呴』婊¤冻鐨勬潯浠躲
鏈夌殑琛屽湪璁$畻鏃舵湭寮曠敤浠讳綍琛ㄣSELECT涔熷彲浠ョ敤浜庢仮澶嶈繖绫昏銆
涓句緥璇存槑锛
mysql> SELECT 1 + 1;
-> 2
鎵鏈夎浣跨敤鐨勫瓙鍙ュ繀椤绘寜璇硶璇存槑涓樉绀虹殑椤哄簭涓ユ牸鍦版帓搴忋備緥濡傦紝涓涓HAVING瀛愬彞蹇呴』浣嶄簬GROUP BY瀛愬彞涔嬪悗锛屽苟浣嶄簬ORDER BY瀛愬彞涔嬪墠銆
路 浣跨敤AS alias_name鍙互涓select_expr缁欏畾涓涓埆鍚嶃傛鍒悕鐢ㄤ綔琛ㄨ揪寮忕殑鍒楀悕锛屽彲浠ョ敤浜GROUP BY銆ORDER BY鎴HAVING瀛愬彞銆備緥濡傦細
路 mysql> SELECT CONCAT(last_name,', ',first_name) AS full_name
路 -> FROM mytable ORDER BY full_name;
鍦ㄤ负select_expr缁欏畾鍒悕鏃讹紝AS鍏抽敭璇嶆槸鑷夌殑銆傚墠闈㈢殑渚嬪瓙鍙互杩欐牱缂栧啓锛
mysql> SELECT CONCAT(last_name,', ',first_name) full_name
-> FROM mytable ORDER BY full_name;
鍥犱负AS鏄嚜閫夌殑锛屽鏋滄偍蹇樿鍦ㄤ袱涓select_expr琛ㄨ揪寮忎箣闂村姞閫楀彿锛屽垯浼氬嚭鐜颁竴涓皬闂锛MySQL浼氭妸绗簩涓〃杈惧紡鐞嗚В涓轰竴涓埆鍚嶃備緥濡傦紝鍦ㄤ互涓嬭鍙ヤ腑锛columnb琚綔涓哄埆鍚嶅寰咃細
mysql> SELECT columna columnb FROM mytable;
鍥犳锛屼娇鐢AS鏄庣‘鍦版寚瀹氬垪鐨勫埆鍚嶏紝鎶婂畠浣滀负涔犳儻锛屾槸涓涓壇濂界殑鎿嶄綔瑙勮寖銆
路 鍦ㄤ竴涓WHERE瀛愬彞涓娇鐢ㄥ垪鍒悕鏄笉鍏佽鐨勶紝鍥犱负褰撴墽琛WHERE瀛愬彞鏃讹紝鍒楀煎彲鑳借繕娌℃湁琚‘瀹氥傝鍙傝A.5.4鑺傦紝鈥滀笌鍒楀埆鍚嶆湁鍏崇殑闂鈥銆
路 FROM table_references瀛愬彞鎸囩ず琛屼粠鍝簺琛ㄤ腑琚仮澶嶃傚鏋滄偍鍛藉悕鐨勮〃澶氫簬涓涓紝鍒欐偍鍦ㄨ繘琛屼竴涓仈鍚堟搷浣溿傝浜嗚В鏈夊叧鑱斿悎璇硶鐨勮鏄庯紝璇峰弬瑙13.2.7.1鑺傦紝鈥淛OIN璇硶鈥銆傚浜庢瘡涓涓鎸囧畾鐨勮〃锛屾偍鍙互鑷夊湴鎸囧畾涓涓埆鍚嶃
路 tbl_name [[AS] alias]
路 [{USE|IGNORE|FORCE} INDEX (key_list)]
浣跨敤USE INDEX銆IGNORE INDEX銆FORCE INDEX鍙互鍚戜紭鍖栫鎻愮ず濡備綍閫夋嫨绱㈠紩銆傝繖閮ㄥ垎鍐呭鍦13.2.7.1鑺傦紝鈥淛OIN璇硶鈥涓繘琛屼簡璁ㄨ銆
鎮ㄥ彲浠ヤ娇鐢SET max_seeks_for_key=value浣滀负涓绉嶆浛浠f柟娉曪紝鏉ヤ績浣MySQL浼樺厛閲囩敤鍏抽敭瀛楁壂鎻忥紝鏇夸唬琛ㄦ壂鎻忋
路 鎮ㄥ彲浠ユ妸褰撳墠鏁版嵁搴撲腑鐨勪竴涓〃浣滀负tbl_name锛堝湪褰撳墠鏁版嵁搴撲腑锛夊紩鐢紝鎴栦綔涓db_name.tbl_name寮曠敤锛屾潵鏄庣‘鍦版寚瀹氫竴涓暟鎹簱銆傛偍鍙互鎶婁竴鍒椾綔涓col_name, tbl_name.col_name寮曠敤鎴栦綔涓db_name.tbl_name.col_name寮曠敤銆傛偍涓嶉渶瑕佸涓涓垪寮曠敤鎸囧畾涓涓tbl_name鎴db_name.tbl_name鍓嶇紑锛岄櫎闈炴寮曠敤鎰忎箟涓嶆槑纭傛剰涔変笉鏄庣‘鏃讹紝瑕佹眰鎸囧畾鏄庣‘鐨勫垪寮曠敤鏍煎紡銆傛湁鍏崇ず渚嬭9.2鑺傦紝鈥滄暟鎹簱銆佽〃銆佺储寮曘佸垪鍜屽埆鍚嶁銆
路 鍦ㄦ病鏈夎〃琚紩鐢ㄧ殑鎯呭喌涓嬶紝鍏佽鎮ㄦ寚瀹DUAL浣滀负涓涓亣鐨勮〃鍚嶃
路 mysql> SELECT 1 + 1 FROM DUAL;
路 -> 2
鏈変簺鏈嶅姟鍣ㄨ姹備竴涓FROM瀛愬彞銆DUAL浠呯敤浜庝笌杩欎簺鏈嶅姟鍣ㄥ吋瀹广傚鏋滄病鏈夎〃琚紩鐢紝鍒MySQL涓嶈姹傝瀛愬彞锛屽墠闈㈢殑璇彞鍙互鎸変互涓嬫柟娉曠紪鍐欙細
mysql> SELECT 1 + 1;
-> 2
路 浣跨敤tbl_name AS alias_name鎴tbl_name alias_name鍙互涓轰竴涓〃寮曠敤璧峰埆鍚嶏細
路 mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
路 -> WHERE t1.name = t2.name;
路 mysql> SELECT t1.name, t2.salary FROM employee t1, info t2
路 -> WHERE t1.name = t2.name;
路 鍦WHERE瀛愬彞涓紝鎮ㄥ彲浠ヤ娇鐢MySQL鏀寔鐨勬墍鏈夊嚱鏁帮紝涓嶈繃鎬昏锛堟荤粨锛夊嚱鏁伴櫎澶栥傝鍙傝绗12绔狅細鍑芥暟鍜屾搷浣滅銆
路 琚夋嫨鐨勭敤浜庤緭鍑虹殑鍒楀彲浠ヤ娇鐢ㄥ垪鍚嶇О銆佸垪鍒悕鎴栧垪浣嶇疆琚紩鐢ㄥ埌ORDER BY鍜GROUP BY瀛愬彞涓傚垪浣嶇疆涓烘暣鏁帮紝浠1寮濮嬶細
路 mysql> SELECT college, region, seed FROM tournament
路 -> ORDER BY region, seed;
路 mysql> SELECT college, region AS r, seed AS s FROM tournament
路 -> ORDER BY r, s;
路 mysql> SELECT college, region, seed FROM tournament
路 -> ORDER BY 2, 3;
瑕佷互鐩稿弽鐨勯『搴忚繘琛屽垎绫伙紝搴旀妸DESC锛堥檷搴忥級鍏抽敭瀛楁坊鍔犲埌ORDER BY瀛愬彞涓殑鍒楀悕绉颁腑銆傞粯璁ゅ间负鍗囧簭锛涜鍊煎彲浠ヤ娇鐢ASC鍏抽敭璇嶆槑纭湴鎸囧畾銆
涓嶅缓璁娇鐢ㄥ垪浣嶇疆锛屽洜涓鸿璇硶宸茬粡浠SQL鏍囧噯涓垹闄ゃ
路 濡傛灉鎮ㄤ娇鐢GROUP BY锛屽垯杈撳嚭琛屾牴鎹GROUP BY鍒楄繘琛屽垎绫伙紝濡傚悓鎮ㄥ鐩稿悓鐨勫垪杩涜浜ORDER BY銆MySQL瀵GROUP BY杩涜浜嗘墿灞曪紝鍥犳鎮ㄥ彲浠ュ湪鍚勫垪锛堝湪瀛愬彞涓繘琛屽懡鍚嶏級鐨勫悗闈㈡寚瀹ASC鍜DESC锛
路 SELECT a, COUNT(b) FROM test_table GROUP BY a DESC
路
MySQL瀵GROUP BY鐨勪娇鐢ㄨ繘琛屼簡鎵╁睍锛屽厑璁搁夋嫨鍦GROUP
BY瀛愬彞涓病鏈夎鎻愬埌鐨勫瓧娈点傚鏋滄偍娌℃湁寰楀埌棰勬湡鐨勭粨鏋滐紝璇烽槄璇GROUP BY鐨勮鏄庯紝璇峰弬瑙12.10鑺傦紝鈥滀笌GROUP BY瀛愬彞鍚屾椂浣跨敤鐨勫嚱鏁板拰淇敼绋嬪簭鈥銆
路 GROUP BY鍏佽涓涓WITH ROLLUP淇グ绗︺傝鍙傝12.10.2鑺傦紝鈥淕ROUP BY淇敼绋嬪簭鈥銆
路 HAVING瀛愬彞鍩烘湰涓婃槸鏈鍚庝娇鐢紝鍙綅浜庤鍙戦佺粰瀹㈡埛绔殑鏉$洰涔嬪墠锛屾病鏈夎繘琛屼紭鍖栥傦紙LIMIT鐢ㄤ簬HAVING涔嬪悗銆傦級
SQL鏍囧噯瑕佹眰HAVING蹇呴』寮曠敤GROUP BY瀛愬彞涓殑鍒楁垨鐢ㄤ簬鎬昏鍑芥暟涓殑鍒椼備笉杩囷紝MySQL鏀寔瀵规宸ヤ綔鎬ц川鐨勬墿灞曪紝骞跺厑璁HAVING鍥犱负SELECT娓呭崟涓殑鍒楀拰澶栭儴瀛愭煡璇腑鐨勫垪銆
濡傛灉HAVING瀛愬彞寮曠敤浜嗕竴涓剰涔変笉鏄庣‘鐨勫垪锛屽垯浼氬嚭鐜拌鍛娿傚湪涓嬮潰鐨勮鍙ヤ腑锛col2鎰忎箟涓嶆槑纭紝鍥犱负瀹冩棦浣滀负鍒悕浣跨敤锛屽張浣滀负鍒楀悕浣跨敤锛
mysql> SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;
鏍囧噯SQL宸ヤ綔鎬ц川鍏锋湁浼樺厛鏉冿紝鍥犳濡傛灉涓涓HAVING鍒楀悕鏃㈣鐢ㄤ簬GROUP BY锛屽張琚敤浣滆緭鍑哄垪娓呭崟涓殑璧蜂簡鍒悕鐨勫垪锛屽垯浼樺厛鏉冭缁欎簣GROUP BY鍒椾腑鐨勫垪銆
路 HAVING涓嶈兘鐢ㄤ簬搴旇鐢ㄤ簬WHERE瀛愬彞鐨勬潯鐩備緥濡傦紝涓嶈兘缂栧啓濡備笅璇彞锛
路 mysql> SELECT col_name FROM tbl_name HAVING col_name > 0;
鑰屽簲杩欎箞缂栧啓锛
mysql> SELECT col_name FROM tbl_name WHERE col_name > 0;
路 HAVING瀛愬彞鍙互寮曠敤鎬昏鍑芥暟锛岃WHERE瀛愬彞涓嶈兘寮曠敤锛
路 mysql> SELECT user, MAX(salary) FROM users
路 -> GROUP BY user HAVING MAX(salary)>10;
锛堝湪鏈変簺杈冩棭鐗堟湰鐨MySQL涓紝鏈鍙ヤ笉杩愯銆傦級
路 LIMIT瀛愬彞鍙互琚敤浜庨檺鍒惰SELECT璇彞杩斿洖鐨勮鏁般LIMIT鍙栦竴涓垨涓や釜鏁板瓧鑷彉閲忥紝鑷彉閲忓繀椤绘槸闈炶礋鐨勬暣鏁板父鏁帮紙褰撲娇鐢ㄥ凡棰勫鐨勮鍙ユ椂闄ゅ锛夈
浣跨敤涓や釜鑷彉閲忔椂锛岀涓涓嚜鍙橀噺鎸囧畾杩斿洖鐨勭涓琛岀殑鍋忕Щ閲忥紝绗簩涓嚜鍙橀噺鎸囧畾杩斿洖鐨勮鏁扮殑鏈澶у笺傚垵濮嬭鐨勫亸绉婚噺涓0锛堜笉鏄1锛夛細
mysql> SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
涓轰簡涓PostgreSQL鍏煎锛MySQL涔熸敮鎸LIMIT row_count OFFSET offset璇硶銆
濡傛灉瑕佹仮澶嶄粠鏌愪釜鍋忕Щ閲忓埌缁撴灉闆嗗悎鐨勬湯绔箣闂寸殑鎵鏈夌殑琛岋紝鎮ㄥ彲浠ュ绗簩涓弬鏁版槸浣跨敤姣旇緝澶х殑鏁般傛湰璇彞鍙互鎭㈠浠庣96琛屽埌鏈鍚庣殑鎵鏈夎锛
mysql> SELECT * FROM tbl LIMIT 95,18446744073709551615;
浣跨敤1涓嚜鍙橀噺鏃讹紝璇ュ兼寚瀹氫粠缁撴灉闆嗗悎鐨勫紑澶磋繑鍥炵殑琛屾暟锛
mysql> SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
鎹㈠彞璇濊锛LIMIT n涓LIMIT 0,n绛変环銆
瀵逛簬宸查澶囩殑璇彞锛屾偍鍙互浣跨敤浣嶇疆淇濇寔绗︺備互涓嬭鍙ュ皢浠tb1琛ㄤ腑杩斿洖涓琛岋細
mysql> SET @a=1;
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?";
mysql> EXECUTE STMT USING @a;
浠ヤ笅璇彞灏嗕粠tb1琛ㄤ腑杩斿洖绗簩鍒扮鍏锛
mysql> SET @skip=1; SET @numrows=5;
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?";
mysql> EXECUTE STMT USING @skip, @numrows;
路 SELECT...INTO OUTFILE 'file_name'褰㈠紡鐨SELECT鍙互鎶婅閫夋嫨鐨勮鍐欏叆涓涓枃浠朵腑銆傝鏂囦欢琚垱寤哄埌鏈嶅姟鍣ㄤ富鏈轰笂锛屽洜姝ゆ偍蹇呴』鎷ユ湁FILE鏉冮檺锛屾墠鑳戒娇鐢ㄦ璇硶銆file_name涓嶈兘鏄竴涓師鏈夌殑鏂囦欢銆傚師鏈夋枃浠朵細闃绘渚嬪鈥/etc/passwd鈥濈殑鏂囦欢鍜屾暟鎹簱琛ㄨ閿姣併
SELECT...INTO OUTFILE璇彞鐨勪富瑕佷綔鐢ㄦ槸璁╂偍鍙互闈炲父蹇熷湴鎶婁竴涓〃杞偍鍒版湇鍔″櫒鏈哄櫒涓娿傚鏋滄偍鎯宠鍦ㄦ湇鍔″櫒涓绘満涔嬪鐨勯儴鍒嗗鎴蜂富鏈轰笂鍒涘缓缁撴灉鏂囦欢锛屾偍涓嶈兘浣跨敤SELECT...INTO OUTFILE銆傚湪杩欑鎯呭喌涓嬶紝鎮ㄥ簲璇ュ湪瀹㈡埛涓绘満涓婁娇鐢ㄦ瘮濡傗mysql 鈥e "SELECT ..." > file_name鈥濈殑鍛戒护锛屾潵鐢熸垚鏂囦欢銆
SELECT...INTO OUTFILE鏄LOAD DATA INFILE鐨勮ˉ璇紱鐢ㄤ簬璇彞鐨exort_options閮ㄥ垎鐨勮娉曞寘鎷儴鍒FIELDS鍜LINES瀛愬彞锛岃繖浜涘瓙鍙ヤ笌LOAD DATA INFILE璇彞鍚屾椂浣跨敤銆傝鍙傝13.2.5鑺傦紝鈥淟OAD DATA INFILE璇硶鈥銆
FIELDS ESCAPED BY鐢ㄤ簬鎺у埗濡備綍鍐欏叆鐗规畩瀛楃銆傚鏋FIELDS ESCAPED BY瀛楃涓嶆槸绌哄瓧绗︼紝鍒欒鐢ㄤ簬鍦ㄨ緭鍑轰腑瀵逛互涓嬪瓧绗﹁鍓嶇紑锛
o FIELDS ESCAPED BY瀛楃
o FIELDS [OPTIONALLY] ENCLOSED BY瀛楃
o FIELDS TERMINATED BY鍜LINES TERMINATED BY鍊肩殑绗竴涓瓧绗
o ASCII 0锛堝湪缂栧啓鏃舵帴鍦ㄨ浆涔夌鍚庨潰鐨勬槸ASCII 鈥0鈥欙紝鑰屼笉鏄竴涓浂鍊煎瓧鑺傦級
濡傛灉FIELDS ESCAPED BY瀛楃鏄┖瀛楃锛屽垯娌℃湁瀛楃琚浆涔夛紝骞朵笖NULL琚綔涓NULL杈撳嚭锛岃屼笉鏄綔涓\N杈撳嚭銆傛寚瀹氫竴涓┖鐨勮浆涔夌涓嶆槸涓涓ソ鐨勪富鎰忋傜壒鍒槸褰撴偍鐨勬暟鎹腑鐨勫瓧娈靛煎寘鍚垰琚粰浜堢殑娓呭崟涓殑瀛楃鏃讹紝鏇存槸濡傛銆
鍏跺師鍥犳槸鎮ㄥ繀椤诲鎵鏈FIELDS TERMINATED BY, ENCLOSED BY, ESCAPED BY鎴LINES TERMINATED BY瀛楃杩涜杞箟锛屾墠鑳藉彲闈犲湴璇诲彇鏂囦欢骞惰繑鍥炪ASCII NUL琚浆涔夛紝浠ヤ究鏇村鏄撳湴浣跨敤璋冮〉绋嬪簭瑙傜湅銆
鐢熸垚鐨勬枃浠朵笉蹇呯鍚SQL璇硶锛屾墍浠ユ病鏈夊叾瀹冪殑瀛楃闇瑕佽杞箟銆
鍦ㄤ笅闈㈢殑渚嬪瓙涓紝鐢熸垚涓涓枃浠讹紝鍚勫肩敤閫楀彿闅斿紑銆傝繖绉嶆牸寮忓彲浠ヨ璁稿绋嬪簭浣跨敤銆
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
路 濡傛灉鎮ㄤ娇鐢INTO DUMPFILE浠f浛INTO OUTFILE锛屽垯MySQL鍙妸涓琛屽啓鍏ュ埌鏂囦欢涓紝涓嶅浠讳綍鍒楁垨琛岃繘琛岀粓姝紝涔熶笉鎵ц浠讳綍杞箟澶勭悊銆傚鏋滄偍鎯宠鎶婁竴涓BLOB鍊煎瓨鍌ㄥ埌鏂囦欢涓紝鍒欒繖涓鍙ユ槸鏈夌敤鐨勩
路 娉ㄩ噴锛氫换浣曠敱INTO OUTFILE鎴INTO DUMPFILE鍒涘缓鐨勬枃浠堕兘鍙互琚湇鍔″櫒涓绘満涓婄殑鎵鏈夌敤鎴风紪鍐欍傚師鍥犳槸锛MySQL鏈嶅姟鍣ㄤ笉鑳藉垱寤鸿繖鏍风殑鏂囦欢锛屽嵆鏂囦欢鐨勬墍鏈夎呬笉鏄鏂囦欢杩愯鏃舵墍灞炵殑鐢ㄦ埛锛堜换浣曟椂鍊欙紝鎮ㄩ兘涓嶈兘鍑轰簬姝ゅ師鍥犳垨鍑轰簬鍏跺畠鍘熷洜鎶mysqld浣滀负鏍规杩愯锛夈傝鏂囦欢蹇呴』鏄叏灞鍙啓鐨勶紝杩欐牱鎮ㄥ氨鍙互鎿嶄綔鍏朵腑鐨勫唴瀹广
路 鏈夌殑杩囩▼搴斿湪缁撴灉闆嗗悎鍐呭鐞嗘暟鎹PROCEDURE瀛愬彞鐢ㄤ簬瀵硅繖浜涜繃绋嬭繘琛屽懡鍚嶃傝浜嗚В绀轰緥锛岃鍙傝27.3.1鑺傦紝鈥滄楠ゅ垎鏋愨銆
路 瀛樺偍寮曟搸浣跨敤椤甸潰鎴栬閿併傚鏋滄偍瀵瑰瓨鍌ㄥ紩鎿庝娇鐢FOR UPDATE锛屽垯鍙楀埌鏌ヨ妫楠岀殑琛屼細琚繘琛屽啓閿佸畾锛岀洿鍒板綋鍓嶄簨鍔$粨鏉熶负姝€備娇鐢LOCK IN SHARE MODE鍙互璁剧疆涓涓叡浜攣銆傚叡浜攣鍙互闃叉鍏跺畠浜嬪姟鏇存柊鎴栧垹闄よ妫楠岀殑琛屻傝鍙傝15.2.10.5鑺傦紝鈥滈攣瀹氳鍙朣ELECT ... FOR UPDATE鍜孲ELECT ... LOCK IN SHARE MODE鈥銆
鍦SELECT鍏抽敭璇嶇殑鍚庨潰锛屾偍鍙互浣跨敤璁稿閫夐」銆傝繖浜涢夐」鍙互褰卞搷璇彞鐨勮繍琛屻
ALL, DISTINCT鍜DISTINCTROW閫夐」鎸囧畾鏄惁閲嶅琛屽簲琚繑鍥炪傚鏋滆繖浜涢夐」娌℃湁琚粰瀹氾紝鍒欓粯璁ゅ间负ALL锛堟墍鏈夌殑鍖归厤琛岃杩斿洖锛夈DISTINCT鍜DISTINCTROW鏄悓涔夎瘝锛岀敤浜庢寚瀹氱粨鏋滈泦鍚堜腑鐨勯噸澶嶈搴旇鍒犻櫎銆
HIGH_PRIORITY, STRAIGHT_JOIN鍜屼互SQL_涓哄紑澶寸殑閫夐」鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘
路 HIGH_PRIORITY缁欎簣SELECT鏇撮珮鐨勪紭鍏堟潈锛岄珮浜庣敤浜庢洿鏂拌〃鐨勮鍙ャ傛偍搴旇鍙鏌ヨ浣跨敤HIGH_PRIORITY銆傛煡璇㈤熷害闈炲父蹇紝鑰屼笖绔嬪埢琚墽琛屻SELECT HIGH_PRIORITY鏌ヨ鍦ㄨ〃琚攣瀹氱敤浜庤鍙栨椂琚彂鍑恒傚嵆浣挎湁涓涓柊鐨勮鍙ユ鍦ㄧ瓑寰呰〃鍙樹负绌洪棽锛屾煡璇篃浼氳繍琛屻
HIGH_PRIORITY涓嶈兘鍜SELECT璇彞鍚屾椂浣跨敤銆SELECT璇彞鏄UNION鐨勪竴閮ㄥ垎銆
路
STRAIGHT_JOIN鐢ㄤ簬淇冧娇浼樺寲绗︽妸琛ㄨ仈鍚堝湪涓璧凤紝椤哄簭鎸夌収杩欎簺琛ㄥ湪FROM瀛愬彞涓帓鍒楃殑椤哄簭銆傚鏋滀紭鍖栫鑱斿悎琛ㄦ椂椤哄簭涓嶄匠锛屾偍鍙互浣跨敤STRAIGHT_JOIN鏉ュ姞蹇煡璇㈢殑閫熷害銆傝鍙傝7.2.1鑺傦紝鈥淓XPLAIN璇硶锛堣幏鍙栧叧浜嶴ELECT鐨勪俊鎭級鈥銆STRAIGHT_JOIN涔熷彲浠ヨ鐢ㄤ簬table_references娓呭崟涓傝鍙傝13.2.7.1鑺傦紝鈥淛OIN璇硶鈥銆
路 SQL_BIG_RESULT鍙互涓GROUP BY鎴DISTINCT鍚屾椂浣跨敤锛屾潵鍛婄煡浼樺寲绗︾粨鏋滈泦鍚堟湁寰堝琛屻傚湪杩欑鎯呭喌涓嬶紝MySQL鐩存帴浣跨敤浠ョ鐩樹负鍩虹鐨勪复鏃惰〃锛堝鏋滈渶瑕佺殑璇濓級銆傚湪杩欑鎯呭喌涓嬶紝MySQL杩樹細浼樺厛杩涜鍒嗙被锛屼笉浼樺厛浣跨敤涓存椂琛ㄣ備复鏃惰〃瀵逛簬GROUP BY缁勫垎甯︽湁鍏抽敭瀛椼
路 SQL_BUFFER_RESULT淇冧娇缁撴灉琚斁鍏ヤ竴涓复鏃惰〃涓傝繖鍙互甯姪MySQL鎻愬墠瑙e紑琛ㄩ攣瀹氾紝鍦ㄩ渶瑕佽姳璐硅緝闀挎椂闂寸殑鎯呭喌涓嬶紝涔熷彲浠ュ府鍔╂妸缁撴灉闆嗗悎鍙戦佸埌瀹㈡埛绔腑銆
路 SQL_SMALL_RESULT鍙互涓GROUP BY鎴DISTINCT鍚屾椂浣跨敤锛屾潵鍛婄煡浼樺寲绗︾粨鏋滈泦鍚堟槸杈冨皬鐨勩傚湪姝ゆ儏鍐典笅锛MySAL浣跨敤蹇熶复鏃惰〃鏉ュ偍瀛樼敓鎴愮殑琛紝鑰屼笉鏄娇鐢ㄥ垎绫汇傚湪MySQL 5.1涓紝閫氬父涓嶉渶瑕佽繖鏍枫
路 SQL_CALC_FOUND_ROWS鍛婄煡MySQL璁$畻鏈夊灏戣搴斾綅浜庣粨鏋滈泦鍚堜腑锛屼笉鑰冭檻浠讳綍LIMIT瀛愬彞銆傝鐨勬暟鐩彲浠ヤ娇鐢SELECT FOUND_ROWS()鎭㈠銆傝鍙傝12.9.3鑺傦紝鈥滀俊鎭嚱鏁扳銆
路 濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓query_cache_type鍊硷紝鍊间负2鎴DEMAND锛屽垯SQL_CACHE鍛婄煡MySQL鎶婃煡璇㈢粨鏋滃瓨鍌ㄥ湪鏌ヨ缂撳瓨涓傚浜庝娇鐢UNION鐨勬煡璇㈡垨瀛愭煡璇紝鏈夐」浼氬奖鍝嶆煡璇腑鐨勬墍鏈SELECT銆傝鍙傝5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测銆
路 SQL_NO_CACHE鍛婄煡MySQL涓嶈鎶婃煡璇㈢粨鏋滃瓨鍌ㄥ湪鏌ヨ缂撳瓨涓傝鍙傝5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测銆傚浜庝竴涓娇鐢UNION鎴栧瓙鏌ヨ鐨勬煡璇紝鏈夐」浼氬奖鍝嶆煡璇腑鐨SELECT銆
MySQL鏀寔浠ヤ笅JOIN璇硶銆傝繖浜涜娉曠敤浜SELECT璇彞鐨table_references閮ㄥ垎鍜屽琛DELETE鍜UPDATE璇彞锛
table_references: table_reference [, table_reference] 鈥 table_reference: table_factor | join_table table_factor: tbl_name [[AS] alias] [{USE|IGNORE|FORCE} INDEX (key_list)] | ( table_references ) | { OJ table_reference LEFT OUTER JOIN table_reference ON conditional_expr } join_table: table_reference [INNER | CROSS] JOIN table_factor [join_condition] | table_reference STRAIGHT_JOIN table_factor | table_reference STRAIGHT_JOIN table_factor ON condition | table_reference LEFT [OUTER] JOIN table_reference join_condition | table_reference NATURAL [LEFT [OUTER]] JOIN table_factor | table_reference RIGHT [OUTER] JOIN table_reference join_condition | table_reference NATURAL [RIGHT [OUTER]] JOIN table_factor join_condition: ON conditional_expr | USING (column_list)
涓涓〃寮曠敤杩樿绉颁负涓涓仈鍚堣〃杈惧紡銆
涓SQL鏍囧噯鐩告瘮锛table_factor鐨勮娉曡鎵╁睍浜嗐SQL鏍囧噯鍙帴鍙table_reference锛岃屼笉鏄渾鎷彿鍐呯殑涓绯诲垪鏉$洰銆
濡傛灉鎴戜滑鎶婁竴绯诲垪table_reference鏉$洰涓殑姣忎釜閫楀彿閮界湅浣滅浉褰撲簬涓涓唴閮ㄨ仈鍚堬紝鍒欒繖鏄竴涓ǔ濡ョ殑鎵╁睍銆備緥濡傦細
SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
鐩稿綋浜庯細
SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
鍦MySQL涓紝CROSS JOIN浠庤娉曚笂璇翠笌INNER JOIN绛夊悓锛堜袱鑰呭彲浠ヤ簰鐩告浛鎹€傚湪鏍囧噯SQL涓紝涓よ呮槸涓嶇瓑鍚岀殑銆INNER JOIN涓ON瀛愬彞鍚屾椂浣跨敤锛CROSS JOIN浠ュ叾瀹冩柟寮忎娇鐢ㄣ
閫氬父锛屽湪鍙惈鏈夊唴閮ㄨ仈鍚堣繍琛岀殑鑱斿悎琛ㄨ揪寮忎腑锛屽渾鎷彿鍙互琚拷鐣ャMySQL涔熸敮鎸佸祵濂楃殑鑱斿悎锛堣7.2.10鑺傦紝鈥淢ySQL濡備綍浼樺寲宓屽Join鈥锛夈
閫氬父锛屾偍涓嶅簲瀵ON閮ㄥ垎鏈変换浣曟潯浠躲ON閮ㄥ垎鐢ㄤ簬闄愬畾鍦ㄧ粨鏋滈泦鍚堜腑鎮ㄦ兂瑕佸摢浜涜銆備絾鏄紝鎮ㄥ簲鍦WHERE瀛愬彞涓寚瀹氳繖浜涙潯浠躲傝繖鏉¤鍒欐湁涓浜涗緥澶栥
鍦ㄥ墠闈㈢殑娓呭崟涓樉绀虹殑{ OJ ... LEFT OUTER JOIN ...}璇硶鐨勭洰鐨勫彧鏄负浜嗕繚鎸佷笌ODBC鐨勫吋瀹规с傝娉曚腑鐨勮姳鎷彿搴旀寜瀛楅潰涔﹀啓锛涜鎷彿涓嶆槸涓棿璇硶銆備腑闂磋娉曠敤浜庤娉曟弿杩扮殑鍏跺畠鍦版柟銆
路 琛ㄥ紩鐢ㄥ彲浠ヤ娇鐢tbl_name AS alias_name鎴tbl_name alias_name鎸囧畾鍒悕锛
路 mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2
路 -> WHERE t1.name = t2.name;
路 mysql> SELECT t1.name, t2.salary FROM employee t1, info t2
路 -> WHERE t1.name = t2.name;
路 ON鏉′欢鍙ユ槸鍙互琚敤浜WHERE瀛愬彞鐨勬牸寮忕殑浠讳綍鏉′欢琛ㄨ揪寮忋
路 濡傛灉瀵逛簬鍦LEFT JOIN涓殑ON鎴USING閮ㄥ垎涓殑鍙宠〃娌℃湁鍖归厤鐨勮褰曪紝鍒欐墍鏈夊垪琚缃负NULL鐨勪竴涓琚敤浜庡彸琛ㄣ傚鏋滀竴涓〃鍦ㄥ叾瀹冭〃涓病鏈夊搴旈儴鍒嗭紝鎮ㄥ彲浠ヤ娇鐢ㄨ繖绉嶆柟娉曞湪杩欑琛ㄤ腑鏌ユ壘璁板綍锛
路 mysql> SELECT table1.* FROM table1
路 -> LEFT JOIN table2 ON table1.id=table2.id
路 -> WHERE table2.id IS NULL;
鏈緥鏌ユ壘鍦table1涓惈鏈変竴涓id鍊肩殑鎵鏈夎銆傚悓鏃讹紝鍦table2涓病鏈夋id鍊硷紙鍗筹紝table1涓殑鎵鏈夎鍦table2涓病鏈夊搴旂殑琛岋級銆傛湰渚嬪亣璁table2.id琚畾涔変负NOT NULL銆傝鍙傝7.2.9鑺傦紝鈥淢ySQL濡備綍浼樺寲LEFT JOIN鍜孯IGHT JOIN鈥銆
路 USING(column_list)瀛愬彞鐢ㄤ簬涓轰竴绯诲垪鐨勫垪杩涜鍛藉悕銆傝繖浜涘垪蹇呴』鍚屾椂鍦ㄤ袱涓〃涓瓨鍦ㄣ傚鏋滆〃a鍜岃〃b閮藉寘鍚垪c1, c2鍜c3锛屽垯浠ヤ笅鑱斿悎浼氬姣旀潵鑷袱涓〃鐨勫搴旂殑鍒楋細
路 a LEFT JOIN b USING (c1,c2,c3)
路 涓や釜琛ㄧ殑NATURAL [LEFT] JOIN琚畾涔変负涓INNER JOIN璇箟鐩稿悓锛屾垨涓庝娇鐢USING瀛愬彞鐨LEFT JOIN璇箟鐩稿悓銆USING瀛愬彞鐢ㄤ簬涓哄悓鏃跺瓨鍦ㄤ簬涓や釜琛ㄤ腑鐨勬墍鏈夊垪杩涜鍛藉悕銆
路 INNER JOIN鍜岋紝锛堥楀彿锛夊湪鏃犺仈鍚堟潯浠朵笅鏄涔夌浉鍚岀殑锛氫袱鑰呴兘鍙互瀵规寚瀹氱殑琛ㄨ绠楀嚭绗涘崱鍎夸箻绉紙涔熷氨鏄锛岀涓涓〃涓殑姣忎竴琛岃鑱斿悎鍒扮浜屼釜琛ㄤ腑鐨勬瘡涓琛岋級銆
路 RIGHT JOIN鐨勪綔鐢ㄤ笌LEFT JOIN鐨勪綔鐢ㄧ被浼笺傝浣夸唬鐮佸彲浠ュ湪鏁版嵁搴撳唴绉绘锛屽缓璁偍浣跨敤LEFT JOIN浠f浛RIGHT JOIN銆
路 STRAIGHT_JOIN涓JOIN鐩稿悓銆傞櫎浜嗘湁涓鐐逛笉涓鏍凤紝宸﹁〃浼氬湪鍙宠〃涔嬪墠琚鍙栥STRAIGH_JOIN鍙互琚敤浜庤繖鏍风殑鎯呭喌锛屽嵆鑱斿悎浼樺寲绗︿互閿欒鐨勯『搴忔帓鍒楄〃銆
鎮ㄥ彲浠ユ彁渚涙彁绀猴紝褰撲粠涓涓〃涓仮澶嶄俊鎭椂锛MySQL搴斾娇鐢ㄥ摢涓储寮曘傞氳繃鎸囧畾USE INDEX锛key_list锛夛紝鎮ㄥ彲浠ュ憡鐭MySQL鍙娇鐢ㄤ竴涓储寮曟潵鏌ユ壘琛ㄤ腑鐨勮銆傚彟涓绉嶈娉IGNORE INDEX锛key_list锛夊彲浠ヨ鐢ㄤ簬鍛婄煡MySQL涓嶈浣跨敤鏌愪簺鐗瑰畾鐨勭储寮曘傚鏋EXPLAIN鏄剧ずMySQL姝e湪浣跨敤鏉ヨ嚜绱㈠紩娓呭崟涓殑閿欒绱㈠紩鏃讹紝杩欎簺鎻愮ず浼氭湁鐢ㄥ銆
鎮ㄤ篃鍙互浣跨敤FORCE INDEX锛屽叾浣滅敤鎺ヨ繎USE INDEX锛key_list锛夛紝涓嶈繃澧炲姞浜嗕竴椤逛綔鐢紝涓娆¤〃鎵弿琚亣璁句负浠d环寰堥珮銆傛崲鍙ヨ瘽璇达紝鍙湁褰撴棤娉曚娇鐢ㄤ竴涓粰瀹氱殑绱㈠紩鏉ユ煡鎵捐〃涓殑琛屾椂锛屾墠浣跨敤琛ㄦ壂鎻忋
USE KEY銆IGNORE KEY鍜FORCE KEY鏄USE INDEX銆IGNORE INDEX鍜FORCE INDEX鐨勫悓涔夎瘝銆
娉ㄩ噴锛氬綋MySQL鍐冲畾濡備綍鍦ㄨ〃涓煡鎵捐骞跺喅瀹氬浣曡繘琛岃仈鍚堟椂锛屼娇鐢USE INDEX銆IGNORE INDEX鍜FORCE INDEX鍙細褰卞搷浣跨敤鍝簺绱㈠紩銆傚綋鍒嗚В涓涓ORDER BY鎴GROUP BY鏃讹紝杩欎簺璇彞涓嶄細褰卞搷鏌愪釜绱㈠紩鏄惁琚娇鐢ㄣ
閮ㄥ垎鐨勮仈鍚堢ず渚嬶細
mysql> SELECT * FROM table1,table2 WHERE table1.id=table2.id;
mysql> SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;
mysql> SELECT * FROM table1 LEFT JOIN table2 USING (id);
mysql> SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id
-> LEFT JOIN table3 ON table2.id=table3.id;
mysql> SELECT * FROM table1 USE INDEX (key1,key2)
-> WHERE key1=1 AND key2=2 AND key3=3;
mysql> SELECT * FROM table1 IGNORE INDEX (key3)
-> WHERE key1=1 AND key2=2 AND key3=3;
瑙7.2.9鑺傦紝鈥淢ySQL濡備綍浼樺寲LEFT JOIN鍜孯IGHT JOIN鈥銆
娉ㄩ噴锛氳嚜鐒惰仈鍚堝拰浣跨敤USING鐨勮仈鍚堬紝鍖呮嫭澶栭儴鑱斿悎鍙橀噺锛屼緷鎹SQL:2003鏍囧噯琚鐞嗐傝繖浜涘彉鏇存椂MySQL涓庢爣鍑SQL鏇村姞鐩哥銆備笉杩囷紝瀵逛簬鏈変簺鑱斿悎锛岃繖浜涘彉鏇翠細瀵艰嚧涓嶅悓鐨勮緭鍑哄垪銆傚彟澶栵紝鏈変簺鏌ヨ鍦ㄦ棫鐗堟湰锛5.0.12浠ュ墠锛夊伐浣滄甯革紝浣嗕篃蹇呴』閲嶆柊缂栧啓锛屼互绗﹀悎姝ゆ爣鍑嗐傚浜庢湁鍏冲綋鍓嶈仈鍚堝鐞嗗拰鏃х増鏈腑鐨勮仈鍚堝鐞嗙殑鏁堟灉鐨勫姣旓紝浠ヤ笅鍒楄〃鎻愪緵浜嗘洿璇︾粏鐨勪俊鎭
路 NATURAL鑱斿悎鎴USING鑱斿悎鐨勫垪浼氫笌鏃х増鏈笉鍚屻傜壒鍒槸锛屼笉鍐嶅嚭鐜板啑浣欑殑杈撳嚭鍒楋紝鐢ㄤ簬SELECT *鎵╁睍鐨勫垪鐨勯『搴忎細涓庝互鍓嶄笉鍚屻
绀轰緥锛
CREATE TABLE t1 (i INT, j INT);
CREATE TABLE t2 (k INT, j INT);
INSERT INTO t1 VALUES(1,1);
INSERT INTO t2 VALUES(1,1);
SELECT * FROM t1 NATURAL JOIN t2;
SELECT * FROM t1 JOIN t2 USING (j);
瀵逛簬鏃х増鏈紝璇彞浼氫骇鐢熶互涓嬭緭鍑猴細
+------+------+------+------+
| i | j | k | j |
+------+------+------+------+
| 1 | 1 | 1 | 1 |
+------+------+------+------+
+------+------+------+------+
| i | j | k | j |
+------+------+------+------+
| 1 | 1 | 1 | 1 |
+------+------+------+------+
鍦ㄧ涓涓SELECT璇彞涓紝鍒i鍚屾椂鍑虹幇鍦ㄤ袱涓〃涓紝涓轰竴涓仈鍚堝垪锛屾墍浠ワ紝渚濇嵁鏍囧噯SQL锛岃鍒楀湪杈撳嚭涓彧鍑虹幇涓娆°備笌姝ょ被浼硷紝鍦ㄧ浜屼釜SELECT璇彞涓紝鍒j鍦USING瀛愬彞涓鍛藉悕锛屽簲鍦ㄨ緭鍑轰腑鍙嚭鐜颁竴娆°備絾鏄紝鍦ㄤ袱绉嶆儏鍐典笅锛屽啑浣欑殑鍒楀潎娌¤娑堥櫎銆傚彟澶栵紝渚濇嵁鏍囧噯SQL锛屽垪鐨勯『搴忎笉姝g‘銆
鐜板湪锛岃鍙ヤ骇鐢熷涓嬭緭鍑猴細
+------+------+------+
| j | i | k |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
+------+------+------+
| j | i | k |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
鍐椾綑鐨勫垪琚秷闄わ紝骞朵笖渚濇嵁鏍囧噯SQL锛屽垪鐨勯『搴忔槸姝g‘鐨勶細
o 绗竴锛屼袱琛ㄥ叡鏈夌殑鍒楋紝鎸夊湪绗竴涓〃涓殑椤哄簭鎺掑垪
o 绗簩锛岀涓涓〃涓壒鏈夌殑鍒楋紝鎸夎琛ㄤ腑鐨勯『搴忔帓鍒
o 绗笁锛岀浜屼釜琛ㄤ腑鐗规湁鐨勫垪锛屾寜璇ヨ〃涓殑椤哄簭鎺掑垪
路 瀵瑰鏂瑰紡鑷劧鑱斿悎鐨勪及绠椾細涓嶅悓銆傛柟寮忚姹傞噸鏂扮紪鍐欐煡璇€傚亣璁炬偍鏈変笁涓〃t1(a,b), t2(c,b)鍜t3(a,c)锛屾瘡涓〃鏈変竴琛岋細t1(1,2), t2(10,2)鍜t3(7,10)銆傚悓鏃讹紝鍋囪杩欎笁涓〃鍏锋湁NATURAL JOIN锛
路 SELECT 鈥 FROM t1 NATURAL JOIN t2 NATURAL JOIN t3;
鍦ㄦ棫鐗堟湰涓紝绗簩涓仈鍚堢殑宸︽搷浣滄暟琚涓烘槸t2锛岀劧鑰屽畠搴旇涓哄祵濂楄仈鍚堬紙t1 NATURAL JOIN t2锛夈傜粨鏋滐紝瀵t3鐨勫垪杩涜妫鏌ユ椂锛屽彧妫鏌ュ叾鍦t2涓殑鍏辨湁鍒椼傚鏋t3涓t1鏈夊叡鏈夊垪锛岃繖浜涘垪涓嶈鐢ㄤ綔equi-join鍒椼傚洜姝わ紝鍦ㄦ棫鐗堟湰鐨MySQL涓紝鍓嶉潰鐨勬煡璇㈣杞崲涓轰笅闈㈢殑equi-join锛
SELECT 鈥 FROM t1, t2, t3
WHERE t1.b = t2.b AND t2.c = t3.c;
姝よ仈鍚堝張鐪佺暐浜嗕竴涓equi-join璋撹锛t1.a = t3.a锛夈傜粨鏋滄槸锛岃鑱斿悎浜х敓涓涓锛岃屼笉鏄┖缁撴灉銆傛纭殑绛変环鏌ヨ濡備笅锛
SELECT 鈥 FROM t1, t2, t3
WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a;
濡傛灉鎮ㄨ姹傚湪褰撳墠鐗堟湰鐨MySQL涓幏寰楀拰鏃х増鏈腑鐩稿悓鐨勬煡璇㈢粨鏋滐紝搴旀妸鑷劧鑱斿悎鏀瑰啓涓虹涓涓equi-join銆
路 鍦ㄦ棫鐗堟湰涓紝閫楀彿鎿嶄綔绗︼紙锛岋級鍜JOIN鍧囨湁鐩稿悓鐨勪紭鍏堟潈锛屾墍浠ヨ仈鍚堣〃杈惧紡t1, t2 JOIN t3琚悊瑙d负((t1, t2) JOIN t3)銆傜幇鍦紝JOIN鏈夋洿楂樼殑浼樺厛鏉冿紝鎵浠ヨ〃杈惧紡琚悊瑙d负(t1, (t2 JOIN t3))銆傝繖涓彉鏇翠細褰卞搷浣跨敤ON瀛愬彞鐨勮鍙ワ紝鍥犱负璇ュ瓙鍙ュ彧鍙傞槄鑱斿悎鎿嶄綔鏁颁腑鐨勫垪銆備紭鍏堟潈鐨勫彉鏇存敼鍙樹簡瀵逛粈涔堟槸鎿嶄綔鏁扮殑鐞嗚В銆
绀轰緥锛
CREATE TABLE t1 (i1 INT, j1 INT);
CREATE TABLE t2 (i2 INT, j2 INT);
CREATE TABLE t3 (i3 INT, j3 INT);
INSERT INTO t1 VALUES(1,1);
INSERT INTO t2 VALUES(1,1);
INSERT INTO t3 VALUES(1,1);
SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3);
鍦ㄦ棫鐗堟湰涓紝SELECT鏄悎娉曠殑锛屽洜涓t1, t2琚殣鍚湴褰掍负(t1,t2)銆傜幇鍦紝JOIN鍙栧緱浜嗕紭鍏堟潈锛屽洜姝ょ敤浜ON瀛愬彞鐨勬搷浣滄暟鏄t2鍜t3銆傚洜涓t1.i1涓嶆槸浠讳綍涓涓搷浣滄暟涓殑鍒楋紝鎵浠ョ粨鏋滄槸鍑虹幇鍦'on clause'涓湁鏈煡鍒't1.i1'鐨勯敊璇傝浣胯仈鍚堝彲浠ヨ澶勭悊锛岀敤浣跨敤鍦嗘嫭鍙锋妸鍓嶄袱涓〃鏄庣‘鍦板綊涓轰竴缁勶紝杩欐牱鐢ㄤ簬ON瀛愬彞鐨勬搷浣滄暟涓(t1,t2)鍜t3锛
SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
鏈彉鏇翠篃閫傜敤浜INNER JOIN锛CROSS JOIN锛LEFT JOIN鍜RIGHT JOIN銆
路 鍦ㄦ棫鐗堟湰涓紝ON瀛愬彞鍙互鍙傞槄鍦ㄥ叾鍙宠竟鍛藉悕鐨勮〃涓殑鍒椼傜幇鍦紝ON瀛愬彞鍙兘鍙傞槄鎿嶄綔鏁般
绀轰緥锛
CREATE TABLE t1 (i1 INT);
CREATE TABLE t2 (i2 INT);
CREATE TABLE t3 (i3 INT);
SELECT * FROM t1 JOIN t2 ON (i1 = i3) JOIN t3;
鍦ㄦ棫鐗堟湰涓紝SELECT璇彞鏄悎娉曠殑銆傜幇鍦ㄨ璇彞浼氳繍琛屽け璐ワ紝鍑虹幇鍦'on clause'涓湭鐭ュ垪'i3'鐨勯敊璇傝繖鏄洜涓i3鏄t3涓殑涓涓〃锛岃t3涓嶆槸ON瀛愬彞涓殑鎿嶄綔鏁般傛湰璇彞搴旇繘琛屽涓嬫敼鍐欙細
SELECT * FROM t1 JOIN t2 JOIN t3 ON (i1 = i3);
路 鍦ㄦ棫鐗堟湰涓紝涓涓USING瀛愬彞鍙互琚敼鍐欎负涓涓ON瀛愬彞銆ON瀛愬彞瀵规瘮浜嗙浉搴旂殑鍒椼備緥濡傦紝浠ヤ笅涓や釜瀛愬彞鍏锋湁鐩稿悓鐨勮涔夛細
路 a LEFT JOIN b USING (c1,c2,c3)
路 a LEFT JOIN b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3
鐜板湪锛岃繖涓や釜瀛愬彞涓嶅啀鏄竴鏍风殑锛
o 鍦ㄥ喅瀹氬摢浜涜婊¤冻鑱斿悎鏉′欢鏃讹紝涓や釜鑱斿悎淇濇寔璇箟鐩稿悓銆
o 鍦ㄥ喅瀹氬摢浜涘垪鏄剧ずSELECT *鎵╁睍鏃讹紝涓や釜鑱斿悎鐨勮涔変笉鐩稿悓銆USING鑱斿悎閫夋嫨瀵瑰簲鍒椾腑鐨勫悎骞跺硷紝鑰ON鑱斿悎閫夋嫨鎵鏈夎〃涓殑鎵鏈夊垪銆傚浜庡墠闈㈢殑USING鑱斿悎锛SELECT *閫夋嫨杩欎簺鍊硷細
o COALESCE(a.c1,b.c1), COALESCE(a.c2,b.c2), COALESCE(a.c3,b.c3)
瀵逛簬ON鑱斿悎锛SELECT *閫夋嫨杩欎簺鍊硷細
a.c1, a.c2, a.c3, b.c1, b.c2, b.c3
浣跨敤鍐呴儴鑱斿悎鏃讹紝COALESCE(a.c1,b.c1)涓a.c1鎴b.c1鐩稿悓锛屽洜涓轰袱鍒楀皢鍏锋湁鐩稿悓鐨勫笺備娇鐢ㄥ閮ㄨ仈鍚堟椂锛堟瘮濡LEFT JOIN锛夛紝涓ゅ垪涓湁涓鍒楀彲浠ヤ负NULL銆傝鍒楀皢浼氫粠缁撴灉涓蹇界暐銆
SELECT ...
UNION [ALL | DISTINCT]
SELECT ...
[UNION [ALL | DISTINCT]
SELECT ...]
UNION鐢ㄤ簬鎶婃潵鑷澶SELECT璇彞鐨勭粨鏋滅粍鍚堝埌涓涓粨鏋滈泦鍚堜腑銆
鍒椾簬姣忎釜SELECT璇彞鐨勫搴斾綅缃殑琚夋嫨鐨勫垪搴斿叿鏈夌浉鍚岀殑绫诲瀷銆傦紙渚嬪锛岃绗竴涓鍙ラ夋嫨鐨勭涓鍒楀簲鍜岃鍏跺畠璇彞閫夋嫨鐨勭涓鍒楀叿鏈夌浉鍚岀殑绫诲瀷銆傦級鍦ㄧ涓涓SELECT璇彞涓浣跨敤鐨勫垪鍚嶇О涔熻鐢ㄤ簬缁撴灉鐨勫垪鍚嶇О銆
SELECT璇彞涓哄父瑙勭殑閫夋嫨璇彞锛屼絾鏄彈鍒板涓嬬殑闄愬畾锛
路 鍙湁鏈鍚庝竴涓SELECT璇彞鍙互浣跨敤INTO OUTFILE銆
路 HIGH_PRIORITY涓嶈兘涓庝綔涓UNION涓閮ㄥ垎鐨SELECT璇彞鍚屾椂浣跨敤銆傚鏋滄偍瀵圭涓涓SELECT鎸囧畾浜HIGH_PRIORITY锛屽垯涓嶄細璧蜂綔鐢ㄣ傚鏋滄偍瀵瑰叾瀹冨悗缁殑SELECT璇彞鎸囧畾浜HIGH_PRIORITY锛屽垯浼氫骇鐢熻娉曢敊璇
濡傛灉鎮ㄥUNION涓嶄娇鐢ㄥ叧閿瘝ALL锛屽垯鎵鏈夎繑鍥炵殑琛岄兘鏄敮涓鐨勶紝濡傚悓鎮ㄥ凡缁忓鏁翠釜缁撴灉闆嗗悎浣跨敤浜DISTINCT銆傚鏋滄偍鎸囧畾浜ALL锛屾偍浼氫粠鎵鏈夌敤杩囩殑SELECT璇彞涓緱鍒版墍鏈夊尮閰嶇殑琛屻
DISTINCT鍏抽敭璇嶆槸涓涓嚜閫夎瘝锛屼笉璧蜂换浣曚綔鐢紝浣嗘槸鏍规嵁SQL鏍囧噯鐨勮姹傦紝鍦ㄨ娉曚腑鍏佽閲囩敤銆傦紙鍦MySQL涓紝DISTINCT浠h〃涓涓叡鐢ㄤ綋鐨勯粯璁ゅ伐浣滄ц川銆傦級
鎮ㄥ彲浠ュ湪鍚屼竴鏌ヨ涓贩鍚UNION ALL鍜UNION DISTINCT銆傝娣峰悎鐨UNION绫诲瀷鎸夌収杩欐牱鐨勬柟寮忓寰咃紝鍗DISTICT鍏辩敤浣撹鐩栦綅浜庡叾宸﹁竟鐨勬墍鏈ALL鍏辩敤浣撱DISTINCT鍏辩敤浣撳彲浠ヤ娇鐢UNION DISTINCT鏄庣‘鍦扮敓鎴愶紝鎴栦娇鐢UNION锛堝悗闈笉鍔DISTINCT鎴ALL鍏抽敭璇嶏級闅愬惈鍦扮敓鎴愩
濡傛灉鎮ㄦ兂浣跨敤ORDER BY鎴LIMIT瀛愬彞鏉ュ鍏ㄩ儴UNION缁撴灉杩涜鍒嗙被鎴栭檺鍒讹紝鍒欏簲瀵瑰崟涓湴SELECT璇彞鍔犲渾鎷彿锛屽苟鎶ORDER BY鎴LIMIT鏀惧埌鏈鍚庝竴涓殑鍚庨潰銆備互涓嬩緥瀛愬悓鏃朵娇鐢ㄤ簡杩欎袱涓瓙鍙ワ細
(SELECT a FROM tbl_name WHERE a=10 AND B=1)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
杩欑ORDER BY涓嶈兘浣跨敤鍖呮嫭琛ㄥ悕绉帮紙涔熷氨鏄紝閲囩敤tbl_name.col_name鏍煎紡鐨勫悕绉帮級鍒楀紩鐢ㄣ傚彲浠ュ湪绗竴涓SELECT璇彞涓彁渚涗竴涓垪鍒悕锛屽苟鍦ORDER BY涓弬闃呭埆鍚嶏紝鎴栦娇鐢ㄥ垪浣嶇疆鍦ORDER BY涓弬闃呭垪銆傦紙棣栭夐噰鐢ㄥ埆鍚嶏紝鍥犱负涓嶅缓璁娇鐢ㄥ垪浣嶇疆銆傦級
鍙﹀锛屽鏋滃甫鍒嗙被鐨勪竴鍒楁湁鍒悕锛屽垯ORDER BY瀛愬彞蹇呴』寮曠敤鍒悕锛岃屼笉鑳藉紩鐢ㄥ垪鍚嶇О銆備互涓嬭鍙ヤ腑鐨勭涓涓鍙ュ繀椤昏繍琛岋紝浣嗘槸绗簩涓細杩愯澶辫触锛屽嚭鐜板湪'order clause'涓湁鏈煡鍒'a'鐨勯敊璇細
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY b;
(SELECT a AS b FROM t) UNION (SELECT ...) ORDER BY a;
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT: 涓轰簡瀵瑰崟涓SELECT浣跨敤ORDER BY鎴LIMIT锛屽簲鎶婂瓙鍙ユ斁鍏ュ渾鎷彿涓傚渾鎷彿鍖呭惈浜SELECT锛
(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
鍦嗘嫭鍙蜂腑鐢ㄤ簬鍗曚釜SELECT璇彞鐨ORDER BY鍙湁褰撲笌LIMIT缁撳悎鍚庯紝鎵嶈捣浣滅敤銆傚惁鍒欙紝ORDER BY琚紭鍖栧幓闄ゃ
UNION缁撴灉闆嗗悎涓殑鍒楃殑绫诲瀷鍜岄暱搴﹁冭檻浜嗚鎵鏈SELECT璇彞鎭㈠鐨勬暟鍊笺備緥濡傦紝鑰冭檻濡備笅璇彞锛
mysql> SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+---------------+
| REPEAT('a',1) |
+---------------+
| a |
| bbbbbbbbbb |
+---------------+
锛堝湪閮ㄥ垎鏃╂湡鐗堟湰鐨MySQL涓紝绗簩琛屽凡琚垹鑺傚埌闀垮害涓1銆傦級
瀛愭煡璇㈡槸鍙︿竴涓鍙ヤ腑鐨勪竴涓SELECT璇彞銆
MySQL鏀寔SQL鏍囧噯瑕佹眰鐨勬墍鏈夊瓙鏌ヨ鏍煎紡鍜屾搷浣滐紝涔熸敮鎸MySQL鐗规湁鐨勫嚑绉嶇壒鎬с
浠ヤ笅鏄竴涓瓙鏌ヨ鐨勪緥瀛愶細
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
鍦ㄦ湰渚嬩腑锛SELECT * FROM t1...鏄閮ㄦ煡璇紙鎴栧閮ㄨ鍙ワ級,锛SELECT column1 FROM t2锛夋槸瀛愭煡璇€傛垜浠彲浠ヨ瀛愭煡璇㈠祵濂楀湪澶栭儴鏌ヨ涓傚疄闄呬笂锛屽瓙鏌ヨ涔熷彲浠ュ祵濂楀湪鍏跺畠瀛愭煡璇腑锛屽祵濂楃▼搴﹀彲浠ュ緢娣便傚瓙鏌ヨ蹇呴』瑕佷綅浜庡渾鎷彿涓
瀛愭煡璇㈢殑涓昏浼樺娍涓猴細
路 瀛愭煡璇㈠厑璁哥粨鏋勫寲鐨勬煡璇紝杩欐牱灏卞彲浠ユ妸涓涓鍙ョ殑姣忎釜閮ㄥ垎闅旂寮銆
路 鏈変簺鎿嶄綔闇瑕佸鏉傜殑鑱斿悎鍜屽叧鑱斻傚瓙鏌ヨ鎻愪緵浜嗗叾瀹冪殑鏂规硶鏉ユ墽琛岃繖浜涙搷浣溿
路 鍦ㄨ澶氫汉鐪嬫潵锛屽瓙鏌ヨ鏄彲璇荤殑銆傚疄闄呬笂锛屽瓙鏌ヨ缁欎簣浜轰滑璋冪敤鏃╂湡SQL鈥滅粨鏋勫寲鏌ヨ璇█鈥濈殑鍘熸湰鐨勬兂娉曪紝杩欐槸瀛愭煡璇㈢殑鍒涙柊銆
浠ヤ笅鏄竴涓ず渚嬭鍙ャ傝璇彞鏄剧ず浜嗘湁鍏冲瓙鏌ヨ璇硶鐨勮鐐广傚瓙鏌ヨ璇硶鐢SQL鏍囧噯鎸囧畾骞惰MySQL鏀寔銆
DELETE FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* no hint */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));
涓涓瓙鏌ヨ浼氳繑鍥炰竴涓爣閲忥紙鍗曚竴鍊硷級銆佷竴涓銆佷竴涓垪鎴栦竴涓〃锛堜竴琛屾垨澶氳鍙婁竴鍒楁垨澶氬垪锛夈傝繖浜涘瓙鏌ヨ琚О涓烘爣閲忋佸垪銆佽鍜岃〃瀛愭煡璇€傚彲杩斿洖涓涓壒瀹氱绫荤粨鏋滅殑瀛愭煡璇㈢粡甯稿彧琚敤浜庣壒瀹氱殑璇涓紝鍦ㄥ悗闈㈠悇鑺備腑鏈夎鏄庛
鏈変簺璇彞鍙互浣跨敤瀛愭煡璇€傚杩欎簺璇彞鐨勭被鍨嬪熀鏈病鏈夐檺瀹氥傚瓙鏌ヨ鍙互鍖呮嫭鏅SELECT鍙互鍖呮嫭鐨勪换浣曞叧閿瘝鎴栧瓙鍙ワ細DISTINCT, GROUP BY, ORDER BY, LIMIT, 鑱斿悎, 绱㈠紩鎻愮ず, UNION缁撴瀯鍖, 璇勬敞鍜屽嚱鏁扮瓑銆
鏈変竴涓檺瀹氭槸锛屼竴涓瓙鏌ヨ鐨勫閮ㄨ鍙ュ繀椤绘槸浠ヤ笅璇彞涔嬩竴锛SELECT, INSERT, UPDATE, DELETE, SET鎴DO銆傝繕鏈変竴涓檺瀹氭槸锛岀洰鍓嶏紝鎮ㄤ笉鑳藉湪涓涓瓙鏌ヨ涓慨鏀逛竴涓〃锛屽張鍦ㄥ悓涓涓〃涓夋嫨銆傝繖閫傜敤浜DELETE, INSERT, REPLACE鍜UPDATE璇彞銆傚湪闄勫綍I锛鐗规ч檺鍒涓粰鍑轰簡瀵瑰瓙鏌ヨ浣跨敤鐨勬洿缁煎悎鐨勮璁恒
CREATE TABLE t1 (s1 INT, s2 CHAR(5) NOT NULL);
INSERT INTO t1 VALUES(100, 'abcde');
SELECT (SELECT s2 FROM t1);
鍦ㄦ湰SELECT涓殑瀛愭煡璇㈣繑鍥炰竴涓崟涓鍊('abcde')銆傝鍗曚竴鍊肩殑鏁版嵁绫诲瀷涓CHAR锛岄暱搴︿负5锛屽瓧绗﹂泦鍜屾暣搴忎笌鍦CREATE TABLE鏃舵湁鏁堢殑榛樿鍊肩浉鍚岋紝骞舵湁涓涓寚绀虹鍙凤紝鎸囩ず鍒椾腑鐨勫煎彲浠ヤ负NULL銆傚疄闄呬笂锛屽熀鏈笂鎵鏈夌殑瀛愭煡璇㈤兘涓NULL銆傚鏋滃湪鏈緥涓娇鐢ㄧ殑琛ㄤ负绌鸿〃锛屽垯瀛愭煡璇㈢殑鍊煎簲涓NULL銆
鍦ㄦ湁浜涙儏鍐典笅锛屾爣閲忓瓙鏌ヨ涓嶈兘浣跨敤銆傚鏋滀竴涓鍙ュ彧鍏佽涓涓枃瀛楀硷紝鎮ㄤ笉鑳戒娇鐢ㄥ瓙鏌ヨ銆備緥濡傦紝LIMIT瑕佹眰鏂囧瓧鏁存暟鑷彉閲忥紝LOAD DATA瑕佹眰涓涓枃瀛楀瓧绗︿覆鏂囦欢鍚嶃傛偍涓嶈兘浣跨敤瀛愭煡璇㈡潵鎻愪緵杩欎簺鍊笺
鍚庨潰鍚勮妭鍖呮嫭鏇寸畝缁冪殑缁撴瀯锛SELECT column1 FROM t1锛夈傚綋鎮ㄥ湪杩欎簺绔犺妭涓鐪嬩緥瀛愭椂锛岃璁炬兂涓涓嬫偍鑷繁鐨勪唬鐮佸寘鍚洿澶氭牱銆佹洿澶嶆潅鐨勭粨鏋勩
涓句緥璇存槑锛屽亣璁炬垜浠埗浣滀袱涓〃锛
CREATE TABLE t1 (s1 INT);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 (s1 INT);
INSERT INTO t2 VALUES (2);
鐒跺悗鎵ц涓涓SELECT锛
SELECT (SELECT s1 FROM t2) FROM t1;
缁撴灉涓2锛屽洜涓t2涓湁涓琛屽寘鍚s1锛s1鏈変竴涓间负2銆
涓涓爣閲忓瓙鏌ヨ鍙互涓轰竴涓〃杈惧紡鐨勪竴閮ㄥ垎銆備笉瑕佸繕璁板渾鎷彿銆傚嵆浣挎槸瀛愭煡璇㈡槸涓涓负鍑芥暟鎻愪緵鑷彉閲忕殑鎿嶄綔鏁版椂锛屼篃涓嶈蹇樿鍦嗘嫭鍙枫備妇渚嬭鏄庯細
SELECT UPPER((SELECT s1 FROM t1)) FROM t2;
瀛愭煡璇㈡渶甯歌鐨勪竴绉嶄娇鐢ㄦ柟寮忓涓嬶細
non_subquery_operand comparison_operator (subquery)
褰comparison_operator鏄互涓 鎿嶄綔绗︿箣涓鏃讹細
= > < >= <= <>
渚嬪锛
... 'a' = (SELECT column1 FROM t1)
鏈夋椂锛屽瓙鏌ヨ鐨勫悎娉曚綅缃彧鑳藉湪姣旇緝寮忕殑鍙充晶锛屾偍鍙互鍙戠幇锛屽湪鏈変簺鏃х殑DBMSs涓粛淇濇寔杩欎竴鐐广
浠ヤ笅鏄竴涓父瑙佹牸寮忕殑瀛愭煡璇㈡瘮杈冪殑渚嬪瓙銆傛偍涓嶈兘浣跨敤鑱斿悎杩涜姝ょ被姣旇緝銆傝〃t1涓湁浜涘间笌琛t2涓殑鏈澶у肩浉鍚屻傝姣旇緝鍙互鏌ユ壘鍑烘墍鏈夎繖绫诲硷細
SELECT column1 FROM t1
WHERE column1 = (SELECT MAX(column2) FROM t2);
涓嬮潰杩樻湁鍙︿竴涓緥瀛愶紝璇ヤ緥瀛愪篃涓嶅彲鑳戒娇鐢ㄨ仈鍚堬紝鍥犱负璇ヤ緥瀛愭秹鍙婂鍏朵腑涓涓〃杩涜鎬昏銆傝〃t1涓殑鏈変簺琛屽惈鏈夌殑鍊间細鍦ㄧ粰瀹氱殑鍒椾腑鍑虹幇涓ゆ銆傝渚嬪瓙鍙互鏌ユ壘鍑烘墍鏈夎繖浜涜锛
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
瀵逛簬閲囩敤杩欎簺鎿嶄綔绗︿箣涓杩涜鐨勬瘮杈冿紝瀛愭煡璇㈠繀椤昏繑鍥炰竴涓爣閲忋傛湁涓涓緥澶栵紝閭e氨鏄紳鍙互鍜岃瀛愭煡璇㈠悓鏃朵娇鐢ㄣ傝鍙傝13.2.8.5鑺傦紝鈥滆瀛愭煡璇⑩銆
璇硶锛
operand comparison_operator ANY (subquery)
operand IN (subquery)
operand comparison_operator SOME (subquery)
ANY鍏抽敭璇嶅繀椤诲悗闈㈡帴涓涓瘮杈冩搷浣滅銆ANY鍏抽敭璇嶇殑鎰忔濇槸鈥滃浜庡湪瀛愭煡璇㈣繑鍥炵殑鍒椾腑鐨勪换涓鏁板硷紝濡傛灉姣旇緝缁撴灉涓TRUE鐨勮瘽锛屽垯杩斿洖TRUE鈥濄備緥濡傦細
SELECT s1 FROM t1 WHERE s1 > ANY (SELECT s1 FROM t2);
鍋囪琛t1涓湁涓琛屽寘鍚紙10锛夈傚鏋滆〃t2鍖呭惈锛21锛14锛7锛夛紝鍒欒〃杈惧紡涓TRUE锛屽洜涓t2涓湁涓涓间负7锛岃鍊煎皬浜10銆傚鏋滆〃t2鍖呭惈锛20锛10锛夛紝鎴栬呭鏋滆〃t2涓虹┖琛紝鍒欒〃杈惧紡涓FALSE銆傚鏋滆〃t2鍖呭惈锛NULL, NULL, NULL锛夛紝鍒欒〃杈惧紡涓UNKNOWN銆
璇嶈IN鏄紳ANY鐨勫埆鍚嶃傚洜姝わ紝杩欎袱涓鍙ユ槸涓鏍风殑锛
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 IN (SELECT s1 FROM t2);
涓嶈繃锛NOT IN涓嶆槸<> ANY鐨勫埆鍚嶏紝浣嗘槸鏄<>
ALL鐨勫埆鍚嶃傝鍙傝13.2.8.4鑺傦紝鈥滀娇鐢ˋLL杩涜瀛愭煡璇鈥銆
璇嶈SOME鏄ANY鐨勫埆鍚嶃傚洜姝わ紝杩欎袱涓鍙ユ槸涓鏍风殑锛
SELECT s1 FROM t1 WHERE s1 <> ANY (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 <> SOME (SELECT s1 FROM t2);
浣跨敤璇嶈SOME鐨勬満浼氬緢灏戯紝浣嗘槸鏈緥鏄剧ず浜嗕负浠涔SOME鏄湁鐢ㄧ殑銆傚浜庡鏁颁汉鏉ヨ锛岃嫳璇煭璇a is not equal to any b鈥濈殑鎰忔濇槸鈥滄病鏈変竴涓b涓a鐩哥瓑鈥濓紝浣嗘槸鍦SQL璇硶涓笉鏄繖涓剰鎬濄傝璇硶鐨勬剰鎬濇槸鈥滄湁閮ㄥ垎b涓a涓嶇浉绛夆濄備娇鐢<> SOME鏈夊姪浜庣‘璁ゆ瘡涓汉閮界悊瑙h鏌ヨ鐨勭湡姝e惈涔夈
operand comparison_operator ALL (subquery)
璇嶈ALL蹇呴』鎺ュ湪涓涓瘮杈冩搷浣滅鐨勫悗闈€ALL鐨勬剰鎬濇槸鈥滃浜庡瓙鏌ヨ杩斿洖鐨勫垪涓殑鎵鏈夊硷紝濡傛灉姣旇緝缁撴灉涓TRUE锛屽垯杩斿洖TRUE銆傗濅緥濡傦細
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
鍋囪琛1涓湁涓琛屽寘鍚紙10锛夈傚鏋滆〃t2鍖呭惈锛-5锛0锛岋紜5锛夛紝鍒欒〃杈惧紡涓TRUE锛屽洜涓10姣t2涓殑鎵鏈変笁涓奸兘澶с傚鏋滆〃t2鍖呭惈锛12锛6锛NULL锛岋紞100锛夛紝鍒欒〃杈惧紡涓FALSE锛屽洜涓鸿〃t2涓湁涓涓12澶т簬10銆傚鏋滆〃t2鍖呭惈锛0锛NULL锛1锛夛紝鍒欒〃杈惧紡涓unknown銆
鏈鍚庯紝濡傛灉琛t2涓虹┖琛紝鍒欑粨鏋滀负TRUE銆傚洜姝わ紝褰撹〃t2涓虹┖琛ㄦ椂锛屼互涓嬭鍙ヤ负TRUE锛
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
浣嗘槸锛屽綋琛t2涓虹┖琛ㄦ椂锛屾湰璇彞涓NULL锛
SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);
鍙﹀锛屽綋琛t2涓虹┖琛ㄦ椂锛屼互涓嬭鍙ヤ负NULL锛
SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);
閫氬父锛屽寘鍚NULL鍊肩殑琛ㄥ拰绌鸿〃涓衡滆竟缂樻儏鍐碘濄傚綋缂栧啓瀛愭煡璇唬鐮佹椂锛岄兘瑕佽冭檻鎮ㄦ槸鍚︽妸杩欎袱绉嶅彲鑳芥ц绠楀湪鍐呫
NOT IN鏄<> ALL鐨勫埆鍚嶃傚洜姝わ紝浠ヤ笅涓や釜璇彞鏄浉鍚岀殑锛
SELECT s1 FROM t1 WHERE s1 <> ALL (SELECT s1 FROM t2);
SELECT s1 FROM t1 WHERE s1 NOT IN (SELECT s1 FROM t2);
SELECT * FROM t1 WHERE (1,2) = (SELECT column1, column2 FROM t2);
SELECT * FROM t1 WHERE ROW(1,2) = (SELECT column1, column2 FROM t2);
濡傛灉鍦ㄨ〃t2鐨勪竴涓涓紝column1=1骞朵笖column2=2锛屽垯鏌ヨ缁撴灉鍧囦负TRUE銆
琛ㄨ揪寮忥紙1锛2锛夊拰ROW锛1锛2锛夋湁鏃惰绉颁负琛屾瀯閫犵銆備袱鑰呮槸绛夊悓鐨勶紝鍦ㄥ叾瀹冪殑璇涓紝涔熸槸鍚堟硶鐨勩備緥濡傦紝浠ヤ笅涓や釜璇彞鍦ㄨ涔変笂鏄瓑鍚岀殑锛堜絾鏄洰鍓嶅彧鏈夌浜屼釜璇彞鍙互琚紭鍖栵級锛
SELECT * FROM t1 WHERE (column1,column2) = (1,1);
SELECT * FROM t1 WHERE column1 = 1 AND column2 = 1;
琛屾瀯閫犵閫氬父鐢ㄤ簬涓庡鑳借繑鍥炰袱涓垨涓や釜浠ヤ笂鍒楃殑瀛愭煡璇㈣繘琛屾瘮杈冦備緥濡傦紝浠ヤ笅鏌ヨ鍙互绛斿璇锋眰锛屸滃湪琛t1涓煡鎵惧悓鏃朵篃瀛樺湪浜庤〃t2涓殑鎵鏈夌殑琛屸濓細
SELECT column1,column2,column3
FROM t1
WHERE (column1,column2,column3) IN
(SELECT column1,column2,column3 FROM t2);
SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
杩囧幓锛EXISTS瀛愭煡璇互SELECT *涓哄紑濮嬶紝浣嗘槸鍙互浠SELECT 5鎴SELECT column1鎴栧叾瀹冪殑涓哄紑濮嬨MySQL鍦ㄨ繖绫诲瓙鏌ヨ涓拷鐣ヤ簡SELECT娓呭崟锛屽洜姝ゆ病鏈夊尯鍒
瀵逛簬鍓嶉潰鐨勪緥瀛愶紝濡傛灉t2鍖呭惈浠讳綍琛岋紝鍗充娇鏄彧鍚湁NULL鍊肩殑琛岋紝EXISTS鏉′欢涔熶负TRUE銆傝繖瀹為檯涓婃槸涓涓笉鍙兘鐨勪緥瀛愶紝鍥犱负鍩烘湰涓婃墍鏈夌殑[NOT] EXISTS瀛愭煡璇㈠潎鍖呭惈鍏宠仈銆備互涓嬫槸涓浜涙洿鐜板疄鐨勪緥瀛愶細
路 鍝簺绉嶇被鐨勫晢搴楀嚭鐜板湪涓涓垨澶氫釜鍩庡競閲岋紵
路 SELECT DISTINCT store_type FROM stores
路 WHERE EXISTS (SELECT * FROM cities_stores
路 WHERE cities_stores.store_type = stores.store_type);
路 鍝簺绉嶇被鐨勫晢搴楁病鏈夊嚭鐜板湪浠讳綍鍩庡競閲岋紵
路 SELECT DISTINCT store_type FROM stores
路 WHERE NOT EXISTS (SELECT * FROM cities_stores
路 WHERE cities_stores.store_type = stores.store_type);
路 鍝簺绉嶇被鐨勫晢搴楀嚭鐜板湪鎵鏈夊煄甯傞噷锛
路 SELECT DISTINCT store_type FROM stores s1
路 WHERE NOT EXISTS (
路 SELECT * FROM cities WHERE NOT EXISTS (
路 SELECT * FROM cities_stores
路 WHERE cities_stores.city = cities.city
路 AND cities_stores.store_type = stores.store_type));
鏈鍚庝竴涓緥瀛愭槸涓涓弻宓屽NOT EXISTS鏌ヨ銆備篃灏辨槸锛岃鏌ヨ鍖呭惈涓涓NOT EXISTS瀛愬彞锛岃瀛愬彞鍙堝寘鍚湪涓涓NOT EXISTS瀛愬彞涓傝鏌ヨ姝e紡鍦板洖绛斾簡杩欎釜闂锛屸滄槸鍚︽湁鏌愪釜鍩庡競鎷ユ湁娌℃湁鍒楀湪Stores涓殑鍟嗗簵锛熲濄傚彲浠ユ瘮杈冨鏄撶殑璇达紝涓涓甫宓屽鐨NOT EXISTS鍙互鍥炵瓟杩欐牱鐨勯棶棰橈紝鈥滄槸鍚﹀浜庢墍鏈夌殑y锛x閮戒负TRUE锛熲
SELECT * FROM t1 WHERE column1 = ANY
(SELECT column1 FROM t2 WHERE t2.column2 = t1.column2);
娉ㄦ剰锛屽嵆浣垮瓙鏌ヨ鐨FROM瀛愬彞涓嶆彁鍙婅〃t1锛岃瀛愭煡璇篃浼氬寘鍚竴涓t1涓竴鍒楃殑寮曠敤銆傛墍浠ワ紝MySQL鐪嬩笂鍘讳綅浜庡瓙鏌ヨ鐨勫閮紝骞跺湪澶栭儴鏌ヨ涓煡鎵t1銆
鍋囪琛t1鍖呭惈涓琛岋紝鍦ㄦ琛屼腑column1=5骞朵笖column2=6锛涘悓鏃讹紝琛t2鍖呭惈涓琛岋紝鍦ㄦ琛屼腑column1=5骞朵笖column2=7銆傜畝鍗曠殑琛ㄨ揪寮... WHERE column1 = ANY (SELECT column1 FROM t2)浼氫负TRUE銆備絾鏄湪鏈緥涓紝鍦ㄥ瓙鏌ヨ涓殑WHERE瀛愬彞涓FALSE锛堝洜涓猴紙5锛6锛変笉绛変簬锛5锛7锛夛級锛屾墍浠ュ瓙鏌ヨ鎬讳綋涓婁负FALSE銆
鑼冨洿鍒掑垎瑙勫垯锛MySQL浠庡唴鍒板杩涜璇勪及銆備緥濡傦細
SELECT column1 FROM t1 AS x
WHERE x.column1 = (SELECT column1 FROM t2 AS x
WHERE x.column1 = (SELECT column1 FROM t3
WHERE x.column2 = t3.column1));
鍦ㄦ湰璇彞涓紝x.column2蹇呴』鏄〃t2涓殑鍒楋紝鍥犱负SELECT column1 FROM t2 AS x ...瀵t2杩涜浜嗛噸鍛藉悕銆傚畠涓嶆槸琛t1涓殑鍒楋紝鍥犱负SELECT column1 FROM t1 ...鏄竴涓洿闈犲鐨勫閮ㄦ煡璇€
瀵逛簬HAVING鎴ORDER BY瀛愬彞涓殑瀛愭煡璇紝MySQL涔熶細鍦ㄥ閮ㄩ夋嫨娓呭崟涓鎵惧垪鍚嶇О銆
瀵逛簬鐗瑰畾鐨勬儏鍐碉紝鐩稿叧鑱旂殑瀛愭煡璇㈣浼樺寲銆備緥濡傦細
val IN (SELECT key_val FROM tbl_name WHERE correlated_condition)
鍚﹀垯锛岃繖浜涘瓙鏌ヨ鏁堢巼涓嶉珮锛屽彲鑳介熷害浼氭參銆傛妸鏌ヨ浣滀负鑱斿悎杩涜鏀瑰啓鍙兘浼氭敼杩涙晥鐜囥
鐩稿叧鑱旂殑瀛愭煡璇笉鑳戒粠澶栭儴鏌ヨ涓紩鐢ㄦ昏鍑芥暟鐨勭粨鏋溿
鍦SELECT璇彞鐨FROM瀛愬彞涓紝瀛愭煡璇㈡槸鍚堟硶鐨勩傚疄闄呯殑璇硶鏄細
SELECT ... FROM (subquery) [AS] name ...
[AS] name瀛愬彞鏄己鍒舵х殑锛屽洜涓FROM瀛愬彞涓殑姣忎釜琛ㄥ繀椤绘湁涓涓悕绉般傚湪瀛愭煡璇㈤夋嫨鍒楄〃涓殑浠讳綍鍒楅兘蹇呴』鏈夊敮涓鐨勫悕绉般傛偍鍙互鍦ㄦ湰鎵嬪唽涓殑鍏跺畠鍦版柟鎵惧埌瀵规湰璇硶鐨勮鏄庛傚湪璇ュ锛屾墍鐢ㄧ殑璇嶈鏄滃鍑鸿〃鈥濄
涓轰簡杩涜璇︾粏璇存槑锛屽亣璁炬偍鏈夊涓嬩竴涓〃锛
CREATE TABLE t1 (s1 INT, s2 CHAR(5), s3 FLOAT);
涓嬮潰浣跨敤浜嗙ず渚嬭〃锛岃В閲婁簡鍦FROM瀛愬彞涓浣曚娇鐢ㄥ瓙鏌ヨ锛
INSERT INTO t1 VALUES (1,'1',1.0);
INSERT INTO t1 VALUES (2,'2',2.0);
SELECT sb1,sb2,sb3
FROM (SELECT s1 AS sb1, s2 AS sb2, s3*2 AS sb3 FROM t1) AS sb
WHERE sb1 > 1;
缁撴灉锛2, '2', 4.0銆
涓嬮潰鏄彟涓涓緥瀛愶細鍋囪鎮ㄦ兂浜嗚В涓涓垎绫诲悗鐨勮〃鐨勪竴缁勫拰鐨勫钩鍧囧笺傞噰鐢ㄥ涓嬫搷浣滐細
SELECT AVG(SUM(column1)) FROM t1 GROUP BY column1;
涓嶈繃锛屾湰鏌ヨ鎻愪緵鎵闇鐨勪俊鎭細
SELECT AVG(sum_column1)
FROM (SELECT SUM(column1) AS sum_column1
FROM t1 GROUP BY column1) AS t1;
娉ㄦ剰锛屽湪瀛愭煡璇腑浣跨敤鐨勫垪鍚嶇О锛sum_column1锛夎鏁寸悊鍒板閮ㄦ煡璇腑銆
FROM瀛愬彞涓殑瀛愭煡璇㈠彲浠ヨ繑鍥炴爣閲忋佸垪銆佽鎴栬〃銆FROM瀛愬彞涓殑瀛愭煡璇笉鑳戒负鏈夊叧鑱旂殑瀛愭煡璇€
鍗充娇瀵EXPLAIN璇彞锛堝嵆寤虹珛涓存椂瀵煎嚭琛級锛FROM瀛愬彞涓殑瀛愭煡璇篃浼氳鎵ц銆傝繖鏄洜涓哄湪浼樺寲杩囩▼涓紝涓婁竴绾х殑鏌ヨ闇瑕佹湁鍏虫墍鏈夎〃鐨勪俊鎭
浠ヤ笅閿欒鍙傜敤浜庡瓙鏌ヨ銆傛湰鑺傛妸杩欎簺閿欒褰掑湪涓璧枫
路 鏉ヨ嚜瀛愭煡璇㈢殑鍒楃殑鏁扮洰涓嶆纭
路 ERROR 1241 (ER_OPERAND_COL)
路 SQLSTATE = 21000
路 Message = "Operand should contain 1 column(s)"
鍦ㄥ嚭鐜颁互涓嬫儏鍐垫椂锛屽彂鐢熸閿欒锛
SELECT (SELECT column1, column2 FROM t2) FROM t1;
濡傛灉鎮ㄧ殑鐩殑鏄繘琛屾瘮杈冿紝鎮ㄥ彲浠ヤ娇鐢ㄨ兘杩斿洖澶氫釜鍒楃殑瀛愭煡璇€傝鍙傝13.2.8.5鑺傦紝鈥滆瀛愭煡璇⑩銆備笉杩囷紝鍦ㄥ叾瀹冪殑璇涓嬶紝瀛愭煡璇㈠繀椤讳负鏍囬噺鎿嶄綔鏁般
路 鏉ヨ嚜瀛愭煡璇㈢殑琛岀殑鏁扮洰涓嶆纭細
路 ERROR 1242 (ER_SUBSELECT_NO_1_ROW)
路 SQLSTATE = 21000
路 Message = "Subquery returns more than 1 row"
濡傛灉鍦ㄨ鍙ヤ腑锛屽瓙鏌ヨ杩斿洖鐨勮澶氫簬涓涓紝鍒欏彂鐢熸閿欒銆傝鑰冭檻浠ヤ笅渚嬪瓙锛
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
濡傛灉SELECT column1 FROM t2鍙繑鍥炰竴琛岋紝鍒欏皢鎵ц浠ュ墠鐨勬煡璇€傚鏋滃瓙鏌ヨ杩斿洖鐨勮澶氫簬涓涓紝鍒欏皢鍑虹幇閿欒1242銆傚湪杩欑鎯呭喌涓嬶紝璇ユ煡璇㈠皢琚敼鍐欎负锛
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
路 鍦ㄥ瓙鏌ヨ涓〃鏍间娇鐢ㄤ笉姝g‘锛
路 Error 1093 (ER_UPDATE_TABLE_USED)
路 SQLSTATE = HY000
路 Message = "You can't specify target table 'x'
路 for update in FROM clause"
鍦ㄥ涓嬫儏鍐典笅锛屽彂鐢熻閿欒锛
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);
鍜SELECT璇彞涓鏍凤紝鍦UPDATE鍜DELETE璇彞涓紝瀛愭煡璇㈡槸鍚堟硶鐨勩傛墍浠ユ偍鍙互鍦UPDATE璇彞涓娇鐢ㄥ瓙鏌ヨ杩涜璧嬪笺備笉杩囷紝鎮ㄤ笉鑳芥妸鍚屼竴涓〃锛堝湪鏈緥涓负琛t1锛夋棦鐢ㄤ簬瀛愭煡璇㈢殑FROM瀛愬彞锛屽張鐢ㄤ簬鏇存柊鐩爣銆
瀵逛簬浜嬪姟瀛樺偍寮曟搸锛屽瓙鏌ヨ鐨勯敊璇細瀵艰嚧鏁翠釜璇彞澶辨晥銆傚浜庨潪浜嬪姟瀛樺偍寮曟搸锛屽湪閬囧埌閿欒涔嬪墠杩涜鐨勬暟鎹慨璁細琚繚鐣欍
寮鍙戣繃绋嬩笉鏂繘灞曪紝鎵浠ヤ粠闀胯繙鏉ョ湅锛屾病鏈変竴涓彲闈犵殑浼樺寲鎶宸с傛湁浜涙妧宸ф偍鍙兘浼氭劅鍏磋叮锛屽苟鍘熸剰閲囩敤锛
路 鏈変簺瀛愬彞浼氬奖鍝嶅湪瀛愭煡璇腑鐨勮鐨勬暟閲忓拰椤哄簭銆備娇鐢ㄨ繖绫诲瓙鍙ャ備緥濡傦細
路 SELECT * FROM t1 WHERE t1.column1 IN
路 (SELECT column1 FROM t2 ORDER BY column1);
路 SELECT * FROM t1 WHERE t1.column1 IN
路 (SELECT DISTINCT column1 FROM t2);
路 SELECT * FROM t1 WHERE EXISTS
路 (SELECT * FROM t2 LIMIT 1);
路 鐢ㄥ瓙鏌ヨ鏇挎崲鑱斿悎銆備緥濡傦紝璇曡繘琛屽涓嬫搷浣滐細
路 SELECT DISTINCT column1 FROM t1 WHERE t1.column1 IN (
路 SELECT column1 FROM t2);
浠f浛濡備笅鎿嶄綔锛
SELECT DISTINCT t1.column1 FROM t1, t2
WHERE t1.column1 = t2.column1;
路 閮ㄥ垎瀛愭煡璇㈠彲浠ヨ杞崲涓鸿仈鍚堬紝浠ヤ究涓庝笉鏀寔瀛愭煡璇㈢殑鏃х増鏈殑MySQL鐩稿吋瀹广備笉杩囷紝鍦ㄦ湁浜涙儏鍐典笅锛屾妸瀛愭煡璇㈣浆鍖栦负鑱斿悎鍙互鎻愰珮鏁堟灉銆傝鍙傝13.2.8.11鑺傦紝鈥滄妸瀛愭煡璇綔涓虹敤浜庢棭鏈烳ySQL鐗堟湰鐨勮仈鍚堣繘琛屾敼鍐欌銆
路 鎶婂瓙鍙ヤ粠瀛愭煡璇㈢殑澶栭儴杞Щ鍒板唴閮ㄣ備緥濡傦紝浣跨敤姝ゆ煡璇細
路 SELECT * FROM t1
路 WHERE s1 IN (SELECT s1 FROM t1 UNION ALL SELECT s1 FROM t2);
浠f浛姝ゆ煡璇細
SELECT * FROM t1
WHERE s1 IN (SELECT s1 FROM t1) OR s1 IN (SELECT s1 FROM t2);
鍙︿竴涓緥瀛愭槸锛屼娇鐢ㄦ鏌ヨ锛
SELECT (SELECT column1 + 5 FROM t1) FROM t2;
浠f浛姝ゆ煡璇細
SELECT (SELECT column1 FROM t1) + 5 FROM t2;
路 浣跨敤琛屽瓙鏌ヨ锛屼唬鏇垮叧鑱斿瓙鏌ヨ銆備妇渚嬭鏄庯紝浣跨敤姝ゆ煡璇細
路 SELECT * FROM t1
路 WHERE (column1,column2) IN (SELECT column1,column2 FROM t2);
浠f浛姝ゆ煡璇細
SELECT * FROM t1
WHERE EXISTS (SELECT * FROM t2 WHERE t2.column1=t1.column1
AND t2.column2=t1.column2);
路 Use NOT (a = ANY (...)) rather than a <> ALL (...).
路 Use x = ANY (table containing (1,2)) rather than x=1 OR x=2.
路 Use = ANY rather than EXISTS.
路 瀵逛簬鍙繑鍥炰竴琛岀殑鏃犲叧鑱斿瓙鏌ヨ锛IN鐨勯熷害鎱簬锛濄備妇渚嬭鏄庯紝浣跨敤姝ゆ煡璇細
路 SELECT * FROM t1 WHERE t1.col_name
路 = (SELECT a FROM t2 WHERE b = some_const);
浠f浛姝ゆ煡璇細
SELECT * FROM t1 WHERE t1.col_name
IN (SELECT a FROM t2 WHERE b = some_const);
浣跨敤杩欎簺鎶宸у彲浠ヤ娇绋嬪簭鏇村揩鎴栨洿鎱€備娇鐢BENCHMARK()鍑芥暟绛MySQL宸ュ叿锛屾偍鍙互浜嗚В鍒板湪鎮ㄦ墍澶勭殑鎯呭喌涓嬶紝鍝簺鎶宸т細鏈夊府鍔┿
MySQL鏈韩杩涜鐨勯儴鍒嗕紭鍖栧寘鎷細
路 MySQL鍙墽琛屼竴娆℃棤鍏宠仈瀛愭煡璇€備娇鐢EXPLAIN纭缁欏畾鐨勫瓙鏌ヨ纭疄鏄棤鍏宠仈鐨勩
路 MySQL鏀瑰啓IN, ALL, ANY鍜SOME瀛愭煡璇紝鐩殑鏄鏋滃瓙鏌ヨ涓殑select-list鍒楀凡缂栧埗绱㈠紩锛屽垯鑳藉彂鎸ュ嚭姝や紭鍔裤
路 MySQL浣跨敤index-lookup鍑芥暟浠f浛浠ヤ笅鏍煎紡鐨勫瓙鏌ヨ銆EXPLAIN鎶婃鍑芥暟鎻忚堪涓虹壒娈婄殑鑱斿悎绫诲瀷锛unique_subquery鎴index_subquery锛夛細
路 ... IN (SELECT indexed_column FROM single_table ...)
路 褰撹〃杈惧紡涓笉鍖呭惈NULL鍊兼垨绌洪泦鏃讹紝MySQL浣跨敤涓涓寘鍚MIN()鎴MAX()鐨勮〃杈惧紡锛屽浠ヤ笅鏍煎紡鐨勮〃杈惧紡杩涜鎵╁睍锛
路 value {ALL|ANY|SOME} {> | < | >= | <=} (non-correlated subquery)
渚嬪锛屾湰WHERE瀛愬彞锛
WHERE 5 > ALL (SELECT x FROM t)
鍙互鐢ㄤ紭鍖栫杩涜濡備笅澶勭悊锛
WHERE 5 > (SELECT MAX(x) FROM t)
鍦MySQL鍐呴儴鎵嬪唽涓湁涓绔犲悕涓衡MySQL濡備綍杞崲瀛愭煡璇⑩濓紝鍙互浠http://dev.mysql.com/doc/鑾峰彇銆
涓句緥璇存槑锛屾湰鏌ヨ锛
SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);
鍙互琚敼鍐欎负锛
SELECT DISTINCT t1.* FROM t1, t2 WHERE t1.id=t2.id;
浠ヤ笅鏌ヨ锛
SELECT * FROM t1 WHERE id NOT IN (SELECT id FROM t2);
SELECT * FROM t1 WHERE NOT EXISTS (SELECT id FROM t2 WHERE t1.id=t2.id);
涔熷彲浠ヤ娇鐢IN()杩涜鏀瑰啓锛
SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;
LEFT [OUTER] JOIN鍙互姣斿搴旂殑瀛愭煡璇㈡洿蹇紝鍥犱负鏈嶅姟鍣ㄥ彲鑳藉鍏惰繘琛屾洿濂界殑浼樺寲鈥斺旇繖涓鐐瑰浜庡崟鐙殑MySQL鏈嶅姟鍣ㄥ苟涓嶆槑纭傚湪SQL-92涔嬪墠锛屼笉瀛樺湪澶栭儴鑱斿悎锛屽洜姝ゅ湪鍋氭煇浜涗簨鎯呮椂锛屽瓙鏌ヨ鏄敮涓鐨勬柟娉曘傜幇鍦紝MySQL鏈嶅姟鍣ㄥ拰鍏跺畠璁稿鍏堣繘鐨勬暟鎹簱绯荤粺閮借兘鎻愪緵澶氱鐨勫閮ㄨ仈鍚堢被鍨嬨
MySQL鏀寔multiple-table DELETE璇彞锛岃璇彞鍙互琚敤浜庨珮鏁堝湴鍒犻櫎琛屻傚垹闄ゆ椂渚濇嵁鏉ヨ嚜涓涓〃鎴栧悓鏃舵潵鑷涓〃鐨勪俊鎭傚悓鏃朵篃鏀寔Multiple-table UPDATE璇彞銆
TRUNCATE [TABLE] tbl_name
TRUNCATE TABLE鐢ㄤ簬瀹屽叏娓呯┖涓涓〃銆備粠閫昏緫涓婅锛岃璇彞涓庣敤浜庡垹闄ゆ墍鏈夎鐨DELETE璇彞绛夊悓锛屼絾鏄湪鏈変簺鎯呭喌涓嬶紝涓よ呭湪浣跨敤涓婃湁鎵涓嶅悓銆
瀵逛簬InnoDB琛紝濡傛灉鏈夐渶瑕佸紩鐢ㄨ〃鐨勫閿檺鍒讹紝鍒TRUNCATE TABLE琚槧灏勫埌DELETE涓婏紱鍚﹀垯浣跨敤蹇熷垹鍑忥紙鍙栨秷鍜岄噸鏂板垱寤鸿〃锛夈備娇鐢TRUNCATE TABLE閲嶆柊璁剧疆AUTO_INCREMENT璁℃暟鍣紝璁剧疆鏃朵笉鑰冭檻鏄惁鏈夊閿檺鍒躲
瀵逛簬鍏跺畠瀛樺偍寮曟搸锛屽湪MySQL 5.1涓紝TRUNCATE TABLE涓DELETE FROM鏈変互涓嬪嚑澶勪笉鍚岋細
路 鍒犲噺鎿嶄綔浼氬彇娑堝苟閲嶆柊鍒涘缓琛紝杩欐瘮涓琛屼竴琛岀殑鍒犻櫎琛岃蹇緢澶氥
路 鍒犲噺鎿嶄綔涓嶈兘淇濊瘉瀵逛簨鍔℃槸瀹夊叏鐨勶紱鍦ㄨ繘琛屼簨鍔″鐞嗗拰琛ㄩ攣瀹氱殑杩囩▼涓皾璇曡繘琛屽垹鍑忥紝浼氬彂鐢熼敊璇
路 琚垹闄ょ殑琛岀殑鏁扮洰娌℃湁琚繑鍥炪
路 鍙琛ㄥ畾涔夋枃浠tbl_name.frm鏄悎娉曠殑锛屽垯鍙互浣跨敤TRUNCATE TABLE鎶婅〃閲嶆柊鍒涘缓涓轰竴涓┖琛紝鍗充娇鏁版嵁鎴栫储寮曟枃浠跺凡缁忚鐮村潖銆
路 琛ㄧ鐞嗙▼搴忎笉璁板緱鏈鍚庤浣跨敤鐨AUTO_INCREMENT鍊硷紝浣嗘槸浼氫粠澶村紑濮嬭鏁般傚嵆浣垮浜MyISAM鍜InnoDB涔熸槸濡傛銆MyISAM鍜InnoDB閫氬父涓嶅啀娆′娇鐢ㄥ簭鍒楀笺
路 褰撹鐢ㄤ簬甯﹀垎鍖虹殑琛ㄦ椂锛TRUNCATE TABLE浼氫繚鐣欏垎鍖猴紱鍗筹紝鏁版嵁鍜岀储寮曟枃浠惰鍙栨秷骞堕噸鏂板垱寤猴紝鍚屾椂鍒嗗尯瀹氫箟锛.par锛夋枃浠朵笉鍙楀奖鍝嶃
TRUNCATE TABLE鏄湪MySQL涓噰鐢ㄧ殑涓涓Oracle SQL鎵╁睍銆
Single-table璇硶锛
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table璇硶锛
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
UPDATE璇硶鍙互鐢ㄦ柊鍊兼洿鏂板師鏈夎〃琛屼腑鐨勫悇鍒椼SET瀛愬彞鎸囩ず瑕佷慨鏀瑰摢浜涘垪鍜岃缁欎簣鍝簺鍊笺WHERE瀛愬彞鎸囧畾搴旀洿鏂板摢浜涜銆傚鏋滄病鏈WHERE瀛愬彞锛屽垯鏇存柊鎵鏈夌殑琛屻傚鏋滄寚瀹氫簡ORDER BY瀛愬彞锛屽垯鎸夌収琚寚瀹氱殑椤哄簭瀵硅杩涜鏇存柊銆LIMIT瀛愬彞鐢ㄤ簬缁欏畾涓涓檺鍊硷紝闄愬埗鍙互琚洿鏂扮殑琛岀殑鏁扮洰銆
UPDATE璇彞鏀寔浠ヤ笅淇グ绗︼細
路 濡傛灉鎮ㄤ娇鐢LOW_PRIORITY鍏抽敭璇嶏紝鍒UPDATE鐨勬墽琛岃寤惰繜浜嗭紝鐩村埌娌℃湁鍏跺畠鐨勫鎴风浠庤〃涓鍙栦负姝€
路 濡傛灉鎮ㄤ娇鐢IGNORE鍏抽敭璇嶏紝鍒欏嵆浣垮湪鏇存柊杩囩▼涓嚭鐜伴敊璇紝鏇存柊璇彞涔熶笉浼氫腑鏂傚鏋滃嚭鐜颁簡閲嶅鍏抽敭瀛楀啿绐侊紝鍒欒繖浜涜涓嶄細琚洿鏂般傚鏋滃垪琚洿鏂板悗锛屾柊鍊间細瀵艰嚧鏁版嵁杞寲閿欒锛屽垯杩欎簺琛岃鏇存柊涓烘渶鎺ヨ繎鐨勫悎娉曠殑鍊笺
濡傛灉鎮ㄥ湪涓涓〃杈惧紡涓氳繃tbl_name璁块棶涓鍒楋紝鍒UPDATE浣跨敤鍒椾腑鐨勫綋鍓嶅笺備緥濡傦紝浠ヤ笅璇彞鎶婂勾榫勫垪璁剧疆涓烘瘮褰撳墠鍊煎涓锛
mysql> UPDATE persondata SET age=age+1;
UPDATE璧嬪艰浠庡乏鍒板彸璇勪及銆備緥濡傦紝浠ヤ笅璇彞瀵瑰勾榫勫垪鍔犲嶏紝鐒跺悗鍐嶈繘琛屽鍔狅細
mysql> UPDATE persondata SET age=age*2, age=age+1;
濡傛灉鎮ㄦ妸涓鍒楄缃负鍏跺綋鍓嶅惈鏈夌殑鍊硷紝鍒MySQL浼氭敞鎰忓埌杩欎竴鐐癸紝浣嗕笉浼氭洿鏂般
濡傛灉鎮ㄦ妸琚凡瀹氫箟涓NOT NULL鐨勫垪鏇存柊涓NULL锛屽垯璇ュ垪琚缃埌涓庡垪绫诲瀷瀵瑰簲鐨勯粯璁ゅ硷紝骞朵笖绱姞璀﹀憡鏁般傚浜庢暟瀛楃被鍨嬶紝榛樿鍊间负0锛涘浜庡瓧绗︿覆绫诲瀷锛岄粯璁ゅ间负绌哄瓧绗︿覆('')锛涘浜庢棩鏈熷拰鏃堕棿绫诲瀷锛岄粯璁ゅ间负鈥zero鈥濆笺
UPDATE浼氳繑鍥炲疄闄呰鏀瑰彉鐨勮鐨勬暟鐩Mysql_info() C API鍑芥暟鍙互杩斿洖琚尮閰嶅拰琚洿鏂扮殑琛岀殑鏁扮洰锛屼互鍙婂湪UPDATE杩囩▼涓骇鐢熺殑璀﹀憡鐨勬暟閲忋
鎮ㄥ彲浠ヤ娇鐢LIMIT row_count鏉ラ檺瀹UPDATE鐨勮寖鍥淬LIMIT瀛愬彞鏄竴涓笌琛屽尮閰嶇殑闄愬畾銆傚彧瑕佸彂鐜板彲浠ユ弧瓒WHERE瀛愬彞鐨row_count琛岋紝鍒欒璇彞涓锛屼笉璁鸿繖浜涜鏄惁琚敼鍙樸
濡傛灉涓涓UPDATE璇彞鍖呮嫭涓涓ORDER BY瀛愬彞锛屽垯鎸夌収鐢卞瓙鍙ユ寚瀹氱殑椤哄簭鏇存柊琛屻
鎮ㄤ篃鍙互鎵ц鍖呮嫭澶氫釜琛ㄧ殑UPDATE鎿嶄綔銆table_references瀛愬彞鍒楀嚭浜嗗湪鑱斿悎涓寘鍚殑琛ㄣ傝璇硶鍦13.2.7.1鑺傦紝鈥淛OIN璇硶鈥涓繘琛屼簡璇存槑銆備互涓嬫槸涓涓緥瀛愶細
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;
浠ヤ笂鐨勪緥瀛愭樉绀哄嚭浜嗕娇鐢ㄩ楀彿鎿嶄綔绗︾殑鍐呴儴鑱斿悎锛屼絾鏄multiple-table UPDATE璇彞鍙互浣跨敤鍦SELECT璇彞涓厑璁哥殑浠讳綍绫诲瀷鐨勮仈鍚堬紝姣斿LEFT JOIN銆
娉ㄩ噴锛氭偍涓嶈兘鎶ORDER BY鎴LIMIT涓multiple-table UPDATE鍚屾椂浣跨敤銆
鍦ㄤ竴涓鏇存敼鐨multiple-table UPDATE涓紝鏈変簺鍒楄寮曠敤銆傛偍鍙渶瑕佽繖浜涘垪鐨UPDATE鏉冮檺銆傛湁浜涘垪琚鍙栦簡锛屼絾鏄病琚慨鏀广傛偍鍙渶瑕佽繖浜涘垪鐨SELECT鏉冮檺銆
濡傛灉鎮ㄤ娇鐢ㄧ殑multiple-table UPDATE璇彞涓寘鍚甫鏈夊閿檺鍒剁殑InnoDB琛紝鍒MySQL浼樺寲绗﹀鐞嗚〃鐨勯『搴忓彲鑳戒笌涓婁笅灞傜骇鍏崇郴鐨勯『搴忎笉鍚屻傚湪姝ゆ儏鍐典笅锛岃鍙ユ棤鏁堝苟琚 鍥炴粴銆傚悓鏃讹紝鏇存柊涓涓崟涓琛紝骞朵笖渚濋潬ON UPDATE鍔熻兘銆傝鍔熻兘鐢InnoDB鎻愪緵锛岀敤浜庡鍏跺畠琛ㄨ繘琛岀浉搴旂殑淇敼銆傝鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥銆
鐩墠锛屾偍涓嶈兘鍦ㄤ竴涓瓙鏌ヨ涓洿鏂颁竴涓〃锛屽悓鏃朵粠鍚屼竴涓〃涓夋嫨銆
{DESCRIBE | DESC} tbl_name [col_name | wild]
DESCRIBE鍙互鎻愪緵鏈夊叧琛ㄤ腑鍚勫垪鐨勪俊鎭傚畠鏄SHOW COLUMNS FROM鐨勫揩鎹锋柟寮忋傝繖浜涜鍙ヤ篃鍙互鏄剧ず璇彞锛岀敤浜庨槄瑙堛
瑙13.5.4.3鑺傦紝鈥淪HOW COLUMNS璇硶鈥銆
col_name鍙互鏄竴涓垪鍚嶇О锛屾垨涓涓寘鍚橈紖鈥欏拰鈥_鈥欑殑閫氶厤绗︾殑瀛楃涓诧紝鐢ㄤ簬鑾峰緱瀵逛簬甯︽湁涓庡瓧绗︿覆鐩稿尮閰嶇殑鍚嶇О鐨勫悇鍒楃殑杈撳嚭銆傛病鏈夊繀瑕佸湪寮曞彿涓寘鍚瓧绗︿覆锛岄櫎闈炲叾涓寘鍚┖鏍兼垨鍏跺畠鐗规畩瀛楃銆
mysql> DESCRIBE city;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
NULL瀛楁鎸囩ず鏄惁NULL鍙互琚瓨鍌ㄥ湪鍒椾腑銆
Key瀛楁鎸囩ず鏄惁璇ュ垪宸茬紪鍒剁储寮曘PRI鐨勫兼寚绀鸿鍒楁槸琛ㄧ殑涓婚敭鐨勪竴閮ㄥ垎銆UNI鎸囩ず锛岃鍒楁槸UNIQUE绱㈠紩鐨勪竴閮ㄥ垎銆MUL鍊兼寚绀猴紝鍦ㄥ垪涓煇涓粰瀹氬煎娆″嚭鐜版槸鍏佽鐨勩
MUL灏嗚鏄剧ず鍦UNIQUE绱㈠紩涓紝鍘熷洜涔嬩竴鏄涓垪浼氱粍鍚堟垚涓涓鍚UNIQUE绱㈠紩锛涘敖绠″垪鐨勭粍鍚堟槸鍞竴鐨勶紝浣嗘瘡涓垪浠嶅彲浠ュ娆″嚭鐜板悓涓涓粰瀹氬笺傛敞鎰忥紝鍦ㄥ鍚堢储寮曚腑锛屽彧鏈夌储寮曟渶宸﹁竟鐨勫垪鍙互杩涘叆Key瀛楁涓
榛樿瀛楁鎸囩ず锛岄粯璁ゅ艰璧嬩簣璇ュ垪銆
Extra瀛楁鍖呭惈鍙互鑾峰彇鐨勪笌缁欏畾鍒楁湁鍏崇殑闄勫姞淇℃伅銆傚湪鎴戜滑鐨勪緥瀛愪腑锛Extra瀛楁鎸囩ず锛Id鍒椾娇鐢AUTO_INCREMENT鍏抽敭璇嶅垱寤恒
濡傛灉鍒楃被鍨嬩笌鎮ㄩ璁$殑渚濇嵁CREATE TABLE璇彞寰楀嚭鐨勫垪绫诲瀷涓嶅悓锛屽垯璇锋敞鎰忥紝MySQL鏈夋椂浼氭敼鍙樺垪绫诲瀷銆傝鍙傝13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹銆
DESCRIBE璇彞琚绔嬪嚭鏉ワ紝鐢ㄤ簬涓Oracle鐩稿吋瀹广
SHOW CREATE TABLE鍜SHOW TABLE STATUS璇彞涔熷彲浠ユ彁渚涙湁鍏宠〃鐨勪俊鎭傝鍙傝13.5.4鑺傦紝鈥淪HOW璇硶鈥銆
USE db_name
USE db_name璇彞鍙互閫氬憡MySQL鎶db_name鏁版嵁搴撲綔涓洪粯璁わ紙褰撳墠锛夋暟鎹簱浣跨敤锛岀敤浜庡悗缁鍙ャ傝鏁版嵁搴撲繚鎸佷负榛樿鏁版嵁搴擄紝鐩村埌璇鐨勭粨灏撅紝鎴栬呯洿鍒板彂甯冧竴涓笉鍚岀殑USE璇彞锛
mysql> USE db1;
mysql> SELECT COUNT(*) FROM mytable; # selects from db1.mytable
mysql> USE db2;
mysql> SELECT COUNT(*) FROM mytable; # selects from db2.mytable
浣跨敤USE璇彞涓轰竴涓壒瀹氱殑褰撳墠鐨勬暟鎹簱鍋氭爣璁帮紝涓嶄細闃荤鎮ㄨ闂叾瀹冩暟鎹簱涓殑琛ㄣ備笅闈㈢殑渚嬪瓙鍙互浠db1鏁版嵁搴撹闂綔鑰呰〃锛屽苟浠db2鏁版嵁搴撹闂紪杈戣〃锛
mysql> USE db1;
mysql> SELECT author_name,editor_name FROM author,db2.editor
-> WHERE author.editor_id = db2.editor.editor_id;
USE璇彞琚绔嬪嚭鏉ワ紝鐢ㄤ簬涓Sybase鐩稿吋瀹广
MySQL閫氳繃SET AUTOCOMMIT, START TRANSACTION, COMMIT鍜ROLLBACK绛夎鍙ユ敮鎸佹湰鍦颁簨鍔★紙鍦ㄧ粰瀹氱殑瀹㈡埛绔繛鎺ヤ腑锛夈傝鍙傝13.4.1鑺傦紝鈥淪TART TRANSACTION, COMMIT鍜孯OLLBACK璇硶鈥銆XA浜嬪姟鏀寔杩樺彲浠ュ厑璁MySQL鍙備笌鍒嗗竷寮忎簨鍔°傝鍙傝13.4.7鑺傦紝鈥淴A浜嬪姟鈥銆
START TRANSACTION | BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET AUTOCOMMIT = {0 | 1}
START TRANSACTION鎴BEGIN璇彞鍙互寮濮嬩竴椤规柊鐨勪簨鍔°COMMIT鍙互鎻愪氦褰撳墠浜嬪姟锛屾槸鍙樻洿鎴愪负姘镐箙鍙樻洿銆ROLLBACK鍙互 鍥炴粴褰撳墠浜嬪姟锛屽彇娑堝叾鍙樻洿銆SET AUTOCOMMIT璇彞鍙互绂佺敤鎴栧惎鐢ㄩ粯璁ょ殑autocommit妯″紡锛岀敤浜庡綋鍓嶈繛鎺ャ
鑷夌殑WORK鍏抽敭璇嶈鏀寔锛岀敤浜COMMIT鍜RELEASE锛屼笌CHAIN鍜RELEASE瀛愬彞銆CHAIN鍜RELEASE鍙互琚敤浜庡浜嬪姟瀹屾垚杩涜闄勫姞鎺у埗銆Completion_type绯荤粺鍙橀噺鐨勫煎喅瀹氫簡榛樿瀹屾垚鐨勬ц川銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆
AND CHAIN瀛愬彞浼氬湪褰撳墠浜嬪姟缁撴潫鏃讹紝绔嬪埢鍚姩涓涓柊浜嬪姟锛屽苟涓旀柊浜嬪姟涓庡垰缁撴潫鐨勪簨鍔℃湁鐩稿悓鐨勯殧绂荤瓑绾сRELEASE瀛愬彞鍦ㄧ粓姝簡褰撳墠浜嬪姟鍚庯紝浼氳鏈嶅姟鍣ㄦ柇寮涓庡綋鍓嶅鎴风鐨勮繛鎺ャ傚寘鍚NO鍏抽敭璇嶅彲浠ユ姂鍒CHAIN鎴RELEASE瀹屾垚銆傚鏋completion_type绯荤粺鍙橀噺琚缃负涓瀹氱殑鍊硷紝浣胯繛閿佹垨閲婃斁瀹屾垚鍙互榛樿杩涜锛屾鏃NO鍏抽敭璇嶆湁鐢ㄣ
榛樿鎯呭喌涓嬶紝MySQL閲囩敤autocommit妯″紡杩愯銆傝繖鎰忓懗鐫锛屽綋鎮ㄦ墽琛屼竴涓敤浜庢洿鏂帮紙淇敼锛夎〃鐨勮鍙ヤ箣鍚庯紝MySQL绔嬪埢鎶婃洿鏂板瓨鍌ㄥ埌纾佺洏涓
濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓簨鍔″畨鍏ㄥ瀷鐨勫瓨鍌ㄥ紩鎿庯紙濡InnoDB, BDB鎴NDB绨囷級锛屽垯鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬭鍙ョ鐢autocommit妯″紡锛
SET AUTOCOMMIT=0;
閫氳繃鎶AUTOCOMMIT鍙橀噺璁剧疆涓洪浂锛岀鐢autocommit妯″紡涔嬪悗锛屾偍蹇呴』浣跨敤COMMIT鎶婂彉鏇村瓨鍌ㄥ埌纾佺洏涓紝鎴栫潃濡傛灉鎮ㄦ兂瑕佸拷鐣ヤ粠浜嬪姟寮濮嬭繘琛屼互鏉ュ仛鍑虹殑鍙樻洿锛屼娇鐢ROLLBACK銆
濡傛灉鎮ㄦ兂瑕佸浜庝竴涓崟涓绯诲垪鐨勮鍙ョ鐢autocommit妯″紡锛屽垯鎮ㄥ彲浠ヤ娇鐢START TRANSACTION璇彞锛
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
浣跨敤START TRANSACTION锛autocommit浠嶇劧琚鐢紝鐩村埌鎮ㄤ娇鐢COMMIT鎴ROLLBACK缁撴潫浜嬪姟涓烘銆傜劧鍚autocommit妯″紡鎭㈠鍒板師鏉ョ殑鐘舵併
BEGIN鍜BEGIN WORK琚綔涓START TRANSACTION鐨勫埆鍚嶅彈鍒版敮鎸侊紝鐢ㄤ簬瀵逛簨鍔¤繘琛屽垵濮嬪寲銆START TRANSACTION鏄爣鍑嗙殑SQL璇硶锛屽苟涓旀槸鍚姩涓涓ad-hoc浜嬪姟鐨勬帹鑽愭柟娉曘BEGIN璇彞涓BEGIN鍏抽敭璇嶇殑浣跨敤涓嶅悓銆BEGIN鍏抽敭璇嶅彲浠ュ惎鍔ㄤ竴涓BEGIN...END澶嶅悎璇彞銆傚悗鑰呬笉浼氬紑濮嬩竴椤逛簨鍔°傝鍙傝20.2.7鑺傦紝鈥淏EGIN ... END澶嶅悎璇彞鈥銆
鎮ㄤ篃鍙互鎸夌収濡備笅鏂规硶寮濮嬩竴椤逛簨鍔★細
START TRANSACTION WITH CONSISTENT SNAPSHOT;
WITH CONSISTENT SNAPSHOT瀛愬彞鐢ㄤ簬鍚姩涓涓竴鑷寸殑璇诲彇锛岀敤浜庡叿鏈夋绫诲姛鑳界殑瀛樺偍寮曟搸銆傜洰鍓嶏紝璇ュ瓙鍙ュ彧閫傜敤浜InnoDB銆傝瀛愬彞鐨勬晥鏋滀笌鍙戝竷涓涓START TRANSACTION锛屽悗闈㈣窡涓涓潵鑷换浣InnoDB琛ㄧ殑SELECT鐨勬晥鏋滀竴鏍枫傝鍙傝15.2.10.4鑺傦紝鈥滀竴鑷寸殑闈為攣瀹氳鈥銆
寮濮嬩竴椤逛簨鍔′細閫犳垚涓涓殣鍚殑UNLOCK TABLES琚墽琛屻
涓轰簡鑾峰緱鏈濂界殑缁撴灉锛屼簨鍔″簲鍙娇鐢ㄧ敱鍗曚竴浜嬪姟瀛樺偍寮曟搸绠$悊鐨勮〃鎵ц銆傚惁鍒欙紝浼氬嚭鐜颁互涓嬮棶棰橈細
路 濡傛灉鎮ㄤ娇鐢ㄧ殑琛ㄦ潵鑷涓簨鍔″畨鍏ㄥ瀷瀛樺偍寮曟搸锛堜緥濡InnoDB鍜BDB锛夛紝骞朵笖浜嬪姟闅旂绛夌骇涓嶆槸SERIALIZABLE锛屽垯鏈夊彲鑳藉綋涓涓簨鍔℃彁浜ゆ椂锛屽叾瀹冩鍦ㄨ繘琛屼腑鐨勩佷娇鐢ㄥ悓鏍风殑琛ㄧ殑浜嬪姟灏嗗彧浼氬彂鐢熺敱绗竴涓簨鍔′骇鐢熺殑鍙樻洿銆備篃灏辨槸锛岀敤娣峰悎寮曟搸涓嶈兘淇濊瘉浜嬪姟鐨勫師瀛愭э紝骞朵細閫犳垚涓嶄竴鑷淬傦紙濡傛灉娣峰悎寮曟搸浜嬪姟涓嶇粡甯告湁锛屽垯鎮ㄥ彲浠ユ牴鎹渶瑕佷娇鐢SET TRANSACTION ISOLATION LEVEL鎶婇殧绂荤瓑绾ц缃埌SERIALIZABLE銆傦級
路 濡傛灉鎮ㄥ湪浜嬪姟涓娇鐢ㄩ潪浜嬪姟瀹夊叏鍨嬭〃锛屽垯瀵硅繖浜涜〃鐨勪换浣曞彉鏇磋绔嬪埢瀛樺偍锛屼笉璁autocommit妯″紡鐨勭姸鎬佸浣曘
濡傛灉鎮ㄥ湪鏇存柊浜嗕簨鍔′腑涓涓簨鍔¤〃涔嬪悗锛屽彂甯冧竴涓ROLLBACK璇彞锛屽垯浼氬嚭鐜颁竴涓ER_WARNING_NOT_COMPLETE_ROLLBACK璀﹀憡銆傚浜嬪姟瀹夊叏鍨嬭〃鐨勫彉鏇磋 鍥炴粴锛屼絾鏄闈炰簨鍔″畨鍏ㄥ瀷琛ㄦ病鏈夊彉鏇淬
姣忎釜浜嬪姟琚瓨鍌ㄥ湪涓涓粍鍧椾腑鐨勪簩杩涘埗鏃ュ織涓紝鍦COMMIT涔嬩笂銆傝鍥炴粴鐨勪簨鍔′笉琚鍏ユ棩蹇椼傦紙渚嬪鎯呭喌锛氬闈炰簨鍔¤〃鐨勬洿鏀逛笉浼氳 鍥炴粴銆傚鏋滀竴涓鍥炴粴鐨勪簨鍔″寘鎷闈炰簨鍔¤〃鐨勬洿鏀癸紝鍒欐暣涓簨鍔′娇鐢ㄤ竴涓湪鏈鐨ROLLBACK璇彞璁″叆鏃ュ織锛屼互纭繚瀵硅繖浜涜〃鐨勬洿鏀硅繘琛屽鍒躲傦級瑙5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織鈥銆
鎮ㄥ彲浠ヤ娇鐢SET TRANSACTION ISOLATION LEVEL鏇存敼浜嬪姟鐨勯殧绂荤瓑绾с傝鍙傝13.4.6鑺傦紝鈥淪ET TRANSACTION璇硶鈥銆
鍥炴粴鍙互鎱㈤熻繍琛屻傚湪鐢ㄦ埛娌℃湁鏄庣‘瑕佹眰鏃讹紝涔熷彲浠ヨ繘琛屽洖婊氾紙渚嬪锛屽綋閿欒鍙戠敓鏃讹級銆傚洜姝わ紝鍦ㄦ槑纭湴鍜岄殣鍚殑锛ROLLBACK SQL鍛戒护锛夊洖婊氭椂锛SHOW PROCESSLIST浼氬湪Stage鍒椾腑鏄剧ずRolling back锛岀敤浜庤繛鎺ャ
鏈変簺璇彞涓嶈兘琚洖婊氥傞氬父锛岃繖浜涜鍙ュ寘鎷暟鎹畾涔夎瑷锛DDL锛夎鍙ワ紝姣斿鍒涘缓鎴栧彇娑堟暟鎹簱鐨勮鍙ワ紝鍜屽垱寤恒佸彇娑堟垨鏇存敼琛ㄦ垨瀛樺偍鐨勫瓙绋嬪簭鐨勮鍙ャ
鎮ㄥ湪璁捐浜嬪姟鏃讹紝涓嶅簲鍖呭惈杩欑被璇彞銆傚鏋滄偍鍦ㄤ簨鍔$殑鍓嶉儴涓彂甯冧簡涓涓笉鑳借鍥炴粴鐨勮鍙ワ紝鍒欏悗閮ㄧ殑鍏跺畠璇彞浼氬彂鐢熼敊璇紝鍦ㄨ繖浜涙儏鍐典笅锛岄氳繃鍙戝竷ROLLBACK璇彞涓嶈兘 鍥炴粴浜嬪姟鐨勫叏閮ㄦ晥鏋溿
浠ヤ笅璇彞锛堜互鍙婂悓涔夎瘝锛夊潎闅愬惈鍦扮粨鏉熶竴涓簨鍔★紝浼间箮鏄湪鎵ц鏈鍙ュ墠锛屾偍宸茬粡杩涜浜嗕竴涓COMMIT銆
路 ALTER FUNCTION, ALTER PROCEDURE, ALTER TABLE, BEGIN, CREATE DATABASE, CREATE FUNCTION, CREATE INDEX, CREATE PROCEDURE, CREATE TABLE, DROP DATABASE, DROP FUNCTION, DROP INDEX, DROP PROCEDURE, DROP TABLE, LOAD MASTER DATA, LOCK TABLES, RENAME TABLE, SET AUTOCOMMIT=1, START TRANSACTION, TRUNCATE TABLE, UNLOCK TABLES.
路 褰撳綋鍓嶆墍鏈夌殑琛ㄥ潎琚攣瀹氭椂锛UNLOCK TABLES鍙互鎻愪氦浜嬪姟銆
路 CREATE TABLE, CREATE DATABASE DROP DATABASE, TRUNCATE TABLE, ALTER FUNCTION, ALTER PROCEDURE, CREATE FUNCTION, CREATE PROCEDURE, DROP FUNCTION鍜DROP PROCEDURE绛夎鍙ヤ細瀵艰嚧涓涓殣鍚彁浜ゃ
路 InnoDB涓殑CREATE TABLE璇彞琚綔涓轰竴涓崟涓浜嬪姟杩涜澶勭悊銆傝繖鎰忓懗鐫锛屾潵鑷敤鎴风殑ROLLBACK涓嶄細鎾ら攢鐢ㄦ埛鍦ㄤ簨鍔″鐞嗚繃绋嬩腑鍒涘缓鐨CREATE TABLE璇彞銆
浜嬪姟涓嶈兘琚祵濂椼傝繖鏄殣鍚COMMIT鐨勭粨鏋溿傚綋鎮ㄥ彂甯冧竴涓START TRANSACTION璇彞鎴栧叾鍚屼箟璇嶆椂锛岃COMMIT琚墽琛岋紝鐢ㄤ簬浠讳綍褰撳墠浜嬪姟銆
SAVEPOINT identifier
ROLLBACK [WORK] TO SAVEPOINT identifier
RELEASE SAVEPOINT identifier
InnoDB鏀寔SQL璇彞SAVEPOINT, ROLLBACK TO SAVEPOINT, RELEASE SAVEPOINT鍜岃嚜閫夌殑鐢ㄤ簬ROLLBACK鐨WORK鍏抽敭璇嶃
SAVEPOINT璇彞鐢ㄤ簬璁剧疆涓涓簨鍔′繚瀛樼偣锛屽甫涓涓爣璇嗙鍚嶇О銆傚鏋滃綋鍓嶄簨鍔℃湁涓涓悓鏍峰悕绉扮殑淇濆瓨鐐癸紝鍒欐棫鐨勪繚瀛樼偣琚垹闄わ紝鏂扮殑淇濆瓨鐐硅璁剧疆銆
ROLLBACK TO SAVEPOINT璇彞浼氬悜浠ュ懡鍚嶇殑淇濆瓨鐐瑰洖婊氫竴涓簨鍔°傚鏋滃湪淇濆瓨鐐硅璁剧疆鍚庯紝褰撳墠浜嬪姟瀵硅杩涜浜嗘洿鏀癸紝鍒欒繖浜涙洿鏀逛細鍦 鍥炴粴涓鎾ら攢銆備絾鏄紝InnoDB涓嶄細閲婃斁琚瓨鍌ㄥ湪淇濆瓨鐐逛箣鍚庣殑瀛樺偍鍣ㄤ腑鐨勮閿佸畾銆傦紙娉ㄦ剰锛屽浜庢柊鎻掑叆鐨勮锛岄攣瀹氫俊鎭瀛樺偍鍦ㄨ涓殑浜嬪姟ID鎵胯浇锛涢攣瀹氭病鏈夎鍒嗗紑瀛樺偍鍦ㄥ瓨鍌ㄥ櫒涓傚湪杩欑鎯呭喌涓嬶紝琛岄攣瀹氬湪鎾ら攢涓閲婃斁銆傦級鍦ㄨ鍛藉悕鐨勪繚瀛樼偣涔嬪悗璁剧疆鐨勪繚瀛樼偣琚垹闄ゃ
濡傛灉璇彞杩斿洖浠ヤ笅閿欒锛屽垯鎰忓懗鐫涓嶅瓨鍦ㄥ甫鏈夋寚瀹氬悕绉扮殑淇濆瓨鐐癸細
ERROR 1181: Got error 153 during ROLLBACK
RELEASE SAVEPOINT璇彞浼氫粠褰撳墠浜嬪姟鐨勪竴缁勪繚瀛樼偣涓垹闄ゅ凡鍛藉悕鐨勪繚瀛樼偣銆備笉鍑虹幇鎻愪氦鎴 鍥炴粴銆傚鏋滀繚瀛樼偣涓嶅瓨鍦紝浼氬嚭鐜伴敊璇
濡傛灉鎮ㄦ墽琛COMMIT鎴栨墽琛屼笉鑳藉懡鍚嶄繚瀛樼偣鐨ROLLBACK锛屽垯褰撳墠浜嬪姟鐨勬墍鏈変繚瀛樼偣琚垹闄ゃ
LOCK TABLES
tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...
UNLOCK TABLES
LOCK TABLES鍙互閿佸畾鐢ㄤ簬褰撳墠绾跨▼鐨勮〃銆傚鏋滆〃琚叾瀹冪嚎绋嬮攣瀹氾紝鍒欓犳垚鍫靛锛岀洿鍒板彲浠ヨ幏鍙栨墍鏈夐攣瀹氫负姝€UNLOCK TABLES鍙互閲婃斁琚綋鍓嶇嚎绋嬩繚鎸佺殑浠讳綍閿佸畾銆傚綋绾跨▼鍙戝竷鍙︿竴涓LOCK TABLES鏃讹紝鎴栧綋涓庢湇鍔″櫒鐨勮繛鎺ヨ鍏抽棴鏃讹紝鎵鏈夌敱褰撳墠绾跨▼閿佸畾鐨勮〃琚殣鍚湴瑙i攣銆
琛ㄩ攣瀹氬彧鐢ㄤ簬闃叉鍏跺畠瀹㈡埛绔繘琛屼笉姝e綋鍦拌鍙栧拰鍐欏叆銆備繚鎸侀攣瀹氾紙鍗充娇鏄鍙栭攣瀹氾級鐨勫鎴风鍙互杩涜琛ㄥ眰绾х殑鎿嶄綔锛屾瘮濡DROP TABLE銆
娉ㄦ剰锛屼笅闈㈡槸瀵逛簨鍔¤〃浣跨敤LOCK TABLES鐨勮鏄庯細
路 鍦ㄥ皾璇曢攣瀹氳〃涔嬪墠锛LOCK TABLES涓嶆槸浜嬪姟瀹夊叏鍨嬬殑锛屼細闅愬惈鍦版彁浜ゆ墍鏈夋椿鎬т簨鍔°傚悓鏃讹紝寮濮嬩竴椤逛簨鍔★紙渚嬪锛屼娇鐢START TRANSACTION锛夛紝浼氶殣鍚湴鎵цUNLOCK TABLES銆傦紙瑙13.4.3鑺傦紝鈥滀細閫犳垚闅愬紡鎻愪氦鐨勮鍙モ銆
路 瀵逛簨鍔¤〃锛堝InnoDB锛変娇鐢LOCK TABLES鐨勬纭柟娉曟槸锛岃缃AUTOCOMMIT=0骞朵笖涓嶈兘璋冪敤UNLOCK TABLES锛岀洿鍒版偍鏄庣‘鍦版彁浜や簨鍔′负姝€傚綋鎮ㄨ皟鐢LOCK TABLES鏃讹紝InnoDB浼氬唴閮ㄥ湴鍙栧叾鑷繁鐨勮〃閿佸畾锛MySQL鍙栧叾鑷繁鐨勮〃閿佸畾銆InnoDB鍦ㄤ笅涓涓彁浜ゆ椂閲婃斁鍏惰〃閿佸畾锛屼絾鏄紝瀵逛簬MySQL锛岃閲婃斁琛ㄩ攣瀹氾紝鎮ㄥ繀椤昏皟鐢UNLOCK TABLES銆傛偍涓嶅簲璇ヨAUTOCOMMIT=1锛屽洜涓洪偅鏍风殑璇濓紝InnoDB浼氬湪璋冪敤LOCK TABLES涔嬪悗绔嬪埢閲婃斁琛ㄩ攣瀹氾紝骞朵笖寰堝鏄撳舰鎴愭閿佸畾銆傛敞鎰忥紝濡傛灉AUTOCOMMIT=1锛屾垜浠牴鏈笉鑳借幏鍙InnoDB琛ㄩ攣瀹氾紝杩欐牱灏卞彲浠ュ府鍔╂棫鐨勫簲鐢ㄨ蒋浠堕伩鍏嶄笉蹇呰鐨勬閿佸畾銆
路 ROLLBACK涓嶄細閲婃斁MySQL鐨勯潪浜嬪姟琛ㄩ攣瀹氥
瑕佷娇鐢LOCK TABLES锛屾偍蹇呴』鎷ユ湁鐩稿叧琛ㄧ殑LOCK TABLES鏉冮檺鍜SELECT鏉冮檺銆
浣跨敤LOCK TABLES鐨勪富瑕佸師鍥犳槸浠挎晥浜嬪姟锛屾垨鍦ㄦ洿鏂拌〃鏃跺姞蹇熷害銆傝繖灏嗗湪鍚庨潰杩涜鏇磋缁嗙殑瑙i噴銆
濡傛灉涓涓嚎绋嬭幏寰楀涓涓〃鍦READ閿佸畾锛岃绾跨▼锛堝拰鎵鏈夊叾瀹冪嚎绋嬶級鍙兘浠庤琛ㄤ腑璇诲彇銆傚鏋滀竴涓嚎绋嬭幏寰楀涓涓〃鐨WRITE閿佸畾锛屽彧鏈変繚鎸侀攣瀹氱殑绾跨▼鍙互瀵硅〃杩涜鍐欏叆銆傚叾瀹冪殑绾跨▼琚樆姝紝鐩村埌閿佸畾琚噴鏀炬椂涓烘銆
READ LOCAL鍜READ涔嬮棿鐨勫尯鍒槸锛READ LOCAL鍏佽鍦ㄩ攣瀹氳淇濇寔鏃讹紝鎵ц闈炲啿绐佹INSERT璇彞锛堝悓鏃舵彃鍏ワ級銆備絾鏄紝濡傛灉鎮ㄦ鎵撶畻鍦MySQL澶栭潰鎿嶄綔鏁版嵁搴撴枃浠讹紝鍚屾椂鎮ㄤ繚鎸侀攣瀹氾紝鍒欎笉鑳戒娇鐢READ LOCAL銆傚浜InnoDB琛紝READ LOCAL涓READ鐩稿悓銆
褰撴偍浣跨敤LOCK TABLES鏃讹紝鎮ㄥ繀椤婚攣瀹氭偍鎵撶畻鍦ㄦ煡璇腑浣跨敤鐨勬墍鏈夌殑琛ㄣ傝櫧鐒朵娇鐢LOCK TABLES璇彞鑾峰緱鐨勯攣瀹氫粛鐒舵湁鏁堬紝浣嗘槸鎮ㄤ笉鑳借闂病鏈夎姝よ鍙ラ攣瀹氱殑浠讳綍鐨勮〃銆傚悓鏃讹紝鎮ㄤ笉鑳藉湪涓娆℃煡璇腑澶氭浣跨敤涓涓凡閿佸畾鐨勮〃鈥斺斾娇鐢ㄥ埆鍚嶄唬鏇匡紝鍦ㄦ鎯呭喌涓嬶紝鎮ㄥ繀椤诲垎鍒幏寰楀姣忎釜鍒悕鐨勯攣瀹氥
mysql> LOCK TABLE t WRITE, t AS t1 WRITE;
mysql> INSERT INTO t SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> INSERT INTO t SELECT * FROM t AS t1;
濡傛灉鎮ㄧ殑鏌ヨ浣跨敤涓涓埆鍚嶅紩鐢ㄤ竴涓〃锛岄偅涔堟偍蹇呴』浣跨敤鍚屾牱鐨勫埆鍚嶉攣瀹氳琛ㄣ傚鏋滄病鏈夋寚瀹氬埆鍚嶏紝鍒欎笉浼氶攣瀹氳琛ㄣ
mysql> LOCK TABLE t READ;
mysql> SELECT * FROM t AS myalias;
ERROR 1100: Table 'myalias' was not locked with LOCK TABLES
鐩稿弽鐨勶紝濡傛灉鎮ㄤ娇鐢ㄤ竴涓埆鍚嶉攣瀹氫竴涓〃锛屾偍蹇呴』浣跨敤璇ュ埆鍚嶅湪鎮ㄧ殑鏌ヨ涓紩鐢ㄨ琛ㄣ
mysql> LOCK TABLE t AS myalias READ;
mysql> SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> SELECT * FROM t AS myalias;
WRITE閿佸畾閫氬父姣READ閿佸畾鎷ユ湁鏇撮珮鐨勪紭鍏堟潈锛屼互纭繚鏇存柊琚敖蹇湴澶勭悊銆傝繖鎰忓懗鐫锛屽鏋滀竴涓嚎绋嬭幏寰椾簡涓涓READ閿佸畾锛屽垯鍙︿竴涓嚎绋嬩細鐢宠涓涓WRITE閿佸畾锛屽悗缁殑READ閿佸畾鐢宠浼氱瓑寰咃紝鐩村埌WRITE绾跨▼鑾峰緱閿佸畾骞堕噴鏀鹃攣瀹氥傛偍鍙互浣跨敤LOW_PRIORITY WRITE閿佸畾鏉ュ厑璁稿叾瀹冪嚎绋嬪湪璇ョ嚎绋嬫鍦ㄧ瓑寰WRITE閿佸畾鏃惰幏寰READ閿佸畾銆傚彧鏈夊綋鎮ㄧ‘瀹氭渶缁堝皢鏈変竴涓椂鏈猴紝姝ゆ椂娌℃湁绾跨▼鎷ユ湁READ閿佸畾鏃讹紝鎮ㄦ墠搴旇浣跨敤LOW_PRIORITY WRITE閿佸畾銆
LOCK TABLES鎸夌収濡備笅鏂瑰紡鎵ц锛
1. 鎸夌収鍐呴儴瀹氫箟鐨勯『搴忥紝瀵规墍鏈夎琚攣瀹氱殑琛ㄨ繘琛屽垎绫汇備粠鐢ㄦ埛鐨勮搴︼紝姝ら『搴忔槸鏈粡瀹氫箟鐨勩
2. 濡傛灉浣跨敤涓涓鍙栧拰涓涓啓鍏ラ攣瀹氬涓涓〃杩涜閿佸畾锛屽垯鎶婂啓鍏ラ攣瀹氭斁鍦ㄨ鍙栭攣瀹氫箣鍓嶃
3. 涓娆¢攣瀹氫竴涓〃锛岀洿鍒扮嚎绋嬪緱鍒版墍鏈夐攣瀹氫负姝€
璇ヨ鍒欑‘淇濊〃閿佸畾涓嶄細鍑虹幇姝婚攣瀹氥備絾鏄紝瀵逛簬璇ヨ鍒欙紝鎮ㄩ渶瑕佹敞鎰忓叾瀹冪殑浜嬫儏锛
濡傛灉鎮ㄦ鍦ㄥ涓涓〃浣跨敤涓涓LOW_PRIORITY WRITE閿佸畾锛岃繖鍙剰鍛崇潃锛MySQL绛夊緟鐗瑰畾鐨勯攣瀹氾紝鐩村埌娌℃湁鐢宠READ閿佸畾鐨勭嚎绋嬫椂涓烘銆傚綋绾跨▼宸茬粡鑾峰緱WRITE閿佸畾锛屽苟姝e湪绛夊緟寰楀埌閿佸畾琛ㄦ竻鍗曚腑鐨勭敤浜庝笅涓涓〃鐨勯攣瀹氭椂锛屾墍鏈夊叾瀹冪嚎绋嬩細绛夊緟WRITE閿佸畾琚噴鏀俱傚鏋滆繖鎴愪负瀵逛簬搴旂敤绋嬪簭鐨勪弗閲嶇殑闂锛屽垯鎮ㄥ簲璇ヨ冭檻鎶婇儴鍒嗚〃杞寲涓轰簨鍔″畨鍏ㄥ瀷琛ㄣ
鎮ㄥ彲浠ュ畨鍏ㄥ湴浣跨敤KILL鏉ョ粨鏉熶竴涓鍦ㄧ瓑寰呰〃閿佸畾鐨勭嚎绋嬨傝鍙傝13.5.5.3鑺傦紝鈥淜ILL璇硶鈥銆
娉ㄦ剰锛屾偍涓嶈兘浣跨敤INSERT DELAYED閿佸畾浠讳綍鎮ㄦ鍦ㄤ娇鐢ㄧ殑琛紝鍥犱负锛屽湪杩欑鎯呭喌涓嬶紝INSERT鐢卞彟涓涓嚎绋嬫墽琛屻
閫氬父锛屾偍涓嶉渶瑕侀攣瀹氳〃锛屽洜涓烘墍鏈夌殑鍗曚釜UPDATE璇彞閮芥槸鍘熷瓙鎬х殑锛涙病鏈夊叾瀹冪殑绾跨▼鍙互骞叉壈浠讳綍鍏跺畠褰撳墠姝e湪鎵ц鐨SQL璇彞銆備絾鏄紝鍦ㄥ嚑绉嶆儏鍐典笅锛岄攣瀹氳〃浼氭湁濂藉锛
路 濡傛灉鎮ㄦ鍦ㄥ涓缁MyISAM琛ㄨ繍琛岃澶氭搷浣滐紝閿佸畾鎮ㄦ鍦ㄤ娇鐢ㄧ殑琛紝鍙互蹇緢澶氥傞攣瀹MyISAM琛ㄥ彲浠ュ姞蹇彃鍏ャ佹洿鏂版垨鍒犻櫎鐨勯熷害銆備笉鍒╂柟闈㈡槸锛屾病鏈夌嚎绋嬪彲浠ユ洿鏂颁竴涓敤READ閿佸畾鐨勮〃锛堝寘鎷繚鎸侀攣瀹氱殑琛級锛屼篃娌℃湁绾跨▼鍙互璁块棶鐢WRITE閿佸畾鐨勮〃锛堥櫎浜嗕繚鎸侀攣瀹氱殑琛ㄤ互澶栵級銆
鏈変簺MyISAM鎿嶄綔鍦LOCK TABLES涔嬩笅鏇村揩鐨勫師鍥犳槸锛MySQL涓嶄細娓呯┖鐢ㄤ簬宸查攣瀹氳〃鐨勫叧閿紦瀛橈紝鐩村埌UNLOCK TABLE琚皟鐢ㄤ负姝€傞氬父锛屽叧閿紦瀛樺湪姣忎釜SQL璇彞涔嬪悗琚竻绌恒
路 濡傛灉鎮ㄦ鍦ㄤ娇鐢MySQL涓殑涓涓笉鏀寔浜嬪姟鐨勫瓨鍌ㄥ紩鎿庯紝鍒欏鏋滄偍鎯宠纭畾鍦SELECT鍜UPDATE涔嬮棿娌℃湁鍏跺畠绾跨▼锛屾偍蹇呴』浣跨敤LOCK TABLES銆傛湰澶勬墍绀虹殑渚嬪瓙瑕佹眰LOCK TABLES锛屼互渚垮畨鍏ㄥ湴鎵ц锛
路 mysql> LOCK TABLES trans READ, customer WRITE;
路 mysql> SELECT SUM(value) FROM trans WHERE customer_id=some_id;
路 mysql> UPDATE customer
路 -> SET total_value=sum_from_previous_statement
路 -> WHERE customer_id=some_id;
路 mysql> UNLOCK TABLES;
濡傛灉娌℃湁LOCK TABLES锛屾湁鍙兘鍙︿竴涓嚎绋嬩細鍦ㄦ墽琛SELECT鍜UPDATE璇彞涔嬮棿鍦trans琛ㄤ腑鎻掑叆涓涓柊琛屻
閫氳繃浣跨敤鐩稿鏇存柊锛UPDATE customer SET value=value+new_value锛夋垨LAST_INSERT_ID()鍑芥暟锛屾偍鍙互鍦ㄨ澶氭儏鍐典笅閬垮厤浣跨敤LOCK TABLES銆傝鍙傝1.8.5.3鑺傦紝鈥滀簨鍔″拰鍘熷瓙鎿嶄綔鈥銆
閫氳繃浣跨敤鐢ㄦ埛灞傜骇鐨勯【闂紡閿佸畾鍑芥暟GET_LOCK()鍜RELEASE_LOCK()锛屾偍涔熷彲浠ュ湪鏈変簺鎯呭喌涓嬮伩鍏嶉攣瀹氳〃銆傝繖浜涢攣瀹氳淇濆瓨鍦ㄦ湇鍔″櫒涓殑涓涓贩缂栬〃涓紝浣跨敤pthread_mutex_lock() 鍜pthread_mutex_unlock()锛屼互鍔犲揩閫熷害銆傝鍙傝12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳銆
瑕佷簡瑙f洿澶氭湁鍏抽攣瀹氳鍒欑殑璇存槑锛岃鍙傝7.3.1鑺傦紝鈥滈攣瀹氭柟娉曗銆
鎮ㄥ彲浠ヤ娇鐢FLUSH TABLES WITH READ LOCK璇彞閿佸畾浣嶄簬鎵鏈夊甫鏈夎鍙栭攣瀹氱殑鏁版嵁搴撲腑鐨勬墍鏈夎〃銆傝鍙傝13.5.5.2鑺傦紝鈥淔LUSH璇硶鈥銆傚鏋滄偍鏈変竴涓彲浠ュ強鏃舵媿鎽勫揩鐓х殑鏂囦欢绯荤粺锛屾瘮濡Veritas锛岃繖鏄幏寰楀浠界殑涓涓潪甯告柟渚跨殑鏂瑰紡銆
娉ㄩ噴锛氬鏋滄偍瀵逛竴涓凡閿佸畾鐨勮〃浣跨敤ALTER TABLE锛岃琛ㄥ彲鑳戒細瑙i攣銆傝鍙傝A.7.1鑺傦紝鈥滀笌ALTER TABLE鏈夊叧鐨勯棶棰樷銆
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }
鏈鍙ョ敤浜庤缃簨鍔¢殧绂荤瓑绾э紝鐢ㄤ簬涓嬩竴涓簨鍔★紝鎴栬呯敤浜庡綋鍓嶄細璇濄
鍦ㄩ粯璁ゆ儏鍐典笅锛SET TRANSACTION浼氫负涓嬩竴涓簨鍔★紙杩樻湭寮濮嬶級璁剧疆闅旂绛夌骇銆傚鏋滄偍浣跨敤GLOBAL鍏抽敭璇嶏紝鍒欒鍙ヤ細璁剧疆鍏ㄥ眬鎬х殑榛樿浜嬪姟绛夌骇锛岀敤浜庝粠璇ョ偣浠ュ悗鍒涘缓鐨勬墍鏈夋柊杩炴帴銆傚師鏈夌殑杩炴帴涓嶅彈褰卞搷銆傝杩涜姝ゆ搷浣滐紝鎮ㄩ渶瑕SUPER鏉冮檺銆備娇鐢SESSION鍏抽敭娴嬪彲浠ヨ缃粯璁や簨鍔$瓑绾э紝鐢ㄤ簬瀵瑰綋鍓嶈繛鎺ユ墽琛岀殑鎵鏈夊皢鏉ヤ簨鍔°
瑕佷簡瑙e姣忎釜InnoDB浜嬪姟闅旂绛夌骇鐨勬弿杩帮紝璇峰弬瑙15.2.10.3鑺傦紝鈥淚nnoDB鍜孴RANSACTION ISOLATION LEVEL鈥銆InnoDB鏀寔MySQL 5.1涓殑鍚勪釜绛夌骇銆傞粯璁ょ殑绛夌骇鏄REPEATABLE READ銆
鎮ㄥ彲浠ヤ娇鐢--transaction-isolation閫夐」锛屽mysqld璁剧疆鍒濆鐨勯粯璁ゅ叏灞闅旂绛夌骇銆傝鍙傝5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」鈥銆
瀵逛簬InnoDB瀛樺偍寮曟搸锛屽彲浠ヨ幏寰楀XA浜嬪姟鐨勬敮鎸併MySQL XA鐨勬墽琛屼緷鎹X/Open CAE鏂囦欢Distributed Transaction Processing: The XA Specification銆傛湰鏂囦欢鐢Open Group鍑虹増锛屽彲浠ヤ粠http://www.opengroup.org/public/pubs/catalog/c193.htm鑾峰彇銆傚湪I.5鑺傦紝鈥滃XA浜嬪姟鐨勯檺鍒垛瀵瑰綋鍓XA鎵ц鐨勯檺鍒惰繘琛屼簡鎻忚堪銆
鍦ㄥ鎴风鏂归潰锛屾病鏈夌壒娈婅姹傘傝繛鎺MySQL鏈嶅姟鍣ㄧ殑XA鎺ュ彛鐢变互XA鍏抽敭璇嶅紑澶寸殑SQL璇彞缁勬垚銆MySQL瀹㈡埛绔繀椤昏兘鍙戦SQL璇彞锛屽苟鑳界悊瑙XA璇彞鎺ュ彛鐨勮涔夛紝浣嗘槸瀹冧滑涓嶉渶瑕佽閾炬帴鍒扮壒瀹氱殑MySQL瀹㈡埛搴撲笂銆
褰撳墠锛屽湪MySQL杩炴帴鍣ㄥ綋涓紝MySQL杩炴帴鍣/J 5.0.0鐩存帴鏀寔XA锛堜篃灏辨槸锛岄氳繃涓涓彲浠ユ帶鍒XA SQL璇彞鎺ュ彛鐨勭瓑绾ф帴鍙o級銆
XA鏀寔鍒嗗竷寮忕殑浜嬪姟锛屽叿澶囪兘鍔涳紝璁╁涓嫭绔嬬殑浜嬪姟璧勬簮鍙傚姞鍏ㄥ眬鐨勪簨鍔°備簨鍔¤祫婧愰氬父鏄RDBMSs锛屼笉杩囦篃鍙互鏄叾瀹冪绫荤殑璧勬簮銆
涓涓叏灞浜嬪姟浼氭秹鍙婂埌澶氫釜琛屽姩锛岃繖浜涜鍔ㄦ湰韬槸浜嬪姟鎬х殑銆備笉杩囷紝鎵鏈夎鍔ㄩ兘蹇呴』浣滀负涓涓兢缁勬垚鍔熷畬鎴愶紝鎴栬呬綔涓轰竴涓兢缁勮鍥炴粴銆傚疄闄呬笂锛岃繖浼氬欢浼ACID鎬ц川锛屸滄彁楂樼瓑绾р濓紝杩欐牱锛屽涓ACID浜嬪姟灏卞彲浠ヤ竴璧锋墽琛岋紝鐩稿綋浜庝篃鎷ユ湁ACID鎬ц川鐨勫叏灞鎿嶄綔鐨勭粍浠躲傦紙浣嗘槸锛屽浜庝竴涓垎甯冨紡浜嬪姟锛屾偍蹇呴』浣跨敤SERAILIZABLE闅旂绛夌骇锛屼互瀹炵幇ACID鎬ц川銆傚浜庝竴涓潪鍒嗗竷寮忎簨鍔★紝浣跨敤REPEATABLE READ灏辫冻澶熶簡銆備絾鏄浜庡垎甯冨紡浜嬪姟锛屼娇鐢REPEATABLE READ鏄笉澶熺殑銆傦級
鍒嗗竷寮忎簨鍔$殑閮ㄥ垎绀轰緥锛
路 搴旂敤绋嬪簭鐩稿綋浜庝竴涓妸娑堟伅浼犻掓湇鍔″拰RDBMS缁勫悎鍦ㄤ竴璧风殑鏁村悎宸ュ叿銆傚簲鐢ㄧ▼搴忓彲浠ョ‘淇濓紝鎵鏈夎繘琛屾秷鎭彂閫併佸洖鏀跺拰澶勭悊鐨勪簨鍔★紙鍚屾椂鍖呭惈涓涓簨鍔℃暟鎹簱锛夊潎鍦ㄤ竴涓叏灞浜嬪姟涓彂鐢熴傛偍鍙互鎶婅繖鐪嬩綔鏄滀簨鍔$數瀛愰偖浠躲傗
路 搴旂敤绋嬪簭鎵ц鐨勮鍔ㄤ細娑夊強鍒颁笉鍚屾暟鎹簱鏈嶅姟鍣紝姣斿MySQL鏈嶅姟鍣ㄥ拰Oracle鏈嶅姟鍣紙鎴栧涓MySQL鏈嶅姟鍣級銆傛秹鍙婂埌澶氫釜鏈嶅姟鍣ㄧ殑琛屽姩蹇呴』浣滀负涓涓叏灞浜嬪姟鐨勪竴閮ㄥ垎鍙戠敓锛岃屼笉鏄綔涓洪拡瀵规瘡涓湇鍔″櫒鐨勭嫭绔嬬殑鏈湴浜嬪姟鍙戠敓銆
路 閾惰鎶婂笎鎴蜂俊鎭繚瀛樺湪RDBMS涓紝骞堕氳繃鑷姩鍙栨鏈猴紙ATMs锛夊垎鍙戝拰鏀跺彇娆犳銆傚繀椤昏纭繚ATM琛屽姩琚纭湴鍙嶆槧鍒板笎鎴蜂腑锛屼絾鏄繖涓嶈兘鍙敱RDBMS鍗曠嫭瀹屾垚銆傚叏灞浜嬪姟绠$悊鍣ㄤ細鏁村悎ATM鍜屾暟鎹簱璧勬簮锛屼互纭繚璐㈠姟浜嬪姟鐨勬暣浣撲竴鑷存с
浣跨敤鍏ㄥ眬浜嬪姟鐨勫簲鐢ㄧ▼搴忔秹鍙婁竴涓垨澶氫釜璧勬簮绠$悊鍣ㄥ拰涓涓簨鍔$鐞嗗櫒锛
路 璧勬簮绠$悊鍣紙RM锛夌敤浜庢彁渚涢氬悜浜嬪姟璧勬簮鐨勯斿緞銆傛暟鎹簱鏈嶅姟鍣ㄦ槸涓绉嶈祫婧愮鐞嗗櫒銆傝绠$悊鍣ㄥ繀椤诲彲浠ユ彁浜ゆ垨 鍥炴粴鐢RM绠$悊鐨勪簨鍔°
路 浜嬪姟绠$悊鍣紙TM锛夌敤浜庡崗璋冧綔涓轰竴涓叏灞浜嬪姟涓閮ㄥ垎鐨勪簨鍔°TM涓庣鐞嗘瘡涓簨鍔$殑RMs杩涜閫氳銆備竴涓叏灞浜嬪姟涓悇涓崟涓簨鍔″潎鏄叏灞浜嬪姟鐨勨滃垎鏀濄傚叏灞浜嬪姟鍜屽悇鍒嗘敮閫氳繃涓绉嶅懡鍚嶆柟娉曡繘琛屾爣璇嗐傝繖绉嶅懡鍚嶆柟娉曞湪鍚庨潰杩涜璁茶堪銆
MySQL鎵цXA MySQL鏃讹紝MySQL鏈嶅姟鍣ㄧ浉褰撲簬涓涓敤浜庣鐞嗗叏灞浜嬪姟涓殑XA浜嬪姟鐨勮祫婧愮鐞嗗櫒銆備笌MySQL鏈嶅姟鍣ㄨ繛鎺ョ殑瀹㈡埛绔浉褰撲簬浜嬪姟绠$悊鍣ㄣ
瑕佹墽琛屼竴涓叏灞浜嬪姟锛屽繀椤荤煡閬撴秹鍙婂埌浜嗗摢浜涚粍浠讹紝骞朵笖鎶婃瘡涓粍浠跺紩鍒颁竴鐐癸紝鍦ㄦ鏃讹紝缁勪欢鍙互琚彁浜ゆ垨鍥炴粴鏃躲傛牴鎹瘡涓粍浠舵姤鍛婄殑鏈夊叧缁勪欢鏁堣兘鐨勫唴瀹癸紝杩欎簺缁勪欢蹇呴』浣滀负涓涓師瀛愭х兢缁勫叏閮ㄦ彁浜ゆ垨 鍥炴粴銆傚嵆锛岃涔堟墍鏈夌殑缁勪欢蹇呴』鎻愪氦锛岃涔堟墍鏈夌殑缁勪欢蹇呴』鍥炴粴銆傝绠$悊涓涓叏灞浜嬪姟锛屽繀椤昏鑰冭檻浠讳綍缁勪欢鎴栬繛鎺ョ綉缁滃彲鑳戒細鏁呴殰銆
鐢ㄤ簬鎵ц鍏ㄥ眬浜嬪姟鐨勮繃绋嬩娇鐢ㄤ袱闃舵鎻愪氦锛2PC锛夛紝鍙戠敓鏃堕棿鍦ㄧ敱鍏ㄥ眬浜嬪姟鐨勫垎鏀繘琛岀殑琛屽姩宸茬粡琚墽琛屼箣鍚庛
1. 鍦ㄧ涓闃舵锛屾墍鏈夌殑鍒嗘敮琚澶囧ソ銆傚嵆锛屽畠浠TM鍛婄煡瑕佸噯澶囨彁浜ゃ傞氬父锛岃繖鎰忓懗鐫鐢ㄤ簬绠$悊鍒嗘敮鐨勬瘡涓RM浼氳褰曞浜庤绋冲畾淇濆瓨鐨勫垎鏀殑琛屽姩銆傚垎鏀寚绀烘槸鍚﹀畠浠彲浠ヨ繖涔堝仛銆傝繖浜涚粨鏋滆鐢ㄤ簬绗簩闃舵銆
2. 鍦ㄧ浜岄樁娈碉紝TM鍛婄煡RMs鏄惁瑕佹彁浜ゆ垨 鍥炴粴銆傚鏋滃湪棰勫鍒嗘敮鏃讹紝鎵鏈夌殑鍒嗘敮鎸囩ず瀹冧滑灏嗚兘澶熸彁浜わ紝鍒欐墍鏈夌殑鍒嗘敮琚憡鐭ヨ鎻愪氦銆傚鏋滃湪棰勫鏃讹紝鏈変换浣曞垎鏀寚绀哄畠灏嗕笉鑳芥彁浜わ紝鍒欐墍鏈夊垎鏀鍛婄煡 鍥炴粴銆
鍦ㄦ湁浜涙儏鍐典笅锛屼竴涓叏灞浜嬪姟鍙兘浼氫娇鐢ㄤ竴闃舵鎻愪氦锛1PC锛夈備緥濡傦紝褰撲竴涓簨鍔$鐞嗗櫒鍙戠幇锛屼竴涓叏灞浜嬪姟鍙敱涓涓簨鍔¤祫婧愮粍鎴愶紙鍗筹紝鍗曚竴鍒嗘敮锛夛紝鍒欒璧勬簮鍙互琚憡鐭ュ悓鏃惰繘琛岄澶囧拰鎻愪氦銆
瑕佸湪MySQL涓墽琛XA浜嬪姟锛屽簲浣跨敤浠ヤ笅璇彞锛
XA {START|BEGIN} xid [JOIN|RESUME]
XA END xid [SUSPEND [FOR MIGRATE]]
XA PREPARE xid
XA COMMIT xid [ONE PHASE]
XA ROLLBACK xid
XA RECOVER
瀵逛簬XA START锛JOIN鍜RESUME瀛愬彞涓嶈鏀寔銆
瀵逛簬XA END锛SUSPEND [FOR MIGRATE]瀛愬彞涓嶈鏀寔銆
姣忎釜XA璇彞浠XA鍏抽敭璇嶄负寮澶达紝澶氭暟璇彞瑕佹眰涓涓xid鍊笺 xid鏄竴涓XA浜嬪姟鏍囪瘑绗︺傚畠鎸囩ず璇ヨ鍙ラ傜敤浜庡摢涓簨鍔°xid鍊肩敱瀹㈡埛绔彁渚涳紝鎴栫敱MySQL鏈嶅姟鍣ㄧ敓鎴愩xid鍊煎寘鍚竴鍒颁笁涓儴鍒嗭細
xid: gtrid [, bqual [, formatID ]]
gtrid鏄竴涓叏灞浜嬪姟鏍囪瘑绗︼紝bqual鏄竴涓垎鏀檺瀹氱锛formatID鏄竴涓暟瀛楋紝鐢ㄤ簬鏍囪瘑鐢gtrid鍜bqual鍊间娇鐢ㄧ殑鏍煎紡銆傛牴鎹娉曠殑琛ㄧず锛bqual鍜formatID鏄嚜閫夌殑銆傚鏋滄病鏈夌粰瀹氾紝榛樿鐨bqual鍊兼槸''銆傚鏋滄病鏈夌粰瀹氾紝榛樿鐨fromatID鍊兼槸1銆
gtrid鍜bqual蹇呴』涓哄瓧绗︿覆鏂囧瓧锛屾瘡涓殑闀垮害鏈澶氫负64瀛楄妭锛堜笉鏄瓧绗︼級銆gtrid鍜bqual鍙互鐢ㄥ绉嶆柟娉曟寚瀹氥傛偍鍙互浣跨敤甯﹀紩鍙风殑瀛楃涓('ab')锛屽崄鍏繘鍒跺瓧绗︿覆(0x6162, X'ab')锛屾垨浣嶅(b'nnnn')銆
formatID鏄竴涓棤绗﹀彿鐨勬暣鏁般
閫氳繃MySQL鏈嶅姟鍣ㄧ殑甯︿笅鍒掔嚎鐨XA鏀寔瀛愮▼搴忥紝gtrid鍜bqual鍊艰鐞嗚В涓轰互瀛楄妭涓哄崟浣嶃備絾鏄紝鍦ㄥ寘鍚XA璇彞鐨SQL璇彞姝e湪琚垎鏋愮殑鍚屾椂锛屾湇鍔″櫒浼氬幓鎿嶄綔涓浜涚壒瀹氱殑瀛楃闆嗐備负浜嗗畨鍏紝鎶gtrid鍜bqual浣滀负鍗佸叚杩涘埗瀛楃涓插啓鍏ャ
閫氬父锛xid鍊肩敱浜嬪姟绠$悊鍣ㄧ敓鎴愩傜敱涓涓TM鐢熸垚鐨勫煎繀椤讳笌鐢卞叾瀹TMs鐢熸垚鐨勫间笉鍚屻備竴涓粰瀹氱殑TM蹇呴』鑳借瘑鍒嚜宸辩殑xid鍊笺傝繖浜涘间綅浜庣敱XA RECOVER璇彞杩斿洖鐨勫兼竻鍗曚腑銆
XA START xid鐢ㄤ簬鍚姩涓涓甫缁欏畾xid鍊肩殑XA浜嬪姟銆傛瘡涓XA浜嬪姟蹇呴』鏈変竴涓敮涓鐨xid鍊硷紝鍥犳璇ュ煎綋鍓嶄笉鑳借鍏跺畠鐨XA浜嬪姟浣跨敤銆備娇鐢gtrid鍜bqual鍊艰瘎浼板敮涓鎬с傛墍鏈変笅鍒楃殑鐢ㄤ簬XA浜嬪姟鐨XA璇彞蹇呴』浣跨敤涓XA START璇彞涓粰瀹氱殑鐩稿悓鐨xid鍊艰繘琛屾寚瀹氥傚鏋滄偍浣跨敤杩欎簺璇彞锛屼絾鏄寚瀹氱殑xid鍊间笌閮ㄥ垎鍘熸湁鐨XA浜嬪姟涓嶅搴旂殑璇濓紝浼氬彂鐢熼敊璇
涓椤规垨澶氶」XA浜嬪姟鍙互鏄悓涓涓叏灞浜嬪姟鐨勪竴閮ㄥ垎銆傚湪涓涓粰瀹氱殑鍏ㄥ眬浜嬪姟涓殑鎵鏈XA浜嬪姟蹇呴』鍦xid鍊间腑浣跨敤鍚屾牱鐨gtrid鍊笺傚嚭浜庤繖涓師鍥狅紝gtrid鍊煎繀椤讳负鍏ㄥ眬鍞竴鐨勶紝杩欐牱锛屾湁鍏充竴涓粰瀹氱殑XA浜嬪姟鏄摢涓叏灞浜嬪姟鐨勪竴閮ㄥ垎鐨勯棶棰樺氨涓嶄細鍚硦涓嶆竻銆傚浜庝竴涓叏灞浜嬪姟涓殑姣忎釜XA浜嬪姟锛xid鍊间腑鐨bqual閮ㄥ垎蹇呴』涓嶄竴鏍枫傦紙bqual鍊煎簲涓嶄竴鏍凤紝杩欎釜瑕佹眰鏄綋鍓嶆墽琛MySQL XA鐨勪竴涓檺鍒舵潯浠躲傝繖涓嶆槸XA瑙勭害鐨勪竴閮ㄥ垎銆傦級
瀵逛簬鍦MySQL鏈嶅姟鍣ㄤ笂鐨勫浜PREPARED鐘舵佺殑XA浜嬪姟锛XA RECOVER璇彞浼氳繑鍥炰俊鎭傦紙瑙13.4.7.2鑺傦紝鈥淴A浜嬪姟鐘舵佲.銆傦級杈撳嚭鍖呮嫭涓涓锛岃琛岀敤浜庢湇鍔″櫒涓婄殑姣忎釜杩欑被XA浜嬪姟锛屼笉璁烘槸鍝釜瀹㈡埛绔惎鍔ㄤ簡瀹冦
XA RECOVER杈撳嚭琛岀湅涓婂幓鍍忚繖鏍凤紙渚嬪锛xid鍊煎寘鎷'abc', 'def'鍜 7绛夐儴鍒嗭級锛
mysql> XA RECOVER;
+----------+--------------+--------------+--------+
| formatID | gtrid_length | bqual_length | data |
+----------+--------------+--------------+--------+
| 7 | 3 | 3 | abcdef |
+----------+--------------+--------------+--------+
杈撳嚭鍒楁湁浠ヤ笅鎰忎箟锛
路 formatID鏄簨鍔xid鐨formatID閮ㄥ垎
路 gtrid_length鏄xid鐨gtrid閮ㄥ垎鐨勯暱搴︼紝浠ュ瓧鑺備负鍗曚綅
路 bqual_length鏄xid鐨bqual閮ㄥ垎鐨勯暱搴︼紝浠ュ瓧鑺備负鍗曚綅
路 data鏄xid鐨gtrid閮ㄥ垎鍜bqual閮ㄥ垎鐨勪覆鑱
1. 浣跨敤XA START鏉ュ惎鍔ㄤ竴涓XA浜嬪姟锛屽苟鎶婂畠鏀惧叆ACTIVE鐘舵併
2. 瀵逛簬涓涓ACTIVE XA浜嬪姟锛屽彂甯冩瀯鎴愪簨鍔$殑SQL璇彞锛岀劧鍚庡彂甯冧竴涓XA END璇彞銆XA END鎶婁簨鍔℃斁鍏IDLE鐘舵併
3. 瀵逛簬涓涓IDLE XA浜嬪姟锛屾偍鍙互鍙戝竷涓涓XA PREPARE璇彞鎴栦竴涓XA COMMIT鈥ONE PHASE璇彞锛
路 XA PREPARE鎶婁簨鍔℃斁鍏PREPARED鐘舵併傚湪姝ょ偣涓婄殑XA RECOVER璇彞灏嗗湪鍏惰緭鍑轰腑鍖呮嫭浜嬪姟鐨xid鍊硷紝鍥犱负XA RECOVER浼氬垪鍑哄浜PREPARED鐘舵佺殑鎵鏈XA浜嬪姟銆
路 XA COMMIT鈥ONE PHASE鐢ㄤ簬棰勫鍜屾彁浜や簨鍔°xid鍊煎皢涓嶄細琚XA RECOVER鍒楀嚭锛屽洜涓轰簨鍔$粓姝€
4. 瀵逛簬涓涓PREPARED XA浜嬪姟锛屾偍鍙互鍙戝竷涓涓XA COMMIT璇彞鏉ユ彁浜ゅ拰缁堟浜嬪姟锛屾垨鑰呭彂甯XA ROLLBACK鏉ュ洖婊氬苟缁堟浜嬪姟銆
涓嬮潰鏄竴涓畝鍗曠殑XA浜嬪姟锛岃浜嬪姟鎶婁竴琛屼綔涓轰竴涓叏灞浜嬪姟鐨勪竴閮ㄥ垎鎻掑叆涓涓〃涓
mysql> XA START 'xatest';
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO mytable (i) VALUES(10);
Query OK, 1 row affected (0.04 sec)
mysql> XA END 'xatest';
Query OK, 0 rows affected (0.00 sec)
mysql> XA PREPARE 'xatest';
Query OK, 0 rows affected (0.00 sec)
mysql> XA COMMIT 'xatest';
Query OK, 0 rows affected (0.00 sec)
鏍规嵁涓涓粰瀹氱殑瀹㈡埛绔繛鎺ョ殑璇锛XA浜嬪姟鍜屾湰鍦帮紙闈XA锛変簨鍔′簰鐩告帓鏂ャ備妇渚嬭鏄庯紝濡傛灉宸茬粡鍙戝竷浜XA START鏉ュ紑濮嬩竴椤XA浜嬪姟锛屽垯鏈湴浜嬪姟涓嶄細琚惎鍔紝鐩村埌XA浜嬪姟宸茬粡琚彁浜ゆ垨琚 鍥炴粴涓烘銆傜浉鍙嶇殑锛屽鏋滃凡缁忎娇鐢START TRANSACTION鍚姩涓涓湰鍦颁簨鍔★紝鍒XA璇彞涓嶈兘琚娇鐢紝鐩村埌璇ヤ簨鍔¤鎻愪氦鎴栬 鍥炴粴涓烘銆
CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']
[, user [IDENTIFIED BY [PASSWORD] 'password']] ...
CREATE USER鐢ㄤ簬鍒涘缓鏂扮殑MySQL璐︽埛銆傝浣跨敤CREATE USER锛屾偍蹇呴』鎷ユ湁mysql鏁版嵁搴撶殑鍏ㄥ眬CREATE USER鏉冮檺锛屾垨鎷ユ湁INSERT鏉冮檺銆傚浜庢瘡涓处鎴凤紝CREATE USER浼氬湪娌℃湁鏉冮檺鐨mysql.user琛ㄤ腑鍒涘缓涓涓柊璁板綍銆傚鏋 璐︽埛宸茬粡瀛樺湪锛屽垯鍑虹幇閿欒銆
浣跨敤鑷夌殑IDENTIFIED BY瀛愬彞锛屽彲浠ヤ负璐︽埛缁欏畾涓涓瘑鐮併user鍊煎拰 瀵嗙爜鐨勭粰瀹氭柟娉曞拰GRANT璇彞涓鏍枫傜壒鍒槸锛岃鍦ㄧ函鏂囨湰涓寚瀹氬瘑鐮侊紝闇蹇界暐PASSWORD鍏抽敭璇嶃傝鎶 瀵嗙爜鎸囧畾涓虹敱PASSWORD()鍑芥暟杩斿洖鐨勬贩缂栧硷紝闇鍖呭惈鍏抽敭瀛PASSWORD銆傝鍙傝13.5.1.3鑺傦紝鈥淕RANT鍜孯EVOKE璇硶鈥銆
DROP USER user [, user] ...
DROP USER璇彞鐢ㄤ簬鍒犻櫎涓涓垨澶氫釜MySQL璐︽埛銆傝浣跨敤DROP USER锛屾偍蹇呴』鎷ユ湁mysql鏁版嵁搴撶殑鍏ㄥ眬CREATE USER鏉冮檺鎴DELETE鏉冮檺銆備娇鐢ㄤ笌GRANT鎴REVOKE鐩稿悓鐨勬牸寮忎负姣忎釜 璐︽埛鍛藉悕锛涗緥濡傦紝'jeffrey'@'localhost'銆 璐︽埛鍚嶇О鐨勭敤鎴峰拰涓绘満閮ㄥ垎涓庣敤鎴疯〃璁板綍鐨User鍜Host鍒楀肩浉瀵瑰簲銆
浣跨敤DROP USER锛屾偍鍙互鍙栨秷涓涓处鎴峰拰鍏舵潈闄愶紝鎿嶄綔濡備笅锛
DROP USER user;
璇ヨ鍙ュ彲浠ュ垹闄ゆ潵鑷墍鏈夋巿鏉冭〃鐨勫笎鎴锋潈闄愯褰曘
瑕佺偣锛DROP USER涓嶈兘鑷姩鍏抽棴浠讳綍鎵撳紑鐨勭敤鎴峰璇濄傝屼笖锛屽鏋滅敤鎴锋湁鎵撳紑鐨勫璇濓紝姝ゆ椂鍙栨秷鐢ㄦ埛锛屽垯鍛戒护涓嶄細鐢熸晥锛岀洿鍒扮敤鎴峰璇濊鍏抽棴鍚庢墠鐢熸晥銆備竴鏃﹀璇濊鍏抽棴锛岀敤鎴蜂篃琚彇娑堬紝姝ょ敤鎴峰啀娆¤瘯鍥剧櫥褰曟椂灏嗕細澶辫触銆傝繖鏄湁鎰忚璁$殑銆
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] {tbl_name | * | *.* | db_name.*} TO user [IDENTIFIED BY [PASSWORD] 'password'] [, user [IDENTIFIED BY [PASSWORD] 'password']] ... [REQUIRE NONE | [{SSL| X509}] [CIPHER 'cipher' [AND]] [ISSUER 'issuer' [AND]] [SUBJECT 'subject']] [WITH with_option [with_option] ...] object_type = TABLE | FUNCTION | PROCEDURE with_option = GRANT OPTION | MAX_QUERIES_PER_HOUR count | MAX_UPDATES_PER_HOUR count | MAX_CONNECTIONS_PER_HOUR count | MAX_USER_CONNECTIONS count
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] {tbl_name | * | *.* | db_name.*} FROM user [, user] ... REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
GRANT鍜REVOKE璇彞鍏佽绯荤粺绠$悊鍛樺垱寤MySQL鐢ㄦ埛 璐︽埛锛屾巿浜堟潈闄愬拰鎾ら攢鏉冮檺銆
MySQL璐︽埛淇℃伅瀛樺偍鍦mysql鏁版嵁搴撶殑琛ㄤ腑銆傚湪绗5绔狅細鏁版嵁搴撶鐞涓鏈暟鎹簱鍜岃闂帶鍒剁郴缁熻繘琛屼簡璇﹀敖鐨勮璁恒傝浜嗚В鏇村璇︾粏淇℃伅锛屾偍搴旇鏌ヨ姝ょ珷銆
濡傛灉鎺堟潈琛ㄦ嫢鏈夊惈鏈mixed-case鏁版嵁搴撴垨琛ㄥ悕绉扮殑鏉冮檺璁板綍锛屽苟涓lower_case_table_names绯荤粺鍙橀噺宸茶缃紝鍒欎笉鑳戒娇鐢REVOKE鎾ら攢鏉冮檺锛屽繀椤荤洿鎺ユ搷绾垫巿鏉冭〃銆傦紙褰lower_case_table_names宸茶缃椂锛GRANT灏嗕笉浼氬垱寤烘绫昏褰曪紝浣嗘槸姝ょ被璁板綍鍙兘宸茬粡鍦ㄨ缃彉閲忎箣鍓嶈鍒涘缓浜嗐傦級
鎺堜簣鐨勬潈闄愬彲浠ュ垎涓哄涓眰绾э細
路 鍏ㄥ眬灞傜骇
鍏ㄥ眬鏉冮檺閫傜敤浜庝竴涓粰瀹氭湇鍔″櫒涓殑鎵鏈夋暟鎹簱銆傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.user琛ㄤ腑銆GRANT ALL ON *.*鍜REVOKE ALL ON *.*鍙巿浜堝拰鎾ら攢鍏ㄥ眬鏉冮檺銆
路 鏁版嵁搴撳眰绾
鏁版嵁搴撴潈闄愰傜敤浜庝竴涓粰瀹氭暟鎹簱涓殑鎵鏈夌洰鏍囥傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.db鍜mysql.host琛ㄤ腑銆GRANT ALL ON db_name.*鍜REVOKE ALL ON db_name.*鍙巿浜堝拰鎾ら攢鏁版嵁搴撴潈闄愩
路 琛ㄥ眰绾
琛ㄦ潈闄愰傜敤浜庝竴涓粰瀹氳〃涓殑鎵鏈夊垪銆傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.talbes_priv琛ㄤ腑銆GRANT ALL ON db_name.tbl_name鍜REVOKE ALL ON db_name.tbl_name鍙巿浜堝拰鎾ら攢琛ㄦ潈闄愩
路 鍒楀眰绾
鍒楁潈闄愰傜敤浜庝竴涓粰瀹氳〃涓殑鍗曚竴鍒椼傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.columns_priv琛ㄤ腑銆傚綋浣跨敤REVOKE鏃讹紝鎮ㄥ繀椤绘寚瀹氫笌琚巿鏉冨垪鐩稿悓鐨勫垪銆
路 瀛愮▼搴忓眰绾
CREATE ROUTINE, ALTER ROUTINE, EXECUTE鍜GRANT鏉冮檺閫傜敤浜庡凡瀛樺偍鐨勫瓙绋嬪簭銆傝繖浜涙潈闄愬彲浠ヨ鎺堜簣涓哄叏灞灞傜骇鍜屾暟鎹簱灞傜骇銆傝屼笖锛岄櫎浜CREATE ROUTINE澶栵紝杩欎簺鏉冮檺鍙互琚巿浜堜负瀛愮▼搴忓眰绾э紝骞跺瓨鍌ㄥ湪mysql.procs_priv琛ㄤ腑銆
褰撳悗缁洰鏍囨槸涓涓〃銆佷竴涓凡瀛樺偍鐨勫嚱鏁版垨涓涓凡瀛樺偍鐨勮繃绋嬫椂锛object_type瀛愬彞搴旇鎸囧畾涓TABLE銆FUNCTION鎴PROCEDURE銆傚綋浠庢棫鐗堟湰鐨MySQL鍗囩骇鏃讹紝瑕佷娇鐢ㄦ湰瀛愬彞锛屾偍蹇呴』鍗囩骇鎮ㄧ殑鎺堟潈琛ㄣ傝鍙傝2.10.2鑺傦紝鈥滃崌绾ф巿鏉冭〃鈥銆
瑕佷娇鐢GRANT鎴REVOKE锛屾偍蹇呴』鎷ユ湁GRANT OPTION鏉冮檺锛屽苟涓旀偍蹇呴』鐢ㄤ簬鎮ㄦ鍦ㄦ巿浜堟垨鎾ら攢鐨勬潈闄愩
瑕佹挙閿鎵鏈夋潈闄愶紝闇浣跨敤浠ヤ笅璇硶銆傛璇硶鐢ㄤ簬鍙栨秷瀵逛簬宸插懡鍚嶇殑鐢ㄦ埛鐨勬墍鏈夊叏灞灞傜骇銆佹暟鎹簱灞傜骇銆佽〃灞傜骇鍜屽垪灞傜骇鐨勬潈闄愩
REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
瑕佷娇鐢ㄦ湰REVOKE璇硶锛屾偍蹇呴』鎷ユ湁mysql鏁版嵁搴撶殑鍏ㄥ眬CREATE USER鏉冮檺鎴UPDATE鏉冮檺銆
瀵逛簬GRANT鍜REVOKE璇彞锛priv_type鍙互琚寚瀹氫负浠ヤ笅浠讳綍涓绉嶏細
鏉冮檺 |
鎰忎箟 |
ALL [PRIVILEGES] |
璁剧疆闄GRANT OPTION涔嬪鐨勬墍鏈夌畝鍗曟潈闄 |
ALTER |
鍏佽浣跨敤ALTER TABLE |
ALTER ROUTINE |
鏇存敼鎴栧彇娑堝凡瀛樺偍鐨勫瓙绋嬪簭 |
CREATE |
鍏佽浣跨敤CREATE TABLE |
CREATE ROUTINE |
鍒涘缓宸插瓨鍌ㄧ殑瀛愮▼搴 |
CREATE TEMPORARY TABLES |
鍏佽浣跨敤CREATE TEMPORARY TABLE |
CREATE USER |
鍏佽浣跨敤CREATE USER, DROP USER, RENAME USER鍜REVOKE ALL PRIVILEGES銆 |
CREATE VIEW |
鍏佽浣跨敤CREATE VIEW |
DELETE |
鍏佽浣跨敤DELETE |
DROP |
鍏佽浣跨敤DROP TABLE |
EXECUTE |
鍏佽鐢ㄦ埛杩愯宸插瓨鍌ㄧ殑瀛愮▼搴 |
FILE |
鍏佽浣跨敤SELECT...INTO OUTFILE鍜LOAD DATA INFILE |
INDEX |
鍏佽浣跨敤CREATE INDEX鍜DROP INDEX |
INSERT |
鍏佽浣跨敤INSERT |
LOCK TABLES |
鍏佽瀵规偍鎷ユ湁SELECT鏉冮檺鐨勮〃浣跨敤LOCK TABLES |
PROCESS |
鍏佽浣跨敤SHOW FULL PROCESSLIST |
REFERENCES |
鏈瀹炴柦 |
RELOAD |
鍏佽浣跨敤FLUSH |
REPLICATION CLIENT |
鍏佽鐢ㄦ埛璇㈤棶浠庡睘鏈嶅姟鍣ㄦ垨涓绘湇鍔″櫒鐨勫湴鍧 |
REPLICATION SLAVE |
鐢ㄤ簬澶嶅埗鍨嬩粠灞炴湇鍔″櫒锛堜粠涓绘湇鍔″櫒涓鍙栦簩杩涘埗鏃ュ織浜嬩欢锛 |
SELECT |
鍏佽浣跨敤SELECT |
SHOW DATABASES |
SHOW DATABASES鏄剧ず鎵鏈夋暟鎹簱 |
SHOW VIEW |
鍏佽浣跨敤SHOW CREATE VIEW |
SHUTDOWN |
鍏佽浣跨敤mysqladmin shutdown |
SUPER |
鍏佽浣跨敤CHANGE MASTER, KILL, PURGE MASTER LOGS鍜SET GLOBAL璇彞锛mysqladmin debug鍛戒护锛涘厑璁告偍杩炴帴锛堜竴娆★級锛屽嵆浣垮凡杈惧埌max_connections銆 |
UPDATE |
鍏佽浣跨敤UPDATE |
USAGE |
鈥滄棤鏉冮檺鈥濈殑鍚屼箟璇 |
GRANT OPTION |
鍏佽鎺堜簣鏉冮檺 |
褰撲粠鏃х増鏈殑MySQL鍗囩骇鏃讹紝瑕佷娇鐢EXECUTE, CREATE VIEW, SHOW VIEW, CREATE USER, CREATE ROUTINE鍜ALTER ROUTINE鏉冮檺锛屾偍蹇呴』棣栧厛鍗囩骇鎮ㄧ殑鎺堟潈琛ㄣ傝鍙傝2.10.2鑺傦紝鈥滃崌绾ф巿鏉冭〃鈥銆
REFERENCES鏉冮檺鐩墠鏈浣跨敤銆
褰撴偍鎯宠鍒涘缓涓涓病鏈夋潈闄愮殑鐢ㄦ埛鏃讹紝鍙互鎸囧畾USAGE銆
浣跨敤SHOW GRANTS鏉ョ‘瀹氬笎鎴锋嫢鏈変粈涔堟潈闄愩傝鍙傝13.5.4.10鑺傦紝鈥淪HOW GRANTS璇硶鈥銆
鎮ㄥ彲浠ラ氳繃浣跨敤ON *.*璇硶璧嬩簣鍏ㄥ眬鏉冮檺锛屾垨閫氳繃浣跨敤ON db_name.*璇硶璧嬩簣鏁版嵁搴撳眰绾ф潈闄愩傚鏋滄偍鎸囧畾浜ON *骞朵笖鎮ㄥ凡缁忛夋嫨浜嗕竴涓粯璁ゆ暟鎹簱锛屽垯鏉冮檺琚祴浜堝埌杩欎釜鏁版嵁搴撲腑銆傦紙璀﹀憡锛氬鏋滄偍鎸囧畾浜ON *鍚屾椂鎮ㄦ病鏈夐夋嫨涓涓粯璁ゆ暟鎹簱锛屽垯鏉冮檺鏄叏灞鐨勩傦級
FILE, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SHOW DATABASES, SHUTDOWN鍜SUPER鏉冮檺鏄鐞嗘ф潈闄愶紝鍙兘杩涜鍏ㄥ眬鎺堟潈锛堜娇鐢ON *.*璇硶锛夈
鍏跺畠鏉冮檺鍙互琚叏灞鎺堟潈锛屾垨琚祴浜堜负鍏跺畠灞傜骇銆
瀵逛簬涓涓〃锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEX鍜ALTER銆
瀵逛簬涓涓垪锛堜篃灏辨槸锛屽綋鎮ㄤ娇鐢ㄤ竴涓column_list瀛愬彞鏃讹級锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸SELECT, INSERT鍜UPDATE銆
鍦ㄥ瓙绋嬪簭灞傜骇锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸ALTER ROUTINE, EXECUTE鍜GRANT OPTION銆CREATE ROUTINE涓嶆槸涓涓瓙绋嬪簭灞傜骇鐨勬潈闄愶紝鍥犱负鎮ㄥ繀椤绘嫢鏈夋鏉冮檺锛屾墠鑳藉垱寤轰竴涓瓙绋嬪簭銆
瀵逛簬鍏ㄥ眬銆佹暟鎹簱銆佽〃鍜屽瓙绋嬪簭灞傜骇锛GRANT ALL鍙兘璧嬩簣鍦ㄦ偍姝e湪鎺堟潈鐨勫眰绾т腑瀛樺湪鐨勬潈闄愩備緥濡傦紝濡傛灉鎮ㄤ娇鐢GRANT ALL ON db_name.*锛岃繖鏄竴涓暟鎹簱灞傜骇璇彞锛屽洜姝や笉浼氭巿浜堝叏灞鏉冮檺锛屽FILE绛夈
MySQL鍏佽鎮ㄥ涓嶅瓨鍦ㄧ殑鏁版嵁搴撶洰鏍囨巿浜堟潈闄愩傚湪姝ゆ儏鍐典笅锛屽皢琚巿浜堢殑鏉冮檺蹇呴』鍖呮嫭CREATE鏉冮檺銆傝繖涓ц川鏄湁鎰忚璁$殑锛岀洰鐨勬槸鍏佽鏁版嵁搴撶鐞嗗憳涓哄皢鍦ㄦ鍚庤鍒涘缓鐨勬暟鎹簱鐩爣棰勫鐢ㄦ埛 璐︽埛鍜屾潈闄愩
瑕佺偣锛氬綋鎮ㄥ彇娑堜竴涓〃鎴栨暟鎹簱鏃讹紝MySQL涓嶄細鑷姩鎾ら攢浠讳綍鏉冮檺銆備絾鏄紝濡傛灉鎮ㄥ彇娑堜竴涓瓙绋嬪簭锛屽垯琚祴浜堣瀛愮▼搴忕殑鎵鏈夊瓙绋嬪簭灞傜骇鐨勬潈闄愰兘琚挙閿銆
娉ㄦ剰锛GRANT璇彞鐢ㄤ簬鍦ㄥ叏灞灞傜骇鎴栨暟鎹簱灞傜骇璧嬩簣鏉冮檺銆傚綋鍦GRANT璇彞涓寚瀹氭暟鎹簱鍚嶇О鏃讹紝鍏佽浣跨敤鈥_鈥欏拰鈥%鈥欓氶厤绗︺傝繖鎰忓懗鐫锛屽鏋滄偍鎯宠浣跨敤鈥_鈥欏瓧绗︿綔涓轰竴涓暟鎹簱鍚嶇О鐨勪竴閮ㄥ垎锛屾偍搴旇鍦GRANT璇彞涓寚瀹氬畠涓衡\_鈥欙紝浠ラ槻姝㈢敤鎴峰彲浠ヨ闂叾瀹冪鍚堟閫氶厤绗︽牸寮忕殑鏁版嵁搴擄紱渚嬪锛GRANT ... ON `foo\_bar`.* TO ...銆
涓轰簡鎺ョ撼瀵规潵鑷换鎰忎富鏈虹殑鐢ㄦ埛鎺堟潈鐨勬潈鍒╋紝MySQL鏀寔浠user_name@host_name鐨勫舰寮忔寚瀹user鍊笺傚鏋滀竴涓user_name鎴host_name涓庝竴涓笉鍔犲紩鍙风殑鏍囪瘑绗︿竴鏍锋槸鍚堟硶鐨勶紝閭d箞鎮ㄤ笉闇瑕佸瀹冨姞寮曞彿銆備笉杩囷紝瑕佹寚瀹氫竴涓寘鍚壒娈婂瓧绗︼紙濡傗-鈥欙級鐨user_name瀛楃涓诧紝鎴栦竴涓寘鍚壒娈婂瓧绗︽垨閫氶厤瀛楃锛堝鈥%鈥欙級锛屽垯寮曞彿鏄繀瑕佺殑锛涗緥濡傦紝'test-user'@'test-hostname'銆傚垎鍒username鍜hostname鍔犲紩鍙枫
鎮ㄥ彲浠ュ湪hostname涓寚瀹氶氶厤绗︺備緥濡user_name@'%.loc.gov'閫傜敤浜庡湪loc.gov鍩熶腑鐨勪换浣曚富鏈虹殑user_name銆傚悓鏃user_name@'144.155.166.%'閫傜敤浜144.155.166 C绾у瓙缃戜腑鐨勪换浣曚富鏈虹殑user_name銆
绠鍗曞舰寮user_name鏄user_name@'%'鐨勫悓涔夎瘝銆
MySQL涓嶆敮鎸usernames涓殑閫氶厤绗︺傞氳繃鎶婂甫鏈User=''鐨勭櫥褰曢」鎻掑叆鍒mysql.user琛ㄤ腑锛屾垨閫氳繃浣跨敤GRANT璇彞鍒涘缓涓涓甫鏈夌┖鍚嶇О鐨勭敤鎴凤紝鍙互瀹氫箟鍖垮悕鐢ㄦ埛锛
mysql> GRANT ALL ON test.* TO ''@'localhost' ...
褰撴妸甯﹀紩鍙风殑鍊兼槸锛岄渶浣跨敤鍙嶅嬀鍙(鈥`鈥)涓烘暟鎹簱銆佽〃銆佸垪鍜屽瓙绋嬪簭鍚嶇О鍔犲紩鍙枫備娇鐢ㄥ崟寮曞彿(鈥'鈥)涓hostnames銆usernames鍜 瀵嗙爜鍔犲紩鍙枫
璀﹀憡锛氬鏋滄偍鍏佽鍖垮悕鐢ㄦ埛杩炴帴鍒MySQL鏈嶅姟鍣紝鍒欐偍搴旇鍚屾椂鍚戞墍鏈夋湰鍦扮敤鎴锋巿浜user_name@localhost鏉冮檺銆傚惁鍒欙紝褰撴湁鍚嶇О鐨勭敤鎴疯瘯鍥句粠鏈湴鏈哄櫒鐧诲綍MySQL鏈嶅姟鍣ㄦ椂锛mysql.user琛ㄤ腑鐨勭敤浜localhost鐨勫尶鍚嶇敤鎴峰笎鎴蜂細琚娇鐢ㄣ
鎮ㄥ彲浠ラ氳繃鎵ц浠ヤ笅鏌ヨ鏉ョ‘瀹氭槸鍚﹁繖閫傚悎浜庢偍銆備互涓嬫煡璇㈠垪涓句簡鎵鏈夊尶鍚嶇敤鎴凤細
mysql> SELECT Host, User FROM mysql.user WHERE User='';
濡傛灉鎮ㄦ兂瑕佸垹闄ゆ湰鍦板尶鍚嶇敤鎴疯处鎴凤紝浠ラ伩鍏嶅嚭鐜板垰鎵嶈皥鍒扮殑闂锛屽垯闇浣跨敤浠ヤ笅璇彞锛
mysql> DELETE FROM mysql.user WHERE Host='localhost' AND User='';
mysql> FLUSH PRIVILEGES;
GRANT鏀寔鏈闀夸负60涓瓧绗︾殑hostnames銆傛暟鎹簱銆佽〃銆佸垪鍜屽瓙绋嬪簭鍚嶇О鏈闀垮彲涓64涓瓧绗︺Usernames鏈闀垮彲涓16涓瓧绗︺ 娉ㄩ噴锛氫笉鑳介氳繃鏇存敼mysql.user琛ㄦ潵鏀瑰彉usernames鐨勫厑璁搁暱搴︺傚鏋滆瘯鍥捐繖涔堝仛锛屼細瀵艰嚧鍑虹幇涓嶅彲棰勮鐨勯棶棰橈紝鍙兘浼氶犳垚鐢ㄦ埛鏃犳硶鐧诲綍MySQL鏈嶅姟鍣ㄣ傞櫎浜嗛噰鐢ㄧ敱MySQL鍏徃鎻愪緵鐨勭敤浜庡崌绾MySQL鏈嶅姟鍣ㄧ殑mysql_fix_privilege_tables鍘熺ǹ涔嬪锛璇锋偍涓嶈浠ヤ换浣曟柟寮忓彉鏇存巿鏉冭〃銆
瀵逛簬琛ㄦ垨鍒楃殑鏉冮檺鏄綔涓哄悇涓潈闄愬眰绾х殑閫昏緫OR鏉冮檺琚檮鍔犲舰鎴愮殑銆備緥濡傦紝濡傛灉mysql.user琛ㄦ寚瀹氫竴涓敤鎴锋嫢鏈夊叏灞SELECT鏉冮檺锛屽垯璇ユ潈闄愪笉鑳借鏁版嵁搴撱佽〃鎴栧垪灞傜骇鐨勭櫥褰曢」瀹氫箟銆
鍙互鎸変笅鍒楁柟娉曡绠楀垪鏉冮檺锛
global privileges
OR (database privileges AND host privileges)
OR table privileges
OR column privileges
鍦ㄥ鏁版儏鍐典笅锛屾偍鍙湪涓涓潈闄愬眰绾т笅鍚戠敤鎴锋巿浜堟潈鍒╋紝鎵浠ュ鍛介氬父涓嶆槸閭d箞澶嶆潅銆傛湁鍏虫潈闄愭鏌ヨ绋嬬殑缁嗚妭锛岃鍙傝5.7鑺傦紝鈥淢ySQL璁块棶鏉冮檺绯荤粺鈥銆
濡傛灉鎮ㄥ涓涓湪mysql.user琛ㄤ腑涓嶅瓨鍦ㄧ殑username/hostname缁勫悎鎺堜簣鏉冮檺锛屽垯澧炲姞涓涓櫥褰曢」骞朵繚鎸佸湪姝ゅ锛岀洿鍒颁娇鐢DELETE璇彞鍒犻櫎涓烘銆傛崲鍙ヨ瘽璇达紝GRANT鍙互鍒涘缓鐢ㄦ埛琛ㄧ櫥褰曢」锛屼絾鏄REVOKE涓嶄細鍙栨秷瀹冧滑锛涙偍蹇呴』浣跨敤DROP USER鎴DELETE鏄庣‘鍦版搷浣溿
濡傛灉鍒涘缓浜嗕竴涓柊鐨勭敤鎴凤紝鎴栬呭鏋滄偍鎷ユ湁鍏ㄥ眬鎺堟潈鏉冮檺锛屽垯鐢ㄦ埛瀵嗙爜琚缃负鐢IDENTIFIED BY瀛愬彞鎸囧畾鐨勫瘑鐮侊紙濡傛灉缁欏畾浜嗕竴涓級銆傚鏋滅敤鎴峰凡鎷ユ湁浜嗕竴涓瘑鐮侊紝鍒欐瀵嗙爜琚柊瀵嗙爜鏇夸唬銆
璀﹀憡锛氬鏋滄偍鍒涘缓浜嗕竴涓柊鐢ㄦ埛锛屼絾鏄笉鎸囧畾IDENTIFIED BY瀛愬彞锛屽垯鐢ㄦ埛娌℃湁 瀵嗙爜銆傝繖鏄緢涓嶅畨鍏ㄧ殑銆備笉杩囷紝鎮ㄥ彲浠ュ惎鐢NO_AUTO_CREATE_USER SQL妯″紡锛屾潵闃叉GRANT鍒涘缓涓涓柊鐢ㄦ埛锛堝惁鍒GRANT浼氳繖涔堝仛锛夛紝闄ら潪缁欏畾浜IDENTIFIED BY鏉ヤ负鏂扮敤鎴锋彁渚涗竴涓瘑鐮併
浣跨敤SET PASSWORD璇彞涔熷彲浠ヨ缃瘑鐮併傝鍙傝13.5.1.5鑺傦紝鈥淪ET PASSWORD璇硶鈥銆
鍦IDENTIFIED BY瀛愬彞涓紝瀵嗙爜搴旇浣滀负鏂囧瓧瀵嗙爜鍙缁欏畾銆傛病鏈夊繀瑕佷娇鐢PASSWORD()鍑芥暟锛屽洜涓鸿鍑芥暟鐢ㄤ簬SET PASSWORD璇彞銆備緥濡傦細
GRANT ... IDENTIFIED BY 'mypass';
濡傛灉鎮ㄤ笉鎯充互鏄庣櫧鐨勬枃瀛楀彂閫佸瘑鐮侊紝骞朵笖鎮ㄧ煡閬PASSWORD()杩斿洖缁欏瘑鐮佺殑娣风紪鍊硷紝鍒欐偍鍙互鎸囧畾娣风紪鍊硷紝鍓嶉潰鍔犲叆鍏抽敭璇PASSWORD锛
GRANT ...
IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4';
鍦ㄤ竴涓C绋嬪簭涓紝鎮ㄥ彲浠ラ氳繃浣跨敤make_scrambled_password() C API鍑芥暟寰楀埌娣风紪鍊笺
濡傛灉鎮ㄤ负涓涓暟鎹簱鎺堜簣鏉冮檺锛屽垯鍦mysql.db琛ㄤ腑锛屼細鏍规嵁闇瑕佸垱寤虹櫥褰曢」銆傚鏋滀娇鐢REVOKE鍒犻櫎浜嗘墍鏈夌殑鏁版嵁搴撴潈闄愶紝鍒欐湰鐧诲綍椤硅鍒犻櫎銆
濡傛灉涓涓敤鎴蜂笉鎷ユ湁琛ㄦ潈闄愶紝鍒欏綋鐢ㄦ埛鐢宠琛ㄦ竻鍗曟椂锛堜緥濡傦紝浣跨敤SHOW TABLES璇彞锛夛紝琛ㄥ悕绉颁笉鏄剧ず銆
SHOW DATABASES鏉冮檺鍏佽璐︽埛閫氳繃鍙戝竷SHOW DATABASE璇彞鏉ヨ鐪嬫暟鎹悕绉般備笉鎷ユ湁姝ゆ潈闄愮殑璐︽埛鍙兘鐪嬪埌浠栦滑鎷ユ湁閮ㄥ垎鏉冮檺鐨勬暟鎹簱锛屽苟涓斿鏋滀娇鐢--skip-show-database閫夐」鍚姩鏈嶅姟鍣紝鍒欐牴鏈笉鑳戒娇鐢ㄦ湰璇彞銆
WITH GRANT OPTION瀛愬彞缁欎簣鐢ㄦ埛鑳藉姏锛屽彲浠ュ湪鎸囧畾鐨勬潈闄愬眰绾э紝鍚戝叾瀹冪敤鎴风粰瀹氬叾鎷ユ湁鐨勪换浣曟潈闄愩傛偍搴旇鐣欏績鎮ㄧ粰浜堜簡璋GRANT OPTION鏉冮檺锛屽洜涓烘嫢鏈変笉鍚屾潈闄愮殑涓や釜鐢ㄦ埛鍙互鑱斿悎浣跨敤鏉冮檺锛
鎮ㄤ笉鑳藉悜鍏跺畠鐢ㄦ埛鎺堜簣鎮ㄨ嚜宸辨病鏈夌殑鏉冮檺锛GRANT OPTION鏉冮檺鍙厑璁告偍璧嬩簣鎮ㄨ嚜宸辨嫢鏈夌殑鏉冮檺銆
瑕佹敞鎰忥紝褰撴偍鍦ㄦ煇涓壒瀹氭潈闄愬眰绾у悜涓涓敤鎴锋巿浜GRANT OPTION鏉冮檺鏃讹紝鐢ㄦ埛鎷ユ湁鐨勮灞傜骇鐨勪换浣曟潈闄愶紙鎴栨湭鏉ュ皢琚粰瀹氱殑鏉冮檺锛変篃鍙互鐢辫鐢ㄦ埛鎺堜簣銆傚亣璁炬偍鍚戜竴涓敤鎴疯祴浜堜簡鏁版嵁搴INSERT鏉冮檺銆傚鏋滄偍鐒跺悗璧嬩簣鏁版嵁搴SELECT鏉冮檺锛屽苟鎸囧畾浜WITH GRANT OPTION锛屽垯璇ョ敤鎴蜂笉浠呭彲浠ュ悜鍏跺畠鐢ㄦ埛缁欎簣SELECT鏉冮檺锛岃繕鍙互缁欎簣INSERT銆傚鏋滄偍鐒跺悗鍚戠敤鎴锋巿浜堟暟鎹簱UPDATE鏉冮檺锛屽垯鐢ㄦ埛鍙互鎺堜簣INSERT, SELECT鍜UPDATE銆
鎮ㄤ笉搴旇鍚戜竴涓父瑙勭敤鎴锋巿浜ALTER鏉冮檺銆傚鏋滄偍杩欎箞鍋氾紝鍒欒鐢ㄦ埛鍙互灏濊瘯閫氳繃瀵硅〃閲嶆柊鍛藉悕鏉ョ牬鍧忔巿鏉冪郴缁燂紒
The MAX_QUERIES_PER_HOUR count, MAX_UPDATES_PER_HOUR count, and MAX_CONNECTIONS_PER_HOUR count options limit the number of queries, updates, and logins a user can perform during any given one-hour period. If count is 0 (the default), this means that there is no limitation for that user. MAX_QUERIES_PER_HOUR count, MAX_UPDATES_PER_HOUR count鍜MAX_CONNECTIONS_PER_HOUR count閫夐」闄愬埗浜嗗湪浠讳綍缁欏畾鐨勪竴灏忔椂鏈熼棿锛岀敤鎴峰彲浠ユ墽琛岀殑鏌ヨ銆佹洿鏂板拰鐧诲綍鐨勬暟鐩傚鏋count鏄0锛堥粯璁ゅ硷級锛岃繖鎰忓懗鐫锛屽璇ョ敤鎴锋病鏈夐檺鍒躲
MAX_USER_CONNECTIONS count閫夐」闄愬埗浜嗚处鎴峰彲浠ュ悓鏃惰繘琛岀殑杩炴帴鐨勬渶澶ф暟鐩傚鏋count鏄0锛堥粯璁ゅ硷級锛屽垯max_user_connections绯荤粺鍙互鍐冲畾璇 璐︽埛鍚屾椂杩炴帴鐨勬暟鐩
娉ㄩ噴锛氳瀵逛竴涓師鏈夌殑鐢ㄦ埛鎸囧畾浠讳綍杩欑被璧勬簮闄愬埗鍨嬮夐」锛屽悓鏃跺張涓嶅奖鍝嶅師鏈夌殑鏉冮檺锛岄渶浣跨敤GRANT USAGE ON *.* ... WITH MAX_...銆
闄や簡鏍规嵁username鍜屽瘑鐮佽繘琛屽父瑙勯壌瀹氬锛MySQL杩樺彲浠ユ鏌X509璇佹槑灞炴с傝涓MySQL璐︽埛鎸囧畾涓SSL鏈夊叧鐨勯夐」锛岄渶浣跨敤GRANT璇彞鐨REQUIRE瀛愬彞銆傦紙瑕佷簡瑙f湁鍏冲湪MySQL涓娇鐢SSL鐨勮儗鏅俊鎭紝璇峰弬瑙5.8.7鑺傦紝鈥滀娇鐢ㄥ畨鍏ㄨ繛鎺モ銆傦級
瀵逛簬涓涓粰瀹氱殑璐︽埛锛屾湁澶氱鍙兘鎬у彲浠ラ檺鍒惰繛鎺ョ被鍨嬶細
路 濡傛灉璐︽埛娌℃湁SSL鎴X509瑕佹眰锛屽苟涓斿鏋username鍜 瀵嗙爜鏄湁鏁堢殑锛屽垯鍏佽涓嶅姞瀵嗚繛鎺ャ備絾鏄紝濡傛灉瀹㈡埛绔湁姝g‘鐨勮瘉鏄庡拰鍏抽敭鏂囦欢锛屽垯鏍规嵁瀹㈡埛绔殑閫夋嫨锛屼篃鍙互浣跨敤鍔犲瘑杩炴帴銆
路 REQUIRE SSL閫夐」鐢ㄤ簬鍛婄煡鏈嶅姟鍣紝瀵逛簬璇ヨ处鎴峰彧鍏佽SSL鍔犲瘑杩炴帴銆傛敞鎰忥紝濡傛灉鏈夊厑璁镐换浣曢潪SSL杩炴帴鐨勮闂帶鍒惰褰曪紝鍒欐湰閫夐」鍙互琚拷鐣ャ
路 mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
路 -> IDENTIFIED BY 'goodsecret' REQUIRE SSL;
路 REQUIRE X509鎰忓懗鐫瀹㈡埛绔繀椤绘嫢鏈変竴涓湁鏁堣瘉鏄庯紝闄ら潪涓嶉渶瑕佺‘鍒囩殑璇佹槑銆佸彂甯冭呭拰涓婚銆傚敮涓鐨勮姹傛槸锛屽簲鍙互浣跨敤CA璇佹槑鍏朵腑涔嬩竴鏉ラ獙璇佺鍚嶃
路 mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
路 -> IDENTIFIED BY 'goodsecret' REQUIRE X509;
路 REQUIRE ISSUER 'issuer'鐢ㄤ簬瀵硅繛鎺ュ皾璇曡繘琛岄檺瀹氾紝瀹㈡埛绔繀椤诲嚭绀轰竴涓敱CA鈥issuer鈥欏彂甯冪殑鏈夋晥鐨X509璇佹槑銆傚鏋滃鎴风鍑虹ず鐨勮瘉鏄庢槸鏈夋晥鐨勶紝浣嗘槸鏈変竴涓笉鍚岀殑鍙戝竷鑰咃紝鍒欐湇鍔″櫒浼氭嫆缁濊繛鎺ャ備娇鐢X509璇佹槑灏辨剰鍛崇潃瑕佸姞瀵嗭紝鎵浠ュ湪杩欑鎯呭喌涓嬶紝SSL閫夐」鏄笉蹇呰鐨勩
路 mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
路 -> IDENTIFIED BY 'goodsecret'
路 -> REQUIRE ISSUER '/C=FI/ST=Some-State/L=Helsinki/
路 O=MySQL Finland AB/CN=Tonu Samuel/Email=tonu@example.com';
娉ㄦ剰锛ISSUER鍊煎簲琚綔涓轰竴涓崟涓瀛楃涓茶緭鍏ャ
路 REQUIRE SUBJECT 'subject'鐢ㄤ簬瀵硅繛鎺ュ皾璇曡繘琛岄檺瀹氾紝瀹㈡埛绔繀椤诲嚭绀轰竴涓寘鍚富棰subject鐨勬湁鏁堢殑X509璇佹槑銆傚鏋滃鎴风鍑虹ず鐨勮瘉鏄庢槸鏈夋晥鐨勶紝浣嗘槸鏈変竴涓笉鍚岀殑涓婚锛屽垯鏈嶅姟鍣ㄤ細鎷掔粷杩炴帴銆
路 mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
路 -> IDENTIFIED BY 'goodsecret'
路 -> REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/
路 O=MySQL demo client certificate/
路 CN=Tonu Samuel/Email=tonu@example.com';
娉ㄦ剰锛SUBJECT鍊煎簲琚綔涓轰竴涓崟涓瀛楃涓茶緭鍏ャ
路 闇瑕REQUIRE CIPHER 'cipher'鏉ョ‘璁や娇鐢ㄤ簡瀵嗙爜鍜岃冻澶熼暱搴︾殑鍏抽敭瀛椼傚鏋滀娇鐢ㄤ簡閲囩敤鐭瀷鍔犲瘑鍏抽敭瀛楃殑鏃х畻娉曪紝SSL鏈韩浼氭瘮杈冭剢寮便備娇鐢ㄦ湰閫夐」锛屾偍鍙互瑕佹眰浣跨敤鐗瑰畾鐨勫瘑鐮佹柟娉曟潵璁稿彲涓涓繛鎺ャ
路 mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
路 -> IDENTIFIED BY 'goodsecret'
路 -> REQUIRE CIPHER 'EDH-RSA-DES-CBC3-SHA';
SUBJECT, ISSUER鍜CIPHER閫夐」鍙互鍦REQUIRE瀛愬彞涓粨鍚堬紝濡備笅锛
mysql> GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
-> IDENTIFIED BY 'goodsecret'
-> REQUIRE SUBJECT '/C=EE/ST=Some-State/L=Tallinn/
O=MySQL demo client certificate/
CN=Tonu Samuel/Email=tonu@example.com'
-> AND ISSUER '/C=FI/ST=Some-State/L=Helsinki/
O=MySQL Finland AB/CN=Tonu Samuel/Email=tonu@example.com'
-> AND CIPHER 'EDH-RSA-DES-CBC3-SHA';
娉ㄦ剰锛SUBJECT鍜ISSUER鍊煎悇鑷簲琚綔涓轰竴涓崟涓瀛楃涓茶緭鍏ャ
鍦REQUIRE鍚勯夐」涔嬮棿锛AND鍏抽敭璇嶆槸鑷夌殑銆
閫夐」鐨勯『搴忔棤鎵璋擄紝浣嗘槸閫夐」涓嶈兘琚寚瀹氫袱娆°
褰mysqld鍚姩鍚庯紝鎵鏈夌殑鏉冮檺琚鍏ュ瓨鍌ㄥ櫒涓傝浜嗚В璇︾粏璇存槑锛岃鍙傝5.7.7鑺傦紝鈥滄潈闄愭洿鏀逛綍鏃剁敓鏁堚銆
娉ㄦ剰锛屽鏋滄偍姝e湪浣跨敤琛ㄦ潈闄愭垨鍒楁潈闄愶紝鍗充娇鍙涓涓敤鎴蜂娇鐢紝鏈嶅姟鍣ㄤ篃浼氬鎵鏈夌敤鎴锋鏌ヨ〃鏉冮檺鍜屽垪鏉冮檺锛岃繖浼氱暐寰檷浣MySQL鐨勯熷害銆備笌姝ょ被浼硷紝濡傛灉鎮ㄥ鏌愪簺鐢ㄦ埛闄愬埗鏌ヨ銆佹洿鏂版垨杩炴帴鐨勬暟鐩紝鍒欐湇鍔″櫒蹇呴』鐩戞祴杩欎簺鍊笺
鏍囧噯SQL鐗堟湰鍜MySQL鐗堟湰鐨GRANT涔嬮棿鐨勬渶澶у尯鍒槸锛
路 鍦MySQL涓紝鏉冮檺涓hostname鍜username鐨勭粍鍚堟湁鍏筹紝涓 鍗曚竴鐨username鏃犲叧銆
路 鏍囧噯SQL涓嶆嫢鏈夊叏灞灞傜骇鎴栨暟鎹簱灞傜骇鏉冮檺锛屼篃涓嶆敮鎸MySQL鏀寔鐨勬墍鏈夋潈闄愮被鍨嬨
路 MySQL涓嶆敮鎸佹爣鍑SQL TRIGGER鎴UNDER鏉冮檺銆
路 鏍囧噯SQL鏉冮檺浠ヤ竴绉嶅垎绛夌骇鐨勬柟寮忚繘琛岀粍缁囥傚鏋滄偍鍙栨秷涓涓敤鎴凤紝鍒欑敤鎴疯鎺堜簣鐨勬墍鏈夋潈闄愰兘琚挙閿銆傚湪MySQL涓紝濡傛灉鎮ㄤ娇鐢DROP USER锛屼篃浼氬姝ゃ傝鍙傝13.5.1.2鑺傦紝鈥淒ROP USER璇硶鈥銆
路 鍦ㄦ爣鍑SQL涓紝褰撴偍鍙栨秷涓涓〃鏃讹紝瀵逛竴涓〃鐨勬墍鏈夋潈闄愪細琚挙閿銆傚湪鏍囧噯SQL涓紝褰撴偍鎾ら攢涓涓潈闄愭椂锛屾牴鎹鏉冮檺琚巿浜堢殑鎵鏈夋潈闄愪篃浼氳鎾ら攢銆傚湪MySQL涓紝鍙湁浣跨敤鏄庣‘鐨REVOKE璇彞锛屾垨閫氳繃鎿嶄綔瀛樺偍鍦MySQL鎺堟潈琛ㄤ腑鐨勫硷紝鎵嶈兘鍙栨秷鏉冮檺銆
路 鍦MySQL涓紝鍙互鍙涓涓〃涓殑閮ㄥ垎鍒楁嫢鏈INSERT鏉冮檺銆傚湪姝ゆ儏鍐典笅锛屽鏋滄偍蹇界暐鎮ㄤ笉鎷ユ湁INSERT鏉冮檺鐨勯偅浜涘垪锛岋紝鎮ㄤ粛鐒跺彲浠ュ琛ㄦ墽琛INSERT璇彞銆傚鏋滄病鏈夊惎鐢ㄤ弗鏍肩殑SQL妯″紡锛屽垯琚拷鐣ョ殑鍒楄璁剧疆涓哄悇鑷殣鍚殑榛樿鍊笺傚湪涓ユ牸妯″紡涓嬶紝濡傛灉鏌愪釜琚拷鐣ョ殑鍒楁病鏈夐粯璁ゅ硷紝鍒欒璇彞琚嫆缁濄5.3.2鑺傦紝鈥淪QL鏈嶅姟鍣ㄦā寮忊瀵逛弗鏍兼ā寮忚繘琛屼簡璁ㄨ銆13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥瀵归殣鍚粯璁ゅ艰繘琛屼簡璁ㄨ銆
鎮ㄤ笉鎷ユ湁INSERT鏉冮檺鐨勫垪琚缃负鍚勮嚜鐨勯粯璁ゅ笺傛爣鍑SQL瑕佹眰鎮ㄦ嫢鏈夋墍鏈夊垪鐨INSERT鏉冮檺銆
鍦MySQL涓紝濡傛灉鎮ㄥ彧鎷ユ湁涓涓〃涓殑閮ㄥ垎鍒楃殑INSERT鏉冮檺锛屽悓鏃讹紝濡傛灉鎮ㄤ粠INSERT璇彞涓拷鐣ユ偍涓嶆嫢鏈夋潈闄愮殑鍒楋紝鍒欐偍浠嶇劧鍙互瀵硅〃鎵цINSERT璇彞锛涢偅浜涘垪灏嗚璁剧疆涓哄悇鑷殑榛樿鍊笺傚湪涓ユ牸妯″紡涓嬶紙鍗冲綋sql_mode='traditional'鏃讹紝濡傛灉鏌愪簺琚拷鐣ョ殑鍒楁病鏈夐粯璁ゅ硷紝鍒INSERT璇彞灏嗚鎷掔粷銆
SET PASSWORD = PASSWORD('some password')
SET PASSWORD FOR user = PASSWORD('some password')
SET PASSWORD璇彞鐢ㄤ簬鍚戜竴涓師鏈MySQL鐢ㄦ埛 璐︽埛璧嬩簣涓涓瘑鐮併
绗竴涓娉曚负褰撳墠鐢ㄦ埛璁剧疆瀵嗙爜銆傚凡浣跨敤涓涓潪鍖垮悕璐︽埛杩炴帴鍒版湇鍔″櫒涓婄殑浠讳綍瀹㈡埛鍗抽兘鍙互鏇存敼璇ヨ处鎴风殑瀵嗙爜銆
绗簩涓娉曚负褰撳墠鏈嶅姟鍣ㄤ富鏈轰笂鐨勪竴涓壒瀹氳处鎴疯缃瘑鐮併傚彧鏈夋嫢鏈mysql鏁版嵁搴UPDATE鏉冮檺鐨勫鎴风鍙互杩欎箞鍋氥user鍊煎簲浠user_name@host_name鐨勬牸寮忚缁欏畾锛屾澶user_name鍜host_name涓mysql.user琛ㄧ櫥褰曢」鐨User鍜Host鍒椾腑鍒楀嚭鐨勫畬鍏ㄤ竴鏍枫備妇渚嬭鏄庯紝濡傛灉鎮ㄦ湁涓涓櫥褰曢」锛User鍜Host鍒楀间负'bob'鍜'%.loc.gov'锛屾偍搴旇鎸夊涓嬫柟娉曞啓璇彞锛
mysql> SET PASSWORD FOR 'bob'@'%.loc.gov' = PASSWORD('newpass');
杩欑浉褰撲簬浠ヤ笅璇彞锛
mysql> UPDATE mysql.user SET Password=PASSWORD('newpass')
-> WHERE User='bob' AND Host='%.loc.gov';
mysql> FLUSH PRIVILEGES;
娉ㄩ噴锛氬鏋滄偍姝e湪浣跨敤涓涓4.1浠ュ墠鐨勫鎴风杩炴帴鍒颁竴涓MySQL 4.1鎴MySQL 4.1浠ュ悗鐨勬湇鍔″櫒锛屽垯鍦ㄩ槄璇5.7.9鑺傦紝鈥淢ySQL 4.1涓殑瀵嗙爜鍝堝笇澶勭悊鈥涔嬪墠锛屼笉鑳戒娇鐢ㄥ墠闈㈢殑SET PASSWORD鎴UPDATE璇彞銆 瀵嗙爜鏍煎紡鍦MySQL 4.1涓彉鏇翠簡锛屽苟涓斿湪鐗瑰畾鎯呭喌涓嬶紝濡傛灉鎮ㄦ洿鏀瑰瘑鐮侊紝鎮ㄥ彲鑳芥棤娉曞湪杩炴帴鍒版湇鍔″櫒涓娿
鎮ㄥ彲浠ラ氳繃鎵цSELECT CURRENT_USER()瑙傜湅鎮ㄥ綋鍓嶇殑閴村畾user@host鐧诲綍椤广
ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...
鏈鍙ョ敤浜庡垎鏋愬拰瀛樺偍琛ㄧ殑鍏抽敭瀛楀垎甯冦傚湪鍒嗘瀽鏈熼棿锛屼娇鐢ㄤ竴涓鍙栭攣瀹氬琛ㄨ繘琛岄攣瀹氥傝繖瀵逛簬MyISAM, BDB鍜InnoDB琛ㄦ湁浣滅敤銆傚浜MyISAM琛紝鏈鍙ヤ笌浣跨敤myisamchk -a鐩稿綋銆
MySQL浣跨敤宸插瓨鍌ㄧ殑鍏抽敭瀛楀垎甯冩潵鍐冲畾锛屽綋鎮ㄥ闄ゅ父鏁颁互澶栫殑瀵硅薄鎵ц鑱斿悎鏃讹紝琛ㄦ寜浠涔堥『搴忚繘琛岃仈鍚堛
鏈鍙ヤ細杩斿洖涓涓惈鏈変互涓嬪垪鐨勮〃锛
鍒 |
鍊 |
Table |
琛ㄥ悕绉 |
Op |
杩涜鍒嗘瀽 |
Msg_type |
鐘舵併侀敊璇佷俊鎭垨璀﹀憡涔嬩竴 |
Msg_text |
娑堟伅 |
鎮ㄥ彲浠ヤ娇鐢SHOW INDEX璇彞妫鏌ュ凡瀛樺偍鐨勫叧閿瓧鍒嗗竷銆傝鍙傝13.5.4.11鑺傦紝鈥淪HOW INDEX璇硶鈥銆
濡傛灉浠庝笂涓涓ANALYZE TABLE璇彞寮濮嬶紝琛ㄦ病鏈夊彉鍖栵紝鍒欎笉鍐嶅垎鏋愯琛ㄣ
ANALYZE TABLE璇彞琚啓鍏ヤ簩杩涘埗鏃ュ織涓紝闄ら潪浣跨敤浜嗚嚜閫夌殑NO_WRITE_TO_BINLOG鍏抽敭璇嶏紙鎴栧叾鍒悕LOCAL锛夈
BACKUP TABLE tbl_name [, tbl_name] ... TO '/path/to/backup/directory'
娉ㄩ噴锛氭湰璇彞涓嶇悊鎯炽傛垜浠鍦ㄥ姫鍔涘鎵句竴绉嶆洿濂界殑鏇夸唬鏂瑰紡锛岃鏂瑰紡灏嗘彁渚涘湪绾垮浠借兘鍔涖傚悓鏃讹紝涔熷彲浠ヤ娇鐢mysqlhotcopy鍘熸湰鏇夸唬銆
BACKUP TABLE鐢ㄤ簬鍦ㄥ埛鏂颁簡鎵鏈夊纾佺洏鐨勭紦鍐插彉鏇村悗锛屾妸鎭㈠琛ㄦ墍闇鐨勬渶灏戞暟鐩殑琛ㄦ枃浠舵嫹璐濆埌澶囦唤鐩綍涓傛湰璇彞鍙MyISAM琛ㄨ捣浣滅敤銆傚畠鍙互鎷疯礉.frm瀹氫箟鏂囦欢鍜.MYD鏁版嵁鏂囦欢銆.MYI绱㈠紩鏂囦欢鍙互浠庤繖涓や釜鏂囦欢涓噸寤恒傛湰鐩綍搴旇鎸囧畾涓轰竴涓畬鏁寸殑璺緞鍚嶃
鍦ㄤ娇鐢ㄦ湰璇彞鍓嶏紝璇峰弬瑙5.9.1鑺傦紝鈥滄暟鎹簱澶囦唤鈥銆
鍦ㄥ浠芥湡闂达紝涓烘瘡涓〃淇濇寔涓涓鍙栭攣瀹氾紝姣忔涓涓紝鍦ㄦ鍦ㄥ浠芥椂閿佸畾銆傚鏋滄偍鎯宠鎶婂涓〃浣滀负涓涓揩鐓ф潵澶囦唤锛堥槻姝㈠畠浠湪澶囦唤鎿嶄綔杩囩▼涓鏇存敼锛夛紝鎮ㄥ繀椤诲疄鐜板彂甯冧竴涓LOCK TABLES璇彞锛屼互鑾峰緱瀵逛竴涓粍缇や腑鐨勬瘡涓〃鐨勮鍙栭攣瀹氥
璇ヨ鍙ヤ細杩斿洖涓涓惈鏈変互涓嬪垪鐨勮〃锛
鍒 |
鍊 |
Table |
琛ㄥ悕绉 |
Op |
杩涜澶囦唤 |
Msg_type |
鐘舵併侀敊璇佷俊鎭垨璀﹀憡涔嬩竴 |
Msg_text |
娑堟伅 |
CHECK TABLE tbl_name [, tbl_name] ... [option] ...
option = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}
妫鏌ヤ竴涓垨澶氫釜琛ㄦ槸鍚︽湁閿欒銆CHECK TABLE瀵MyISAM鍜InnoDB琛ㄦ湁浣滅敤銆傚浜MyISAM琛紝鍏抽敭瀛楃粺璁℃暟鎹鏇存柊銆
CHECK TABLE涔熷彲浠ユ鏌ヨ鍥炬槸鍚︽湁閿欒锛屾瘮濡傚湪瑙嗗浘瀹氫箟涓寮曠敤鐨勮〃宸蹭笉瀛樺湪銆
CHECK TABLE璇彞浼氳繑鍥炰竴涓惈鏈変互涓嬪垪鐨勮〃锛
鍒 |
鍊 |
Table |
琛ㄥ悕绉 |
Op |
杩涜妫鏌 |
Msg_type |
鐘舵併侀敊璇佷俊鎭垨閿欒涔嬩竴 |
Msg_text |
娑堟伅 |
娉ㄦ剰锛岃璇彞鍙兘浼氫负姣忎釜琚鏌ョ殑琛ㄤ骇鐢熷琛屼俊鎭傛渶鍚庝竴琛屾湁涓涓Msg_type鐘舵佸笺Msg_text閫氬父搴斾负OK銆傚鏋滄偍娌℃湁寰楀埌OK锛屾垨琛ㄥ凡缁忔洿鏂颁簡锛屽垯鎮ㄩ氬父搴旇杩愯淇鍚庣殑琛ㄣ傝鍙傝5.9.4鑺傦紝鈥滆〃缁存姢鍜屽穿婧冩仮澶嶁銆傝〃宸茬粡鏇存柊浜嗭紝杩欐剰鍛崇潃琛ㄧ殑瀛樺偍寮曟搸鎸囩ず娌℃湁蹇呰妫鏌ヨ〃銆
鍙互缁欎簣鐨勪笉鍚岀殑妫鏌ラ夐」鍒椾簬涓嬭〃涓傝繖浜涢夐」鍙傜敤浜庢鏌MyISAM琛ㄣ傚浜InnoDB琛ㄥ拰瑙嗗浘锛岃繖浜涢夐」琚拷鐣ャ
绫诲瀷 |
鎰忎箟 |
QUICK |
涓嶆壂鎻忚锛屼笉妫鏌ラ敊璇殑閾炬帴銆 |
FAST |
鍙鏌ユ病鏈夎姝g‘鍏抽棴鐨勮〃銆 |
CHANGED |
鍙鏌ヤ笂娆℃鏌ュ悗琚洿鏀圭殑琛紝鍜屾病鏈夎姝g‘鍏抽棴鐨勮〃銆 |
MEDIUM |
鎵弿琛岋紝浠ラ獙璇佽鍒犻櫎鐨勯摼鎺ユ槸鏈夋晥鐨勩備篃鍙互璁$畻鍚勮鐨勫叧閿瓧鏍¢獙鍜岋紝骞朵娇鐢ㄨ绠楀嚭鐨勬牎楠屽拰楠岃瘉杩欎竴鐐广 |
EXTENDED |
瀵规瘡琛岀殑鎵鏈夊叧閿瓧杩涜涓涓叏闈㈢殑鍏抽敭瀛楁煡鎵俱傝繖鍙互纭繚琛ㄦ槸100锛呬竴鑷寸殑锛屼絾鏄姳鐨勬椂闂磋緝闀裤 |
濡傛灉娌℃湁鎸囧畾QUICK, MEDIUM鎴EXTENDED閫夐」锛屽垯瀵逛簬鍔ㄦ佹牸寮MyISAM琛紝榛樿妫鏌ョ被鍨嬫槸MEDIUM銆傝繖涓庡琛ㄨ繍琛myisamchk --medium-check tbl_name鐨勭粨鏋滅浉鍚屻傚浜庨潤鎬佹牸寮MyISAM琛紝榛樿妫鏌ョ被鍨嬩篃鏄MEDIUM锛岄櫎闈CHANGED鎴FAST宸茶鎸囧畾銆傚湪姝ゆ儏鍐典笅锛岄粯璁ゅ间负QUICK銆傚浜CHANGED鍜FAST锛岃鎵弿琚烦杩囷紝鍥犱负琛屾瀬灏戣鐮村潖銆
鎮ㄥ彲浠ョ粍鍚堟鏌ラ夐」锛屽涓嬮潰鐨勪緥瀛愭墍绀恒傝渚嬪瓙瀵硅〃杩涜浜嗕竴涓揩閫熸鏌ワ紝鏉ユ煡鐪嬭琛ㄦ槸鍚﹁姝g‘鍏抽棴锛
CHECK TABLE test_table FAST QUICK;
娉ㄩ噴锛氬湪鏈変簺鎯呭喌涓嬶紝CHECK TABLE浼氭洿鏀硅〃銆傚鏋滆〃琚爣璁颁负鈥corrupted鈥濇垨鈥not closed properly鈥濓紝鍒欏嚭鐜拌繖绉嶆儏鍐点備絾鏄CHECK TABLE涓嶄細鎵惧嚭琛ㄤ腑鐨勯棶棰樸傚湪杩欑鎯呭喌涓嬶紝CHECK TABLE浼氭妸琛ㄦ爣璁颁负鑹ソ銆
濡傛灉涓涓〃琚牬鍧忥紝寰堟湁鍙兘闂鍦ㄧ储寮曚腑锛岃屼笉鍦ㄦ暟鎹儴鍒嗕腑銆傛墍鏈夊墠杩扮殑妫鏌ョ被鍨嬮兘鍙互褰诲簳鍦版鏌ョ储寮曪紝鍥犳锛屽彲浠ユ壘鍑哄鏁扮殑閿欒銆
濡傛灉鎮ㄥ彧鎯宠妫鏌ユ偍鍋囧畾鐨勮〃鏄壇濂界殑锛屾偍搴旇涓嶄娇鐢ㄦ鏌ラ夐」鎴QUICK閫夐」銆傚綋鎮ㄦ椂闂村寙蹇欐椂锛屽簲浣跨敤QUICK銆QUICK鏃犳硶鎵惧嚭鏁版嵁鏂囦欢涓殑閿欒鐨勯闄╅潪甯稿皬銆傦紙鍦ㄥ鏁版儏鍐典笅锛屽湪姝e父浣跨敤涓紝MySQL搴旇兘鍦ㄦ暟鎹枃浠朵腑鎵惧嚭閿欒銆傚鏋滄壘鍑轰簡閿欒锛岃〃琚爣璁颁负鈥corrupted鈥濓紝骞朵笉鑳借浣跨敤锛岀洿鍒颁慨澶嶄负姝€傦級
濡傛灉鎮ㄦ兂瑕佹椂甯告鏌ヨ〃锛FAST鍜CHANGED澶氭暟鎯呭喌涓嬩粠鍘熸湰涓浣跨敤锛堜緥濡傦紝浠cron涓鎵ц锛夈傚湪澶氭暟鎯呭喌涓嬶紝FAST浼樺厛浜CHANGED銆傦紙鍙湁涓绉嶆儏鍐FAST涓嶄紭鍏堜簬CHANGED锛岄偅灏辨槸褰撴偍鎬鐤戞偍鍦MyISAM浠g爜涓彂鐜颁簡閿欒銆傦級
褰MySQL璇曞浘閫氳繃鍏抽敭瀛楁洿鏂颁竴琛屾垨鏌ユ壘涓琛屾椂锛屽鏋滄偍宸茬粡杩愯浜嗕竴涓父瑙勬鏌ュ悗浣嗕粛寰楀埌鏉ヨ嚜琛ㄧ殑濂囨殑閿欒锛屾鏃朵娇鐢EXTENDED銆傦紙濡傛灉甯歌鐨勬鏌ヨ繍琛屾垚鍔燂紝鍒欏熀鏈敤涓嶇潃EXTENDED銆傦級
琚CHECK TABLE鎶ュ憡鐨勯儴鍒嗛棶棰樹笉浼氳鑷姩淇锛
路 鍙戠幇琛屻傛琛屼腑锛auto_increment鍒楁湁0鍊笺
杩欐剰鍛崇潃锛屾偍鍦ㄨ〃涓湁涓琛岋紝璇ヨ鐨AUTO_INCREMENT绱㈠紩鍒楀寘鍚0鍊笺傦紙鍙互閫氳繃浣跨敤UPDATE璇彞锛屾槑纭湴鎶婂垪璁剧疆涓0锛屼互鍒涘缓涓涓AUTO_INCREMENT鍒椾负0鐨勮銆傦級
杩欐湰韬笉鏄竴涓敊璇紝浣嗘槸濡傛灉鎮ㄥ喅瀹氳浆鍌ㄨ〃骞舵仮澶嶈〃锛屾垨瀵硅〃杩涜ALTER TABLE锛岄偅涔堜細瀵艰嚧鍑虹幇楹荤儲銆傚湪姝ゆ儏鍐典笅锛AUTO_INCREMENT鍒椾細鏍规嵁AUTO_INCREMENT鍒楃殑缁撴灉鏇存敼鍊硷紝杩欎細瀵艰嚧鍑虹幇闂锛屽閲嶅鍏抽敭瀛楅敊璇瓑銆
瑕佹秷闄よ鍛婏紝鍙渶鎵ц涓涓UPDATE璇彞锛屾妸鍒楄缃负闄0浠ュ鐨勫笺
CHECKSUM TABLE tbl_name [, tbl_name] ... [ QUICK | EXTENDED ]
鎶ュ憡涓涓〃鏍¢獙鍜屻
濡傛灉鎸囧畾浜QUICK锛屽垯鎶ュ憡娲绘ц〃鏍¢獙鍜岋紝鍚﹀垯鎶ュ憡NULL銆傝繖鏄潪甯稿揩鐨勩傛椿鎬ц〃閫氳繃鎸囧畾CHECKSUM锛1琛ㄩ夐」鍚敤锛岀洰鍓嶅彧鏀寔鐢ㄤ簬MyISAM琛ㄣ傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥銆
鍦EXTENDED妯″紡涓嬶紝鏁翠釜琛ㄨ涓琛屼竴琛屽湴璇诲彇锛屽苟璁$畻鏍¢獙鍜屻傚浜庡ぇ鍨嬭〃锛岃繖鏄潪甯告參鐨勩
榛樿鎯呭喌涓嬶紝濡傛灉鏃㈡病鏈夋寚瀹QUICK锛屼篃娌℃湁鎸囧畾EXTENDED锛屽苟涓斿鏋滆〃瀛樺偍寮曟搸鏀寔锛屽垯MySQL杩斿洖涓涓椿鎬ф牎楠屽拰锛屽惁鍒欎細瀵硅〃杩涜鎵弿銆
CHECKSUM TABLE瀵逛簬涓嶅瓨鍦ㄧ殑琛ㄤ細杩斿洖NULL銆傚浜庤繖绉嶆儏鍐碉紝浼氱敓鎴愪竴涓鍛娿
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...
濡傛灉鎮ㄥ凡缁忓垹闄や簡琛ㄧ殑涓澶ч儴鍒嗭紝鎴栬呭鏋滄偍宸茬粡瀵瑰惈鏈夊彲鍙橀暱搴﹁鐨勮〃锛堝惈鏈VARCHAR, BLOB鎴TEXT鍒楃殑琛級杩涜浜嗗緢澶氭洿鏀癸紝鍒欏簲浣跨敤OPTIMIZE TABLE銆傝鍒犻櫎鐨勮褰曡淇濇寔鍦ㄩ摼鎺ユ竻鍗曚腑锛屽悗缁殑INSERT鎿嶄綔浼氶噸鏂颁娇鐢ㄦ棫鐨勮褰曚綅缃傛偍鍙互浣跨敤OPTIMIZE TABLE鏉ラ噸鏂板埄鐢ㄦ湭浣跨敤鐨勭┖闂达紝骞舵暣鐞嗘暟鎹枃浠剁殑纰庣墖銆
鍦ㄥ鏁扮殑璁剧疆涓紝鎮ㄦ牴鏈笉闇瑕佽繍琛OPTIMIZE TABLE銆傚嵆浣挎偍瀵瑰彲鍙橀暱搴︾殑琛岃繘琛屼簡澶ч噺鐨勬洿鏂帮紝鎮ㄤ篃涓嶉渶瑕佺粡甯歌繍琛岋紝姣忓懆涓娆℃垨姣忔湀涓娆″嵆鍙紝鍙鐗瑰畾鐨勮〃杩愯銆
OPTIMIZE TABLE鍙MyISAM, BDB鍜InnoDB琛ㄨ捣浣滅敤銆
瀵逛簬MyISAM琛紝OPTIMIZE TABLE鎸夊涓嬫柟寮忔搷浣滐細
1. 濡傛灉琛ㄥ凡缁忓垹闄ゆ垨鍒嗚В浜嗚锛屽垯淇琛ㄣ
2. 濡傛灉鏈绱㈠紩椤佃繘琛屽垎绫伙紝鍒欒繘琛屽垎绫汇
3. 濡傛灉琛ㄧ殑缁熻鏁版嵁娌℃湁鏇存柊锛堝苟涓旈氳繃瀵圭储寮曡繘琛屽垎绫讳笉鑳藉疄鐜颁慨澶嶏級锛屽垯杩涜鏇存柊銆
瀵逛簬BDB琛紝OPTIMIZE TABLE鐩墠琚槧灏勫埌ANALYZE TABLE涓娿傚浜InnoDB琛紝OPTIMIZE TABLE琚槧灏勫埌ALTER TABLE涓婏紝杩欎細閲嶅缓琛ㄣ傞噸寤烘搷浣滆兘鏇存柊绱㈠紩缁熻鏁版嵁骞堕噴鏀炬垚绨囩储寮曚腑鐨勬湭浣跨敤鐨勭┖闂淬傝鍙傝13.5.2.1鑺傦紝鈥淎NALYZE TABLE璇硶鈥銆
浣跨敤鈥skip-new鎴栤safe-mode閫夐」鍙互鍚姩mysqld銆傞氳繃鍚姩mysqld锛屾偍鍙互浣OPTIMIZE TABLE瀵瑰叾瀹冭〃绫诲瀷璧蜂綔鐢ㄣ
娉ㄦ剰锛屽湪OPTIMIZE TABLE杩愯杩囩▼涓紝MySQL浼氶攣瀹氳〃銆
OPTIMIZE TABLE璇彞琚啓鍏ュ埌浜岃繘鍒舵棩蹇椾腑锛岄櫎闈炰娇鐢ㄤ簡鑷夌殑NO_WRITE_TO_BINLOG鍏抽敭璇嶏紙鎴栧叾鍒悕LOCAL锛夈傚凡缁忚繖涔堝仛浜嗭紝鍥犳锛岀敤浜MySQL鏈嶅姟鍣ㄧ殑OPTIMIZE TABLE鍛戒护鐨勪綔鐢ㄧ浉褰撲簬涓涓鍒朵富鏈嶅姟鍣紝鍦ㄩ粯璁ゆ儏鍐典笅锛岃繖浜涘懡浠ゅ皢琚鍒跺埌澶嶅埗浠庡睘鏈嶅姟鍣ㄤ腑銆
REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE
tbl_name [, tbl_name] ... [QUICK] [EXTENDED] [USE_FRM]
REPAIR TABLE鐢ㄤ簬淇琚牬鍧忕殑琛ㄣ傞粯璁ゆ儏鍐典笅锛REPAIR TABLE涓myisamchk --recover tbl_name鍏锋湁鐩稿悓鐨勬晥鏋溿REPAIR TABLE瀵MyISAM鍜ARCHIVE琛ㄨ捣浣滅敤銆傝鍙傝15.1鑺傦紝鈥淢yISAM瀛樺偍寮曟搸鈥, 15.8鑺傦紝鈥淎RCHIVE瀛樺偍寮曟搸鈥銆
閫氬父锛屾偍鍩烘湰涓婁笉蹇呰繍琛屾璇彞銆備絾鏄紝濡傛灉鐏鹃毦鍙戠敓锛REPAIR TABLE寰堟湁鍙兘浠MyISAM琛ㄤ腑鎵惧洖鎵鏈夋暟鎹傚鏋滄偍鐨勮〃缁忓父琚牬鍧忥紝鎮ㄥ簲璇ュ敖鍔涙壘鍒板師鍥狅紝浠ラ伩鍏嶄娇鐢REPAIR TALBE銆傝鍙傝A.4.2鑺傦紝鈥滃鏋淢ySQL渚濈劧宕╂簝锛屽簲浣滀簺浠涔堚銆傚悓鏃朵篃瑙15.1.4鑺傦紝鈥淢yISAM琛ㄦ柟闈㈢殑闂鈥銆
鏈鍙ヤ細杩斿洖涓涓惈鏈変互涓嬪垪鐨勮〃锛
鍒 |
鍊 |
Table |
琛ㄥ悕绉 |
Op |
杩涜淇 |
Msg_type |
鐘舵併侀敊璇佷俊鎭垨璀﹀憡涔嬩竴 |
Msg_text |
娑堟伅 |
瀵逛簬姣忎釜琚慨澶嶇殑琛紝REPAIR TABLE璇彞浼氫骇鐢熷琛岀殑淇℃伅銆備笂涓琛屽惈鏈変竴涓Msg_type鐘舵佸笺Msg_test閫氬父搴斾负OK銆傚鏋滄偍娌℃湁寰楀埌OK锛屾偍搴旇灏濊瘯浣跨敤myisamchk --safe-recover淇琛紝鍥犱负REPAIR TABLE灏氫笉浼氭墽琛屾墍鏈夌殑myisamchk閫夐」銆傛垜浠鍒掑湪灏嗘潵浣垮畠鐨勭伒娲绘ф洿寮恒
濡傛灉缁欏畾浜QUICK锛屽垯REPAIR TABLE浼氬皾璇曞彧淇绱㈠紩鏍戙傝繖绉嶇被鍨嬬殑淇涓庝娇鐢myisamchk --recover --quick鐩镐技銆
濡傛灉鎮ㄤ娇鐢EXTENDED锛屽垯MySQL浼氫竴琛屼竴琛屽湴鍒涘缓绱㈠紩琛岋紝浠f浛浣跨敤鍒嗙被涓娆″垱寤轰竴涓储寮曘傝繖绉嶇被鍨嬬殑淇涓庝娇鐢myisamchk --safe-recover鐩镐技銆
瀵逛簬REPAIR TABLE锛岃繕鏈変竴绉USE_FRM妯″紡鍙互鍒╃敤銆傚鏋.MYI绱㈠紩鏂囦欢缂哄け鎴栨爣棰樿鐮村潖锛屽垯浣跨敤姝ゆā寮忋傚湪杩欑妯″紡涓嬶紝MySQL鍙互浣跨敤鏉ヨ嚜.frm鏂囦欢閲嶆柊鍒涘缓.MYI鏂囦欢銆傝繖绉嶄慨澶嶄笉鑳戒娇鐢myisamchk鏉ュ畬鎴愩 娉ㄩ噴锛氬彧鑳藉湪鎮ㄤ笉鑳戒娇鐢ㄥ父瑙REPAIR妯″紡鏄紝鎵嶈兘浣跨敤姝ゆā寮忋.MYI鏍囬鍖呭惈閲嶈鐨勮〃鍏冩暟鎹紙鐗瑰埆鏄紝褰撳墠鐨AUTO_INCREMENT鍊煎拰Delete閾炬帴锛夈傝繖浜涘厓鏁版嵁鍦REPAIR...USE_FRM涓涪澶便傚鏋滆〃琚帇缂╋紝鍒欎笉鑳戒娇鐢USE_FRM銆傚洜涓烘湰淇℃伅涔熷瓨鍌ㄥ湪.MYI鏂囦欢涓
REPAIR TABLE璇彞琚啓鍏ヤ簩杩涘埗鏃ュ織涓紝闄ら潪浣跨敤浜嗚嚜閫夌殑NO_WRITE_TO_BINLOG鍏抽敭璇嶏紙鎴栧叾鍒悕LOCAL锛夈
璀﹀憡锛氬鏋滃湪REPAIR TABLE杩愯杩囩▼涓紝鏈嶅姟鍣ㄥ仠鏈猴紝鍒欏湪閲嶆柊鍚姩涔嬪悗锛屽湪鎵ц鍏跺畠鎿嶄綔涔嬪墠锛屾偍蹇呴』绔嬪埢瀵硅〃鍐嶆墽琛屼竴涓REPAIR TABLE璇彞銆傦紙閫氳繃鍒朵綔涓涓浠芥潵鍚姩鏄竴涓ソ鍔炴硶銆傦級鍐嶆渶涓嶅埄鎯呭喌涓嬶紝鎮ㄥ彲浠ユ湁涓涓柊鐨勫共鍑鐨勭储寮曟枃浠讹紝涓嶅惈鏈夊叧鏁版嵁鏂囦欢鐨勪俊鎭傜劧鍚庯紝鎮ㄦ墽琛岀殑涓嬩竴涓搷浣滀細瑕嗙洊鏁版嵁鏂囦欢銆傝繖寰堝皯鍙戠敓锛屼絾鏄槸鏈夊彲鑳界殑銆
RESTORE TABLE tbl_name [, tbl_name] ... FROM '/path/to/backup/directory'
鐢ㄤ簬鎭㈠鏉ヨ嚜鐢BACKUP TABLE鍒朵綔鐨勫浠界殑琛ㄣ傚師鏈夌殑琛ㄤ笉浼氳瑕嗙洊锛涘鏋滄偍璇曞浘瑕嗙洊涓涓師鏈夌殑琛紝浼氬彂鐢熼敊璇傚拰BACKUP TABLE涓鏍凤紝RESTORE TABLE鐩墠鍙MyISAM琛ㄨ捣浣滅敤銆傜洰褰曞簲琚寚瀹氫负涓涓畬鏁磋矾寰勫悕銆
姣忎釜琛ㄧ殑澶囦唤鍖呮嫭鍏.frm鏍煎紡鏂囦欢鍜.MYD鏁版嵁鏂囦欢銆傛仮澶嶆搷浣滀細鎭㈠杩欎簺鏂囦欢锛岀劧鍚庝娇鐢ㄨ繖浜涙枃浠舵潵閲嶅缓.MYI绱㈠紩鏂囦欢銆傛仮澶嶆搷浣滄瘮澶囦唤鎿嶄綔鑺辩殑鏃堕棿鏇撮暱锛岃繖鏄洜涓洪渶瑕侀噸寤虹储寮曘傝〃鍚湁鐨勭储寮曡秺澶氾紝鑺辩殑鏃堕棿灏辫秺闀裤
璇ヨ鍙ヤ細杩斿洖涓涓惈鏈変互涓嬪垪鐨勮〃锛
鍒 |
鍊 |
Table |
琛ㄥ悕绉 |
Op |
杩涜鎭㈠ |
Msg_type |
鐘舵併侀敊璇佷俊鎭垨璀﹀憡涔嬩竴 |
Msg_text |
娑堟伅 |
SET variable_assignment [, variable_assignment] ...
variable_assignment:
user_var_name = expr
| [GLOBAL | SESSION] system_var_name = expr
| @@[global. | session.]system_var_name = expr
SET鐢ㄤ簬璁剧疆涓嶅悓绫诲瀷鐨勫彉閲忋傝繖浜涘彉閲忎細褰卞搷鏈嶅姟鍣ㄦ垨瀹㈡埛绔殑鎿嶄綔銆SET鍙互鐢ㄤ簬鍚戠敤鎴峰彉閲忔垨绯荤粺鍙橀噺璧嬪笺
鐢ㄤ簬鍒嗛厤璐︽埛瀵嗙爜鐨SET PASSWORD璇彞鍦13.5.1.5鑺傦紝鈥淪ET PASSWORD璇硶鈥涓繘琛屼簡璁ㄨ銆
澶氭暟绯荤粺鍙橀噺鍙互鍦ㄨ繍琛屾椂闂磋鏇存敼銆傚彲浠ヨ鍔ㄦ佽缃殑绯荤粺鍙橀噺鍦5.3.3.1鑺傦紝鈥滃姩鎬佺郴缁熷彉閲忊涓繘琛屼簡璁ㄨ銆
娉ㄩ噴锛氭棫鐗堟湰鐨MySQL閲囩敤SET OPTION浣滀负杩欎釜鍛戒护锛屼絾鏄敱浜庢湁浜SET锛岀幇鍦ㄤ笉璧炴垚浣跨敤SET OPTION銆
浠ヤ笅渚嬪瓙鏄剧ず浜嗘偍鍙互鐢ㄤ簬璁剧疆鍙橀噺鐨勪笉鍚岃娉曘
鐢ㄦ埛鍙橀噺鍙互琚啓浣@var_name锛屽苟鍙互杩涜濡備笅璁剧疆锛
SET @var_name = expr;
鍦9.3鑺傦紝鈥滅敤鎴峰彉閲忊涓粰鍑轰簡鏈夊叧鐢ㄦ埛鍙橀噺鐨勬洿澶氫俊鎭
绯荤粺鍙橀噺鍙互琚綔涓var_name寮曠敤鍒SET璇彞涓傚湪鍚嶇О鐨勫墠闈㈠彲浠ヨ嚜閫夊湴娣诲姞GLOBAL鎴@@global锛屼互鏄庣‘鍦版寚绀鸿鍙橀噺鏄叏灞鍙橀噺銆傛垨鑰呭湪鍚嶇О鍓嶉潰娣诲姞SESSION, @@session锛屾垨@@锛屼互鎸囩ず瀹冩槸涓涓細璇濆彉閲忋LOCAL鍜@@local鏄SESSION鍜@@session鍦板悓涔夎瘝銆傚鏋滄病鏈変慨鏀圭锛屽垯SET璁剧疆浼氳瘽鍙橀噺銆
鏀寔绯荤粺鍙橀噺鐨@@var_name璇硶锛屼互渚夸娇MySQL璇硶涓庡叾瀹冩暟鎹簱绯荤粺鐩稿吋瀹广
濡傛灉鎮ㄥ湪鍚屼竴涓鍙ヤ腑璁剧疆澶氫釜绯荤粺鍙橀噺锛屽垯鏈鍚庝竴涓GLOBAL鎴SESSION閫夐」琚敤浜庢病鏈夋寚瀹氭ā寮忕殑鍙橀噺銆
SET sort_buffer_size=10000;
SET @@local.sort_buffer_size=10000;
SET GLOBAL sort_buffer_size=1000000, SESSION sort_buffer_size=1000000;
SET @@sort_buffer_size=1000000;
SET @@global.sort_buffer_size=1000000, @@local.sort_buffer_size=1000000;
濡傛灉鎮ㄤ娇鐢SESSION锛堥粯璁ゆ儏鍐碉級璁剧疆涓涓郴缁熷彉閲忥紝鍒欒鍊间粛鐒舵湁鏁堬紝鐩村埌褰撳墠浼氳瘽缁撴潫涓烘锛屾垨鑰呯洿鍒版偍鍚у彉閲忚缃负涓涓笉鍚岀殑鍊间负姝€傚鏋滄偍浣跨敤GLOBAL锛堣姹SUPER鏉冮檺锛夋潵璁剧疆涓涓郴缁熷彉閲忥紝鍒欒鍊艰璁颁綇锛屽苟琚敤浜庢柊鐨勮繛鎺ワ紝鐩村埌鏈嶅姟鍣ㄩ噸鏂板惎鍔ㄤ负姝€傚鏋滄偍鎯宠杩涜姘镐箙寮忓彉閲忚缃紝鎮ㄥ簲璇ユ妸瀹冩斁鍏ヤ竴涓夐」鏂囦欢銆傝鍙傝4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥銆
涓轰簡闃叉涓嶆纭殑浣跨敤锛屽鏋滄偍浣跨敤SET GLOBAL鏃跺悓鏃朵娇鐢ㄤ簡涓涓彧鑳戒笌SET SESSION鍚屾椂浣跨敤鐨勫彉閲忥紝鎴栬呭鏋滄偍鍦ㄨ缃竴涓叏灞鍙橀噺鏃舵湭鎸囧畾GLOBAL锛堟垨@@锛夛紝鍒MySQL浼氫骇鐢熶竴涓敊璇
濡傛灉鎮ㄦ兂瑕佹妸涓涓SESSION鍙橀噺璁剧疆涓GLOBAL鍊兼垨鎶婁竴涓GLOBAL鍊艰缃负鍐呴儴MySQL榛樿鍊硷紝闇浣跨敤DEFAULT鍏抽敭璇嶃備緥濡傦紝鍦ㄦ妸max_join_size浼氳瘽鍊艰缃负鍏ㄥ眬鍊兼椂锛屼互涓嬩袱涓鍙ユ槸涓鏍风殑锛
SET max_join_size=DEFAULT;
SET @@session.max_join_size=@@global.max_join_size;
鎮ㄥ彲浠ヤ娇鐢SHOW VARIABLES鏉ュ緱鍒扮郴缁熷彉閲忔竻鍗曘傦紙瑙13.5.4.21鑺傦紝鈥淪HOW VARIABLES璇硶鈥銆傦級瑕佽幏寰椾笌鏍峰紡鍖归厤鐨勪竴涓叿浣撶殑鍙橀噺鍚嶇О鎴栬呭悕绉版竻鍗曪紝闇浣跨敤LIKE瀛愬彞锛屼娇鐢ㄦ柟娉曞涓嬶細
SHOW VARIABLES LIKE 'max_join_size';
SHOW GLOBAL VARIABLES LIKE 'max_join_size';
瑕佸緱鍒板悕绉颁笌鏍峰紡鍖归厤鐨勫彉閲忕殑娓呭崟锛岄渶浣跨敤閫氶厤绗︹%鈥欙細
SHOW VARIABLES LIKE 'have%';
SHOW GLOBAL VARIABLES LIKE 'have%';
閫氶厤绗﹀彲浠ヨ鐢ㄤ簬鐩稿尮閰嶇殑鏍峰紡涓殑浠讳綍浣嶇疆銆
鎮ㄤ篃鍙互閫氳繃浣跨敤@@[global.|local.]var_name璇硶鍜SELECT鏉ュ緱鍒板硷細
SELECT @@max_join_size, @@global.max_join_size;
褰撴偍浣跨敤SELECT @@var_name锛堝嵆鎮ㄤ笉鎸囧畾鍏ㄥ眬銆佷細璇濇垨鏈湴锛夋潵鎭㈠涓涓彉閲忔椂锛屽垯MySQL浼氳繑鍥SESSION鍊硷紙濡傛灉瀛樺湪锛夋垨鑰GLOBAL鍊笺
浠ヤ笅娓呭崟鐢ㄤ簬鎻忚堪甯︽湁闈炴爣鍑嗚娉曠殑鍙橀噺锛屾垨鎻忚堪鍦ㄧ郴缁熷彉閲忔竻鍗曚腑锛堣5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆傦級涓病鏈夋弿杩扮殑鍙橀噺銆傚敖绠¤繖浜涘彉閲忔病鏈夎SHOW VARIABLES鏄剧ず锛屼絾鏄偍鍙互浣跨敤SELECT鏉ヨ幏寰楀畠浠殑鍊硷紙渚嬪鎯呭喌鏄紝浣跨敤CHARACTER SET鍜SET NAMES锛夈備緥濡傦細
mysql> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 1 |
+--------------+
路 AUTOCOMMIT = {0 | 1}
璁剧疆autocommit妯″紡銆傚鏋滆缃负1锛屽垯鎵鏈夊琛ㄧ殑鏇存敼浼氱珛鍒荤敓鏁堛傚鏋滆缃负0锛屽垯鎮ㄥ繀椤讳娇鐢COMMIT鏉ユ帴鍙椾竴涓簨鍔★紝鎴栦娇鐢ROLLBACK鏉ュ彇娑堝畠銆傚鏋滄偍鎶AUTOCOMMIT妯″紡浠0鏀逛负1锛屽垯MySQL浼氬寮鏀句簨鍔℃墽琛屼竴涓嚜鍔COMMIT銆傚紑濮嬩竴涓簨鍔$殑鍙︿竴绉嶆柟娉曟槸浣跨敤涓涓START TRANSACTION鎴BEGIN璇彞銆傝鍙傝13.4.1鑺傦紝鈥淪TART TRANSACTION, COMMIT鍜孯OLLBACK璇硶鈥銆
路 BIG_TABLES = {0 | 1}
濡傛灉璁剧疆涓1锛屾墍鏈夌殑涓存椂琛ㄨ瀛樺偍鍦ㄧ鐩樹腑锛岃屼笉鏄瓨鍌ㄥ湪鍌ㄥ瓨鏈熶腑銆傝繖鏍蜂細绋嶅井鎱簺锛屼絾鏄浜庨渶瑕佷竴涓ぇ鍨嬩复鏃惰〃鐨SELECT鎿嶄綔锛屼笉浼氬彂鐢The table tbl_name is full閿欒銆傚浜庝竴涓柊杩炴帴锛岄粯璁ゅ间负0锛堜娇鐢ㄥ瓨鍌ㄥ櫒鍐呴儴涓存椂琛級銆傞氬父锛屾偍涓嶅繀璁剧疆姝ゅ彉閲忥紝鍥犱负鏍规嵁闇瑕侊紝瀛樺偍鍣ㄥ唴閮ㄨ〃浼氳鑷姩杞崲涓轰互纾佺洏涓哄熀纭鐨勮〃銆傦紙 娉ㄩ噴锛氭湰鍙橀噺浠ュ墠琚懡鍚嶄负SQL_BIG_TABLES銆傦級
路 CHARACTER SET {charset_name | DEFAULT}
鏈鍙ヤ娇鐢ㄧ粰瀹氱殑鏄犲皠涓烘墍鏈夋潵鑷鎴风鍜屾寚鍚戝鎴风鐨勫瓧绗︿覆寤虹珛鏄犲皠銆傛偍鍙互閫氳繃鍦MySQL婧愬垎甯冧腑缂栬緫sql/convert.cc鏉ユ坊鍔犳柊鐨勬槧灏勩SET CHARACTER SET鐢ㄤ簬璁惧畾涓変釜浼氳瘽绯荤粺鍙橀噺锛character_set_client鍜character_set_results琚缃负缁欏畾鐨勫瓧绗﹂泦锛character_set_connection琚缃负character_set_database鍊笺
鍙互閫氳繃浣跨敤DEFAULT鍊兼仮澶嶉粯璁ょ殑鏄犲皠銆
娉ㄦ剰锛SET CHARACTER SET鐨勮娉曚笌璁剧疆鍏跺畠閫夐」鐨勮娉曚笉鍚屻
路 FOREIGN_KEY_CHECKS = {0 | 1}
濡傛灉璁剧疆涓1锛堥粯璁ゆ儏鍐碉級锛屽垯妫鏌InnoDB琛ㄧ殑澶栭敭闄愬埗鏉′欢銆傚鏋滆缃负0锛屽垯闄愬埗鏉′欢琚拷鐣ャ傚鏋滈噸鏂拌浇鍏InnoDB琛ㄦ椂鎸夌収鐨勯『搴忎笌涓婄骇/涓嬬骇鐩綍鎵瑕佹眰鐨勯『搴忎笉鍚岋紝姝ゆ椂绂佺敤澶栭敭妫鏌ユ槸鏈夌敤鐨勩傝鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥銆
路 IDENTITY = value
璇ュ彉閲忔槸LAST_INSERT_ID鍙橀噺鐨勫悓涔夎瘝銆傝鍙橀噺鐨勪綔鐢ㄦ槸淇濇寔涓庡叾瀹冩暟鎹簱鍏煎銆傛偍鍙互浣跨敤SELECT @@IDENTITY璇诲彇鍏跺硷紝骞跺彲浠ヤ娇鐢SET IDENTITY璁剧疆瀹冦
路 INSERT_ID = value
鐢ㄤ簬璁剧疆灏嗚浠ヤ笅INSERT鎴ALTER TABLE璇彞浣跨敤鐨勫笺傛鍊煎湪鎻掑叆涓涓AUTO_INCREMENT鍊兼椂浣跨敤銆傛湰璇彞涓昏鍜屼簩杩涘埗鏃ュ織鍚屾椂浣跨敤銆
路 LAST_INSERT_ID = value
鐢ㄤ簬璁惧畾灏嗕粠LAST_INSERT_ID()琚繑鍥炵殑鍊笺傚綋鎮ㄥ湪鐢ㄤ簬鏇存柊琛ㄧ殑璇彞涓娇鐢LAST_INSERT_ID()鏃讹紝瀹冭瀛樺偍鍦ㄤ簩杩涘埗鏃ュ織涓傝缃鍙橀噺涓嶄細鏇存柊鐢mysql_insert_id() C API鍑芥暟杩斿洖鐨勫笺
路 NAMES {'charset_name' | DEFAULT}
SET NAMES鐢ㄤ簬鎶婁笁涓細璇濈郴缁熷彉閲character_set_client, character_set_connection鍜character_set_results璁剧疆涓虹粰瀹氱殑瀛楃闆嗐傛妸character_set_connection璁剧疆涓charset_name鏃讹紝鍚屾椂鎶collation_connection璁剧疆涓charset_name鐨勯粯璁ゆ暣搴忋
浣跨敤涓涓DEFAULT鍊煎彲浠ユ仮澶嶉粯璁ょ殑鏄犲皠銆
娉ㄦ剰锛SET NAMES鐨勮娉曚笌鐢ㄤ簬璁剧疆鍏跺畠閫夐」鐨勮娉曚笉鍚屻
路 ONE_SHOT
杩欎笉鏄竴涓湇鍔″櫒绯荤粺鍙橀噺锛屼絾鏄畠鍙互琚敤鏉ュ奖鍝嶇敤浜庤缃瓧绗﹂泦銆佹暣搴忓拰鏃跺尯鐨勫彉閲忕殑鏁堟灉銆ONE_SHOT涓昏琚敤浜庡鍒讹細mysqlbinlog浣跨敤SET ONE_SHOT鏉ユ殏鏃跺湴淇敼瀛楃闆嗐佹暣搴忓拰鏃跺尯鍙橀噺鐨勫硷紝浠ュ弽鏄犲嚭瀹冧滑鍘熷厛鐨勫笺
鎮ㄤ笉鑳藉湪浣跨敤ONE_SHOT鏃朵娇鐢ㄩ櫎鍏佽鐨勫彉閲忎互澶栫殑鍙橀噺锛涘鏋滄偍杩欎箞鍋氾紝鎮ㄤ細寰楀埌濡備笅閿欒锛
mysql> SET ONE_SHOT max_allowed_packet = 1;
ERROR 1382 (HY000): The 'SET ONE_SHOT' syntax is reserved for purposes internal to the MySQL server
濡傛灉鍚屾椂浣跨敤ONE_SHOT鍜岃鍏佽鐨勫彉閲忥紝鍒欎細鏍规嵁瑕佹眰鏇存敼鍙橀噺锛屼絾鏄細鍦ㄤ笅涓涓鍙ュ悗锛岄噸鏂拌缃墍鏈夌殑瀛楃闆嗐佹暣搴忓拰涓庢椂鍖烘湁鍏崇殑鏈嶅姟鍣ㄧ郴缁熷彉閲忋傚敮涓鐨勪緥澶栨槸锛屽綋涓嬩竴涓鍙ユ槸SET璇彞鏃讹紝涓嶄細杩涜閲嶆柊璁剧疆銆傛崲鍙ヨ瘽璇达紝鍦ㄤ笅涓涓潪SET璇彞涔嬪悗锛屾墠浼氳繘琛岄噸鏂拌缃備緥濡傦細
mysql> SET ONE_SHOT character_set_connection = latin5;
mysql> SET ONE_SHOT collation_connection = latin5_turkish_ci;
mysql> SHOW VARIABLES LIKE '%_connection';
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| character_set_connection | latin5 |
| collation_connection | latin5_turkish_ci |
+--------------------------+-------------------+
mysql> SHOW VARIABLES LIKE '%_connection';
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| character_set_connection | latin1 |
| collation_connection | latin1_swedish_ci |
+--------------------------+-------------------+
路 SQL_NOTES = {0 | 1}
褰撹缃负1鏃讹紙榛樿鎯呭喌锛夛紝鈥滄敞鎰忊濅竴绾х殑璀︽姤琚褰曚笅鏉ャ傚綋璁剧疆涓0鏃讹紝鈥滄敞鎰忊濊鍛婅鍘嬪埗銆Mysqldump鍖呭惈杈撳嚭锛岀敤浜庢妸姝ゅ彉閲忚缃负0锛岃繖鏍凤紝瀵逛簬涓嶄細褰卞搷閲嶆柊杞藉叆鎿嶄綔鏁翠綋鎬х殑浜嬩欢锛岄噸鏂拌浇鍏ヨ浆鍌ㄦ枃浠舵椂涓嶄細浜х敓璀﹀憡銆
路 SQL_AUTO_IS_NULL = {0 | 1}
濡傛灉璁剧疆涓1锛堥粯璁ゆ儏鍐碉級锛屾偍鍙互閫氳繃浣跨敤浠ヤ笅缁撴瀯鏌ユ壘鍖呭惈涓涓AUTO_INCREMENT鍒楃殑琛ㄧ殑鏈鍚庢彃鍏ョ殑琛岋細
WHERE auto_increment_column IS NULL
姝ゆц川琚湁浜ODBC绋嬪簭锛屾瘮濡Access浣跨敤銆
路 SQL_BIG_SELECTS = {0 | 1}
濡傛灉璁惧畾涓0锛屽垯MySQL浼氭斁寮冩湁鍙兘浼氳姳寰堥暱鏃堕棿鏉ユ墽琛岀殑SELECT璇彞锛堜篃灏辨槸锛屽浜庤繖浜涜鍙ワ紝浼樺寲绋嬪簭浼扮畻琚鏌ョ殑琛岀殑鏁扮洰瓒呰繃浜max_join_size鐨勫硷級銆傚綋涓涓笉濡ュ綋鐨WHERE璇彞琚彂甯冨悗锛屾湰璇彞鏈夌敤銆備竴涓柊杩炴帴鐨勯粯璁ゅ间负1锛岃繖鍙互鍏佽鎵鏈夌殑SELECT璇彞銆
濡傛灉鎮ㄦ妸max_join_size绯荤粺鍙橀噺璁剧疆涓洪櫎DEFAULT浠ュ鐨勫硷紝鍒SQL_BIG_SELECTS琚缃负0銆
路 SQL_BUFFER_RESULT = {0 | 1}
SQL_BUFFER_RESULT浼氳揩浣挎潵鑷SELECT璇彞鐨勭粨鏋滆鏀惧叆涓存椂琛ㄤ腑銆傝繖鍙互甯姪MySQL鏃╃偣瑙i櫎琛ㄩ攣瀹氥傚綋闇瑕佽姳杈冮暱鏃堕棿鎶婄粨鏋滃彂閫佺粰瀹㈡埛绔椂锛岃繖鏄湁濂藉鐨勩
路 SQL_LOG_BIN = {0 | 1}
濡傛灉璁剧疆涓0锛屽垯瀹㈡埛绔殑浜岃繘鍒舵棩蹇椾腑涓嶄細璁板綍鏃ュ織銆傚鎴风蹇呴』鎷ユ湁SUPER鏉冮檺鏉ヨ缃閫夐」銆
路 SQL_LOG_OFF = {0 | 1}
濡傛灉璁剧疆涓1锛屽垯姝ゅ鎴风鐨勬绘煡璇㈡棩蹇椾腑涓嶄細璁板綍鏃ュ織銆傚鎴风蹇呴』鎷ユ湁SUPER鏉冮檺鏉ヨ缃閫夐」銆
路 SQL_LOG_UPDATE = {0 | 1}
涓嶈禐鎴愪娇鐢ㄦ湰鍙橀噺銆傛湰鍙橀噺琚槧灏勫埌SQL_LOG_BIN銆
路 SQL_QUOTE_SHOW_CREATE = {0 | 1}
濡傛灉璁剧疆涓1锛屽垯SHOW CREATE TABLE浼氬琛ㄥ拰鍒楃殑鍚嶇О鍔犲紩鍙枫傚鏋滆缃负0锛屽垯鍔犲紩鍙锋搷浣滆绂佺敤銆傞粯璁ゆ儏鍐典笅锛屾湰閫夐」琚惎鐢紝鍥犳瀵逛簬鍚湁闇瑕佸姞寮曞彿鐨勫悕绉扮殑琛紝澶嶅埗鎿嶄綔璧蜂綔鐢ㄣ傝鍙傝13.5.4.5鑺傦紝鈥淪HOW CREATE TABLE璇硶鈥銆
路 SQL_SAFE_UPDATES = {0 | 1}
濡傛灉璁剧疆涓1锛屽垯MySQL浼氭斁寮冨湪WHERE瀛愬彞鎴LIMIT瀛愬彞涓笉浣跨敤鍏抽敭瀛楃殑UPDATE鎴DELETE璇彞銆傝繖鏍凤紝褰撳叧閿瓧浣跨敤涓嶆纭椂锛屼篃鏈夊彲鑳界悊瑙UPDATE鎴DELETE璇彞銆傝繖鏍峰氨鍙互鏇存敼鎴栧垹闄ゅぇ閲忕殑琛屻
路 SQL_SELECT_LIMIT = {value | DEFAULT}
浠SELECT璇彞杩斿洖鐨勮褰曠殑鏈澶ф暟鐩傚浜庝竴涓柊杩炴帴锛岄粯璁ゅ兼槸鈥unlimited鈥濄傚鏋滄偍鏇存敼浜嗛檺鍊硷紝鍙互浣跨敤SQL_SELECT_LIMIT DEFAULT鍊兼仮澶嶉粯璁ゅ笺
濡傛灉SELECT鏈変竴涓LIMIT瀛愬彞锛屽垯LIMIT浼樺厛浜SQL_SELECT_LIMIT鍊笺
SQL_SELECT_LIMT涓嶉傜敤浜庡湪琚瓨鍌ㄧ殑瀛愮▼搴忎腑鎵ц鐨SELECT璇彞銆傚畠涔熶笉閫傜敤浜庝笉浼氫骇鐢熷皢琚繑鍥炲埌瀹㈡埛绔殑缁撴灉闆嗗悎鐨SELECT璇彞銆傝繖浜涘寘鎷瓙鏌ヨ涓殑SELECT璇彞锛CREATE TABLE...SELECT鍜INSERT INTO...SELECT銆
路 SQL_WARNINGS = {0 | 1}
鏈彉閲忕敤浜庢帶鍒跺綋鍑虹幇璀﹀憡鏃讹紝鍗曡INSERT璇彞鏄惁浜х敓涓涓俊鎭瓧绗︿覆銆傞粯璁ゅ间负0銆傛妸鍊艰缃负1锛屾潵浜х敓涓涓俊鎭瓧绗︿覆銆
路 TIMESTAMP = {timestamp_value | DEFAULT}
鐢ㄤ簬涓烘瀹㈡埛绔缃椂闂淬傚綋鎮ㄤ娇鐢ㄤ簩杩涘埗鏃ュ織鏉ユ仮澶嶈鏃讹紝鏈鍙ョ敤浜庡緱鍒板師濮嬬殑鏃堕棿鏍囪銆timestamp_value搴斾负涓涓Unix鏃堕棿鏍囪锛岃屼笉鏄MySQL鏃堕棿鏍囪銆
路 UNIQUE_CHECKS = {0 | 1}
濡傛灉璁剧疆涓1锛堥粯璁ゆ儏鍐碉級锛屽垯浼氬InnoDB琛ㄤ腑鐨勪簩绾х储寮曟墽琛屽敮涓鎬ф鏌ャ傚鏋滆缃负0锛屽垯瀵逛簬琚彃鍏ュ埌InnoDB鐨勬彃鍏ョ紦鍐插櫒涓殑绱㈠紩鐧诲綍椤癸紝涓嶆墽琛屽敮涓鎬ф鏌ャ傚鏋滄偍鍙互鑲畾鎮ㄧ殑鏁版嵁涓嶈繚鍙嶅敮涓鎬ц姹傦紝鍒欐偍鍙互鎶婃鍊艰瀹氫负0锛屼互鍔犲揩鍚InnoDB瀵煎叆澶у瀷琛ㄧ殑閫熷害銆
SHOW鏈夊绉嶅舰寮忥紝鍙互鎻愪緵鏈夊叧鏁版嵁搴撱佽〃銆佸垪鎴栨湇鍔″櫒鐘舵佺殑淇℃伅銆傛湰鑺傚彊杩颁互涓嬪唴瀹癸細
SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [LIKE 'pattern']
SHOW CREATE DATABASE db_name
SHOW CREATE TABLE tbl_name
SHOW DATABASES [LIKE 'pattern']
SHOW ENGINE engine_name {LOGS | STATUS }
SHOW [STORAGE] ENGINES
SHOW ERRORS [LIMIT [offset,] row_count]
SHOW GRANTS FOR user
SHOW INDEX FROM tbl_name [FROM db_name]
SHOW INNODB STATUS
SHOW [BDB] LOGS
SHOW PRIVILEGES
SHOW [FULL] PROCESSLIST
SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern']
SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern']
SHOW [OPEN] TABLES [FROM db_name] [LIKE 'pattern']
SHOW TRIGGERS
SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern']
SHOW WARNINGS [LIMIT [offset,] row_count]
SHOW璇彞杩樻湁涓浜涘舰寮忥紝鍙互鎻愪緵鏈夊叧澶嶅埗鍨嬩富鏈嶅姟鍣ㄥ拰浠庡睘鏈嶅姟鍣ㄧ殑淇℃伅銆傝繖浜涘舰寮忓湪13.6鑺傦紝鈥滃鍒惰鍙モ涓繘琛屼簡鍙欒堪銆
SHOW BINLOG EVENTS
SHOW MASTER LOGS
SHOW MASTER STATUS
SHOW SLAVE HOSTS
SHOW SLAVE STATUS
濡傛灉涓涓粰瀹氱殑SHOW璇彞鐨勮娉曞寘鎷竴涓LIKE 'pattern'閮ㄥ垎锛屽垯'pattern'鏄竴涓彲浠ュ寘鍚SQL 鈥%鈥欏拰鈥_鈥欓氶厤绗︾殑瀛楃涓层傚浜庢妸璇彞杈撳嚭鍊奸檺瀹氫负鍖归厤鍊硷紝鏈牱寮忔槸鏈夌敤鐨勩
SHOW CHARACTER SET [LIKE 'pattern']
SHOW CHARACTER SET璇彞鐢ㄤ簬鏄剧ず鎵鏈夊彲鐢ㄧ殑瀛楃闆嗐傝璇彞鍙栦竴涓嚜閫夌殑LIKE瀛愬彞銆傝瀛愬彞鎸囩ず鍝簺瀛楃闆嗗悕绉板彲浠ュ尮閰嶃備妇渚嬭鏄庯細
mysql> SHOW CHARACTER SET LIKE 'latin%';
+---------+-----------------------------+-------------------+--------+
| Charset | Description | Default collation | Maxlen |
+---------+-----------------------------+-------------------+--------+
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
+---------+-----------------------------+-------------------+--------+
Maxlen鍒楁樉绀虹敤浜庡瓨鍌ㄤ竴涓瓧绗︾殑鏈澶х殑瀛楄妭鏁扮洰銆
SHOW COLLATION [LIKE 'pattern']
鏉ヨ嚜SHOW COLLATION鐨勮緭鍑哄寘鎷墍鏈夊彲鐢ㄧ殑瀛楃闆嗐傝璇彞鍙栦竴涓嚜閫夌殑LIKE瀛愬彞銆傝瀛愬彞鐨pattern鎸囩ず鍝簺鏁村簭鍚嶇О鍙互鍖归厤銆備妇渚嬭鏄庯細
mysql> SHOW COLLATION LIKE 'latin1%';
+-------------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+-------------------+---------+----+---------+----------+---------+
| latin1_german1_ci | latin1 | 5 | | | 0 |
| latin1_swedish_ci | latin1 | 8 | Yes | Yes | 0 |
| latin1_danish_ci | latin1 | 15 | | | 0 |
| latin1_german2_ci | latin1 | 31 | | Yes | 2 |
| latin1_bin | latin1 | 47 | | Yes | 0 |
| latin1_general_ci | latin1 | 48 | | | 0 |
| latin1_general_cs | latin1 | 49 | | | 0 |
| latin1_spanish_ci | latin1 | 94 | | | 0 |
+-------------------+---------+----+---------+----------+---------+
Default鍒楁寚绀哄浜庡叾瀛楃闆嗭紝鏁村簭鍊兼槸鍚︽槸榛樿鍊笺Compiled鎸囩ず瀛楃闆嗘槸鍚﹁缂栬緫鍒版湇鍔″櫒涓Sortlen涓庡瀛楃涓诧紙鍦ㄥ瓧绗﹂泦涓〃杈撅級鍒嗙被鎵闇鐨勫瓨鍌ㄥ櫒鐨勬暟閲忔湁鍏炽
SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [LIKE 'pattern']
SHOW COLUMNS鏄剧ず鍦ㄤ竴涓粰瀹氳〃涓殑鍚勫垪鐨勪俊鎭傚浜庤瘯鍥撅紝鏈鍙ヤ篃璧蜂綔鐢ㄣ
濡傛灉鍒楃被鍨嬩笌鏍规嵁鎮ㄧ殑CREATE TABLE璇彞鎵棰勬湡鐨勫垪绫诲瀷涓嶅悓锛屽垯闇娉ㄦ剰锛屽綋鎮ㄥ垱寤烘垨鏇存敼琛ㄦ椂锛MySQL鏈夋椂浼氭洿鏀瑰垪绫诲瀷銆傚嚭鐜拌繖绉嶆儏鍐电殑鏉′欢鍦13.1.5.1鑺傦紝鈥滄矇瀵傜殑鍒楄鏍煎彉鏇粹涓繘琛屼簡鎻忚堪銆
FULL鍏抽敭璇嶄細浣垮緱杈撳嚭涓寘鍚偍鎷ユ湁鐨勬潈闄愶紝骞跺寘鍚姣忎竴鍒楀悇鑷殑璇勬敞銆
鎮ㄥ彲浠ヤ娇鐢db_name.tbl_name浣滀负tbl_name FROM db_name璇硶鐨勫彟涓绉嶅舰寮忋傛崲鍙ヨ瘽璇达紝杩欎袱涓鍙ユ槸绛変环鐨勶細
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
SHOW FIELDS鏄SHOW COLUMNS鐨勫悓涔夎瘝銆傛偍涔熷彲浠ヤ娇鐢mysqlshow db_name tbl_name鍛戒护鍒椾妇琛ㄧ殑鍚勫垪銆
DESCRIBE璇彞鎻愪緵涓SHOW COLUMNS鐩歌繎鐨勪俊鎭傝鍙傝13.3.1鑺傦紝鈥淒ESCRIBE璇硶锛堣幏鍙栧叧浜庡垪鐨勪俊鎭級鈥銆
SHOW CREATE {DATABASE | SCHEMA} db_name
鏄剧ず鐢ㄤ簬鍒涘缓缁欏畾鏁版嵁搴CREATE DATABASE璇彞銆備篃鍙互浣跨敤SHOW CREATE SCHEMA銆
mysql> SHOW CREATE DATABASE test\G
*************************** 1. row ***************************
Database: test
Create Database: CREATE DATABASE `test`
/*!40100 DEFAULT CHARACTER SET latin1 */
mysql> SHOW CREATE SCHEMA test\G
*************************** 1. row ***************************
Database: test
Create Database: CREATE DATABASE `test`
/*!40100 DEFAULT CHARACTER SET latin1 */
SHOW CREATE TABLE tbl_name
鏄剧ず鐢ㄤ簬鍒涘缓缁欏畾琛ㄧ殑CREATE TABLE璇彞銆傛湰璇彞瀵硅鍥句篃璧蜂綔鐢ㄣ
mysql> SHOW CREATE TABLE t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE t (
id INT(11) default NULL auto_increment,
s char(60) default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM
鏍规嵁SQL_QUOTE_SHOW_CREATE閫夐」锛SHOW CREATE TABLE浼氬琛ㄥ悕绉板拰鍒楀悕绉板姞寮曞彿銆傝鍙傝13.5.3鑺傦紝鈥淪ET璇硶鈥銆
SHOW ENGINE engine_name {LOGS | STATUS }
SHOW ENGINE鏄剧ず瀛樺偍寮曟搸鐨勬棩蹇楁垨鐘舵佷俊鎭傜洰鍓嶆敮鎸佷互涓嬭鍙ワ細
SHOW ENGINE BDB LOGS
SHOW ENGINE INNODB STATUS
SHOW ENGINE BDB LOGS鏄剧ず鍘熸湁BDB鏃ュ織鏂囦欢鐨勭姸鎬佷俊鎭傚畠浼氳繑鍥炰互涓嬪瓧娈碉細
路 File
閫氬悜鏃ュ織鏂囦欢鐨勫畬鏁磋矾寰勩
路 Type
鏃ュ織鏂囦欢绫诲瀷锛堢敤浜Berkeley DB鏃ュ織鏂囦欢鐨BDB锛夈
路 Status
鏃ュ織鏂囦欢鐨勭姸鎬侊紙濡傛灉鏂囦欢鍙互琚彇娑堬紝鍒欎负FREE銆傚鏋滄枃浠惰浜嬪姟瀛愮郴缁熼渶瑕侊紝鍒欎负IN USE锛
SHOW ENGINE INNODB STATUS鏄剧ずInnoDB瀛樺偍寮曟搸鐘舵佺殑鍏ㄩ潰淇℃伅銆
杩欎簺璇彞鐨勬棫鐨勫悓涔夎瘝锛堢幇鍦ㄤ笉璧炴垚浣跨敤锛夋槸SHOW [BDB] LOGS鍜SHOW INNODB STATUS銆
SHOW ENGINE鍙互浠MySQL 4.1.2璧蜂娇鐢ㄣ
SHOW [STORAGE] ENGINES
SHOW ENGINES鏄剧ず瀛樺偍寮曟搸鐨勭姸鎬佷俊鎭傚浜庢鏌ヤ竴涓瓨鍌ㄥ紩鎿庢槸鍚﹁鏀寔锛屾垨鑰呭浜庢煡鐪嬮粯璁ゅ紩鎿庢槸浠涔堬紝鏈鍙ュ崄鍒嗘湁鐢ㄣSHOW TABLE TYPES鏄悓涔夎瘝锛屼絾涓嶈禐鎴愪娇鐢ㄣ
mysql> SHOW ENGINES\G *************************** 1. row *************************** Engine: MyISAM Support: DEFAULT Comment: Default engine as of MySQL 3.23 with great performance *************************** 2. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables *************************** 3. row *************************** Engine: HEAP Support: YES Comment: Alias for MEMORY *************************** 4. row *************************** Engine: MERGE Support: YES Comment: Collection of identical MyISAM tables *************************** 5. row *************************** Engine: MRG_MYISAM Support: YES Comment: Alias for MERGE *************************** 6. row *************************** Engine: ISAM Support: NO Comment: Obsolete storage engine, now replaced by MyISAM *************************** 7. row *************************** Engine: MRG_ISAM Support: NO Comment: Obsolete storage engine, now replaced by MERGE *************************** 8. row *************************** Engine: InnoDB Support: YES Comment: Supports transactions, row-level locking, and foreign keys *************************** 9. row *************************** Engine: INNOBASE Support: YES Comment: Alias for INNODB *************************** 10. row *************************** Engine: BDB Support: NO Comment: Supports transactions and page-level locking *************************** 11. row *************************** Engine: BERKELEYDB Support: NO Comment: Alias for BDB *************************** 12. row *************************** Engine: NDBCLUSTER Support: DISABLED Comment: Clustered, fault-tolerant, memory-based tables *************************** 13. row *************************** Engine: NDB Support: DISABLED Comment: Alias for NDBCLUSTER *************************** 14. row *************************** Engine: EXAMPLE Support: NO Comment: Example storage engine *************************** 15. row *************************** Engine: ARCHIVE Support: YES Comment: Archive storage engine *************************** 16. row *************************** Engine: CSV Support: YES Comment: CSV storage engine *************************** 17. row *************************** Engine: FEDERATED Support: NO Comment: Federated MySQL storage engine *************************** 18. row *************************** Engine: BLACKHOLE Support: YES Comment: /dev/null storage engine (anything you write to it disappears)
Support鍊兼寚绀烘煇涓瓨鍌ㄥ紩鎿庢槸鍚﹁鏀寔锛屽苟鎸囩ず鍝釜鏄粯璁ゅ紩鎿庛備緥濡傦紝濡傛灉鏈嶅姟鍣ㄤ互--default-table-type=InnoDB閫夐」涓鸿捣濮嬶紝鍒InnoDB琛岀殑Support鍊间负DEFAULT鍊笺傝鍙傝绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷銆
SHOW ERRORS [LIMIT [offset,] row_count]
SHOW COUNT(*) ERRORS
鏈鍙ヤ笌SHOW WARNINGS鎺ヨ繎锛屼笉杩囪璇彞鍙樉绀洪敊璇紝涓嶅悓鏃舵樉绀洪敊璇佽鍛婂拰娉ㄦ剰銆
LIMIT瀛愬彞涓SELECT璇彞鍏锋湁鐩稿悓鐨勮娉曪紝璇峰弬瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆
SHOW COUNT(*) ERRORS璇彞鏄剧ず閿欒鐨勬暟鐩傛偍涔熷彲浠ヤ粠error_count鍙橀噺涓壘鍥炴鏁扮洰锛
SHOW COUNT(*) ERRORS;
SELECT @@error_count;
瑕佷簡瑙f洿澶氫俊鎭紝璇峰弬瑙13.5.4.22鑺傦紝鈥淪HOW WARNINGS璇硶鈥銆
SHOW GRANTS FOR user
鏈鍙ュ垪鍑轰簡鍦ㄤ负MySQL鐢ㄦ埛璐︽埛澶嶅埗鏉冮檺鏃跺繀椤诲彂甯冪殑GRANT璇彞銆
mysql> SHOW GRANTS FOR 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
瑕佸褰撳墠鐨勪細璇濆垪鍑烘潈闄愶紝鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬭鍙ヤ箣涓锛
SHOW GRANTS;
SHOW GRANTS FOR CURRENT_USER;
SHOW GRANTS FOR CURRENT_USER();
SHOW INDEX FROM tbl_name [FROM db_name]
SHOW INDEX浼氳繑鍥炶〃绱㈠紩淇℃伅銆傚叾鏍煎紡涓ODBC涓殑SQLStatistics璋冪敤鐩镐技銆
SHOW INDEX浼氳繑鍥炰互涓嬪瓧娈碉細
路 Table
琛ㄧ殑鍚嶇О銆
路 Non_unique
濡傛灉绱㈠紩涓嶈兘鍖呮嫭閲嶅璇嶏紝鍒欎负0銆傚鏋滃彲浠ワ紝鍒欎负1銆
路 Key_name
绱㈠紩鐨勫悕绉般
路 Seq_in_index
绱㈠紩涓殑鍒楀簭鍒楀彿锛屼粠1寮濮嬨
路 Column_name
鍒楀悕绉般
路 Collation
鍒椾互浠涔堟柟寮忓瓨鍌ㄥ湪绱㈠紩涓傚湪MySQL涓紝鏈夊尖A鈥欙紙鍗囧簭锛夋垨NULL锛堟棤鍒嗙被锛夈
路 Cardinality
绱㈠紩涓敮涓鍊肩殑鏁扮洰鐨勪及璁″笺傞氳繃杩愯ANALYZE TABLE鎴myisamchk -a鍙互鏇存柊銆傚熀鏁版牴鎹瀛樺偍涓烘暣鏁扮殑缁熻鏁版嵁鏉ヨ鏁帮紝鎵浠ュ嵆浣垮浜庡皬鍨嬭〃锛岃鍊间篃娌℃湁蹇呰鏄簿纭殑銆傚熀鏁拌秺澶э紝褰撹繘琛岃仈鍚堟椂锛MySQL浣跨敤璇ョ储寮曠殑鏈轰細灏辫秺澶с
路 Sub_part
濡傛灉鍒楀彧鏄閮ㄥ垎鍦扮紪鍏ョ储寮曪紝鍒欎负琚紪鍏ョ储寮曠殑瀛楃鐨勬暟鐩傚鏋滄暣鍒楄缂栧叆绱㈠紩锛屽垯涓NULL銆
路 Packed
鎸囩ず鍏抽敭瀛楀浣曡鍘嬬缉銆傚鏋滄病鏈夎鍘嬬缉锛屽垯涓NULL銆
路 Null
濡傛灉鍒楀惈鏈NULL锛屽垯鍚湁YES銆傚鏋滄病鏈夛紝鍒欒鍒楀惈鏈NO銆
路 Index_type
鐢ㄨ繃鐨勭储寮曟柟娉曪紙BTREE, FULLTEXT, HASH, RTREE锛夈
路 Comment
澶氱璇勬敞銆
鎮ㄥ彲浠ヤ娇鐢db_name.tbl_name浣滀负tbl_name FROM db_name璇硶鐨勫彟涓绉嶅舰寮忋傝繖涓や釜璇彞鏄瓑浠风殑锛
mysql> SHOW INDEX FROM mytable FROM mydb;
mysql> SHOW INDEX FROM mydb.mytable;
SHOW KEYS鏄SHOW INDEX鐨勫悓涔夎瘝銆傛偍涔熷彲浠ヤ娇鐢mysqlshow -k db_name tbl_name鍛戒护鍒椾妇涓涓〃鐨勭储寮曘
SHOW INNODB STATUS
鍦MySQL 5.1涓紝杩欐槸SHOW ENGINE INNODB STATUS鐨勫悓涔夎瘝锛屼絾涓嶈禐鎴愪娇鐢ㄣ傝鍙傝13.5.4.7鑺傦紝鈥淪HOW ENGINE璇硶鈥銆
SHOW [BDB] LOGS
鍦MySQL 5.1涓紝杩欐槸SHOW ENGINE BDB LOGS鐨勫悓涔夎瘝锛屼絾鏄笉璧炴垚浣跨敤銆傝鍙傝13.5.4.7鑺傦紝鈥淪HOW ENGINE璇硶鈥銆
SHOW OPEN TABLES [FROM db_name] [LIKE 'pattern']
SHOW OPEN TABLES鍒椾妇鍦ㄨ〃缂撳瓨涓綋鍓嶈鎵撳紑鐨勯潪TEMPORARY琛ㄣ傝鍙傝7.4.9鑺傦紝鈥淢ySQL濡備綍鎵撳紑鍜屽叧闂〃鈥銆
SHOW OPEN TABLES浼氳繑鍥炰互涓嬪瓧娈碉細
路 Database
鍚湁璇ヨ〃鐨勬暟鎹簱銆
路 Table
琛ㄥ悕绉般
路 In_use
琛ㄥ綋鍓嶈鏌ヨ浣跨敤鐨勬鏁般傚鏋滆鏁颁负闆讹紝鍒欒〃鏄墦寮鐨勶紝浣嗘槸褰撳墠娌℃湁琚娇鐢ㄣ
路 Name_locked
琛ㄥ悕绉版槸鍚﹁閿佸畾銆傚悕绉伴攣瀹氱敤浜庡彇娑堣〃鎴栧琛ㄨ繘琛岄噸鍛藉悕绛夋搷浣溿
SHOW PRIVILEGES
SHOW PRIVILEGES鏄剧ずMySQL鏈嶅姟鍣ㄦ敮鎸佺殑绯荤粺鏉冮檺娓呭崟銆傜‘鍒囩殑杈撳嚭鏍规嵁鎮ㄧ殑鏈嶅姟鍣ㄧ殑鐗堟湰鑰屽畾銆
mysql> SHOW PRIVILEGES\G
*************************** 1. row ***************************
Privilege: Alter
Context: Tables
Comment: To alter the table
*************************** 2. row ***************************
Privilege: Alter routine
Context: Functions,Procedures
Comment: To alter or drop stored functions/procedures
*************************** 3. row ***************************
Privilege: Create
Context: Databases,Tables,Indexes
Comment: To create new databases and tables
*************************** 4. row ***************************
Privilege: Create routine
Context: Functions,Procedures
Comment: To use CREATE FUNCTION/PROCEDURE
*************************** 5. row ***************************
Privilege: Create temporary tables
Context: Databases
Comment: To use CREATE TEMPORARY TABLE
*************************** 6. row ***************************
Privilege: Create view
Context: Tables
Comment: To create new views
*************************** 7. row ***************************
Privilege: Create user
Context: Server Admin
Comment: To create new users
*************************** 8. row ***************************
Privilege: Delete
Context: Tables
Comment: To delete existing rows
*************************** 9. row ***************************
Privilege: Drop
Context: Databases,Tables
Comment: To drop databases, tables, and views
*************************** 10. row ***************************
Privilege: Execute
Context: Functions,Procedures
Comment: To execute stored routines
*************************** 11. row ***************************
Privilege: File
Context: File access on server
Comment: To read and write files on the server
*************************** 12. row ***************************
Privilege: Grant option
Context: Databases,Tables,Functions,Procedures
Comment: To give to other users those privileges you possess
*************************** 13. row ***************************
Privilege: Index
Context: Tables
Comment: To create or drop indexes
*************************** 14. row ***************************
Privilege: Insert
Context: Tables
Comment: To insert data into tables
*************************** 15. row ***************************
Privilege: Lock tables
Context: Databases
Comment: To use LOCK TABLES (together with SELECT privilege)
*************************** 16. row ***************************
Privilege: Process
Context: Server Admin
Comment: To view the plain text of currently executing queries
*************************** 17. row ***************************
Privilege: References
Context: Databases,Tables
Comment: To have references on tables
*************************** 18. row ***************************
Privilege: Reload
Context: Server Admin
Comment: To reload or refresh tables, logs and privileges
*************************** 19. row ***************************
Privilege: Replication client
Context: Server Admin
Comment: To ask where the slave or master servers are
*************************** 20. row ***************************
Privilege: Replication slave
Context: Server Admin
Comment: To read binary log events from the master
*************************** 21. row ***************************
Privilege: Select
Context: Tables
Comment: To retrieve rows from table
*************************** 22. row ***************************
Privilege: Show databases
Context: Server Admin
Comment: To see all databases with SHOW DATABASES
*************************** 23. row ***************************
Privilege: Show view
Context: Tables
Comment: To see views with SHOW CREATE VIEW
*************************** 24. row ***************************
Privilege: Shutdown
Context: Server Admin
Comment: To shut down the server
*************************** 25. row ***************************
Privilege: Super
Context: Server Admin
Comment: To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.
*************************** 26. row ***************************
Privilege: Update
Context: Tables
Comment: To update existing rows
*************************** 27. row ***************************
Privilege: Usage
Context: Server Admin
Comment: No privileges - allow connect only
SHOW [FULL] PROCESSLIST
SHOW PROCESSLIST鏄剧ず鍝簺绾跨▼姝e湪杩愯銆傛偍涔熷彲浠ヤ娇鐢mysqladmin processlist璇彞寰楀埌姝や俊鎭傚鏋滄偍鏈SUPER鏉冮檺锛屾偍鍙互鐪嬪埌鎵鏈夌嚎绋嬨傚惁鍒欙紝鎮ㄥ彧鑳界湅鍒版偍鑷繁鐨勭嚎绋嬶紙涔熷氨鏄紝涓庢偍姝e湪浣跨敤鐨MySQL璐︽埛鐩稿叧鐨勭嚎绋嬶級銆傝鍙傝13.5.5.3鑺傦紝鈥淜ILL璇硶鈥銆傚鏋滄偍涓嶄娇鐢FULL鍏抽敭璇嶏紝鍒欏彧鏄剧ず姣忎釜鏌ヨ鐨勫墠100涓瓧绗︺
鏈鍙ユ姤鍛TCP/IP杩炴帴鐨勪富鏈哄悕绉帮紙閲囩敤host_name:client_port鏍煎紡锛夛紝浠ユ柟渚垮湴鍒ゅ畾鍝釜瀹㈡埛绔鍦ㄥ仛浠涔堛
濡傛灉鎮ㄥ緱鍒扳too many connections鈥濋敊璇俊鎭紝骞朵笖鎯宠浜嗚В姝e湪鍙戠敓鐨勬儏鍐碉紝鏈鍙ユ槸闈炲父鏈夌敤鐨勩MySQL淇濈暀涓涓澶栫殑杩炴帴锛岃鎷ユ湁SUPER鏉冮檺鐨 璐︽埛浣跨敤锛屼互纭繚绠$悊鍛樿兘澶熼殢鏃惰繛鎺ュ拰妫鏌ョ郴缁燂紙鍋囪鎮ㄦ病鏈夋妸姝ゆ潈闄愮粰浜堟墍鏈夌殑鐢ㄦ埛锛夈
鍦ㄦ潵鑷SHOW PROCESSLIST鐨勮緭鍑轰腑甯歌鐨勪竴浜涚姸鎬侊細
路 Checking table
绾跨▼姝e湪鎵ц锛堣嚜鍔級琛ㄦ牸妫鏌ャ
路 Closing tables
鎰忓懗鐫绾跨▼姝e湪鍒锋柊鏇存敼鍚庣殑琛ㄦ暟鎹紝骞舵鍦ㄥ叧闂娇鐢ㄨ繃鐨勮〃銆傝繖搴旇鏄竴涓揩閫熺殑鎿嶄綔銆傚鏋滀笉蹇紝鍒欐偍搴旇楠岃瘉鎮ㄧ殑纾佺洏娌℃湁鍏呮弧锛屽苟涓旂鐩樻病鏈夎瓒呰礋鑽蜂娇鐢ㄣ
路 Connect Out
杩炴帴鍒颁富鏈嶅姟鍣ㄤ笂鐨勪粠灞炴湇鍔″櫒銆
路 Copying to tmp table on disk
涓存椂缁撴灉闆嗗悎澶т簬tmp_table_size銆傜嚎绋嬫妸涓存椂琛ㄤ粠瀛樺偍鍣ㄥ唴閮ㄦ牸寮忔敼鍙樹负纾佺洏妯″紡锛屼互鑺傜害瀛樺偍鍣ㄣ
路 Creating tmp table
绾跨▼姝e湪鍒涘缓涓涓复鏃惰〃锛屼互淇濇寔閮ㄥ垎缁撴灉銆
路 deleting from main table
鏈嶅姟鍣ㄦ鍦ㄦ墽琛屽琛ㄥ垹闄ょ殑绗竴閮ㄥ垎锛屽彧浠庣涓涓〃涓垹闄ゃ
路 deleting from reference tables
鏈嶅姟鍣ㄦ鍦ㄦ墽琛屽琛ㄥ垹闄ょ殑绗簩閮ㄥ垎锛屼粠鍏跺畠琛ㄤ腑鍒犻櫎鍖归厤鐨勮銆
路 Flushing tables
绾跨▼姝e湪鎵цFLUSH TABLES锛屽苟姝e湪绛夊緟鎵鏈夌嚎绋嬶紝浠ュ叧闂〃銆
路 FULLTEXT initialization
鏈嶅姟鍣ㄦ鍦ㄥ噯澶囨墽琛屼竴涓嚜鐒惰瑷鍏ㄦ枃鏈悳绱€
路 Killed
鏈変汉宸茬粡鍚戠嚎绋嬪彂閫佷簡涓涓KILL鍛戒护銆傚湪涓嬩竴娆℃鏌ョ粓姝㈡爣璁版椂锛屽簲鏀惧純銆傝鏍囪鍦MySQL鐨勬瘡涓ぇ寰幆涓兘妫鏌ワ紝浣嗘槸鍦ㄦ湁浜涙儏鍐典笅锛岀嚎绋嬬粓姝㈠彧闇瑕佽緝鐭殑鏃堕棿銆傚鏋滆绾跨▼琚叾瀹冪嚎绋嬮攣瀹氾紝鍒欏彧瑕佸叾瀹冪嚎绋嬫帴瑙﹂攣瀹氾紝缁堟鎿嶄綔灏变細鐢熸晥銆
路 Locked
璇ユ煡璇㈣鍏跺畠鏌ヨ閿佸畾銆
路 Sending data
绾跨▼姝e湪涓SELECT璇彞澶勭悊琛岋紝鍚屾椂姝e湪鍚戝鎴风鍙戦佹暟鎹
路 Sorting for group
绾跨▼姝e湪杩涜鍒嗙被锛屼互婊¤冻GROUP BY瑕佹眰銆
路 Sorting for order
绾跨▼姝e湪杩涜鍒嗙被锛屼互婊¤冻ORDER BY瑕佹眰銆
路 Opening tables
绾跨▼姝e湪璇曞浘鎵撳紑涓涓〃銆傝繖搴旇鏄潪甯稿揩鐨勮繃绋嬶紝闄ら潪鎵撳紑鎿嶄綔鍙楀埌闃绘銆備緥濡傦紝涓涓ALTER TABLE鎴栦竴涓LOCK TABLE璇彞鍙互闃绘鎵撳紑涓涓〃锛岀洿鍒拌鍙ュ畬鎴愪负姝€
路 Removing duplicates
鏌ヨ姝e湪浣跨敤SELECT DISTINCT銆備娇鐢ㄦ椂锛屽湪鏃╂湡闃舵锛MySQL涓嶈兘浼樺寲涓嶅悓鐨勬搷浣溿傚洜姝わ紝MySQL瑕佹眰涓涓澶栫殑闃舵锛屼互渚垮湪鎶婄粨鏋滃彂閫佺粰瀹㈡埛绔箣鍓嶅彇娑堟墍鏈夌殑澶嶅埗琛屻
路 Reopen table
绾跨▼寰楀埌涓涓〃閿佸畾锛屼絾鏄湪寰楀埌閿佸畾鍚庤閫氱煡甯︿笅鏂圭殑琛ㄧ粨鏋勫凡鏇存敼浜嗐傚畠宸茬粡閲婃斁浜嗛攣瀹氾紝鍏抽棴浜嗚〃锛屽苟璇曞浘閲嶆柊鎵撳紑瀹冦
路 Repair by sorting
淇浠g爜姝e湪浣跨敤涓涓垎绫绘潵鍒涘缓绱㈠紩銆
路 Repair with keycache
淇浠g爜姝e湪閫氳繃鍏抽敭缂撳瓨涓涓帴涓涓湴浣跨敤鍒涘缓鍏抽敭瀛椼傝繖姣旈氳繃鍒嗙被淇瑕佹參寰堝銆
路 Searching rows for update
绾跨▼姝e湪杩涜绗竴闃舵锛屼互鍦ㄦ洿鏂颁箣鍓嶏紝鏌ユ壘鎵鏈夊尮閰嶇殑琛屻傚鏋UPDATE姝e湪鏇存敼鐢ㄤ簬鏌ユ壘鐩稿叧琛岀殑绱㈠紩锛屽垯蹇呴』杩欎箞鍋氥
路 Sleeping
绾跨▼姝e湪绛夊緟瀹㈡埛绔紝浠ュ悜瀹冨彂閫佷竴涓柊璇彞銆
路 System lock
绾跨▼姝e湪绛夊緟寰楀埌涓涓敤浜庤〃鐨勫閮ㄧ郴缁熼攣瀹氥傚鏋滄偍娌℃湁姝e湪浣跨敤澶氫釜姝e湪璁块棶鍚屼竴涓〃鐨mysqld鏈嶅姟鍣紝鍒欐偍鍙互浣跨敤--skip-external-locking閫夐」绂佺敤绯荤粺閿佸畾銆
路 Upgrading lock
INSERT DELAYED绠$悊绋嬪簭姝e湪璇曞浘寰楀埌涓涓〃閿佸畾锛屼互鎻掑叆琛屻
路 Updating
绾跨▼姝e湪鎼滅储琛岋紝骞舵鍦ㄦ洿鏂拌繖浜涜銆
路 User Lock
绾跨▼姝e湪绛夊緟GET_LOCK()銆
路 Waiting for tables
绾跨▼寰楀埌涓涓氱煡锛岃〃鐨勫簳灞傜粨鏋勫凡缁忔敼鍙橈紝闇瑕侀噸鏂版墦寮琛ㄤ互寰楀埌鏂扮殑缁撴瀯銆備絾鏄紝涓轰簡鑳介噸鏂版墦寮琛紝蹇呴』绛夊緟锛岀洿鍒版墍鏈夊叾瀹冪殑绾跨▼宸茬粡鍏抽棴浜嗘鍦ㄨ璐ㄨ鐨勮〃銆
濡傛灉鍏跺畠绾跨▼宸茬粡瀵规鍦ㄨ璐ㄨ鐨勮〃浣跨敤浜FLUSH TABLES鎴栦互涓嬭鍙ヤ箣涓锛FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE鎴OPTIMIZE TABLE锛涘垯浼氬嚭鐜伴氱煡銆
路 waiting for handler insert
INSERT DELAYED绠$悊绋嬪簭宸茬粡澶勭悊浜嗘墍鏈夊浜庣瓑寰呯姸鎬佺殑鎻掑叆锛屽苟姝e湪绛夊緟鏂版彃鍏ャ
澶氭暟鐘舵佸搴斾簬闈炲父蹇殑鎿嶄綔銆傚鏋滀竴涓嚎绋嬪湪杩欎簺鐘舵佷笅鍋滅暀浜嗘暟绉掞紝鍒欏彲鑳芥槸鏈夐棶棰橈紝闇瑕佽繘琛岃皟鏌ャ
鏈変竴浜涘叾瀹冪殑鐘舵侊紝鍦ㄥ墠闈㈢殑娓呭崟涓病鏈夋彁鍙婏紝浣嗘槸鍏朵腑鏈夊緢澶氱姸鎬佸浜庢煡鎵炬湇鍔″櫒涓殑绋嬪簭閿欒鏄湁鐢ㄧ殑銆
SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern']
SHOW STATUS鎻愪緵鏈嶅姟鍣ㄧ姸鎬佷俊鎭傛淇℃伅涔熷彲浠ヤ娇鐢mysqladmin extended-status鍛戒护鑾峰緱銆
姝ゅ鏄剧ず浜嗗眬閮ㄧ殑杈撳嚭銆傚浜庢偍鐨勬湇鍔″櫒锛屽彉閲忓拰鍊肩殑娓呭崟鍙互鏄笉鍚岀殑銆傚湪5.3.4鑺傦紝鈥滄湇鍔″櫒鐘舵佸彉閲忊涓粰鍑轰簡姣忎釜鍙橀噺鐨勬剰涔夈
mysql> SHOW STATUS;
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Bytes_received | 155372598 |
| Bytes_sent | 1176560426 |
| Connections | 30023 |
| Created_tmp_disk_tables | 0 |
| Created_tmp_tables | 8340 |
| Created_tmp_files | 60 |
... ... ...
| Open_tables | 1 |
| Open_files | 2 |
| Open_streams | 0 |
| Opened_tables | 44600 |
| Questions | 2026873 |
... ... ...
| Table_locks_immediate | 1920382 |
| Table_locks_waited | 0 |
| Threads_cached | 0 |
| Threads_created | 30022 |
| Threads_connected | 1 |
| Threads_running | 1 |
| Uptime | 80380 |
+--------------------------+------------+
浣跨敤LIKE瀛愬彞锛岃璇彞鍙樉绀哄尮閰嶈鏍峰紡鐨勯偅浜涘彉閲忥細
mysql> SHOW STATUS LIKE 'Key%';
+--------------------+----------+
| Variable_name | Value |
+--------------------+----------+
| Key_blocks_used | 14955 |
| Key_read_requests | 96854827 |
| Key_reads | 162040 |
| Key_write_requests | 7589728 |
| Key_writes | 3813196 |
+--------------------+----------+
浣跨敤GLOBAL閫夐」锛屾偍鍙互寰楀埌鎵鏈MySQL杩炴帴鐨勭姸鎬佸笺備娇鐢SESSION锛屾偍鍙互寰楀埌鎵鏈夊綋鍓嶈繛鎺ョ殑鐘舵佸笺傚鏋滄偍涓や釜閫夐」閮戒笉浣跨敤锛屽垯榛樿鍊间负SESSION銆LOCAL鏄SESSION鐨勫悓涔夎瘝銆
娉ㄦ剰锛屾湁浜涚姸鎬佸彉閲忓彧鏈変竴涓叏灞鍊笺傚浜庤繖浜涘彉閲忥紝浣跨敤GLOBAL鍜SESSION浼氬緱鍒板悓鏍风殑鍊笺
SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern']
SHOW TABLE STATUS鐨勬ц川涓SHOW TABLE绫讳技锛屼笉杩囷紝鍙互鎻愪緵姣忎釜琛ㄧ殑澶ч噺淇℃伅銆傛偍涔熷彲浠ヤ娇鐢mysqlshow --status db_name鍛戒护寰楀埌姝ゆ竻鍗曘
鏈鍙ヤ篃鏄剧ず瑙嗗浘淇℃伅銆
瀵逛簬NDB Cluster琛紝鏈鍙ョ殑杈撳嚭鏄剧ずAvg_row_length鍜Data_length鍒楃殑閫傚綋鍊硷紝涓嶈繃BLOB鍒楁病鏈夎鑰冭檻杩涙潵銆傚彟澶栵紝澶嶅埗鏁伴噺鍦Comment鍒椾腑鏄剧ず锛堜綔涓number_of_replicas锛夈
SHOW TABLE STATUS浼氳繑鍥炰互涓嬪瓧娈碉細
路 Name
琛ㄧ殑鍚嶇О銆
路 Engine
琛ㄧ殑瀛樺偍寮曟搸銆傚湪MySQL 4.1.2涔嬪墠锛屾湰鍊艰鏍囪涓Type銆傝鍙傝绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷銆
路 Version
琛ㄧ殑.frm鏂囦欢鐨勭増鏈彿銆
路 Row_format
琛屽瓨鍌ㄦ牸寮忥紙Fixed, Dynamic, Compressed, Redundant, Compact锛夈InnoDB琛ㄧ殑鏍煎紡琚姤鍛婁负Redundant鎴Compact銆
路 Rows
琛岀殑鏁扮洰銆傞儴鍒嗗瓨鍌ㄥ紩鎿庯紝濡MyISAM锛屽瓨鍌ㄧ簿纭殑鏁扮洰銆
瀵逛簬鍏跺畠瀛樺偍寮曟搸锛屾瘮濡InnoDB锛屾湰鍊兼槸涓涓ぇ绾︾殑鏁帮紝涓庡疄闄呭肩浉宸彲杈40鍒50锛呫傚湪杩欎簺鎯呭喌涓嬶紝浣跨敤SELECT COUNT(*)鏉ヨ幏寰楀噯纭殑鏁扮洰銆
瀵逛簬鍦INFORMATION_SCHEMA鏁版嵁搴撲腑鐨勮〃锛Rows鍊间负NULL銆
路 Avg_row_length
骞冲潎鐨勮闀垮害銆
路 Data_length
鏁版嵁鏂囦欢鐨勯暱搴︺
路 Max_data_length
鏁版嵁鏂囦欢鐨勬渶澶ч暱搴︺傚鏋滅粰瀹氫簡鏁版嵁鎸囬拡鐨勫ぇ灏忥紝杩欐槸鍙互琚瓨鍌ㄥ湪琛ㄤ腑鐨勬暟鎹殑瀛楄妭鎬绘暟銆
路 Index_length
绱㈠紩鏂囦欢鐨勯暱搴︺
路 Data_free
琚暣搴忥紝浣嗘槸鏈娇鐢ㄧ殑瀛楄妭鐨勬暟鐩
路 Auto_increment
涓嬩竴涓AUTO_INCREMENT鍊笺
路 Create_time
浠涔堟椂鍊欒〃琚垱寤恒
路 Update_time
浠涔堟椂鍊欐暟鎹枃浠惰鏈鍚庝竴娆℃洿鏂般
路 Check_time
浠涔堟椂鍊欒〃琚渶鍚庝竴娆℃鏌ャ備笉鏄墍鏈夌殑瀛樺偍寮曟搸姝ゆ椂閮芥洿鏂帮紝鍦ㄦ鎯呭喌涓嬶紝鍊间负NULL銆
路 Collation
琛ㄧ殑瀛楃闆嗗拰鏁村簭銆
路 Checksum
娲绘ф牎楠屽拰鍊笺
路 Create_options
鍜CREATE TABLE鍚屾椂浣跨敤鐨勯澶栭夐」銆
路 Comment
鍒涘缓琛ㄦ椂浣跨敤鐨勮瘎娉紙鎴栬呮湁鍏充负浠涔MySQL鍙互璁块棶琛ㄤ俊鎭殑璇存槑锛夈
鍦ㄨ〃璇勬敞涓紝InnoDB琛ㄦ姤鍛婅〃鎵灞炵殑琛ㄧ┖闂寸殑绌洪棽绌洪棿銆傚浜庝竴涓綅浜庡叡浜〃绌洪棿涓殑琛紝杩欐槸鍏变韩琛ㄧ┖闂翠腑鐨勭┖闂茬┖闂淬傚鏋滄偍姝e湪浣跨敤澶氫釜琛ㄧ┖闂达紝骞朵笖璇ヨ〃鏈夎嚜宸辩殑琛ㄧ┖闂达紝鍒欑┖闂茬┖闂村彧鐢ㄤ簬姝よ〃銆
瀵逛簬MEMORY (HEAP)琛紝Data_length, Max_data_length鍜Index_length鍊艰繎浼间簬琚暣搴忕殑瀛樺偍鍣ㄧ殑瀹為檯鍊笺傛暣搴忕畻娉曢鐣欎簡澶ч噺鐨勫瓨鍌ㄥ櫒锛屼互鍑忓皯鏁村簭鎿嶄綔鐨勬暟閲忋
瀵逛簬瑙嗗浘锛岀敱SHOW TABLE STATUS鏄剧ず鐨勬墍鏈夊瓧娈靛潎涓NULL銆備緥澶栨儏鍐垫槸Name鎸囩ず涓鸿鍥惧悕绉板悓鏃Comment绉颁负瑙嗗浘銆
SHOW [FULL] TABLES [FROM db_name] [LIKE 'pattern']
SHOW TABLES鍒椾妇浜嗙粰瀹氭暟鎹簱涓殑闈TEMPORARY琛ㄣ傛偍涔熷彲浠ヤ娇鐢mysqlshow db_name鍛戒护寰楀埌姝ゆ竻鍗曘
鏈懡浠や篃鍒椾妇鏁版嵁搴撲腑鐨勫叾瀹冭鍥俱傛敮鎸FULL淇敼绗︼紝杩欐牱SHOW FULL TABLES灏卞彲浠ユ樉绀虹浜屼釜杈撳嚭鍒椼傚浜庝竴涓〃锛岀浜屽垪鐨勫间负BASE TABLE锛涘浜庝竴涓鍥撅紝绗簩鍒楃殑鍊间负VIEW銆
娉ㄩ噴锛氬鏋滄偍瀵逛簬涓涓〃娌℃湁鏉冮檺锛屽垯璇ヨ〃涓嶄細鍦ㄦ潵鑷SHOW TABLES鎴栫殑mysqlshow db_name杈撳嚭涓樉绀恒
SHOW TRIGGERS [FROM db_name] [LIKE expr]
SHOW TRIGGERS鍒楀嚭浜嗙洰鍓嶈MySQL鏈嶅姟鍣ㄥ畾涔夌殑瑙﹀彂绋嬪簭銆
瀵逛簬鍦21.3鑺傦紝鈥滀娇鐢ㄨЕ鍙戠▼搴忊涓畾涔夌殑瑙﹀彂绋嬪簭ins_sum锛屾湰璇彞鐨勮緭鍑烘樉绀哄涓嬶細
mysql> SHOW TRIGGERS LIKE 'acc%';
+---------+--------+---------+-------------------------------+--------+---------+
| Trigger | Event | Table | Statement | Timing | Created |
+---------+--------+---------+-------------------------------+--------+---------+
| ins_sum | INSERT | account | SET @sum = @sum + NEW.amount | BEFORE | NULL |
+---------+--------+---------+-------------------------------+--------+---------+
娉ㄩ噴锛氬綋浣跨敤涓涓惈鏈SHOW TRIGGERS鐨LIKE瀛愬彞鏃讹紝寰呭尮閰嶇殑琛ㄨ揪寮忥紙expr锛変細涓庤Е鍙戠▼搴忓畾涔夋椂鎵鍦ㄧ殑琛ㄧ殑鍚嶇О鐩告瘮杈冿紝鑰屼笉涓庤Е鍙戠▼搴忕殑鍚嶇О鐩告瘮杈冿細
mysql> SHOW TRIGGERS LIKE 'ins%';
Empty set (0.01 sec)
瀵规湰璇彞杈撳嚭涓殑鍚勫垪鐨勭畝瑕佽В閲婂涓嬶細
路 Trigger: 瑙﹀彂绋嬪簭鐨勫悕绉般
路 Event: 璋冪敤瑙﹀彂绋嬪簭鐨勬椂闂淬傚繀椤讳负'INSERT', 'UPDATE'鎴 'DELETE'.涔嬩竴銆
路 Table: 瑙﹀彂绋嬪簭瀹氫箟鏃跺搴旂殑琛ㄣ
路 Statement: 褰撹Е鍙戠▼搴忚璋冪敤鏃舵墽琛岀殑璇彞銆傝繖涓庡湪INFORMATION_SCHEMA.TRIGGERS鐨ACTION_STATEMENT鍒椾腑鏄剧ず鐨勬枃鏈竴鏍枫
路 Timing: 'BEFORE'鎴'AFTER'涓や釜鍊间箣涓銆
路 Created: 鐩墠锛屾湰鍒楃殑鍊间负NULL銆
涓轰簡鎵цSHOW TRIGGERS锛屾偍蹇呴』鎷ユ湁SUPER鏉冮檺銆
SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern']
SHOW VARIABLES鏄剧ず浜嗛儴闂MySQL绯荤粺鍙橀噺鐨勫笺傛湰淇℃伅涔熷彲浠ヤ娇鐢mysqladmin variables鍛戒护鑾峰緱銆
浣跨敤GLOBAL閫夐」锛屾偍鍙互鑾峰緱琚敤浜MySQL鏂拌繛鎺ョ殑鍊笺備娇鐢SESSION锛屾偍鍙互寰楀埌瀵逛簬褰撳墠杩炴帴璧锋晥鐨勫笺傚鏋滄偍涓や釜閫夐」閮戒笉浣跨敤锛岄粯璁ゅ间负SESSION銆
LOCAL鏄SESSION鐨勫悓涔夎瘝銆
濡傛灉榛樿鍊间笉鍚堥傦紝褰mysqld鍚姩鏃舵垨鍦SET璇彞杩愯杩囩▼涓紝鎮ㄥ彲浠ヤ娇鐢ㄥ懡浠よ閫夐」璁剧疆澶氭暟鐨勮繖绫诲彉閲忋傝鍙傝5.3.1鑺傦紝鈥mysqld鍛戒护琛岄夐」鈥鍜13.5.3鑺傦紝鈥淪ET璇硶銆
姝ゅ鏄剧ず浜嗛儴鍒嗙殑杈撳嚭銆傚浜庢偍鐨勬湇鍔″櫒锛屽彉閲忓拰鍊肩殑娓呭崟浼氭湁鎵涓嶅悓銆傚湪5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥涓粰鍑轰簡姣忎釜鍙橀噺鐨勬剰涔夈傚湪7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥涓彁渚涗簡鏈夊叧璋冩暣鍙橀噺鐨勪俊鎭
mysql> SHOW VARIABLES;
+---------------------------------+-----------------------------------------------+
| Variable_name | Value |
+---------------------------------+-----------------------------------------------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| automatic_sp_privileges | ON |
| back_log | 50 |
| basedir | /home/jon/bin/mysql-5.1/ |
| binlog_cache_size | 32768 |
| bulk_insert_buffer_size | 8388608 |
| character_set_client | latin1 |
| character_set_connection | latin1 |
... ...
| max_user_connections | 0 |
| max_write_lock_count | 4294967295 |
| multi_range_count | 256 |
| myisam_data_pointer_size | 6 |
| myisam_max_sort_file_size | 2147483647 |
| myisam_recover_options | OFF |
| myisam_repair_threads | 1 |
| myisam_sort_buffer_size | 8388608 |
| ndb_autoincrement_prefetch_sz | 32 |
| ndb_cache_check_time | 0 |
| ndb_force_send | ON |
... ... ...
| time_zone | SYSTEM |
| timed_mutexes | OFF |
| tmp_table_size | 33554432 |
| tmpdir | |
| transaction_alloc_block_size | 8192 |
| transaction_prealloc_size | 4096 |
| tx_isolation | REPEATABLE-READ |
| updatable_views_with_limit | YES |
| version | 5.1.2-alpha-log |
| version_comment | Source distribution |
| version_compile_machine | i686 |
| version_compile_os | suse-linux |
| wait_timeout | 28800 |
+---------------------------------+-----------------------------------------------+
浣跨敤LIKE瀛愬彞锛屾湰璇彞鍙樉绀轰笌鏍峰紡鐩稿尮閰嶇殑鍙橀噺锛
mysql> SHOW VARIABLES LIKE 'have%';
+-----------------------+----------+
| Variable_name | Value |
+-----------------------+----------+
| have_archive | YES |
| have_bdb | NO |
| have_blackhole_engine | YES |
| have_compress | YES |
| have_crypt | YES |
| have_csv | YES |
| have_example_engine | NO |
| have_federated_engine | NO |
| have_geometry | YES |
| have_innodb | YES |
| have_isam | NO |
| have_ndbcluster | DISABLED |
| have_openssl | NO |
| have_partition_engine | YES |
| have_query_cache | YES |
| have_raid | NO |
| have_rtree_keys | YES |
| have_symlink | YES |
+-----------------------+----------+
SHOW WARNINGS [LIMIT [offset,] row_count]
SHOW COUNT(*) WARNINGS
SHOW WARNINGS鏄剧ず鐢变笂涓涓敓鎴愭秷鎭殑璇彞瀵艰嚧鐨勯敊璇佽鍛婂拰娉ㄦ剰娑堟伅銆傚鏋滀笂涓涓娇鐢ㄨ〃鐨勮鍙ユ湭鐢熸垚娑堟伅锛屽垯浠涔堜篃涓嶆樉绀恒SHOW ERRORS鏄叾鐩稿叧璇彞锛屽彧鏄剧ず閿欒銆傝鍙傝13.5.4.9鑺傦紝鈥淪HOW ERRORS璇硶鈥銆
瀵逛簬浣跨敤涓涓〃鐨勬瘡涓柊璇彞锛屾秷鎭竻鍗曞潎閲嶆柊璁剧疆銆
SHOW COUNT(*) WARNINGS璇彞鏄剧ず閿欒銆佽鍛婂拰娉ㄦ剰鐨勬绘暟銆傛偍涔熷彲浠ヤ粠warning_count鍙橀噺涓壘鍥炴鏁扮洰銆
SHOW COUNT(*) WARNINGS;
SELECT @@warning_count;
濡傛灉max_error_count绯荤粺鍙橀噺璁剧疆寰楄繃浣庯紝浠ヨ嚧浜庢湁鐨勬秷鎭病鏈夎瀛樺偍锛屽垯warning_count鍊煎彲鑳芥瘮鐢SHOW WARNINGS鏄剧ず鐨勬秷鎭暟鐩澶с傛湰鑺傚悗閮ㄦ樉绀虹殑渚嬪瓙灞曠ず浜嗚繖绫绘儏鍐垫槸濡備綍鍙戠敓鐨勩
LIMIT瀛愬彞鍏锋湁涓SELECT璇彞鐩稿悓鐨勮娉曘傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆
MySQL鏈嶅姟鍣ㄤ細鍙戝洖鐢变笂涓涓鍙ュ紩璧风殑閿欒銆佽鍛婂拰娉ㄦ剰鐨勬绘暟銆傚鏋滄偍姝e湪浣跨敤C API锛屽垯姝ゅ煎彲浠ラ氳繃璋冪敤mysql_warning_count()鏉ヨ幏寰椼傝鍙傝25.2.3.69鑺傦紝鈥渕ysql_warning_count()鈥銆
瀵逛簬濡LOAD DATA INFILE绛夎鍙ュ拰濡INSERT, UPDATE, CREATE TABLE鍜ALTER TABLE绛DML璇彞锛屼細鐢熸垚璀﹀憡銆
浠ヤ笅DROP TABLE璇彞浼氬鑷翠竴涓敞鎰忥細
mysql> DROP TABLE IF EXISTS no_such_table;
mysql> SHOW WARNINGS;
+-------+------+-------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------+
| Note | 1051 | Unknown table 'no_such_table' |
+-------+------+-------------------------------+
浠ヤ笅鏄竴涓畝鍗曠殑渚嬪瓙锛屾樉绀轰簡瀵逛簬CREATE TABLE鐨勪竴涓娉曡鍛婏紝鍜屽浜INSERT鐨勮浆鎹㈣鍛婏細
mysql> CREATE TABLE t1 (a TINYINT NOT NULL, b CHAR(4)) TYPE=MyISAM;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1287
Message: 'TYPE=storage_engine' is deprecated, use
'ENGINE=storage_engine' instead
1 row in set (0.00 sec)
mysql> INSERT INTO t1 VALUES(10,'mysql'),(NULL,'test'),
-> (300,'Open Source');
Query OK, 3 rows affected, 4 warnings (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 4
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Warning
Code: 1265
Message: Data truncated for column 'b' at row 1
*************************** 2. row ***************************
Level: Warning
Code: 1263
Message: Data truncated, NULL supplied to NOT NULL column 'a' at row 2
*************************** 3. row ***************************
Level: Warning
Code: 1264
Message: Data truncated, out of range for column 'a' at row 3
*************************** 4. row ***************************
Level: Warning
Code: 1265
Message: Data truncated for column 'b' at row 3
4 rows in set (0.00 sec)
瑕佸瓨鍌ㄧ殑閿欒銆佽鍛婂拰娉ㄦ剰娑堟伅鐨勬渶澶ф暟鐩敱max_error_count绯荤粺鍙橀噺鎺у埗銆傞粯璁ゆ儏鍐典笅锛岃鍊间负64銆傝鏇存敼鎮ㄦ兂瑕佸瓨鍌ㄧ殑淇℃伅鐨勬暟鐩紝闇鏇存敼max_error_count鍊笺傚湪涓嬮潰鐨勪緥瀛愪腑锛ALTER TABLE璇彞浼氫骇鐢熶笁涓鍛婃秷鎭紝浣嗘槸鍙湁涓涓瀛樺偍锛屽洜涓max_error_count琚缃负1锛
mysql> SHOW VARIABLES LIKE 'max_error_count';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_error_count | 64 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> SET max_error_count=1;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE t1 MODIFY b CHAR;
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 3
mysql> SELECT @@warning_count;
+-----------------+
| @@warning_count |
+-----------------+
| 3 |
+-----------------+
1 row in set (0.01 sec)
mysql> SHOW WARNINGS;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1263 | Data truncated for column 'b' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
瑕佺鐢ㄨ鍛婏紝闇鎶max_error_count璁剧疆涓0銆傚湪姝ゆ儏鍐典笅锛warning_count浠嶇劧鎸囩ず鏈夊灏戣鍛婂凡缁忓彂鐢燂紝浣嗘槸杩欎簺娑堟伅涓嶈瀛樺偍銆
鎮ㄥ彲浠ユ妸SQL_NOTES浼氳瘽鍙橀噺璁剧疆涓0锛屼娇鈥滄敞鎰忊濈骇鍒殑璀﹀憡涓嶈璁板綍銆
CACHE INDEX
tbl_index_list [, tbl_index_list] ...
IN key_cache_name
tbl_index_list:
tbl_name [[INDEX|KEY] (index_name[, index_name] ...)]
CACHE INDEX璇彞鎶婅〃绱㈠紩鍒嗛厤缁欐煇涓叧閿紦瀛樸傝璇彞鍙敤浜MyISAM琛ㄣ
涓嬪垪璇彞鎶婄储寮曚粠琛t1, t2鍜t3鍒嗛厤鍒板悕涓hot_cache鐨勫叧閿紦瀛橈細
mysql> CACHE INDEX t1, t2, t3 IN hot_cache;
+---------+--------------------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+--------------------+----------+----------+
| test.t1 | assign_to_keycache | status | OK |
| test.t2 | assign_to_keycache | status | OK |
| test.t3 | assign_to_keycache | status | OK |
+---------+--------------------+----------+----------+
CACHE INDEX璇硶鍏佽鎮ㄦ寚瀹氾紝鍙湁鏉ヨ嚜琛ㄧ殑鐗瑰畾绱㈠紩搴旇鍒嗛厤缁欑紦瀛樸備絾鏄紝褰撳墠鐨勫疄鏂戒細鎶婃墍鏈夌殑琛ㄧ储寮曞垎閰嶇粰缂撳瓨锛屾墍浠ュ繀椤绘寚瀹氳〃鍚嶇О锛屼笉鑳芥寚瀹氬叾瀹冪殑銆
琚紩鐢ㄥ埌CACHE INDEX璇彞涓殑鍏抽敭缂撳瓨鍙互杩欐牱鍒涘缓锛屽嵆閫氳繃浣跨敤涓涓弬鏁拌缃鍙ユ垨鍦ㄦ湇鍔″櫒鍙傛暟璁剧疆涓缃叾澶у皬銆備妇渚嬭鏄庯細
mysql> SET GLOBAL keycache1.key_buffer_size=128*1024;
鍏抽敭缂撳瓨鍙傛暟鍙互琚綔涓轰竴涓粨鏋勫寲绯荤粺鍙橀噺鐨勬垚鍒嗚繘琛岃闂傝鍙傝9.4.1鑺傦紝鈥滅粨鏋勫紡绯荤粺鍙橀噺鈥銆
鍦ㄦ偍鍙互鎶婄储寮曞垎閰嶇粰涓涓叧閿紦瀛樹互鍓嶏紝缂撳瓨蹇呴』瀛樺湪锛
mysql> CACHE INDEX t1 IN non_existent_cache;
ERROR 1284 (HY000): Unknown key cache 'non_existent_cache'
榛樿鎯呭喌涓嬶紝琛ㄧ储寮曡鍒嗛厤缁欏湪鏈嶅姟鍣ㄥ惎鍔ㄦ椂琚垱寤虹殑涓伙紙榛樿锛夐敭缂撳瓨銆傚綋涓涓敭楂橀熺紦鍐茶鐮村潖鏃讹紝鎵鏈夎鍒嗛厤鍒版缂撳瓨涓殑绱㈠紩浼氬啀娆¤鍒嗛厤缁欓粯璁ょ殑 閿珮閫熺紦鍐层
绱㈠紩鐨勫垎閰嶄細瀵规湇鍔″櫒浜х敓鍏ㄥ眬鎬у奖鍝嶏細濡傛灉涓涓鎴风鎶婁竴涓储寮曞垎閰嶇粰涓涓粰瀹氱殑缂撳瓨锛屽垯涓嶈浠涔堝鎴风鍙戝竷鏌ヨ锛屾湰缂撳瓨閮借鐢ㄤ簬鎵鏈夋秹鍙婄储寮曠殑鏌ヨ銆
FLUSH [LOCAL | NO_WRITE_TO_BINLOG] flush_option [, flush_option] ...
濡傛灉鎮ㄦ兂瑕佹竻闄MySQL浣跨敤鐨勯儴鍒嗗唴閮ㄧ紦瀛橈紝鎮ㄥ簲璇ヤ娇鐢FLUSH璇彞銆傝鎵цFLUSH锛屾偍蹇呴』鎷ユ湁RELOAD鏉冮檺銆
flush_option鍙互涓轰互涓嬬殑浠讳綍涓涓細
路 HOSTS
鐢ㄤ簬娓呯┖涓绘満缂撳瓨琛ㄣ傚鏋滄湁鐨勪富鏈烘洿鏀逛簡IP鍙锋垨濡傛灉鎮ㄥ緱鍒颁簡閿欒淇℃伅Host host_name is blocked锛屽垯鎮ㄥ簲璇ュ埛鏂颁富鏈鸿〃銆傚綋鍦ㄨ繛鎺ュ埌MySQL鏈嶅姟鍣ㄦ椂锛屽鏋滃浜庝竴涓粰瀹氱殑涓绘満锛屾帴杩炲嚭鐜伴敊璇滃浜max_connect_errors鈥濓紝姝ゆ椂锛MySQL浼氬亣瀹氬嚭鐜颁簡閿欒锛屽苟闃绘涓绘満鍚庣画鐨勮繛鎺ョ敵璇枫傚埛鏂颁富鏈鸿〃鍏佽涓绘満灏濊瘯鍐嶆杩炴帴銆傝鍙傝A.2.5鑺傦紝鈥滀富鏈虹殑host_name琚睆钄鈥銆傛偍鍙互浣跨敤max_connect_errors=999999999鍚姩mysqld锛屼互閬垮厤姝ら敊璇俊鎭
路 DES_KEY_FILE
鐢ㄤ簬鍦ㄦ湇鍔″櫒鍚姩鏃讹紝浠庨噰鐢--des-key-file閫夐」鎸囧畾鐨勬枃浠堕噸鏂拌浇鍏DES鍏抽敭瀛椼
路 LOGS
鐢ㄤ簬鍏抽棴骞堕噸鏂版墦寮鎵鏈夌殑鏃ュ織鏂囦欢銆傚鏋滄偍宸茬粡鎸囧畾浜嗕竴涓洿鏂版棩蹇楁枃浠舵垨涓涓簩杩涘埗鏃ュ織鏂囦欢锛屽悓鏃舵病鏈夋墿灞曪紝鍒欑浉瀵逛簬鍓嶄竴涓枃浠讹紝鏃ュ織鏂囦欢鐨勬墿灞曞彿澧炲姞1銆傚鏋滄偍鍦ㄦ枃浠跺悕绉颁腑浣跨敤浜嗕竴涓墿灞曪紝鍒MySQL浼氬叧闂苟閲嶆柊鎵撳紑鏃ュ織鏂囦欢銆傚湪Unix涓紝褰撶浉mysqld鏈嶅姟鍣ㄥ彂閫佷竴涓SIGHUP淇″彿鏃讹紝涔熶細濡傛锛堜緥澶栨儏鍐垫槸閮ㄥ垎Mac OS X 10.3鐗堟湰銆傚湪杩欎簺鐗堟湰涓紝mysqld蹇界暐SIGHUP鍜SIGQUIT锛夈
濡傛灉鏈嶅姟鍣ㄤ娇鐢--log-error閫夐」锛屽垯FLUSH LOGS浼氬鑷撮敊璇棩蹇楄閲嶅懡鍚嶏紙浣跨敤鍚庣紑-old锛夛紝鍚屾椂mysqld浼氬垱寤轰竴涓柊鐨勭┖鏃ュ織鏂囦欢銆傚鏋滄病鏈夌粰瀹--log-error閫夐」锛屽垯涓嶄細杩涜閲嶅懡鍚嶃
路 PRIVILEGES
鐢ㄤ簬浠mysql鏁版嵁搴撲腑鐨勬巿鏉冭〃閲嶆柊杞藉叆鏉冮檺銆
路 QUERY CACHE
瀵规煡璇㈢紦瀛樿繘琛屾暣鐞嗙鐗囷紝浠ユ洿濂藉緱鍒╃敤瀛樺偍鍣ㄣ備笌RESET QUERY CACHE涓嶅悓锛屾湰璇彞涓嶄細浠庣紦瀛樹腑鍙栨秷浠讳綍鏌ヨ銆
路 STATUS
鐢ㄤ簬鎶婂鏁扮姸鎬佸彉閲忛噸鏂拌缃负闆躲傚彧鍦ㄥ綋璋冭瘯鏌ヨ鏃讹紝鎮ㄦ墠搴旇浣跨敤姝ら」銆傝鍙傝1.7.1.3鑺傦紝鈥滃浣曢氭姤缂洪櫡鍜岄棶棰樷銆
路 {TABLE | TABLES} [tbl_name [, tbl_name] ...]
褰撴病鏈夎〃琚懡鍚嶆椂锛屽叧闂墍鏈夋墦寮鐨勮〃锛屽苟杩娇鎵鏈夋鍦ㄤ娇鐢ㄧ殑琛ㄥ叧闂傝繖涔熶細鍒锋柊鏌ヨ缂撳瓨銆傛椤瑰惈鏈変竴涓垨澶氫釜琛ㄥ悕绉帮紝鍙埛鏂扮粰瀹氱殑琛ㄣ傚拰RESET QUERY CACHE璇彞涓鏍凤紝FLUSH TABLES杩樹細鍙栨秷鏉ヨ嚜鏌ヨ缂撳瓨鐨勬墍鏈夋煡璇㈢粨鏋溿
路 TABLES WITH READ LOCK
瀵逛簬鎵鏈夊甫璇诲彇閿佸畾鐨勬暟鎹簱锛屽叧闂墍鏈夋墦寮鐨勮〃锛屽苟閿佸畾鎵鏈夌殑琛紝鐩村埌鎮ㄦ墽琛UNLOCK TABLES涓烘銆傚鏋滄偍鎷ユ湁涓涓彲浠ュ強鏃惰繘琛屽揩鐓х殑鏂囦欢绯荤粺锛屾瘮濡Veritas锛屽垯杩欐槸杩涜澶囦唤鐨勯潪甯告柟渚跨殑鏂规硶銆
路 USER_RESOURCES
鐢ㄤ簬鎶婃墍鏈夋瘡灏忔椂鐢ㄦ埛璧勬簮閲嶆柊璁剧疆涓洪浂銆傝繖鍙互浣垮凡缁忚揪鍒颁簡姣忓皬鏃惰繛鎺ャ佹煡璇㈡垨鏇存柊闄愬肩殑瀹㈡埛绔珛鍒婚噸鏂版仮澶嶆椿鎬сFLUSH USER_RESOURCES涓嶉傜敤浜庡悓鏃惰繛鎺ョ殑鏈澶ч檺鍊笺傝鍙傝13.5.1.3鑺傦紝鈥淕RANT鍜孯EVOKE璇硶鈥銆
FLUSH璇彞琚啓鍏ヤ簩杩涘埗鏃ュ織锛岄櫎闈炰娇鐢ㄤ簡鑷夌殑NO_WRITE_TO_BINLOG鍏抽敭瀛楋紙鎴栧叾鍒悕LOCAL锛夈 娉ㄩ噴锛氬湪浠讳綍鎯呭喌涓嬶紝FLUSH LOGS, FLUSH MASTER, FLUSH SLAVE鍜FLUSH TABLES WITH READ LOCK閮戒笉浼氳璁板叆鏃ュ織锛屽洜涓哄鏋滃畠浠澶嶅埗鍒颁竴涓粠灞炴湇鍔″櫒涓婏紝浼氬鑷村嚭鐜伴棶棰樸
鎮ㄤ篃鍙互浣跨敤flush-hosts, flush-logs, flush-privileges, flush-status鎴flush-tables鍛戒护璁块棶鍚湁mysqladmin搴旂敤绋嬪簭鐨勮鍙ャ
娉ㄩ噴锛氬湪MySQL 5.1.2-alpha涓紝涓嶅彲鑳藉湪宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑鍙戝竷FLUSH璇彞銆備笉杩囷紝鎮ㄥ彲浠ュ湪宸插瓨鍌ㄧ殑杩囩▼涓娇鐢FLUSH锛屽彧瑕佸畠浠笉浼氫粠宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑琚皟鐢ㄣ傝鍙傝I.1鑺傦紝鈥滃瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勯檺鍒垛銆
瑕佷簡瑙f湁鍏RESET璇彞涓庡鍒跺悓鏃朵娇鐢ㄧ殑淇℃伅锛屼篃鍙互瑙13.5.5.5鑺傦紝鈥淩ESET璇硶鈥銆
KILL [CONNECTION | QUERY] thread_id
姣忎釜涓mysqld鐨勮繛鎺ラ兘鍦ㄤ竴涓嫭绔嬬殑绾跨▼閲岃繍琛岋紝鎮ㄥ彲浠ヤ娇鐢SHOW PROCESSLIST璇彞鏌ョ湅鍝簺绾跨▼姝e湪杩愯锛屽苟浣跨敤KILL thread_id璇彞缁堟涓涓嚎绋嬨
KILL鍏佽鑷夌殑CONNECTION鎴QUERY淇敼绗︼細
路 KILL CONNECTION涓庝笉鍚慨鏀圭鐨KILL涓鏍凤細瀹冧細缁堟涓庣粰瀹氱殑thread_id鏈夊叧鐨勮繛鎺ャ
路 KILL QUERY浼氱粓姝㈣繛鎺ュ綋鍓嶆鍦ㄦ墽琛岀殑璇彞锛屼絾鏄細淇濇寔杩炴帴鐨勫師鐘躲
濡傛灉鎮ㄦ嫢鏈PROCESS鏉冮檺锛屽垯鎮ㄥ彲浠ユ煡鐪嬫墍鏈夌嚎绋嬨傚鏋滄偍鎷ユ湁SUPER鏉冮檺锛屾偍鍙互缁堟鎵鏈夌嚎绋嬪拰璇彞銆傚惁鍒欙紝鎮ㄥ彧鑳芥煡鐪嬪拰缁堟鎮ㄨ嚜宸辩殑绾跨▼鍜岃鍙ャ
鎮ㄤ篃鍙互浣跨敤mysqladmin processlist鍜mysqladmin kill鍛戒护鏉ユ鏌ュ拰缁堟绾跨▼銆
娉ㄩ噴锛氭偍涓嶈兘鍚屾椂浣跨敤KILL鍜Embedded MySQL Server搴擄紝鍥犱负鍐呮鐨勬湇鍔″櫒鍙繍琛屼富鏈哄簲鐢ㄧ▼搴忕殑绾跨▼銆傚畠涓嶈兘鍒涘缓浠讳綍鑷韩鐨勮繛鎺ョ嚎绋嬨
褰撴偍杩涜涓涓KILL鏃讹紝瀵圭嚎绋嬭缃竴涓壒鏈夌殑缁堟鏍囪銆傚湪澶氭暟鎯呭喌涓嬶紝绾跨▼缁堟鍙兘瑕佽姳涓浜涙椂闂达紝杩欐槸鍥犱负缁堟鏍囪鍙細鍦ㄥ湪鐗瑰畾鐨勯棿闅旇妫鏌ワ細
路 鍦SELECT, ORDER BY鍜GROUP BY寰幆涓紝鍦ㄨ鍙栦竴缁勮鍚庢鏌ユ爣璁般傚鏋滆缃簡缁堟鏍囪锛屽垯璇ヨ鍙ヨ鏀惧純銆
路 鍦ALTER TABLE杩囩▼涓紝鍦ㄦ瘡缁勮浠庡師鏉ョ殑琛ㄤ腑琚鍙栧墠锛屾鏌ョ粓姝㈡爣璁般傚鏋滆缃簡缁堟鏍囪锛屽垯璇彞琚斁寮冿紝涓存椂琛ㄨ鍒犻櫎銆
路 鍦UPDATE鎴DELETE杩愯鏈熼棿锛屽湪姣忎釜缁勮鍙栦箣鍚庝互鍙婃瘡涓凡鏇磋鎴栧凡鍒犻櫎鐨勮涔嬪悗锛屾鏌ョ粓姝㈡爣璁般傚鏋滅粓姝㈡爣璁拌璁剧疆锛屽垯璇ヨ鍙ヨ鏀惧純銆傛敞鎰忥紝濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ簨鍔★紝鍒欏彉鏇翠笉浼氳 鍥炴粴銆
路 GET_LOCK()浼氭斁寮冨拰杩斿洖NULL銆
路 INSERT DELAYED绾跨▼浼氬揩閫熷湴鍒锋柊锛堟彃鍏ワ級瀹冨湪瀛樺偍鍣ㄤ腑鐨勬墍鏈夌殑琛岋紝鐒跺悗缁堟銆
路 濡傛灉绾跨▼鍦ㄨ〃閿佸畾绠$悊绋嬪簭涓紙鐘舵侊細閿佸畾锛夛紝鍒欒〃閿佸畾琚揩閫熷湴鏀惧純銆
路 濡傛灉鍦ㄥ啓鍏ヨ皟鐢ㄤ腑锛岀嚎绋嬫鍦ㄧ瓑寰呯┖闂茬殑纾佺洏绌洪棿锛屽垯鍐欏叆琚斁寮冿紝骞朵即闅"disk full"閿欒娑堟伅銆
路 璀﹀憡锛氬MyISAM琛ㄧ粓姝竴涓REPAIR TABLE鎴OPTIMIZE TABLE鎿嶄綔浼氬鑷村嚭鐜颁竴涓鎹熷潖鐨勬病鏈夌敤鐨勮〃銆傚杩欐牱鐨勮〃鐨勪换浣曡鍙栨垨鍐欏叆閮戒細澶辫触锛岀洿鍒版偍鍐嶆浼樺寲鎴栦慨澶嶅畠锛堜笉涓柇锛夈
LOAD INDEX INTO CACHE
tbl_index_list [, tbl_index_list] ...
tbl_index_list:
tbl_name
[[INDEX|KEY] (index_name[, index_name] ...)]
[IGNORE LEAVES]
LOAD INDEX INTO CACHE璇彞浼氭妸涓涓〃绱㈠紩棰勮浇鍏ュ埌鏌愪釜鍏抽敭缂撳瓨涓傚畠宸茬粡琚竴涓槑纭殑CACHE INDEX璇彞鍒嗛厤鍒版鍏抽敭缂撳瓨涓傛垨鑰咃紝琛ㄧ储寮曡棰勮浇鍏ュ埌榛樿鐨勫叧閿紦瀛樹腑銆LOAD INDEX INTO CACHE鍙敤浜MyISAM琛ㄣ
IGNORE LEAVES淇敼绗﹀彧浼氬鑷寸储寮曠殑闈炲彾瀛愯妭鐐硅棰勮浇鍏ャ
瀵逛簬琛t1鍜t2锛屼互涓嬭鍙ヤ細棰勮浇鍏ョ储寮曠殑鑺傜偣锛堢储寮曠粍锛夛細
mysql> LOAD INDEX INTO CACHE t1, t2 IGNORE LEAVES;
+---------+--------------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+--------------+----------+----------+
| test.t1 | preload_keys | status | OK |
| test.t2 | preload_keys | status | OK |
+---------+--------------+----------+----------+
鏈鍙ヤ細棰勮浇鍏ユ墍鏈夋潵鑷t1鐨勭储寮曠粍銆傚畠鍙杞藉叆鏉ヨ嚜t2鐨勯潪鍙跺瓙鑺傜偣鐨勭粍銆
LOAD INDEX INTO CACHE璇硶鍏佽鎮ㄦ寚瀹氾紝鍙湁鏉ヨ嚜琛ㄧ殑鐗瑰畾鐨勭储寮曞簲琚杞藉叆銆備絾鏄紝褰撳墠瀹炴柦浼氭妸鎵鏈夌殑琛ㄧ储寮曢杞藉叆缂撳瓨涓紝鎵浠ヤ竴瀹氳鎸囧畾琛ㄥ悕绉帮紝涓嶈兘鎸囧畾鍏跺畠鐨勩
RESET reset_option [, reset_option] ...
RESET璇彞琚敤浜庢竻闄や笉鍚岀殑鏈嶅姟鍣ㄦ搷浣滅殑鐘舵併傚畠涔熶綔涓FLUSH璇彞鐨勬洿寮哄ぇ鐨勭増鏈傝鍙傝13.5.5.2鑺傦紝鈥淔LUSH璇硶鈥銆
涓轰簡鎵цRESET锛屾偍蹇呴』鎷ユ湁RELOAD鏉冮檺銆
reset_option鍙互涓轰互涓嬬殑浠讳綍涓椤癸細
路 MASTER
鍙互鍒犻櫎鍒椾簬绱㈠紩鏂囦欢涓殑鎵鏈変簩杩涘埗鏃ュ織锛屾妸浜岃繘鍒舵棩蹇楃储寮曟枃浠堕噸鏂拌缃负绌猴紝骞跺垱寤轰竴涓柊鐨勪簩杩涘埗鏃ュ織鏂囦欢銆傦紙鍦ㄤ互鍓嶇増鏈殑MySQL涓紝琚О涓FLUSH MASTER銆傦級瑙13.6.1鑺傦紝鈥滅敤浜庢帶鍒朵富鏈嶅姟鍣ㄧ殑SQL璇彞鈥銆
路 QUERY CACHE
浠庢煡璇㈢紦瀛樹腑鍙栨秷鎵鏈夌殑鏌ヨ缁撴灉銆
路 SLAVE
鍙互浣夸粠灞炴湇鍔″櫒蹇樿鍏跺湪涓绘湇鍔″櫒浜岃繘鍒舵棩蹇椾腑鐨勫鍒朵綅缃紝鍙﹀锛屼篃鍙互閫氳繃鍒犻櫎鍘熸湁鐨勪腑缁ф棩蹇楁枃浠跺拰寮濮嬩竴涓柊鏂囦欢鏉ラ噸鏂拌缃腑缁ф棩蹇椼傝鍙傝13.6.2鑺傦紝鈥滅敤浜庢帶鍒朵粠鏈嶅姟鍣ㄧ殑SQL璇彞鈥銆
鍙互閫氳繃SQL鐣岄潰鎺у埗澶嶅埗銆傛湰鑺傝璁轰簡鐢ㄤ簬绠$悊涓诲鍒舵湇鍔″櫒鐨勮鍙ャ13.6.2鑺傦紝鈥滅敤浜庢帶鍒朵粠鏈嶅姟鍣ㄧ殑SQL璇彞鈥璁ㄨ浜嗙敤浜庣鐞嗕粠灞炴湇鍔″櫒鐨勮鍙ャ
PURGE {MASTER | BINARY} LOGS TO 'log_name'
PURGE {MASTER | BINARY} LOGS BEFORE 'date'
鐢ㄤ簬鍒犻櫎鍒椾簬鍦ㄦ寚瀹氱殑鏃ュ織鎴栨棩鏈熶箣鍓嶇殑鏃ュ織绱㈠紩涓殑鎵鏈変簩杩涘埗鏃ュ織銆傝繖浜涙棩蹇椾篃浼氫粠璁板綍鍦ㄦ棩蹇楃储寮曟枃浠朵腑鐨勬竻鍗曚腑琚垹闄わ紝杩欐牱琚粰瀹氱殑鏃ュ織鎴愪负绗竴涓
渚嬪锛
PURGE MASTER LOGS TO 'mysql-bin.010';
PURGE MASTER LOGS BEFORE '2003-04-02 22:46:26';
BEFORE鍙橀噺鐨date鑷彉閲忓彲浠ヤ负'YYYY-MM-DD hh:mm:ss'鏍煎紡銆MASTER鍜BINARY鏄悓涔夎瘝銆
濡傛灉鎮ㄦ湁涓涓椿鎬х殑浠庡睘鏈嶅姟鍣紝璇ユ湇鍔″櫒褰撳墠姝e湪璇诲彇鎮ㄦ鍦ㄨ瘯鍥惧垹闄ょ殑鏃ュ織涔嬩竴锛屽垯鏈鍙ヤ笉浼氳捣浣滅敤锛岃屾槸浼氬け璐ワ紝骞朵即闅忎竴涓敊璇備笉杩囷紝濡傛灉浠庡睘鏈嶅姟鍣ㄦ槸浼戞鐨勶紝骞朵笖鎮ㄧ宸ф竻鐞嗕簡鍏舵兂瑕佽鍙栫殑鏃ュ織涔嬩竴锛屽垯浠庡睘鏈嶅姟鍣ㄥ惎鍔ㄥ悗涓嶈兘澶嶅埗銆傚綋浠庡睘鏈嶅姟鍣ㄦ鍦ㄥ鍒舵椂锛屾湰璇彞鍙互瀹夊叏杩愯銆傛偍涓嶉渶瑕佸仠姝㈠畠浠
瑕佹竻鐞嗘棩蹇楋紝闇鎸夌収浠ヤ笅姝ラ锛
1. 鍦ㄦ瘡涓粠灞炴湇鍔″櫒涓婏紝浣跨敤SHOW SLAVE STATUS鏉ユ鏌ュ畠姝e湪璇诲彇鍝釜鏃ュ織銆
2. 浣跨敤SHOW MASTER LOGS鑾峰緱涓绘湇鍔″櫒涓婄殑涓绯诲垪鏃ュ織銆
3. 鍦ㄦ墍鏈夌殑浠庡睘鏈嶅姟鍣ㄤ腑鍒ゅ畾鏈鏃╃殑鏃ュ織銆傝繖涓槸鐩爣鏃ュ織銆傚鏋滄墍鏈夌殑浠庡睘鏈嶅姟鍣ㄦ槸鏇存柊鐨勶紝杩欐槸娓呭崟涓婄殑鏈鍚庝竴涓棩蹇椼
4. 鍒朵綔鎮ㄥ皢瑕佸垹闄ょ殑鎵鏈夋棩蹇楃殑澶囦唤銆傦紙杩欎釜姝ラ鏄嚜閫夌殑锛屼絾鏄缓璁噰鐢ㄣ傦級
5. 娓呯悊鎵鏈夌殑鏃ュ織锛屼絾鏄笉鍖呮嫭鐩爣鏃ュ織銆
SHOW BINLOG EVENTS
[IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
鐢ㄤ簬鍦ㄤ簩杩涘埗鏃ュ織涓樉绀轰簨浠躲傚鏋滄偍涓嶆寚瀹'log_name'锛屽垯鏄剧ず绗竴涓簩杩涘埗鏃ュ織銆
LIMIT瀛愬彞鍜SELECT璇彞鍏锋湁鐩稿悓鐨勮娉曘傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆
娉ㄩ噴锛氬綋鏈鍔″櫒鎶婁簩杩涘埗鏃ュ織鐨勫畬鏁村唴瀹癸紙璇ユ棩蹇楀寘鎷鏁扮殑鐢MySQL鎵ц鐨勬煡璇級杞偍鍒stdout鏃讹紝鍙戝竷涓涓笉鍚LIMIT瀛愬彞鐨SHOW BINLOG EVENTS鍙互鍚姩涓涓繃绋嬶紝璇ヨ繃绋嬮潪甯告秷鑰楁椂闂村苟娑堣楄祫婧銆傝鎶婁簩杩涘埗鏃ュ織淇濆瓨鍒颁竴涓枃鏈枃浠朵腑锛岀敤浜庝互鍚庣殑妫鏌ュ拰鍒嗘瀽锛岄渶浣跨敤mysqlbinlog搴旂敤绋嬪簭銆傝鍙傝8.6鑺傦紝鈥渕ysqlbinlog锛氱敤浜庡鐞嗕簩杩涘埗鏃ュ織鏂囦欢鐨勫疄鐢ㄥ伐鍏封銆
SHOW MASTER LOGS
SHOW BINARY LOGS
鐢ㄤ簬鍒楀嚭鏈嶅姟鍣ㄤ腑鐨勪簩杩涘埗鏃ュ織鏂囦欢銆傛湰璇彞琚敤浣13.6.1.1鑺傦紝鈥淧URGE MASTER LOGS璇硶鈥涓墍杩扮殑杩囩▼鐨勪竴閮ㄥ垎锛岀敤浜庣‘瀹氬摢浜涙棩蹇楀彲浠ヨ娓呯悊銆
mysql> SHOW BINARY LOGS;
+---------------+-----------+
| Log_name | File_size |
+---------------+-----------+
| binlog.000015 | 724935 |
| binlog.000016 | 733481 |
+---------------+-----------+
SHOW BINARY LOGS涓SHOW MASTER LOGS鐩稿綋銆
SHOW MASTER STATUS
鐢ㄤ簬鎻愪緵涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楁枃浠剁殑鐘舵佷俊鎭備緥濡傦細
mysql > SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73 | test | manual,mysql |
+---------------+----------+--------------+------------------+
澶嶅埗鎿嶄綔鍙互閫氳繃SQL鐣岄潰鎺у埗銆傛湰鑺傝璁轰簡鐢ㄤ簬绠$悊浠庡睘澶嶅埗鏈嶅姟鍣ㄧ殑璇彞銆13.6.1鑺傦紝鈥滅敤浜庢帶鍒朵富鏈嶅姟鍣ㄧ殑SQL璇彞鈥璁ㄨ浜嗙敤浜庣鐞嗕富鏈嶅姟鍣ㄧ殑璇彞銆
CHANGE MASTER TO master_def [, master_def] ...
master_def:
MASTER_HOST = 'host_name'
| MASTER_USER = 'user_name'
| MASTER_PASSWORD = 'password'
| MASTER_PORT = port_num
| MASTER_CONNECT_RETRY = count
| MASTER_LOG_FILE = 'master_log_name'
| MASTER_LOG_POS = master_log_pos
| RELAY_LOG_FILE = 'relay_log_name'
| RELAY_LOG_POS = relay_log_pos
| MASTER_SSL = {0|1}
| MASTER_SSL_CA = 'ca_file_name'
| MASTER_SSL_CAPATH = 'ca_directory_name'
| MASTER_SSL_CERT = 'cert_file_name'
| MASTER_SSL_KEY = 'key_file_name'
| MASTER_SSL_CIPHER = 'cipher_list'
鍙互鏇存敼浠庡睘鏈嶅姟鍣ㄧ敤浜庝笌涓绘湇鍔″櫒杩涜杩炴帴鍜岄氳鐨勫弬鏁般
MASTER_USER, MASTER_PASSWORD, MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH, MASTER_SSL_CERT, MASTER_SSL_KEY鍜MASTER_SSL_CIPHER鐢ㄤ簬鍚戜粠灞炴湇鍔″櫒鎻愪緵鏈夊叧濡備綍涓庝富鏈嶅姟鍣ㄨ繛鎺ョ殑淇℃伅銆
鍗充娇瀵逛簬鍦ㄧ紪璇戞椂娌℃湁SSL鏀寔鐨勪粠灞炴湇鍔″櫒锛SSL閫夐」(MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH, MASTER_SSL_CERT, MASTER_SSL_KEY鍜MASTER_SSL_CIPHER)涔熷彲浠ヨ鏇存敼銆傚畠浠淇濆瓨鍒master.info鏂囦欢涓紝浣嗘槸浼氳蹇界暐锛岀洿鍒版偍浣跨敤涓涓SSL鏀寔宸插惎鐢ㄧ殑鏈嶅姟鍣ㄣ
濡傛灉鎮ㄤ笉鎸囧畾涓涓粰瀹氱殑鍙傛暟锛屽垯瀹冧細淇濇寔鍏跺師鏈夌殑鍊笺備緥澶栨儏鍐靛湪鍚庨潰鐨勮璁轰腑杩涜浜嗚鏄庛備妇渚嬭鏄庯紝濡傛灉鐢ㄤ簬杩炴帴鍒版偍鐨MySQL涓绘湇鍔″櫒鐨 瀵嗙爜琚洿鏀逛簡锛屾偍鍙渶鍙戝竷杩欎簺璇彞锛屽氨鍙互鍛婄煡浠庡睘鏈嶅姟鍣ㄦ柊鐨勫瘑鐮侊細
mysql> STOP SLAVE; -- if replication was running
mysql> CHANGE MASTER TO MASTER_PASSWORD='new3cret';
mysql> START SLAVE; -- if you want to restart replication
娌℃湁蹇呰鎸囧畾娌℃湁鏀瑰彉鐨勫弬鏁帮紙涓绘満銆佹帴鍙c佺敤鎴风瓑锛夈
MASTER_HOST鍜MASTER_PORT鏄富鏈嶅姟鍣ㄤ富鏈哄拰鍏TCP/IP鎺ュ彛鐨勪富鏈哄悕锛堟垨IP鍦板潃锛夈傛敞鎰忥紝濡傛灉MASTER_HOST涓localhost鐩哥瓑锛岄偅涔堬紝鍜MySQL鐨勫叾瀹冮儴鍒嗕竴鏍凤紝鎺ュ彛鍙互琚拷鐣ワ紙渚嬪锛屽鏋滃彲浠ヤ娇鐢Unix鎻掓Ы鏂囦欢锛夈
濡傛灉鎮ㄦ寚瀹氫簡MASTER_HOST鎴MASTER_PORT锛屽垯浠庡睘鏈嶅姟鍣ㄤ細鍋囧畾涓绘湇鍔″櫒涓庝互鍓嶄笉涓鏍凤紙鍗充娇鎮ㄦ寚瀹氱殑涓绘満鎴栨帴鍙e间笌褰撳墠鍊兼槸涓鏍风殑銆傦級鍦ㄦ鎯呭喌涓嬶紝涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楃殑鍚嶇О鍜屼綅缃殑鍘熸湁鍊间笉鍐嶉傜敤锛屽洜姝わ紝濡傛灉鎮ㄤ笉鎸囧畾璇彞涓殑MASTER_LOG_FILE鍜MASTER_LOG_POS锛MASTER_LOG_FILE=''鍜MASTER_LOG_POS=4浼氳闈欓粯鍦版坊鍔犮
鍦MASTER_LOG_FILE鍜MASTER_LOG_POS鍧愭爣鐐癸紝浠庡睘鏈嶅姟鍣I/O绾跨▼鍦ㄥ惎鍔ㄤ箣鍚庝粠涓绘湇鍔″櫒璇诲彇銆傚鏋滄偍鍙寚瀹氫簡鍏朵腑涓涓紝鍒欎粠灞炴湇鍔″櫒涓嶈兘鎸囧畾RELAY_LOG_FILE鎴RELAY_LOG_POS銆傚鏋MSATER_LOG_FILE鍜MASTER_LOG_POS閮芥病鏈夎鎸囧畾锛屽垯浠庡睘鏈嶅姟鍣ㄤ細浣跨敤鍦CHANGE MASTER琚彂甯冨墠鐨勬渶鍚庝竴涓slave SQL thread鍧愭爣銆傚綋鎮ㄥ彧鎯虫敼鍙樿浣跨敤鐨 瀵嗙爜鏃讹紝杩欏彲浠ョ‘淇濆鍒剁殑杩炵画鎬с傚嵆浣夸粠灞炴湇鍔″櫒SQL绾跨▼钀藉悗浜庝粠灞炴湇鍔″櫒I/O绾跨▼锛屼篃鍙互纭繚澶嶅埗鐨勮繛缁с
CHANGE MASTER浼氬垹闄ゆ墍鏈夌殑涓户鏃ュ織鏂囦欢骞跺惎鍔ㄤ竴涓柊鐨勬棩蹇楋紝闄ら潪鎮ㄦ寚瀹氫簡RELAY_LOG_FILE鎴RELAY_LOG_POS銆傚湪姝ゆ儏鍐典笅锛屼腑缁ф棩蹇楄淇濇寔锛relay_log_purge鍏ㄥ眬鍙橀噺琚潤榛樺湴璁剧疆涓0銆
CHANGE MASTER TO鍙互鏇存柊master.info鍜relay-log.info鏂囦欢鐨勫唴瀹广
褰撴偍鎷ユ湁涓绘湇鍔″櫒蹇収骞舵嫢鏈夋棩蹇楀拰瀵瑰簲鐨勫亸绉婚噺鏃讹紝CHANGE MASTER瀵逛簬璁剧疆浠庡睘鏈嶅姟鍣ㄦ槸鏈夌敤鐨勩傚湪鎶婂揩鐓ц浇鍏ヤ粠灞炴湇鍔″櫒涔嬪悗锛屾偍鍙互鍦ㄤ粠灞炴湇鍔″櫒涓婅繍琛CHANGE MASTER TO MASTER_LOG_FILE='log_name_on_master', MASTER_LOG_POS=log_offset_on_master銆
涓句緥璇存槑锛
mysql> CHANGE MASTER TO
-> MASTER_HOST='master2.mycompany.com',
-> MASTER_USER='replication',
-> MASTER_PASSWORD='bigs3cret',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='master2-bin.001',
-> MASTER_LOG_POS=4,
-> MASTER_CONNECT_RETRY=10;
mysql> CHANGE MASTER TO
-> RELAY_LOG_FILE='slave-relay-bin.006',
-> RELAY_LOG_POS=4025;
绗竴涓緥瀛愬彲浠ユ洿鏀逛富鏈嶅姟鍣ㄥ強鍏朵簩杩涘埗鏃ュ織鍧愭爣銆傚綋鎯宠璁剧疆浠庡睘鏈嶅姟鍣ㄦ潵澶嶅埗涓绘湇鍔″櫒鏃朵娇鐢ㄣ
绗簩涓緥瀛愭樉绀轰簡杈冨皯琚娇鐢ㄧ殑涓涓搷浣溿傚綋浠庡睘鏈嶅姟鍣ㄥ惈鏈変腑缁ф棩蹇楋紝骞朵笖鎮ㄥ嚭浜庢煇绉嶅師鍥犳兂瑕佹墽琛屾鏃ュ織鏃朵娇鐢ㄣ傝杩欎箞鍋氭椂锛屼笉闇瑕佽繛鎺ヤ富鏈嶅姟鍣ㄣ傛偍鍙渶瑕佷娇鐢CHANGE MASTER TO骞跺惎鍔SQL绾跨▼锛START SLAVE SQL_THREAD锛夈
鎮ㄧ敋鑷冲彲浠ュ湪涓涓敤浜庣嫭绔嬮潪浠庡睘鏈嶅姟鍣ㄧ殑闈炲鍒跺瀷璁剧疆涓娇鐢ㄧ浜岀鎿嶄綔锛屽湪宕╂簝涔嬪悗杩涜澶嶅師銆傚亣璁炬偍鐨勬湇鍔″櫒宸插穿婧冿紝鍚屾椂鎮ㄥ凡鎭㈠浜嗗浠姐傛偍鎯宠閲嶆柊鎾斁鏈嶅姟鍣ㄨ嚜宸辩殑浜岃繘鍒舵棩蹇楋紙涓嶆槸涓户鏃ュ織锛岃屾槸姝h鐨勪簩杩涘埗鏂囦欢锛夛紝渚嬪鍚嶄负myhost-bin.*銆傞鍏堬紝搴斿湪瀹夊叏鐨勫湴鏂瑰埗浣滆繖浜涗簩杩涘埗鏃ュ織鐨勫浠斤紝浠ラ槻鎮ㄦ病鏈夊畬鍏ㄩ伒瀹堜互涓嬫楠わ紝鎰忓鍦拌鏈嶅姟鍣ㄦ竻鐞嗕簡浜岃繘鍒舵枃浠躲備娇鐢SET GLOBAL relay_log_purge=0锛岃繘涓姝ュ鍔犲畨鍏ㄦс傜劧鍚庡惎鍔ㄤ笉鍚--log-bin閫夐」鐨勬湇鍔″櫒銆備娇鐢--replicate-same-server-id, --relay-log=myhost-bin锛堣鏈嶅姟鍣ㄧ浉淇★紝杩欎簺姝h鐨勪簩杩涘埗鏃ュ織鏄腑缁ф棩蹇楋級鍜--skip-slave-start options閫夐」銆傚綋鏈嶅姟鍣ㄥ惎鍔ㄥ悗锛屽彂甯冧互涓嬭鍙ワ細
mysql> CHANGE MASTER TO
-> RELAY_LOG_FILE='myhost-bin.153',
-> RELAY_LOG_POS=410,
-> MASTER_HOST='some_dummy_string';
mysql> START SLAVE SQL_THREAD;
鏈嶅姟鍣ㄤ細璇诲彇骞舵墽琛岃嚜宸辩殑浜岃繘鍒舵棩蹇楋紝瀹屾垚宕╂簝澶嶅師銆傚綋澶嶅師瀹屾垚鍚庯紝杩愯STOP SLAVE锛屽叧闂湇鍔″櫒锛屽垹闄master.info鍜relay-log.info锛屽苟浣跨敤鍘熸潵鐨勯夐」閲嶆柊鍚姩鏈嶅姟鍣ㄣ
瑕佽鏈嶅姟鍣ㄨ涓哄畠鏄竴涓粠灞炴湇鍔″櫒锛岄渶瑕佹寚瀹MASTER_HOST锛堢敋鑷充娇鐢ㄥ亣鍊硷級銆
LOAD DATA FROM MASTER
鏈懡浠ょ敤浜庡涓绘湇鍔″櫒杩涜蹇収锛屽苟鎷疯礉鍒颁粠灞炴湇鍔″櫒涓娿傚畠鍙互鏇存柊MASTER_LOG_FILE鍜MASTER_LOG_POS鐨勫硷紝杩欐牱锛屼粠灞炴湇鍔″櫒灏卞彲浠ヤ粠姝g‘鐨勪綅缃紑濮嬭繘琛屽鍒躲備娇鐢--replicate-*-do-*鍜--replicate-*-ignore-*閫夐」鎸囧畾鐨勮〃鍜屾暟鎹簱鎺掗櫎瑙勫垯鍧囪鍏戠幇銆--replicate-rewrite-db娌℃湁琚冭檻銆傝繖鏄洜涓轰娇鐢ㄦ湰閫夐」锛岀敤鎴峰氨鍙互璁剧疆涓涓緥濡--replicate-rewrite-db=db1->db3鍜--replicate-rewrite-db=db2->db3鐨勯潪鍞竴鏄犲皠銆傚綋浠庝富鏈嶅姟鍣ㄨ浇鍏ヨ〃鏃讹紝璇ユ槧灏勪細浣夸粠灞炴湇鍔″櫒鍙戠敓娣锋穯銆
鏈鍙ョ殑浣跨敤鍙椾互涓嬫潯浠剁殑鍒剁害锛
路 鍙MyISAM琛ㄨ捣浣滅敤銆傚鏋滆瘯鍥捐浇鍏ヤ竴涓潪MyISAM琛紝浼氬鑷翠互涓嬮敊璇細
路 ERROR 1189 (08S01): Net error reading from master
路 褰撴媿鎽勫揩鐓ф椂锛屼細鑾峰緱瀵逛富鏈嶅姟鍣ㄧ殑鍏ㄥ眬璇诲彇閿佸畾銆傚湪杞藉叆鎿嶄綔鏈熼棿锛岃閿佸畾浼氶樆姝㈠涓绘湇鍔″櫒鐨勬洿鏂般
濡傛灉鎮ㄦ鍦ㄨ浇鍏ュぇ琛紝鎮ㄥ彲鑳藉繀椤诲涓绘湇鍔″櫒鍜屼粠灞炴湇鍔″櫒鍧囧鍔net_read_timeout鍜net_write_timeout鍊笺傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆
娉ㄦ剰锛LOAD DATA FROM MASTER涓嶄粠mysql鏁版嵁搴撴嫹璐濅换浣曡〃銆傝繖鍙互鏇村鏄撳湴璁╀富鏈嶅姟鍣ㄥ拰浠庡睘鏈嶅姟鍣ㄦ嫢鏈変笉鍚岀殑鐢ㄦ埛鍜屾潈闄愩
LOAD DATA FROM MASTER璇彞瑕佹眰鐢ㄤ簬杩炴帴涓绘湇鍔″櫒鐨勫鍒跺笎鎴凤紝浠ヤ究璁╀富鏈嶅姟鍣ㄦ嫢鏈RELOAD鍜SUPER鏉冮檺锛屽苟璁╂墍鏈夋偍鎯宠杞藉叆鐨勪富鏈嶅姟鍣ㄨ〃鎷ユ湁SELECT鏉冮檺銆傛墍鏈夌殑鐢ㄦ埛涓嶆嫢鏈SELECT鏉冮檺鐨勪富鏈嶅姟鍣ㄨ〃鍧囪LOAD DATA FROM MASTER蹇界暐銆傝繖鏄洜涓轰富鏈嶅姟鍣ㄤ細瀵圭敤鎴烽殣钘忓畠浠細LOAD DATA FROM MASTER浼氳皟鐢SHOW DATABASES浠ヤ簡瑙h杞藉叆鐨勪富鏈嶅姟鍣ㄦ暟鎹簱锛屼絾鏄SHOW DATABASES鍙細杩斿洖鐢ㄦ埛鏈夐儴鍒嗘潈闄愮殑鏁版嵁搴撱傝鍙傝13.5.4.6鑺傦紝鈥淪HOW DATABASES璇硶鈥銆傚湪浠庡睘鏈嶅姟鍣ㄦ柟闈紝鍙戝竷LOAD DATA FROM MASTER鐨勭敤鎴峰簲鎷ユ湁鎺堟潈锛屼互鍙栨秷鎴栧垱寤鸿澶嶅埗鐨勬暟鎹簱鍜岃〃銆
LOAD TABLE tbl_name FROM MASTER
鐢ㄤ簬鎶婅〃鐨勬嫹璐濅粠涓绘湇鍔″櫒杞Щ鍒颁粠灞炴湇鍔″櫒銆傛湰璇彞鐨勪富瑕佷綔鐢ㄦ槸璋冭瘯LOAD DATA FROM MASTER銆傚畠瑕佹眰鐢ㄤ簬杩炴帴涓绘湇鍔″櫒鐨勫笎鎴锋嫢鏈夊涓绘湇鍔″櫒鐨RELOAD鍜SUPER鏉冮檺锛屽苟鎷ユ湁瀵硅杞藉叆鐨勪富鏈嶅姟鍣ㄨ〃鐨SELECT鏉冮檺銆傚湪浠庡睘鏈嶅姟鍣ㄦ柟闈紝鍙戝竷LOAD TABLE FROM MASTER鐨勭敤鎴峰簲鎷ユ湁鍙栨秷鍜屽垱寤鸿〃鐨勬潈闄愩
鐢ㄤ簬LOAD DATA FROM MASTER鐨勬潯浠朵篃閫傜敤浜庤繖閲屻備妇渚嬭鏄庯紝LOAD TABLE FROM MASTER浠呭浜MyISAM琛ㄨ捣浣滅敤銆傚LOAD DATA FROM MASTER鐨勬殏鍋滄敞鎰忎篃閫傜敤銆
SELECT MASTER_POS_WAIT('master_log_file', master_log_pos)
杩欏疄闄呬笂鏄竴涓嚱鏁帮紝鑰屼笉鏄竴涓鍙ャ傚畠琚敤浜庣‘璁わ紝浠庡睘鏈嶅姟鍣ㄥ凡璇诲彇骞舵墽琛屼簡鍒拌揪涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楃殑缁欏畾浣嶇疆銆傝浜嗚В瀹屾暣鐨勬弿杩帮紝璇峰弬瑙12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳RESET SLAVE
鐢ㄤ簬璁╀粠灞炴湇鍔″櫒蹇樿鍏跺湪涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織涓殑澶嶅埗浣嶇疆銆傛湰璇彞琚敤浜庤繘琛屼竴涓槑纭殑鍚姩锛氬畠浼氬垹闄master.info鍜relay-log.info鏂囦欢锛屼互鍙婃墍鏈夌殑涓户鏃ュ織锛屽苟鍚姩涓涓柊鐨勪腑缁ф棩蹇椼
娉ㄩ噴锛氭墍鏈夌殑涓户鏃ュ織琚垹闄わ紝鍗充娇瀹冧滑娌℃湁琚粠灞炴湇鍔″櫒SQL绾跨▼瀹屽叏鐨勬墽琛屻傦紙濡傛灉鎮ㄥ凡缁忓彂甯冧簡涓涓SLAVE璇彞鎴栧鏋滀粠灞炴湇鍔″櫒鐨勮浇鍏ラ噺寰堝ぇ锛屽垯杩欏浜庝竴涓鍒朵粠灞炴湇鍔″櫒鏄竴涓緢鍙兘鍑虹幇鐨勬儏鍐点傦級
瀛樺偍鍦master.info鏂囦欢涓殑杩炴帴淇℃伅閫氳繃浣跨敤鍦ㄥ搴旂殑鍚姩閫夐」涓寚瀹氱殑鍊硷紝琚珛鍗抽噸鏂拌缃簡銆傛淇℃伅鍖呮嫭涓绘湇鍔″櫒涓绘満銆佷富鏈嶅姟鍣ㄦ帴鍙c佷富鏈嶅姟鍣ㄧ敤鎴峰拰涓绘湇鍔″櫒 瀵嗙爜绛夊笺傚綋浠庡睘鏈嶅姟鍣SQL绾跨▼琚腑姝㈡椂锛屽畠浣嶄簬姝e湪澶嶅埗鐨勪复鏃惰〃鐨勪腑闂达紝骞朵笖鍙戝竷浜RESET SLAVE锛屽垯宸茶澶嶅埗鐨勪复鏃惰〃鍦ㄤ粠灞炴湇鍔″櫒涓鍒犻櫎銆
SHOW SLAVE STATUS
鐢ㄤ簬鎻愪緵鏈夊叧浠庡睘鏈嶅姟鍣ㄧ嚎绋嬬殑鍏抽敭鍙傛暟鐨勪俊鎭傚鏋滄偍浣跨敤mysql瀹㈡埛绔彂甯冩璇彞锛屽垯鎮ㄥ彲浠ヤ娇鐢ㄤ竴涓\G璇彞缁堟绗︽潵鑾峰緱鏇翠究浜庨槄璇荤殑绔栧悜鐗堥潰锛岃屼笉鏄娇鐢ㄥ垎鍙凤細
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: localhost
Master_User: root
Master_Port: 3306
Connect_Retry: 3
Master_Log_File: gbichot-bin.005
Read_Master_Log_Pos: 79
Relay_Log_File: gbichot-relay-bin.005
Relay_Log_Pos: 548
Relay_Master_Log_File: gbichot-bin.005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 79
Relay_Log_Space: 552
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 8
SHOW SLAVE STATUS浼氳繑鍥炰互涓嬪瓧娈碉細
路 Slave_IO_State
SHOW PROCESSLIST杈撳嚭鐨State瀛楁鐨勬嫹璐濄SHOW PROCESSLIST鐢ㄤ簬浠庡睘I/O绾跨▼銆傚鏋滅嚎绋嬫鍦ㄨ瘯鍥捐繛鎺ュ埌涓绘湇鍔″櫒锛屾鍦ㄧ瓑寰呮潵鑷富鏈嶅姟鍣ㄧ殑鏃堕棿鎴栨鍦ㄨ繛鎺ュ埌涓绘湇鍔″櫒绛夛紝鏈鍙ヤ細閫氱煡鎮ㄣ傚湪6.3鑺傦紝鈥滃鍒跺疄鏂界粏鑺傗涓垪鍑轰簡鍙兘鐨勭姸鎬併傛棫鐗堟湰鐨MySQL鍦ㄨ繛鎺ヤ富鏈嶅姟鍣ㄤ笉鎴愬姛鏃讹紝鍏佽绾跨▼缁х画杩愯銆傚浜庢棫鐗堟湰鐨MySQL锛岃鐪嬫瀛楁鏄繀椤荤殑銆傚鏋滃畠姝e湪杩愯锛屽垯鏃犻棶棰橈紱濡傛灉瀹冩病鏈夎繍琛岋紝鍒欐偍浼氬湪Last_Error瀛楁涓彂鐜伴敊璇紙鍚庨潰鏈夎鏄庯級銆
路 Master_Host
褰撳墠鐨勪富鏈嶅姟鍣ㄤ富鏈恒
路 Master_User
琚敤浜庤繛鎺ヤ富鏈嶅姟鍣ㄧ殑褰撳墠鐢ㄦ埛銆
路 Master_Port
褰撳墠鐨勪富鏈嶅姟鍣ㄦ帴鍙c
路 Connect_Retry
--master-connect-retry閫夐」鐨勫綋鍓嶅
路 Master_Log_File
I/O绾跨▼褰撳墠姝e湪璇诲彇鐨勪富鏈嶅姟鍣ㄤ簩杩涘埗鏃ュ織鏂囦欢鐨勫悕绉般
路 Read_Master_Log_Pos
鍦ㄥ綋鍓嶇殑涓绘湇鍔″櫒浜岃繘鍒舵棩蹇椾腑锛I/O绾跨▼宸茬粡璇诲彇鐨勪綅缃
路 Relay_Log_File
SQL绾跨▼褰撳墠姝e湪璇诲彇鍜屾墽琛岀殑涓户鏃ュ織鏂囦欢鐨勫悕绉般
路 Relay_Log_Pos
鍦ㄥ綋鍓嶇殑涓户鏃ュ織涓紝SQL绾跨▼宸茶鍙栧拰鎵ц鐨勪綅缃
路 Relay_Master_Log_File
鐢SQL绾跨▼鎵ц鐨勫寘鍚鏁拌繎鏈熶簨浠剁殑涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楁枃浠剁殑鍚嶇О銆
路 Slave_IO_Running
I/O绾跨▼鏄惁琚惎鍔ㄥ苟鎴愬姛鍦拌繛鎺ュ埌涓绘湇鍔″櫒涓娿傚浜庢棫鐗堟湰鐨MySQL锛堝湪4.1.14鍜5.0.12涔嬪墠锛夛紝濡傛灉I/O绾跨▼宸茶鍚姩锛屽嵆浣夸粠灞炴湇鍔″櫒浠嶆病鏈夎繛鎺ュ埌涓绘湇鍔″櫒涓婏紝Slave_IO_Running涔熷皢琚缃埌YES銆
路 Slave_SQL_Running
SQL绾跨▼鏄惁琚惎鍔ㄣ
路 Replicate_Do_DB, Replicate_Ignore_DB
浣跨敤--replicate-do-db鍜--replicate-ignore-db閫夐」鎸囧畾鐨勬暟鎹簱娓呭崟銆
路 Replicate_Do_Table, Replicate_Ignore_Table, Replicate_Wild_Do_Table, Replicate_Wild_Ignore_Table
浣跨敤--replicate-do-table, --replicate-ignore-table, --replicate-wild-do-table鍜--replicate-wild-ignore_table閫夐」鎸囧畾鐨勮〃娓呭崟銆
路 Last_Errno, Last_Error
琚鏁版渶杩戣鎵ц鐨勬煡璇㈣繑鍥炵殑閿欒鏁伴噺鍜岄敊璇秷鎭傞敊璇暟閲忎负0骞朵笖娑堟伅涓虹┖瀛楃涓叉剰鍛崇潃鈥滄病鏈夐敊璇濄傚鏋Last_Error鍊间笉鏄┖鍊硷紝瀹冧篃浼氬湪浠庡睘鏈嶅姟鍣ㄧ殑閿欒鏃ュ織涓綔涓烘秷鎭樉绀恒
涓句緥璇存槑锛
Last_Errno: 1051
Last_Error: error 'Unknown table 'z'' on query 'drop table z'
璇ユ秷鎭寚绀猴紝琛z鏇剧粡瀛樺湪浜庡湪涓绘湇鍔″櫒涓苟宸茶鍙栨秷浜嗭紝浣嗘槸瀹冩病鏈夊湪浠庡睘鏈嶅姟鍣ㄤ腑瀛樺湪杩囷紝鍥犳瀵逛簬浠庡睘鏈嶅姟鍣紝DROP TABLE澶辫触銆傦紙涓句緥璇存槑锛屽湪璁剧疆澶嶅埗鏃讹紝濡傛灉鎮ㄥ繕璁颁簡鎶婃琛ㄦ嫹璐濆埌浠庡睘鏈嶅姟鍣ㄤ腑锛屽垯杩欐湁鍙兘鍙戠敓銆傦級
路 Skip_Counter
鏈杩戣浣跨敤鐨勭敤浜SQL_SLAVE_SKIP_COUNTER鐨勫笺
路 Exec_Master_Log_Pos
鏉ヨ嚜涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織鐨勭敱SQL绾跨▼鎵ц鐨勪笂涓涓椂闂寸殑浣嶇疆锛Relay_Master_Log_File锛夈傚湪涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織涓殑(Relay_Master_Log_File, Exec_Master_Log_Pos)瀵瑰簲浜庡湪涓户鏃ュ織涓殑(Relay_Log_File, Relay_Log_Pos)銆
路 Relay_Log_Space
鎵鏈夊師鏈夌殑涓户鏃ュ織缁撳悎璧锋潵鐨勬诲ぇ灏忋
路 Until_Condition, Until_Log_File, Until_Log_Pos
鍦START SLAVE璇彞鐨UNTIL瀛愬彞涓寚瀹氱殑鍊笺
Until_Condition鍏锋湁浠ヤ笅鍊硷細
o 濡傛灉娌℃湁鎸囧畾UNTIL瀛愬彞锛屽垯娌℃湁鍊
o 濡傛灉浠庡睘鏈嶅姟鍣ㄦ鍦ㄨ鍙栵紝鐩村埌杈惧埌涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織鐨勭粰瀹氫綅缃负姝紝鍒欏间负Master
o 濡傛灉浠庡睘鏈嶅姟鍣ㄦ鍦ㄨ鍙栵紝鐩村埌杈惧埌鍏朵腑缁ф棩蹇楃殑缁欏畾浣嶇疆涓烘锛屽垯鍊间负Relay
Until_Log_File鍜Until_Log_Pos鐢ㄤ簬鎸囩ず鏃ュ織鏂囦欢鍚嶅拰浣嶇疆鍊笺傛棩蹇楁枃浠跺悕鍜屼綅缃煎畾涔変簡SQL绾跨▼鍦ㄥ摢涓偣涓鎵ц銆
路 Master_SSL_Allowed, Master_SSL_CA_File, Master_SSL_CA_Path, Master_SSL_Cert, Master_SSL_Cipher, Master_SSL_Key
杩欎簺瀛楁鏄剧ず浜嗚浠庡睘鏈嶅姟鍣ㄤ娇鐢ㄧ殑鍙傛暟銆傝繖浜涘弬鏁扮敤浜庤繛鎺ヤ富鏈嶅姟鍣ㄣ
Master_SSL_Allowed鍏锋湁浠ヤ笅鍊硷細
o 濡傛灉鍏佽瀵逛富鏈嶅姟鍣ㄨ繘琛SSL杩炴帴锛屽垯鍊间负Yes
o 濡傛灉涓嶅厑璁稿涓绘湇鍔″櫒杩涜SSL杩炴帴锛屽垯鍊间负No
o 濡傛灉鍏佽SSL杩炴帴锛屼絾鏄粠灞炴湇鍔″櫒娌℃湁璁SSL鏀寔琚惎鐢紝鍒欏间负Ignored銆
涓SSL鏈夊叧鐨勫瓧娈电殑鍊煎搴斾簬--master-ca, --master-capath, --master-cert, --master-cipher鍜--master-key閫夐」鐨勫笺
路 Seconds_Behind_Master
鏈瓧娈垫槸浠庡睘鏈嶅姟鍣ㄢ滆惤鍚庘濆灏戠殑涓涓寚绀恒傚綋浠庡睘SQL绾跨▼姝e湪杩愯鏃讹紙澶勭悊鏇存柊锛夛紝鏈瓧娈典负鍦ㄤ富鏈嶅姟鍣ㄤ笂鐢辨绾跨▼鎵ц鐨勬渶杩戠殑涓涓簨浠剁殑鏃堕棿鏍囪寮濮嬶紝宸茬粡杩囩殑绉掓暟銆傚綋姝ょ嚎绋嬭浠庡睘鏈嶅姟鍣I/O绾跨▼璧朵笂锛屽苟杩涘叆闂茬疆鐘舵侊紝绛夊緟鏉ヨ嚜I/O绾跨▼鐨勬洿澶氱殑浜嬩欢鏃讹紝鏈瓧娈典负闆躲傛讳箣锛屾湰瀛楁娴嬮噺浠庡睘鏈嶅姟鍣SQL绾跨▼鍜屼粠灞炴湇鍔″櫒I/O绾跨▼涔嬮棿鐨勬椂闂村樊璺濓紝鍗曚綅浠ョ璁°
濡傛灉涓绘湇鍔″櫒鍜屼粠灞炴湇鍔″櫒涔嬮棿鐨勭綉缁滆繛鎺ヨ緝蹇紝鍒欎粠灞炴湇鍔″櫒I/O绾跨▼浼氶潪甯告帴杩戜富鏈嶅姟鍣紝鎵浠ユ湰瀛楁鑳藉鍗佸垎杩戜技鍦版寚绀猴紝浠庡睘鏈嶅姟鍣SQL绾跨▼姣斾富鏈嶅姟鍣ㄨ惤鍚庡灏戙傚鏋滅綉缁滆緝鎱紝鍒欒繖绉嶆寚绀轰笉鍑嗙‘锛涗粠灞SQL绾跨▼缁忓父浼氳刀涓婅鍙栭熷害杈冩參鍦颁粠灞炴湇鍔″櫒I/O绾跨▼锛屽洜姝わ紝Seconds_Behind_Master缁忓父鏄剧ず鍊间负0銆傚嵆浣I/O绾跨▼钀藉悗浜庝富鏈嶅姟鍣ㄦ椂锛屼篃鏄姝ゃ傛崲鍙ヨ瘽璇达紝鏈垪鍙閫熷害蹇殑缃戠粶鏈夌敤銆
鍗充娇涓绘湇鍔″櫒鍜屼粠灞炴湇鍔″櫒涓嶅叿鏈夌浉鍚岀殑鏃堕挓锛屾椂闂村樊璁$畻涔熶細璧蜂綔鐢紙褰撲粠灞炴湇鍔″櫒I/O绾跨▼鍚姩鏃讹紝璁$畻鏃堕棿宸傚苟鍋囧畾浠庢鏃朵互鍚庯紝鏃堕棿宸繚鎸佷笉鍙橈級銆傚鏋滀粠灞SQL绾跨▼涓嶈繍琛岋紝鎴栬呭鏋滀粠灞炴湇鍔″櫒I/O绾跨▼涓嶈繍琛屾垨鏈笌涓绘湇鍔″櫒杩炴帴锛屽垯Seconds_Behind_Master涓NULL锛堟剰涔変负鈥滄湭鐭モ濓級銆備妇渚嬭鏄庯紝濡傛灉鍦ㄩ噸鏂拌繛鎺ヤ箣鍓嶏紝浠庡睘鏈嶅姟鍣I/O绾跨▼浼戠湢浜master-connect-retry绉掞紝鍒欐樉绀NULL锛屽洜涓轰粠灞炴湇鍔″櫒涓嶇煡閬撲富鏈嶅姟鍣ㄦ鍦ㄥ仛浠涔堬紝涔熶笉鑳芥湁鎶婃彙鍦拌钀藉悗澶氬皯銆
鏈瓧娈垫湁涓涓檺鍒躲傛椂闂存爣璁伴氳繃澶嶅埗琚繚鐣欙紝杩欐剰鍛崇潃锛屽鏋滀竴涓富鏈嶅姟鍣M1鏈韩鏄竴涓粠灞炴湇鍔″櫒M0锛屽垯鏉ヨ嚜M1鐨binlog鐨勪换浣曚簨浠讹紙閫氳繃澶嶅埗鏉ヨ嚜M0鐨binlog鐨勪簨浠惰屼骇鐢燂級锛屼笌鍘熶簨浠跺叿鏈夌浉鍚岀殑鏃堕棿鏍囪銆傝繖鍙互浣MySQL鎴愬姛鍦板鍒TIMESTAMP銆備絾鏄紝Seconds_Behind_Master鐨勭己鐐规槸锛屽鏋M1涔熸敹鍒版潵鑷鎴风鐨勭洿鎺ユ洿鏂帮紝鍒欏间細闅忔満鍙樺寲锛屽洜涓烘湁鏃舵渶杩戠殑M1鏃堕棿鏉ヨ嚜M0锛屾湁鏃舵潵鑷洿鎺ユ洿鏂帮紝鏈杩戠殑鏃堕棿鏍囪涔熸槸濡傛銆
START SLAVE [thread_type [, thread_type] ... ]
START SLAVE [SQL_THREAD] UNTIL
MASTER_LOG_FILE = 'log_name', MASTER_LOG_POS = log_pos
START SLAVE [SQL_THREAD] UNTIL
RELAY_LOG_FILE = 'log_name', RELAY_LOG_POS = log_pos
thread_type: IO_THREAD | SQL_THREAD
涓嶅惈閫夐」鐨START SLAVE浼氬悓鏃跺惎鍔ㄤ袱涓粠灞炴湇鍔″櫒绾跨▼銆I/O绾跨▼浠庝富鏈嶅姟鍣ㄤ腑璇诲彇鏌ヨ锛屽苟鎶婂畠浠瓨鍌ㄥ湪涓户鏃ュ織涓SQL绾跨▼璇诲彇涓户鏃ュ織骞舵墽琛屾煡璇€START SLAVE瑕佹眰SUPER鏉冮檺銆
濡傛灉START SLAVE鎴愬姛鍦板惎鍔ㄤ簡浠庡睘鏈嶅姟鍣ㄧ嚎绋嬶紝鍒欎細杩斿洖锛屼笉浼氬嚭鐜伴敊璇備絾鏄紝鍗充娇鍦ㄦ鎯呭喌涓嬶紝涔熸湁鍙兘鍑虹幇杩欐牱鐨勭幇璞♀斺旀湇鍔″櫒绾跨▼鍚姩浜嗭紝鐒跺悗鍙堝仠姝簡锛堜緥濡傦紝鍥犱负瀹冧滑娌℃湁鎴愬姛鍦拌繛鎺ュ埌涓绘湇鍔″櫒涓婏紝鎴栬呮病鏈夎兘璇诲彇浜岃繘鍒舵棩蹇楋紝鎴栬呭嚭鐜颁簡鍏跺畠闂锛夈START SLAVE瀵规涓嶄細鍙戝嚭璀﹀憡銆傛偍蹇呴』妫鏌ヤ粠灞炴湇鍔″櫒鐨勯敊璇棩蹇楋紝鏌ョ湅鏄惁鏈夌敱浠庡睘鏈嶅姟鍣ㄧ嚎绋嬩骇鐢熺殑閿欒娑堟伅锛屾垨鑰呬娇鐢SHOW SLAVE STATUS妫鏌ュ畠浠槸鍚﹁繍琛屾甯搞
鎮ㄥ彲浠ユ妸IO_THREAD鍜SQL_THREAD閫夐」娣诲姞鍒拌鍙ヤ腑锛屾寚鏄庡摢浜涚嚎绋嬪皢瑕佸惎鍔ㄣ
鍙互娣诲姞涓涓UNTIL瀛愬彞锛屾寚瀹氫粠灞炴湇鍔″櫒搴斿惎鍔ㄥ苟杩愯锛岀洿鍒SQL绾跨▼杈惧埌涓绘湇鍔″櫒浜岃繘鍒舵棩蹇椾腑鐨勪竴涓粰瀹氱偣涓烘銆傚綋SQL绾跨▼杈惧埌姝ょ偣鏃讹紝瀹冧細鍋滄銆傚鏋滃湪璇ヨ鍙ヤ腑鎸囧畾浜SQL_THREAD閫夐」锛屽垯瀹冨彧浼氬惎鍔SQL绾跨▼銆傚惁鍒欙紝瀹冧細鍚屾椂鍚姩涓や釜浠庡睘鏈嶅姟鍣ㄧ嚎绋嬨傚鏋SQL绾跨▼姝e湪杩愯锛屽垯UNTIL瀛愬彞琚拷鐣ワ紝骞跺彂甯冧竴涓鍛娿
瀵逛簬涓涓UNTIL瀛愬彞锛屾偍蹇呴』鍚屾椂鎸囧畾涓涓棩蹇楁枃浠跺悕鍜屼綅缃備笉瑕佹妸涓绘湇鍔″櫒鍜屼腑缁ф棩蹇楅夐」娣峰悎鍦ㄤ竴璧枫
UNTIL鏉′欢鐢变竴涓悗缁殑STOP SLAVE璇彞锛屾垨涓涓笉鍖呮嫭UNTIL瀛愬彞鐨START SLAVE璇彞锛屾垨涓涓湇鍔″櫒閲嶅惎鍛戒护閲嶆柊璁剧疆銆
UNTIL瀛愬彞瀵逛簬璋冭瘯澶嶅埗鎿嶄綔鏄湁鐢ㄧ殑锛屾垨鑰呭彲鐢ㄤ簬淇冧娇澶嶅埗鎿嶄綔缁х画锛岀洿鍒版帴杩戜竴涓壒瀹氱殑鐐规椂涓烘锛屽湪姝ょ偣锛屾偍鎯宠閬垮厤璁╀粠灞炴湇鍔″櫒澶嶅埗涓涓鍙ャ備妇渚嬭鏄庯紝濡傛灉鍦ㄤ富鏈嶅姟涓婃墽琛屼簡涓涓笉鏄庢櫤鐨DROP TABLE璇彞锛屾偍鍙互浣跨敤UNTIL鏉ュ憡鐭ヤ粠灞炴湇鍔″櫒锛屾墽琛屽埌姝ょ偣灏卞仠姝紝涓嶈鍐嶇户缁簡銆傝鏌ユ壘璇ヤ簨浠舵槸浠涔堬紝闇瀵逛富鏈嶅姟鍣ㄦ棩蹇楁垨浠庡睘涓户鏃ュ織浣跨敤mysqlbinlog锛屾垨閫氳繃浣跨敤SHOW BINLOG EVENTS璇彞銆
濡傛灉鎮ㄦ鍦ㄤ娇鐢UNTIL锛岃浠庡睘鏈嶅姟鍣ㄦ垚娈靛湴澶勭悊宸插鍒剁殑鏌ヨ锛屽垯寤鸿鎮ㄤ娇鐢--skip-slave-start閫夐」鏉ュ惎鍔ㄤ粠灞炴湇鍔″櫒锛屼互闃叉褰撲粠灞炴湇鍔″櫒鍚姩鏃讹紝SQL绾跨▼杩愯銆傛渶濂藉湪涓涓夐」鏂囦欢涓娇鐢ㄦ閫夐」锛岃屼笉鏄湪鍛戒护琛屼腑浣跨敤锛岃繖鏍凤紝濡傛灉鍙戠敓浜嗘剰鏂欏鐨勬湇鍔″櫒閲嶆柊鍚姩锛屽畠涔熶笉浼氳蹇樿銆
SHOW SLAVE STATUS璇彞鍖呮嫭浜嗚緭鍑哄瓧娈点傝繖浜涘瓧娈垫樉绀轰簡UNTIL鏉′欢鐨勫綋鍓嶅笺
鍦ㄤ互鍓嶇増鏈殑MySQL涓紝鏈鍙ヨ绉颁负SLAVE START銆傚湪MySQL 5.1涓粛鐒舵帴鍙楄繖绉嶇敤娉曪紝浠ヤ究涓庝互鍓嶇増鏈吋瀹广備絾鐜板湪涓嶈禐鎴愪娇鐢ㄣ
STOP SLAVE [thread_type [, thread_type] ... ]
thread_type: IO_THREAD | SQL_THREAD
鐢ㄤ簬涓浠庡睘鏈嶅姟鍣ㄧ嚎绋嬨STOP SLAVE瑕佹眰SUPER鏉冮檺銆
鍜START SLAVE鐩镐技锛屾湰璇彞鍦ㄤ娇鐢ㄦ椂鍙互鍔IO_THREAD鍜SQL_THREAD閫夐」锛屾寚鏄庡皢琚腑姝㈢殑绾跨▼銆
鍦ㄤ互鍓嶇増鏈殑MySQL涓紝鏈鍙ヨ绉颁负SLAVE STOP銆傚湪MySQL 5.1涓粛鐒舵帴鍙楄繖绉嶇敤娉曪紝浠ヤ究涓庝互鍓嶇増鏈吋瀹广備絾鏄幇鍦ㄤ笉璧炴垚浣跨敤銆
MySQL 5.1瀵规湇鍔″櫒涓鏂圭殑棰勫埗璇彞鎻愪緵鏀寔銆傚鏋滄偍浣跨敤鍚堥傜殑瀹㈡埛绔紪绋嬬晫闈紝鍒欒繖绉嶆敮鎸佸彲浠ュ彂鎸ュ湪MySQL 4.1涓疄鏂界殑楂樻晥瀹㈡埛绔/鏈嶅姟鍣ㄤ簩杩涘埗鍗忚鐨勪紭鍔裤傚欓夌晫闈㈠寘鎷MySQL C API瀹㈡埛绔簱锛堢敤浜C绋嬪簭锛夈MySQL Connector/J锛堢敤浜Java绋嬪簭锛夊拰MySQL Connector/NET銆備緥濡傦紝C API鍙互鎻愪緵涓濂楄兘缁勬垚棰勫埗璇彞API鐨勫嚱鏁拌皟鐢ㄣ傝鍙傝25.2.4鑺傦紝鈥淐 API棰勫鐞嗚鍙モ銆傚叾瀹冭瑷鐣岄潰鍙互瀵逛娇鐢ㄤ簡浜岃繘鍒跺崗璁紙閫氳繃鍦C瀹㈡埛绔簱涓摼鎺ワ級鐨勯鍒惰鍙ユ彁渚涙敮鎸併傛湁涓涓緥瀛愭槸PHP 5.0涓殑mysqli鎵╁睍銆
瀵归鍒惰鍙ワ紝杩樻湁涓涓SQL鐣岄潰鍙互鍒╃敤銆備笌鍦ㄦ暣涓鍒惰鍙API涓娇鐢ㄤ簩杩涘埗鍗忚鐩告瘮锛屾湰鐣岄潰鏁堢巼娌℃湁閭d箞楂橈紝浣嗘槸瀹冧笉瑕佹眰缂栫▼锛屽洜涓哄湪SQL灞傜骇锛屽彲浠ョ洿鎺ュ埄鐢ㄦ湰鐣岄潰锛
路 褰撴偍鏃犳硶鍒╃敤缂栫▼鐣岄潰鏃讹紝鎮ㄥ彲浠ヤ娇鐢ㄦ湰鐣岄潰銆
路 鏈変簺绋嬪簭鍏佽鎮ㄥ彂閫SQL璇彞鍒板皢琚墽琛岀殑鏈嶅姟鍣ㄤ腑锛屾瘮濡mysql瀹㈡埛绔▼搴忋傛偍鍙互浠庤繖浜涚▼搴忎腑浣跨敤鏈晫闈€
路 鍗充娇瀹㈡埛绔鍦ㄤ娇鐢ㄦ棫鐗堟湰鐨勫鎴风搴擄紝鎮ㄤ篃鍙互浣跨敤鏈晫闈€傚敮涓鐨勮姹傛槸锛屾偍鑳藉杩炴帴鍒颁竴涓敮鎸侀鍒惰鍙SQL璇硶鐨勬湇鍔″櫒涓娿
棰勫埗璇彞鐨SQL璇硶鍦ㄤ互涓嬫儏鍐典笅浣跨敤锛
路 鍦ㄧ紪浠g爜鍓嶏紝鎮ㄦ兂瑕佹祴璇曢鍒惰鍙ュ湪鎮ㄧ殑搴旂敤绋嬪簭涓繍琛屽緱濡備綍銆傛垨鑰呬篃璁镐竴涓簲鐢ㄧ▼搴忓湪鎵ц棰勫埗璇彞鏃舵湁闂锛屾偍鎯宠纭畾闂鏄粈涔堛
路 鎮ㄦ兂瑕佸垱寤轰竴涓祴璇曟渚嬶紝璇ユ渚嬫弿杩颁簡鎮ㄤ娇鐢ㄩ鍒惰鍙ユ椂鍑虹幇鐨勯棶棰橈紝浠ヤ究鎮ㄧ紪鍒剁▼搴忛敊璇姤鍛娿
路 鎮ㄩ渶瑕佷娇鐢ㄩ鍒惰鍙ワ紝浣嗘槸鎮ㄦ棤娉曚娇鐢ㄦ敮鎸侀鍒惰鍙ョ殑缂栫▼API銆
棰勫埗璇彞鐨SQL璇硶鍩轰簬涓変釜SQL璇彞锛
PREPARE stmt_name FROM preparable_stmt;
EXECUTE stmt_name [USING @var_name [, @var_name] ...];
{DEALLOCATE | DROP} PREPARE stmt_name;
PREPARE璇彞鐢ㄤ簬棰勫涓涓鍙ワ紝骞惰祴浜堝畠鍚嶇Оstmt_name锛屽熸鍦ㄤ互鍚庡紩鐢ㄨ璇彞銆傝鍙ュ悕绉板妗堜緥涓嶆晱鎰熴preparable_stmt鍙互鏄竴涓枃瀛楀瓧绗︿覆锛屼篃鍙互鏄竴涓寘鍚簡璇彞鏂囨湰鐨勭敤鎴峰彉閲忋傝鏂囨湰蹇呴』灞曠幇涓涓崟涓鐨SQL璇彞锛岃屼笉鏄涓鍙ャ備娇鐢ㄦ湰璇彞锛屸?鈥欏瓧绗﹀彲浠ヨ鐢ㄤ簬鍒朵綔鍙傛暟锛屼互鎸囩ず褰撴偍鎵ц鏌ヨ鏃讹紝鏁版嵁鍊煎湪鍝噷涓庢煡璇㈢粨鍚堝湪涓璧枫傗?鈥欏瓧绗︿笉搴斿姞寮曞彿锛屽嵆浣挎偍鎯宠鎶婂畠浠笌瀛楃涓插肩粨鍚堝湪涓璧凤紝涔熶笉瑕佸姞寮曞彿銆傚弬鏁板埗浣滅鍙兘琚敤浜庢暟鎹煎簲璇ュ嚭鐜扮殑鍦版柟锛屼笉鐢ㄤ簬SQL鍏抽敭璇嶅拰鏍囪瘑绗︾瓑銆
濡傛灉甯︽湁姝ゅ悕绉扮殑棰勫埗璇彞宸茬粡瀛樺湪锛屽垯鍦ㄦ柊鐨勮瑷琚澶囦互鍓嶏紝瀹冧細琚殣鍚湴瑙i櫎鍒嗛厤銆傝繖鎰忓懗鐫锛屽鏋滄柊璇彞鍖呭惈涓涓敊璇苟涓斾笉鑳借棰勫锛屽垯浼氳繑鍥炰竴涓敊璇紝骞朵笖涓嶅瓨鍦ㄥ甫鏈夌粰瀹氬悕绉拌鍙ャ
棰勫埗璇彞鐨勮寖鍥存槸瀹㈡埛绔細璇濄傚湪姝や細璇濆唴锛岃鍙ヨ鍒涘缓銆傚叾瀹冨鎴风鐪嬩笉鍒板畠銆
鍦ㄩ澶囦簡涓涓鍙ュ悗锛屾偍鍙娇鐢ㄤ竴涓EXECUTE璇彞锛堣璇彞寮曠敤浜嗛鍒惰鍙ュ悕绉帮級鏉ユ墽琛屽畠銆傚鏋滈鍒惰鍙ュ寘鍚换浣曞弬鏁板埗閫犵锛屽垯鎮ㄥ繀椤绘彁渚涗竴涓垪涓句簡鐢ㄦ埛鍙橀噺锛堝叾涓寘鍚涓庡弬鏁扮粨鍚堢殑鍊硷級鐨USING瀛愬彞銆傚弬鏁板煎彧鑳芥湁鐢ㄦ埛鍙橀噺鎻愪緵锛USING瀛愬彞蹇呴』鍑嗙‘鍦版寚鏄庣敤鎴峰彉閲忋傜敤鎴峰彉閲忕殑鏁扮洰涓庤鍙ヤ腑鐨勫弬鏁板埗閫犵鐨勬暟閲忎竴鏍峰銆
鎮ㄥ彲浠ュ娆℃墽琛屼竴涓粰瀹氱殑棰勫埗璇彞锛屽湪姣忔鎵ц鍓嶏紝鎶婁笉鍚岀殑鍙橀噺浼犻掔粰瀹冿紝鎴栨妸鍙橀噺璁剧疆涓轰笉鍚岀殑鍊笺
瑕佸涓涓鍒惰鍙ヨВ闄ゅ垎閰嶏紝闇浣跨敤DEALLOCATE PREPARE璇彞銆傚皾璇曞湪瑙i櫎鍒嗛厤鍚庢墽琛屼竴涓鍒惰鍙ヤ細瀵艰嚧閿欒銆
濡傛灉鎮ㄧ粓姝簡涓涓鎴风浼氳瘽锛屽悓鏃舵病鏈夊浠ュ墠宸查鍒剁殑璇彞瑙i櫎鍒嗛厤锛屽垯鏈嶅姟鍣ㄤ細鑷姩瑙i櫎鍒嗛厤銆
浠ヤ笅SQL璇彞鍙互琚敤鍦ㄩ鍒惰鍙ヤ腑锛CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE鍜屽鏁扮殑SHOW璇彞銆傜洰鍓嶄笉鏀寔鍏跺畠璇彞銆
浠ヤ笅渚嬪瓙鏄剧ず浜嗛澶囦竴涓鍙ョ殑涓ょ鏂规硶銆傝璇彞鐢ㄤ簬鍦ㄧ粰瀹氫簡涓や釜杈圭殑闀垮害鏃讹紝璁$畻涓夎褰㈢殑鏂滆竟銆
绗竴涓緥瀛愭樉绀哄浣曢氳繃浣跨敤鏂囧瓧瀛楃涓叉潵鍒涘缓涓涓鍒惰鍙ワ紝浠ユ彁渚涜鍙ョ殑鏂囨湰锛
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET @a = 3;
mysql> SET @b = 4;
mysql> EXECUTE stmt1 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 5 |
+------------+
mysql> DEALLOCATE PREPARE stmt1;
绗簩涓緥瀛愭槸鐩镐技鐨勶紝涓嶅悓鐨勬槸鎻愪緵浜嗚鍙ョ殑鏂囨湰锛屼綔涓轰竴涓敤鎴峰彉閲忥細
mysql> SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> PREPARE stmt2 FROM @s;
mysql> SET @a = 6;
mysql> SET @b = 8;
mysql> EXECUTE stmt2 USING @a, @b;
+------------+
| hypotenuse |
+------------+
| 10 |
+------------+
mysql> DEALLOCATE PREPARE stmt2;
棰勫埗璇彞鐨SQL璇硶涓嶈兘琚敤浜庡甫宓屽鐨勯鏍间腑銆備篃灏辨槸璇达紝琚紶閫掔粰PREPARE鐨勮鍙ユ湰韬笉鑳芥槸涓涓PREPARE, EXECUTE鎴DEALLOCATE PREPARE璇彞銆
棰勫埗璇彞鐨SQL璇硶涓庝娇鐢ㄩ鍒惰鍙API璋冪敤涓嶅悓銆備緥濡傦紝鎮ㄤ笉鑳戒娇鐢mysql_stmt_prepare() C API鍑芥暟鏉ラ澶囦竴涓PREPARE, EXECUTE鎴DEALLOCATE PREPARE璇彞銆
棰勫埗璇彞鐨SQL璇硶鍙互鍦ㄥ凡瀛樺偍鐨勮繃绋嬩腑浣跨敤锛屼絾鏄笉鑳藉湪宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑浣跨敤銆
褰撲娇鐢ㄩ鍒惰鍙ユ椂锛屽彲浠ュ湪LIMIT瀛愬彞涓娇鐢ㄥ崰浣嶇銆傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆
杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆