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

Chapter聽13.聽SQL Statement Syntax - MySQL 5.1参考手册中文版

绗13绔狅細SQL璇彞璇硶

鏈珷浠嬬粛浜SQL璇彞鐨勮娉曘

13.1. 鏁版嵁瀹氫箟璇彞

13.1.1.聽ALTER DATABASE璇硶

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

13.1.2.聽ALTER TABLE璇硶

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_INCREMENTAVG_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, INSERTCREATE鏉冮檺銆

         IGNOREMySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚鏋滃湪鏂拌〃涓湁閲嶅鍏抽敭瀛楋紝鎴栬呭綋STRICT妯″紡鍚姩鍚庡嚭鐜拌鍛婏紝鍒欎娇鐢IGNORE鎺у埗ALTER TABLE鐨勮繍琛屻傚鏋滄病鏈夋寚瀹IGNORE锛屽綋閲嶅鍏抽敭瀛楅敊璇彂鐢熸椂锛屽鍒舵搷浣滆鏀惧純锛岃繑鍥炲墠涓姝ラ銆傚鏋滄寚瀹氫簡IGNORE锛屽垯瀵逛簬鏈夐噸澶嶅叧閿瓧鐨勮锛屽彧浣跨敤绗竴琛岋紝鍏跺畠鏈夊啿绐佺殑琛岃鍒犻櫎銆傚苟涓旓紝瀵归敊璇艰繘琛屼慨姝o紝浣夸箣灏介噺鎺ヨ繎姝g‘鍊笺

         鎮ㄥ彲浠ュ湪涓涓ALTER TABLE璇彞閲屽啓鍏ュ涓ADD, ALTER, DROPCHANGE瀛愬彞锛屼腑闂寸敤閫楀彿鍒嗗紑銆傝繖鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚湪鏍囧噯SQL涓紝姣忎釜ALTER TABLE璇彞涓瘡涓瓙鍙ュ彧鍏佽浣跨敤涓娆°備緥濡傦紝鍦ㄤ竴涓鍙ヤ腑鍙栨秷澶氫釜鍒楋細

                mysql> ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

         CHANGE col_name, DROP col_nameDROP INDEXMySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘

         MODIFYOracleALTER 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涓殑ADDCHANGE瀛愬彞鐩稿悓鐨勮娉曘傛敞鎰忥紝姝よ娉曞寘鎷垪鍚嶇О锛岃屼笉鍙槸鍒楃被鍨嬨傝鍙傝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;

         濡傛灉鎮ㄤ娇鐢CHANGEMODITY缂╃煭鍒楅暱鏃讹紝鍒椾腑瀛樺湪鏈夌储寮曪紝骞朵笖缂╃煭鍚庣殑鍒楅暱灏忎簬绱㈠紩闀垮害锛屽垯MySQL浼氳嚜鍔ㄧ缉鐭储寮曠殑闀垮害銆

         褰撴偍浣跨敤CHANGEMODIFY鏇存敼鍒楃殑绫诲瀷鏃讹紝MySQL浼氬敖閲忔妸鍘熸湁鐨勫垪鍊艰浆鍖栦负鏂扮殑绫诲瀷銆

         鎮ㄥ彲浠ヤ娇鐢FIRSTAFTER col_name鍦ㄤ竴涓〃琛屼腑鐨勬煇涓壒瀹氫綅缃坊鍔犲垪銆傞粯璁ゆ妸鍒楁坊鍔犲埌鏈鍚庛傛偍涔熷彲浠ュ湪CHANGEMODIFY璇彞涓娇鐢FIRSTAFTER

         AFTER COLUMN鐢ㄤ簬鎸囧畾鍒楃殑鏂伴粯璁ゅ硷紝鎴栧垹闄ゆ棫鐨勯粯璁ゅ笺傚鏋滄棫鐨勯粯璁ゅ艰鍒犻櫎鍚屾椂鍒楀间负NULL锛屽垯鏂扮殑榛樿鍊间负NULL銆傚鏋滃垪鍊间笉鑳戒负NULLMySQL浼氭寚瀹氫竴涓粯璁ゅ硷紝璇峰弬瑙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 KEYPRIMARY KEY锛屽垯UNIQUE KEYPRIMARY KEY浼氳鍌ㄥ瓨鍦ㄩ潪鍞竴绱㈠紩涔嬪墠锛岃繖鏍MySQL灏卞彲浠ュ敖鏃╁湴妫鏌ュ嚭閲嶅鍏抽敭瀛椼

         ORDER BY鐢ㄤ簬鍦ㄥ垱寤烘柊琛ㄦ椂锛岃鍚勮鎸変竴瀹氱殑椤哄簭鎺掑垪銆傛敞鎰忥紝鍦ㄦ彃鍏ュ拰鍒犻櫎鍚庯紝琛ㄤ笉浼氫粛淇濇寔姝ら『搴忋傚綋鎮ㄧ煡閬撳鏁版儏鍐典笅鎮ㄤ細鎸夌収鐗瑰畾鐨勯『搴忔煡璇㈠悇琛屾椂锛屽彲浠ヤ娇鐢ㄨ繖涓夐」锛涘湪瀵硅〃杩涜浜嗗ぇ鐨勬敼鍔ㄥ悗锛岄氳繃浣跨敤姝ら夐」锛屾偍鍙互鎻愰珮鏌ヨ鏁堢巼銆傚湪鏈変簺鎯呭喌涓嬶紝濡傛灉琛ㄦ寜鍒楁帓搴忥紝瀵逛簬MySQL鏉ヨ锛屾帓搴忓彲鑳戒細鏇寸畝鍗曘

         濡傛灉鎮ㄥ涓涓MyISAM琛ㄤ娇鐢ALTER TABLE锛屽垯鎵鏈夐潪鍞竴绱㈠紩浼氳鍒涘缓鍒颁竴涓崟鐙殑鎵归噷锛堝拰REPAIR TABLE鐩稿悓锛夈傚綋鎮ㄦ湁璁稿绱㈠紩鏃讹紝杩欐牱鍋氬彲浠ヤ娇ALTER TABLE鐨勯熷害鏇村揩銆

杩欓」鍔熻兘鍙互鏄庣‘婵娲汇ALTER TABLE...DISABLE KEYSMySQL鍋滄鏇存柊MyISAM琛ㄤ腑鐨勯潪鍞竴绱㈠紩銆傜劧鍚庝娇鐢ALTER TABLE ... ENABLE KEYS閲嶆柊鍒涘缓涓㈠け鐨勭储寮曘傝繘琛屾鎿嶄綔鏃讹紝MySQL閲囩敤涓绉嶇壒娈婄殑绠楁硶锛屾瘮涓涓帴涓涓湴鎻掑叆鍏抽敭瀛楄蹇緢澶氥傚洜姝わ紝鍦ㄨ繘琛屾垚鎵规彃鍏ユ搷浣滃墠鍏堜娇鍏抽敭瀛楃鐢ㄥ彲浠ュぇ澶у湴鍔犲揩閫熷害銆備娇鐢ALTER TABLE ... DISABLE KEYS闄や簡闇瑕佽幏寰椾互鍓嶆彁鍒扮殑鏉冮檺浠ュ锛岃繕闇瑕佽幏寰INDEX鏉冮檺銆

         Innodb瀛樺偍寮曟搸鏀寔FOREIGN KEYREFERENCES瀛愬彞銆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 DIRECTORYINDEX 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 SETALTER TABLE...CHARACTER SET鏄瓑浠风殑锛屽彧鐢ㄤ簬鏇存敼榛樿鐨勮〃瀛楃闆嗐

         濡傛灉InnoDB琛ㄥ湪鍒涘缓鏃讹紝浣跨敤浜.ibd鏂囦欢涓殑鑷繁鐨勮〃绌洪棿锛屽垯杩欐牱鐨勬枃浠跺彲浠ヨ鍒犻櫎鍜屽鍏ャ備娇鐢ㄦ璇彞鍒犻櫎.ibd鏂囦欢锛

                ALTER TABLE tbl_name DISCARD TABLESPACE;

姝よ鍙ョ敤浜庡垹闄ゅ綋鍓嶇殑.ibd鏂囦欢锛屾墍浠ュ簲棣栧厛纭鎮ㄦ湁涓涓浠姐傚鏋滃湪琛ㄧ┖闂磋鍒犻櫎鍚庡皾璇曟墦寮琛ㄦ牸锛屽垯浼氬嚭鐜伴敊璇

瑕佹妸澶囦唤鐨.ibd鏂囦欢杩樺師鍒拌〃涓紝闇鎶婃鏂囦欢澶嶅埗鍒版暟鎹簱鐩綍涓紝鐒跺悗涔﹀啓姝よ鍙ワ細

ALTER TABLE tbl_name IMPORT TABLESPACE;

15.2.6.6鑺傦紝鈥滀娇鐢ㄦ寜琛ㄧ殑琛ㄧ┖闂粹

         浣跨敤mysql_info() C API鍑芥暟锛屾偍鍙互浜嗚В鏈夊灏戣褰曞凡琚鍒讹紝浠ュ強锛堝綋浣跨敤IGNORE鏃讹級鏈夊灏戣褰曠敱浜庨噸澶嶅叧閿瓧鐨勫師鍥犲凡琚垹闄ゃ傝鍙傝25.2.3.34鑺傦紝鈥渕ysql_info()鈥

         ALTER TABLE涔熷彲浠ョ敤浜庡甯﹀垎鍖虹殑琛ㄨ繘琛岄噸鏂板垎鍖猴紝鍔熻兘鍖呮嫭娣诲姞銆佸彇娑堛佸悎骞跺拰鎷嗗垎鍚勫垎鍖猴紝杩樺彲浠ョ敤浜庤繘琛屽垎鍖虹淮鎶ゃ

瀵瑰甫鍒嗗尯鐨勮〃浣跨敤partition_options瀛愬彞鍜ALTER TABLE鍙互瀵硅〃杩涜閲嶆柊鍒嗗尯锛屼娇鐢ㄦ椂渚濇嵁partition_options瀹氫箟鐨勫垎鍖烘柟娉曘傛湰瀛愬彞浠PARTITION BY涓哄紑澶达紝鐒跺悗浣跨敤涓庣敤浜CREATE TABLEpartition_options瀛愬彞涓鏍风殑璇硶鍜岃鍒欙紙瑕佷簡瑙h缁嗕俊鎭紝璇峰弬瑙13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥锛夈傛敞閲婏細MySQL 5.1鏈嶅姟鍣ㄧ洰鍓嶆帴鍙楁璇硶锛屼絾鏄笉瀹為檯鎵ц锛涚瓑MySQL 5.1寮鍙戝嚭鏉ュ悗锛屽皢鎵ц姝よ娉曘

鐢ㄤ簬ALTER TABLE ADD PARTITIONpartition_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鐢ㄤ簬鍙栨秷涓涓垨澶氫釜RANGELIST鍒嗗尯銆傛鍛戒护涓嶈兘鐢ㄤ簬HASHKEY 鍒嗗尯锛涚敤浜庤繖涓や釜鍒嗗尯鏃讹紝搴斾娇鐢COALESCE PARTITION锛堣鍚庯級銆傚鏋滆鍙栨秷鐨勫垎鍖哄叾鍚嶇О鍒椾簬partition_names娓呭崟涓紝鍒欏偍瀛樺湪姝ゅ垎鍖轰腑鐨勬暟鎹篃琚彇娑堛備緥濡傦紝濡傛灉浠ュ墠宸插畾涔夌殑琛t1锛屾偍鍙互閲囩敤濡備笅鏂规硶鍙栨秷鍚嶇О涓p0p1鐨勫垎鍖猴細

ALTER TABLE DROP PARTITION p0, p1;

ADD PARTITIONDROP PARTITION鐩墠涓嶆敮鎸IF [NOT] EXISTS銆備篃涓嶅彲鑳藉涓涓垎鍖烘垨涓涓凡鍒嗗尯鐨勮〃杩涜閲嶅懡鍚嶃傚鏋滄偍甯屾湜瀵逛竴涓垎鍖鸿繘琛岄噸鍛藉悕锛屾偍蹇呴』鍙栨秷鍒嗗尯锛屽啀閲嶆柊寤虹珛锛涘鏋滄偍甯屾湜瀵逛竴涓凡鍒嗗尯鐨勮〃杩涜閲嶆柊鍛藉悕锛屾偍蹇呴』鍙栨秷鎵鏈夊垎鍖猴紝鐒跺悗瀵硅〃杩涜閲嶅懡鍚嶏紝鍐嶆坊鍔犺鍙栨秷鐨勫垎鍖恒

COALESCE PARTITION鍙互鐢ㄤ簬浣跨敤HASHKEY杩涜鍒嗗尯鐨勮〃锛屼互渚夸娇鐢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涓垎鍖轰腑锛堢紪鍙蜂负0123鐨勫垎鍖猴級銆

濡傛灉瑕佹洿鏀归儴鍒嗗垎鍖猴紝浣嗕笉鏇存敼鎵鏈夌殑鍒嗗尯锛屾偍鍙互浣跨敤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 TABLEREPAIR TABLE绛夊懡浠わ紙杩欎簺鍛戒护涓嶆敮鎸佺敤浜庡垎鍖鸿〃锛夋墽琛屻傝繖浜涘瓙鍙ュ寘鎷ANALYZE PARTITION, CHECK PARTITION, OPTIMIZE PARTITION, REBUILD PARTITIONREPAIR 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;

鎶婂垪aINTERGER鏇存敼涓TINYINT NOT NULL锛堝悕绉颁繚鎸佷笉鍙橈級锛屽苟鎶婂垪bCHAR(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鍒楀繀椤荤紪鍒剁储寮曘傚悓鏃舵垜浠畾涔cNOT 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寮濮嬨

A.7.1鑺傦紝鈥滀笌ALTER TABLE鏈夊叧鐨勯棶棰樷

13.1.3.聽CREATE DATABASE璇硶

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鏈嶅姟鍣ㄧ殑瀹㈡埛绔

13.1.4.聽CREATE INDEX璇硶

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,...锛夌殑涓涓垪娓呭崟鍒涘缓鍑轰竴涓鍒楃储寮曘傞氳繃涓叉帴缁欏畾鍒椾腑鐨勫硷紝纭畾绱㈠紩鍊肩殑鏍煎紡銆

瀵逛簬CHARVARCHAR鍒楋紝鍙敤涓鍒楃殑涓閮ㄥ垎灏卞彲鍒涘缓绱㈠紩銆傚垱寤虹储寮曟椂锛屼娇鐢col_name(length)璇硶锛屽鍓嶇紑缂栧埗绱㈠紩銆傚墠缂鍖呮嫭姣忓垪鍊肩殑鍓length瀛楃銆BLOBTEXT鍒椾篃鍙互缂栧埗绱㈠紩锛屼絾鏄繀椤荤粰鍑哄墠缂闀垮害銆

姝ゅ灞曠ず鐨勮鍙ョ敤浜庡垱寤轰竴涓储寮曪紝绱㈠紩浣跨敤鍒楀悕绉扮殑鍓10涓瓧绗︺

CREATE INDEX part_of_name ON customer (name(10));

鍥犱负澶氭暟鍚嶇О鐨勫墠10涓瓧绗﹂氬父涓嶅悓锛屾墍浠ユ绱㈠紩涓嶄細姣斾娇鐢ㄥ垪鐨勫叏鍚嶅垱寤虹殑绱㈠紩閫熷害鎱㈠緢澶氥傚彟澶栵紝浣跨敤鍒楃殑涓閮ㄥ垎鍒涘缓绱㈠紩鍙互浣跨储寮曟枃浠跺ぇ澶у噺灏忥紝浠庤岃妭鐪佷簡澶ч噺鐨勭鐩樼┖闂达紝鏈夊彲鑳芥彁楂INSERT鎿嶄綔鐨勯熷害銆

鍓嶇紑鏈闀夸负255瀛楄妭銆傚浜MyISAMInnoDB琛紝鍓嶇紑鏈闀夸负1000瀛楄妭銆傛敞鎰忓墠缂鐨勯檺闀夸互瀛楄妭璁★紝鑰CREATE INDEX璇彞涓殑鍓嶇紑闀垮害鎸囩殑鏄瓧绗︾殑鏁扮洰銆傚浜庝娇鐢ㄥ瀛楄妭瀛楃闆嗙殑鍒楋紝鍦ㄦ寚瀹氬垪鐨勫墠缂闀垮害鏃讹紝瑕佽冭檻杩欎竴鐐广

MySQL 5.1涓細

         鍙湁褰撴偍姝e湪浣跨敤MyISAM, InnoDBBDB琛ㄧ被鍨嬫椂锛屾偍鍙互鍚戞湁NULL鍊肩殑鍒椾腑娣诲姞绱㈠紩銆

         鍙湁褰撴偍姝e湪浣跨敤MyISAM, BDBInnoDB琛ㄧ被鍨嬫椂锛屾偍鍙互鍚BLOBTEXT鍒椾腑娣诲姞绱㈠紩銆

涓涓index_col_name瑙勭害鍙互浠ASCDESC涓虹粨灏俱傝繖浜涘叧閿瘝灏嗘潵鍙互鎵╁睍锛岀敤浜庢寚瀹氶掑鎴栭掑噺绱㈠紩鍊煎瓨鍌ㄣ傜洰鍓嶏紝杩欎簺鍏抽敭璇嶈鍒嗘瀽锛屼絾鏄蹇界暐锛涚储寮曞煎潎浠ラ掑椤哄簭瀛樺偍銆

閮ㄥ垎鍌ㄥ瓨寮曟搸鍏佽鍦ㄥ垱寤虹储寮曟椂鎸囧畾绱㈠紩绫诲瀷銆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, VARCHARTEXT鍒楃紪鍒剁储寮曪紝骞朵笖鍙兘鍦MyISAM琛ㄤ腑缂栧埗銆傝鍙傝12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解

SPATIAL绱㈠紩鍙兘瀵圭┖闂村垪缂栧埗绱㈠紩锛屽苟涓斿彧鑳藉湪MyISAM琛ㄤ腑缂栧埗銆傜┖闂村垪绫诲瀷鍦绗19绔狅細MySQL涓殑绌洪棿鎵╁睍涓繘琛屼簡鎻忚堪銆

13.1.5.聽CREATE TABLE璇硶

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琛紝瀛樺偍寮曟搸鍙互鍒涘缓鏁版嵁鍜岀储寮曟枃浠躲傚洜姝わ紝瀵逛簬姣忎釜MyISAMtbl_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

瀵逛簬MyISAMBDB琛紝鎮ㄥ彲浠ュ湪涓涓鍒楀叧閿瓧涓寚瀹氫竴涓AUTO_INCREMENT娆$骇鍒椼傝鍙傝3.6.9鑺傦紝鈥滀娇鐢ˋUTO_INCREMENT鈥

涓轰簡璁MySQL涓庨儴鍒ODBC搴旂敤杞欢鐩稿吋瀹癸紝鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬫煡璇㈡柟娉曟壘鍒版渶鍚庝竴涓彃鍏ヨ鐨AUTO_INCREMENT鍊硷細

SELECT * FROM tbl_name WHERE auto_col IS NULL

         瀛楃鍒楃殑瀹氫箟鍙互鍖呮嫭涓涓CHARACTER SET灞炴э紝鐢ㄦ潵鎸囧畾瀛楃闆嗭紝涔熷彲浠ユ寚瀹氬垪鐨勬暣搴忋傝浜嗚В璇︾粏鎯呭喌锛岃鍙傝绗10绔狅細瀛楃闆嗘敮鎸CHARSETCHARACTER 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灞炴р

BLOBTEXT鍒椾笉鑳借璧嬩簣榛樿鍊笺

濡傛灉鍦ㄥ垪瀹氫箟涓病鏈夋槑纭殑DEFAULT鍊硷紝鍒MySQL鎸夌収濡備笅瑙勫垯纭畾榛樿鍊硷細

濡傛灉鍒楀彲浠ヤ娇鐢NULL浣滀负鍊硷紝鍒欎娇鐢DEFAULT NULL瀛愬彞瀵瑰垪杩涜瀹氫箟銆傦紙鍦MySQL鐨勬棭鏈熺増鏈腑涔熷姝ゃ傦級

濡傛灉鍒椾笉鑳戒娇鐢NULL浣滀负鍊硷紝鍒MySQL瀵瑰垪杩涜瀹氫箟鏃朵笉浣跨敤DEFAULT瀛愬彞銆傝緭鍏ユ暟鎹椂锛屽鏋INSERTREPLACE璇彞涓嶅寘鎷垪鐨勫硷紝鍒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));

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

瀵逛簬涓涓粰瀹氱殑琛紝鎮ㄥ彲浠ヤ娇鐢SHOW CREATE TABLE璇彞鏉ユ煡鐪嬮偅浜涘垪鏈夋槑纭殑DEFAULT瀛愬彞銆

         瀵逛簬鍒楃殑璇勬敞鍙互浣跨敤COMMENT閫夐」鏉ヨ繘琛屾寚瀹氥傝瘎娉ㄩ氳繃SHOW CREATE TABLESHOW 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 NULLMySQL搴旈殣鍚湴瀹氫箟杩欎簺鍒椼備竴涓〃鍙湁涓涓PRIMARY KEY銆傚鏋滄偍娌℃湁PRIMARY KEY骞朵笖涓涓簲鐢ㄧ▼搴忚姹傚湪琛ㄤ腑浣跨敤PRIMARY KEY锛屽垯MySQL杩斿洖绗竴涓UNIQUE绱㈠紩锛屾绱㈠紩娌℃湁浣滀负PRIMARY KEYNULL鍒椼

         鍦ㄥ凡鍒涘缓鐨勮〃涓紝PRIMARY KEY鐨勪綅缃渶闈犲墠锛岀劧鍚庢槸鎵鏈夌殑UNIQUE绱㈠紩锛岀劧鍚庢槸闈炲敮涓绱㈠紩銆傝繖鍙互甯姪MySQL浼樺寲绋嬪簭閫夋嫨浼樺厛浣跨敤鍝釜绱㈠紩锛屽苟涓旀洿蹇熺殑妫娴嬪嚭閲嶅鐨UNIQUE鍏抽敭瀛椼

         PRIMARY KEY鍙互鏄竴涓鍒楃储寮曘備絾鏄紝鍦ㄥ垪瑙勭害涓娇鐢PRIMARY KEY鍏抽敭瀛楀睘鎬ф棤娉曞垱寤哄鍒楃储寮曘傝繖涔堝仛鍙兘鎶婁竴涓垪鏍囪涓轰富鍒椼傛偍蹇呴』浣跨敤涓涓崟鐙殑PRIMARY KEYindex_col_name, ...锛夊瓙鍙ャ

         濡傛灉PRIMARY KEYUNIQUE绱㈠紩鍙寘鎷竴涓垪锛屽苟涓旀鍒椾负鏁存暟绫诲瀷锛屽垯鎮ㄤ篃鍙互鍦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涓紝鍙湁MyISAMInnoDB, BDBMEMORY瀛樺偍寮曟搸鏀寔鍦ㄥ惈鏈NULL鍊肩殑鍒椾腑缂栫储寮曘傚湪鍏跺畠鎯呭喌涓嬶紝鎮ㄥ繀椤诲畾涔夊凡缂栫储寮曠殑鍒椾负NOT NULL锛屽惁鍒欎細鍑虹幇閿欒銆

         鍦ㄤ竴涓储寮曡绾︿腑浣跨敤col_name(length)璇硶锛屾偍鍙互鍒涘缓涓涓储寮曪紝姝ょ储寮曞彧浣跨敤涓涓CHARVARCHAR鍒楃殑绗竴涓length瀛楃銆傚彧瀵瑰垪鍊肩殑鍓嶇紑缂栧埗绱㈠紩鍙互浣跨储寮曟枃浠跺ぇ澶у噺灏忋傝鍙傝7.4.3鑺傦紝鈥滃垪绱㈠紩鈥

MyISAMInnoDB瀛樺偍寮曟搸涔熸敮鎸佸BLOBTEXT鍒楃紪绱㈠紩銆傚綋瀵BLOBTEXT鍒楃紪绱㈠紩鏃讹紝鎮ㄥ繀椤讳负绱㈠紩鎸囧畾涓涓墠缂闀垮害銆備緥濡傦細

CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));

瀵逛簬MyISAMInnoDB琛紝鍓嶇紑鏈闀垮彲浠ヤ负1000瀛楄妭锛屽浜庡叾瀹冭〃鏍肩被鍨嬶紝鏈闀垮彲浠ヤ负255瀛楄妭銆傛敞鎰忓墠缂闀垮害闄愬间互瀛楄妭涓哄崟浣嶏紝鑰屽湪CREATE TABLE璇彞涓殑鍓嶇紑闀垮害鐢ㄥ瓧绗︽暟鐩潵琛ㄨ堪銆傚綋涓轰竴涓娇鐢ㄥ瀛楄妭瀛楃闆嗙殑鍒楁寚瀹氬墠缂闀垮害鏃讹紝涓瀹氳鑰冭檻鍒拌繖涓鐐广

         涓涓index_col_name瑙勭害鍙互浠ASCDESC缁撳熬銆傝繖浜涘叧閿瘝鍙互鍦ㄥ皢鏉ヨ繘琛屾墿灞曪紝鐢ㄤ簬鎸囧畾鍗囧簭鎴栭檷搴忕殑绱㈠紩鍊煎瓨鍌ㄣ傚綋鍓嶏紝杩欎簺鍏抽敭璇嶈鍒嗘瀽浣嗘槸琚拷鐣ワ紱绱㈠紩鍊煎潎浠ュ崌搴忓偍瀛樸

         褰撴偍鍦SELECT涓殑TEXT鍒楁垨BLOB鍒椾腑浣跨敤ORDER BYGROUP BY鏃讹紝鏈嶅姟鍣ㄥ彧浣跨敤鍒濆鐨勫瓧鑺傛暟鐩鍊艰繘琛屽垎绫汇傚瓧鑺傛暟鐩敱max_sort_length绯荤粺鍙橀噺杩涜鎸囩ず銆傝鍙傝11.4.3鑺傦紝鈥淏LOB鍜孴EXT绫诲瀷

         鎮ㄥ彲浠ュ垱寤虹壒娈婄殑FULLTEXT绱㈠紩锛岀敤浜庡叏鏂囨悳绱€傚彧鏈MyISAM琛ㄧ被鍨嬫敮鎸FULLTEXT绱㈠紩銆FULLTEXT绱㈠紩鍙彲浠ヤ粠CHAR, VARCHARTEXT鍒椾腑鍒涘缓銆傛暣涓垪閮戒細琚紪鍏ョ储寮曪紱涓嶆敮鎸佸閮ㄥ垎鍒楃紪绱㈠紩銆傚鏋滃凡鎸囧畾锛屽墠缂闀垮害浼氳蹇界暐銆傝浜嗚В杩愯鐨勮缁嗚鏄庯紝璇峰弬瑙12.7鑺傦紝鈥滃叏鏂囨悳绱㈠姛鑳解

         鎮ㄥ彲浠ヤ负绌洪棿鍒楃被鍨嬪垱寤SPATIAL绱㈠紩銆傚彧鏈MyISAM琛ㄦ敮鎸佺┖闂寸被鍨嬶紝宸茬紪绱㈠紩鐨勫垪蹇呴』澹版槑涓NOT NULL銆傝鍙傝绗19绔狅細MySQL涓殑绌洪棿鎵╁睍

         InnoDB琛ㄦ敮鎸佸澶栭敭闄愬埗鏉′欢杩涜妫鏌ャ傝鍙傝15.2鑺傦紝鈥淚nnoDB瀛樺偍寮曟搸鈥銆傛敞鎰忥紝鍦InnoDB涓紝FOREIGN KEY璇硶姣旀湰鑺傚紑濮嬫椂浠嬬粛鐨CREATE TABLE璇彞鐨勮娉曟洿涓ユ牸锛氳寮曠敤鐨勮〃涓殑鍒楀繀椤绘湁鏄庣‘鐨勫懡鍚嶃InnoDB鏀寔澶栭敭鐨ON DELETEON UPDATE涓ょ鎿嶄綔銆傛湁鍏崇簿纭娉曠殑璇存槑锛岃鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥

瀵逛簬鍏跺畠瀛樺偍寮曟搸锛MySQL鏈嶅姟鍣ㄥCREATE TABLE璇彞涓殑FOREIGN KEYREFERENCES璇硶杩涜鍒嗘瀽锛屼絾涓嶉噰鍙栬繘涓姝ョ殑琛屽姩銆傛墍鏈夌殑瀛樺偍寮曟搸鍧囧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_flag1銆傞潤鎬佽〃鍦ㄨ璁板綍涓娇鐢ㄤ竴浣嶇敤浣滀綅鏍囪銆備綅鏍囪鎸囩ず璇ヨ鏄惁宸茶鍒犻櫎銆傚浜庡姩鎬佽〃锛delete_flag0锛屽洜涓哄湪鍔ㄦ佽鏍囬涓凡瀛樺偍浜嗕綅鏍囪銆

杩欎簺璁$畻鏂规硶涓嶉傜敤浜InnoDB琛ㄣ傚浜InnoDB琛紝NULL鍒楃殑瀛樺偍閲忎笌NOT NULL鍒楃殑瀛樺偍閲忔病鏈夊尯鍒

ENGINETYPE閫夐」鐢ㄤ簬涓鸿〃鎸囧畾瀛樺偍寮曟搸銆ENGINE鏄閫夌殑閫夐」鍚嶇О銆

ENGINETYPE閫夐」閲囩敤浠ヤ笅鍊硷細

瀛樺偍寮曟搸

璇存槑

ARCHIVE

妗f瀛樺偍寮曟搸銆傝鍙傝15.8鑺傦紝鈥淎RCHIVE瀛樺偍寮曟搸鈥

BDB

甯﹂〉闈㈤攣瀹氱殑浜嬪姟瀹夊叏琛ㄣ備篃绉颁负BerkeleyDB銆傝鍙傝15.5鑺傦紝鈥淏DB (BerkeleyDB)瀛樺偍寮曟搸鈥

CSV

鍊间箣闂寸敤閫楀彿闅斿紑鐨勮〃銆傝鍙傝15.9鑺傦紝鈥淐SV瀛樺偍寮曟搸

EXAMPLE

绀轰緥寮曟搸銆傝鍙傝15.6鑺傦紝鈥淓XAMPLE瀛樺偍寮曟搸鈥

FEDERATED

鍙互璁块棶杩滅▼琛ㄧ殑瀛樺偍寮曟搸銆傝鍙傝15.7鑺傦紝鈥淔EDERATED瀛樺偍寮曟搸鈥

HEAP

15.4鑺傦紝鈥淢EMORY (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

鎴愮皣琛紝瀹归敊琛紝浠ュ瓨鍌ㄥ櫒涓哄熀纭鐨勮〃銆備篃绉颁负NDB銆傝鍙傝绗17绔狅細MySQL绨

瑕佷簡瑙f湁鍏MySQL瀛樺偍寮曟搸鐨勬洿澶氫俊鎭紝璇峰弬瑙绗15绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

濡傛灉琚寚瀹氱殑瀛樺偍寮曟搸鏃犳硶鍒╃敤锛屽垯MySQL浣跨敤MyISAM浠f浛銆備緥濡傦紝涓涓〃瀹氫箟鍖呮嫭ENGINE=BDB閫夐」锛屼絾鏄MySQL鏈嶅姟鍣ㄤ笉鏀寔BDB琛紝鍒欒〃琚垱寤轰负MyISAM琛ㄣ傝繖鏍凤紝濡傛灉鎮ㄥ湪涓绘満涓婃湁浜嬪姟琛紝浣嗗湪浠庡睘鏈轰笂鍒涘缓鐨勬槸闈炰氦浜掑紡琛紙浠ュ姞蹇熷害锛夋椂锛屽彲浠ヨ繘琛屽鍒惰缃傚湪MySQL 5.1涓紝濡傛灉娌℃湁閬靛畧瀛樺偍寮曟搸瑙勭害锛屽垯浼氬嚭鐜拌鍛娿

鍏跺畠琛ㄩ夐」鐢ㄤ簬浼樺寲琛ㄧ殑鎬ц川銆傚湪澶氭暟鎯呭喌涓嬶紝鎮ㄤ笉蹇呮寚瀹氳〃閫夐」銆傝繖浜涢夐」閫傜敤浜庢墍鏈夊瓨鍌ㄥ紩鎿庯紝鍙︽湁璇存槑闄ゅ锛

         AUTO_INCREMENT

琛ㄧ殑鍒濆AUTO_INCREMENT鍊笺傚湪MySQL 5.1涓紝鏈夐」鍙傜敤浜MyISAMMEMORY琛ㄣInnoDB涔熸敮鎸佹湰閫夐」銆傚鏋滃紩鎿庝笉鏀寔AUTO_INCREMENT琛ㄩ夐」锛屽垯瑕佽缃紩鎿庣殑绗竴涓auto-increment鍊硷紝闇鎻掑叆涓涓滃亣鈥濊銆傝琛岀殑鍊兼瘮鍒涘缓琛ㄥ悗鐨勫煎皬涓锛岀劧鍚庡垹闄よ鍋囪銆

瀵逛簬鍦CREATE TABLE璇彞涓敮鎸AUTO_INCREMENT琛ㄩ夐」鐨勫紩鎿庯紝鎮ㄤ篃鍙互浣跨敤ALTER TABLE tbl_name AUTO_INCREMENT = n鏉ラ噸鏂拌缃AUTO_INCREMENT鍊笺

         AVG_ROW_LENGTH

琛ㄤ腑骞冲潎琛岄暱搴︾殑杩戜技鍊笺傚彧闇瑕佸鍚昂瀵稿彲鍙樼殑璁板綍鐨勫ぇ鍨嬭〃杩涜姝ら」璁剧疆銆

褰撳垱寤轰竴涓MyISAM琛ㄦ椂锛MySQL浣跨敤MAX_ROWSAVG_ROW_LENGTH閫夐」鐨勪箻绉潵纭畾寰楀嚭鐨勮〃鏈夊澶с傚鏋滄湁涓涓夐」鏈寚瀹氾紝鍒欒〃鐨勬渶澶у昂瀵镐负65,536TB鏁版嵁銆傦紙濡傛灉鎿嶄綔绯荤粺涓嶆敮鎸佽繖涔堝ぇ鐨勬枃浠讹紝鍒欒〃鐨勫昂瀵歌闄愬畾鍦ㄦ搷浣滅郴缁熺殑闄愬煎銆傦級濡傛灉鎮ㄦ兂缂╁皬鎸囬拡灏哄浣跨储寮曟洿灏忥紝閫熷害鏇村揩锛屽苟涓旀偍涓嶉渶瑕佸ぇ鏂囦欢锛屽垯鎮ㄥ彲浠ラ氳繃璁剧疆myisam_data_pointer_size绯荤粺鍙橀噺鏉ュ噺灏戦粯璁ゆ寚閽堢殑灏哄銆傦紙瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆傦級濡傛灉鎮ㄥ笇鏈涙墍鏈夌殑琛ㄥ彲浠ユ墿澶э紝瓒呰繃榛樿闄愬硷紝骞朵笖鎰挎剰璁╄〃绋嶅井鎱㈢偣锛屽苟绋嶅井澶х偣锛屽垯鎮ㄥ彲浠ラ氳繃璁剧疆姝ゅ彉閲忓鍔犻粯璁ゆ寚閽堢殑灏哄銆

         [DEFAULT] CHARACTER SET

鐢ㄤ簬涓鸿〃鎸囧畾涓涓粯璁ゅ瓧绗﹂泦銆CHARSETCHARACTER 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鏃讹紝瀛樺偍寮曟搸鍙帇缂╅暱鐨CHARVARCHAR鍒楋紙浠呴檺浜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琛ㄣ傚浜庨潤鎬佽鎴栭暱搴﹀彲鍙樿锛屾閫夐」鍊煎彲浠ヤ负FIXEDDYNAMICmyisampack鐢ㄤ簬鎶婄被鍨嬭缃负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

褰撴偍鎯宠鎶婁竴缁勭浉鍚岀殑琛ㄥ綋浣滀竴涓〃浣跨敤鏃讹紝閲囩敤UNIONUNION浠呴傜敤浜MERGE琛ㄣ傝鍙傝15.3鑺傦紝鈥淢ERGE瀛樺偍寮曟搸鈥

瀵逛簬鎮ㄦ槧灏勫埌涓涓MERGE琛ㄤ笂鐨勮〃锛屾偍蹇呴』鎷ユ湁SELECT, UPDATEDELETE鏉冮檺銆傦紙娉ㄩ噴锛氫互鍓嶏紝鎵鏈夎浣跨敤鐨勮〃蹇呴』浣嶄簬鍚屼竴涓暟鎹簱涓紝骞朵綔涓MERGE琛ㄣ傝繖浜涢檺鍒朵笉鍐嶉傜敤銆傦級

         INSERT_METHOD

濡傛灉鎮ㄥ笇鏈涘湪MERGE琛ㄤ腑鎻掑叆鏁版嵁锛屾偍蹇呴』鐢INSERT_METHOD鎸囧畾搴旀彃鍏ヨ鐨勮〃銆INSERT_METHOD閫夐」浠呯敤浜MERGE琛ㄣ備娇鐢FIRSTLAST鎶婅鎻掑叆鍒扮涓涓垨鏈鍚庝竴涓〃涓紱鎴栬呬娇鐢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瀛愬彞銆傛湰瀛愬彞鍖呭惈鐢ㄤ簬纭畾鍒嗗尯鐨勫嚱鏁帮紱璇ュ嚱鏁颁細杩斿洖涓涓暣鍊硷紝鑼冨洿浠1num銆傛澶num涓哄垎鍖虹殑鏁扮洰銆傛鍑芥暟涓彲浠ヤ娇鐢ㄧ殑閫夐」鏄剧ず鍦ㄤ笅闈㈢殑娓呭崟涓 瑕佺偣锛氬湪鏈妭寮濮嬫椂浠嬬粛鐨勭敤浜partition_options鐨勮娉曚腑鏄剧ず鐨勯夐」锛屽苟涓嶆槸閮借兘鐢ㄤ簬鎵鏈夊垎鍖虹被鍨嬨傝浜嗚В鍚勭绫诲瀷鍏蜂綋鐨勪俊鎭 锛岃鍙傝浠ヤ笅鍚勭被鍨嬬殑娓呭崟銆傝浜嗚В鏈夊叧鍦MySQL涓殑鍒嗗尯鐨勬搷浣滃拰浣跨敤鎯呭喌鐨勫叏闈㈣鏄庯紝浠ュ強瑕佷簡瑙h〃鍒涘缓鐨勭ず渚嬪拰涓MySQL鍒嗗尯鏈夊叧鐨勫叾瀹冨懡浠わ紝璇峰弬瑙绗18绔狅細鍒嗗尯

o        HASHexpr锛夛細鐢ㄤ簬娣风紪涓涓垨澶氫釜鍒楋紝鍒涘缓涓涓叧閿瓧锛岀敤浜庢斁缃锛屽苟纭畾琛岀殑浣嶇疆銆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 THANVALUES 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 THANVALUES 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, JavaPHP涔熷姝わ級銆備篃灏辨槸璇达紝瀛愬彞蹇呴』鎸夌収杩欐牱涓绉嶆柟娉曟帓鍒楋紝姣忎竴涓悗缁殑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 THANVALUES 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瀛愬彞璇存槑鐨勫垎鍖虹殑鎬绘暟鐩哥瓑銆

娉ㄩ噴锛氫笉璁烘偍鍦ㄥ垱寤轰竴涓敱RANGELIST杩涜鍒嗗尯鐨勮〃鏃舵槸鍚︿娇鐢ㄤ簡PARTITIONS瀛愬彞锛屾偍蹇呴』鍦ㄨ〃瀹氫箟涓寘鎷嚦灏戜竴涓PARTITION VALUES锛堣鍚庯級銆

o        涓涓垎鍖哄彲浠ヨ嚜閫夊垎闅旀垚澶氫釜瀛愬垎鍖恒備娇鐢ㄨ嚜閫夌殑SUBPARTITION BY瀛愬彞鍙互鎸囩ず銆傚瓙鍒嗗尯鍙互鐢HASHKEY杩涜鍒嗛殧銆備袱绉嶆柟娉曞缓绔嬬殑瀛愬垎鍖哄潎涓LINEAR銆傚垎闅斿瓙鍒嗗尯鏃剁殑鎿嶄綔鏂瑰紡涓庝互鍓嶆弿杩扮殑鍒嗗尯绫诲瀷鐨勬搷浣滄柟寮忎竴鏍枫傦紙鏃犳硶鐢LISTRANGE杩涜瀛愬垎鍖哄垎闅斻傦級

浣跨敤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 DIRECTORYINDEX DIRECTORY鍙互琚敤浜庢寚绀烘湰鍒嗗尯鐨勬暟鎹拰绱㈠紩鍚勮嚜鐨勫瓨鍌ㄤ綅缃殑鐩綍銆data_dirindex_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 DIRECTORYINDEX DIRECTORY鐨勬搷浣滄柟娉曚笌CREATE TABLE璇彞涓殑table_option瀛愬彞鐨勬搷浣滄柟娉曚竴鏍枫傛table_option瀛愬彞鐢ㄤ簬浣嶄簬MyISAM琛ㄧ鐞嗙▼搴忎笅鐨勫悇琛ㄣ

鍙互涓烘瘡涓垎鍖烘寚瀹氫竴涓暟鎹洰褰曞拰涓涓储寮曠洰褰曘傚鏋滀笉鎸囧畾锛屽垯鏁版嵁鍜岀储寮曡瀛樺偍鍦ㄩ粯璁ょ殑MySQL鏁版嵁鐩綍涓

o        MAX_ROWSMIN_ROWS鍒嗗埆鐢ㄤ簬灏嗚瀛樺偍鍦ㄥ垎鍖轰腑鐨勮鏁扮洰鏈澶у煎拰琛屾暟鐩渶灏忓笺max_number_of_rowsmin_number_of_rows鐨勫蹇呴』涓烘鏁存暟銆傚拰鍏锋湁鍚屾牱鍚嶇О鐨勬闈㈤夐」涓鏍凤紝max_number_of_rowsmin_number_of_rows鍙綔涓哄鏈嶅姟鍣ㄧ殑鈥滃缓璁濆硷紝骞朵笉鏄‖鎬ч檺鍊笺

o        鑷夌殑TABLESPACE瀛愬彞鍙互鐢ㄤ簬涓哄垎鍖烘寚瀹氫竴涓闈㈢┖闂淬備粎鐢ㄤ簬MySQL Cluster

o        鑷夌殑[STORAGE] ENGINE瀛愬彞鍙互鎶婃湰鍒嗗尯涓〃鐨勭被鍨嬫敼涓烘寚瀹氱殑绫诲瀷銆傝〃鐨勭被鍨嬪彲浠ユ槸鏈MySQL鏈嶅姟鍣ㄦ敮鎸佺殑鎵鏈夌被鍨嬨STORAGE鍏抽敭瀛楀拰绛夊彿(=)鍧囦负鑷夐」銆傚鏋滄病鏈変娇鐢ㄦ閫夐」璁剧疆鍒嗗尯瀛樺偍寮曟搸锛屽垯閫傜敤浜庢暣涓〃鐨勫紩鎿庡彲浠ョ敤浜庢鍒嗗尯銆

娉ㄩ噴锛氬垎鍖虹鐞嗙▼搴忓浜PARTITIONSUBPARTITION鍧囨帴鍙[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鍏抽敭璇嶅锛岀敤浜庡瓙鍒嗗尯瀹氫箟鐨勮娉曚笌鐢ㄤ簬鍒嗗尯瀹氫箟鐨勮娉曚竴鏍枫

瀛愬垎鍖哄繀椤荤敱HASHKEY瀹屾垚锛屽苟涓斿彧鑳藉RANGELIST鍒嗗尯杩涜瀛愬垎鍖恒傝鍙傝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 DIRECTORYINDEX DIRECTORY琛ㄩ夐」銆

鎮ㄥ彲浠ュ湪SELECT鍓嶅鍔IGNOREREPLACE锛屾寚绀哄浣曞澶嶅埗鍞竴鍏抽敭瀛楀肩殑璁板綍杩涜鎿嶇旱銆備娇鐢IGNORE鍚庯紝濡傛灉鏂拌褰曞鍒朵簡鍘熸湁鐨勫敮涓鍏抽敭瀛楀肩殑璁板綍锛屽垯鏂拌褰曡涓㈠純銆備娇鐢REPLACE鍚庯紝鏂拌褰曟浛鎹㈠叿鏈夌浉鍚岀殑鍞竴鍏抽敭瀛楀肩殑璁板綍銆傚鏋滄病鏈夋寚瀹IGNOREREPLACE锛屽垯鍑虹幇澶氶噸鍞竴鍏抽敭瀛楀兼椂浼氬鑷村彂鐢熼敊璇

涓轰簡纭繚鏇存柊鏃ュ織/浜岃繘浣嶆棩蹇楀彲浠ヨ鐢ㄤ簬鍐嶆鍒涘缓鍘熻〃锛MySQL涓嶅厑璁稿湪CREATE TABLE...SELECT杩囩▼涓繘琛岃仈鍚堟彃鍏ャ

13.1.5.1. 娌夊瘋鐨勫垪瑙勬牸鍙樻洿

鍦ㄦ湁浜涙儏鍐典笅锛岃緝鏃╃増鏈殑MySQL浼氶潤榛樺湴鏇存敼鍦CREATE TABLEALTER TABLE璇彞涓粰瀹氱殑鍒楄绾︺傚湪MySQL 5.1涓笉浼氳繘琛岃繖绫诲彉鏇淬傚鏋滀娇鐢ㄦ寚瀹氱殑鏁版嵁绫诲瀷鏃犳硶鍒涘缓鍒楋紝鍒欎細鍑虹幇閿欒銆

13.1.6.聽DROP DATABASE璇硶

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鏈嶅姟鍣ㄧ殑瀹㈡埛绔

13.1.7.聽DROP INDEX璇硶

DROP INDEX index_name ON tbl_name

DROP INDEX鐢ㄤ簬浠庤〃tbl_name涓彇娑堝悕绉颁负index_name鐨勭储寮曘傛湰璇彞琚槧灏勫埌涓涓ALTER TABLE璇彞涓紝鐢ㄤ簬鍙栨秷绱㈠紩銆傝鍙傝13.1.2鑺傦紝鈥淎LTER TABLE璇硶鈥

13.1.8.聽DROP 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璇硶鈥

RESTRICTCASCADE鍙互浣垮垎鍖烘洿瀹规槗銆傜洰鍓嶏紝RESTRICTCASCADE涓嶈捣浣滅敤銆

娉ㄩ噴锛氶櫎闈炴偍浣跨敤TEMPORARY鍏抽敭璇嶏紝DROP TABLE浼氳嚜鍔ㄦ彁浜ゅ綋鍓嶇殑鏈夋晥鐨勪簨鍔°

TEMPORARY鍏抽敭璇嶅叿鏈変互涓嬩綔鐢細

         璇彞鍙彇娑TEMPORARY琛ㄣ

         璇彞涓嶄細缁堟姝e湪杩涜涓殑浜嬪姟銆

         涓嶄細鏌ラ獙瀛樺彇鏉冦傦紙TEMPORARY琛ㄤ粎瀵逛簬鍒涘缓璇ヨ〃鐨勫鎴风鏄彲瑙佺殑锛屾墍浠ユ煡楠屾槸涓嶅繀瑕佺殑銆傦級

浣跨敤TEMPORARY鏄‘淇濇偍涓嶄細鎰忓鍙栨秷涓涓潪TEMPORARY琛ㄧ殑鑹ソ鏂规硶銆

13.1.9.聽RENAME TABLE璇硶

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鏃讹紝鎮ㄤ笉鑳芥湁琚攣瀹氱殑琛紝涔熶笉鑳芥湁澶勪簬娲绘х姸鎬佺殑浜嬪姟銆傛偍杩樺繀椤绘嫢鏈夊師琛ㄧ殑ALTERDROP鏉冮檺锛屼互鍙婃柊琛ㄧ殑CREATEINSERT鏉冮檺銆

濡傛灉MySQL瀵瑰涓〃杩涜閲嶅懡鍚嶆椂閬囧埌浜嗛敊璇紝MySQL浼氬鎵鏈夊凡琚噸鍛藉悕鐨勮〃杩涜鍙嶅悜閲嶅懡鍚嶏紝杩斿洖鍒板師鏉ョ殑鐘舵併

鍙鎮ㄤ笉灏濊瘯閫氳繃閲嶅懡鍚嶆妸瑙嗗浘鍔犲叆鍙︿竴涓暟鎹簱涓紝鍒RENAME TABLE涔熷彲浠ョ敤浜庤鍥俱

13.2. 鏁版嵁鎿嶄綔璇彞

13.2.1.聽DELETE璇硶

鍗曡〃璇硶锛

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瀛愬彞锛夊垹闄よ〃涓殑鎵鏈夎锛屽垯瀵逛簬鎵鏈夌殑琛ㄧ被鍨嬶紙闄InnoDBMyISAM澶栵級锛屽簭鍒楅噸鏂扮紪鎺掋傚浜InnoDB琛紝姝ら」鎿嶄綔鏈変竴浜涗緥澶栵紝鍦15.2.6.3鑺傦紝鈥淎UTO_INCREMENT鍒楀浣曞湪InnoDB涓繍琛屸涓繘琛屼簡璁ㄨ銆

瀵逛簬MyISAMBDB琛紝鎮ㄥ彲浠ユ妸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鍙互鍔犲揩閫熷害銆傝繖鏍峰仛鍙互閲嶆柊寤虹珛绱㈠紩锛岃屼笉鏄繘琛屽ぇ閲忕殑绱㈠紩鍧楀悎骞舵搷浣溿

鐢ㄤ簬DELETEMySQL鍞竴鐨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 BYLIMIT

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

鐩墠锛屾偍涓嶈兘浠庝竴涓〃涓垹闄わ紝鍚屾椂鍙堝湪瀛愭煡璇腑浠庡悓涓涓〃涓夋嫨銆

13.2.2.聽DO璇硶

DO expr [, expr] ...
DO鐢ㄤ簬鎵ц琛ㄨ揪寮忥紝浣嗘槸涓嶈繑鍥炰换浣曠粨鏋溿DOSELECT expr鐨勭畝鍖栬〃杈炬柟寮DO鏈変竴涓紭鍔匡紝灏辨槸濡傛灉鎮ㄤ笉澶叧蹇冪粨鏋滅殑璇濓紝DO鐨勯熷害绋嶅揩銆

DO涓昏鐢ㄤ簬鎵ц鏈夊壇浣滅敤鐨勫嚱鏁帮紝姣斿RELEASE_LOCK()

13.2.3.聽HANDLER璇硶

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鍙互鐢ㄤ簬MyISAMInnoDB琛ㄣ

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...NEXTHANDLER...PREV鎵弿涓

浣跨敤HANDLER鎺ュ彛浠f浛甯歌鐨SELECT璇彞鏈夊涓師鍥狅細

         HANDLERSELECT鏇村揩锛

o        涓涓寚瀹氱殑瀛樺偍寮曟搸绠$悊绋嬪簭鐩爣涓轰簡HANDLER...OPEN杩涜鏁村簭銆傝鐩爣琚噸鏂扮敤浜庤琛ㄧ殑鍚庣画鐨HANDLER璇彞锛涗笉闇瑕佸姣忎釜璇彞杩涜閲嶆柊鍒濆鍖栥

o        娑夊強鐨勫垎鏋愯緝灏戙

o        娌℃湁浼樺寲绋嬪簭鎴栨煡璇㈡牎楠屽紑閿銆

o        鍦ㄤ袱涓鐞嗙▼搴忚姹備箣闂达紝涓嶉渶瑕侀攣瀹氳〃銆

o        绠$悊绋嬪簭鎺ュ彛涓嶉渶瑕佹彁渚涘瑙備竴鑷寸殑鏁版嵁锛堜緥濡傦紝鍏佽鏃犳潯鐞嗙殑璇诲彇锛夛紝鎵浠ュ瓨鍌ㄥ紩鎿庡彲浠ヤ娇鐢ㄤ紭鍖栵紝鑰SELECT閫氬父涓嶅厑璁镐娇鐢ㄤ紭鍖栥

         鏈変簺搴旂敤绋嬪簭浣跨敤涓ISAM杩戜技鐨勬帴鍙d笌MySQL杩炴帴銆備娇鐢HANDLER鍙互鏇村鏄撳湴涓庤繖浜涘簲鐢ㄧ▼搴忚繛鎺ャ

         HANDLER鍏佽鎮ㄩ噰鐢ㄤ竴绉嶇壒娈婄殑鏂瑰紡杩涘嚭鏁版嵁搴撱傝屼娇鐢SELECT鏃堕毦浠ラ噰鐢紙鎴栦笉鍙兘閲囩敤锛夎繖绉嶆柟寮忋傛湁浜涘簲鐢ㄧ▼搴忓彲浠ユ彁渚涗竴涓氦浜掑紡鐨勭敤鎴锋帴鍙d笌鏁版嵁搴撹繛鎺ャ傚綋涓庤繖浜涘簲鐢ㄧ▼搴忓悓鏃朵娇鐢ㄦ椂锛岀敤HANDLER鎺ュ彛瑙傜湅鏁版嵁鏇村姞鑷劧銆

13.2.4.聽INSERT璇硶

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...VALUESINSERT...SET褰㈠紡鐨勮鍙ユ牴鎹槑纭寚瀹氱殑鍊兼彃鍏ヨ銆INSERT...SELECT褰㈠紡鐨勮鍙ユ彃鍏ヤ粠鍏跺畠琛ㄤ腑閫夊嚭鐨勮銆傚湪13.2.4.1鑺傦紝鈥淚NSERT ... SELECT璇硶鈥涓INSERT...SELECT杩涜浜嗚繘涓姝ョ殑璁ㄨ銆

琛屽簲琚彃鍏ュ埌tbl_name琛ㄤ腑銆傚彲浠ユ寜浠ヤ笅鏂规硶鎸囧畾鍒椼傛湰璇彞鍚戣繖浜涘垪鎻愪緵鍊笺

         鍒楀悕绉版竻鍗曟垨SET瀛愬彞鏄庣‘鐨勬寚绀轰簡鍒椼

         濡傛灉鎮ㄤ笉涓INSERT...VALUESINSERT...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'锛屾彃鍏ュ煎垎鍒槸199919.992119.9921001999銆傚瓨鍌ㄥ湪INTYEAR鍒椾腑鐨勫间负1999鐨勫師鍥犳槸锛屽湪浠庡瓧绗︿覆鍒版暣鏁扮殑杞寲涓紝鍙妸瀛楃涓茬殑鍓嶉潰閮ㄥ垎鐪嬩綔鏈夋晥鐨勬暣鏁版垨骞翠唤銆傚浜庢诞鐐瑰垪鍜屽浐瀹氱偣鍒楋紝鍦ㄤ粠瀛楃涓插埌娴偣鐨勮浆鍖栦腑锛屾妸鏁翠釜瀛楃涓插潎鐪嬩綔鏈夋晥鐨勬诞鐐瑰笺

琛ㄨ揪寮expr鍙互寮曠敤鍦ㄥ兼竻鍗曚腑宸茶缃殑鎵鏈夊垪銆備緥濡傦紝鎮ㄥ彲浠ヨ繖涔堟搷浣滐紝鍥犱负鐢ㄤ簬col2鐨勫煎紩鐢ㄤ簡col1锛岃col1宸茬粡琚祴鍊硷細

mysql> INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);

浣嗘槸浠ヤ笅璇彞涓嶅悎娉曪紝鍥犱负鐢ㄤ簬col1鐨勫煎紩鐢ㄤ簡col2锛岃col2col1涔嬪悗琚祴鍊硷細

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, TEXTBLOB锛変腑鎻掑叆鐨勫瓧绗︿覆瓒呰繃浜嗗垪鐨勬渶澶ч暱搴︺傛鍊艰鍒犺妭鍒板垪鐨勬渶澶ч暱搴︺

         鍚戞棩鏈熸垨鏃堕棿鍒椾腑鎻掑叆鐨勫煎浜庤鍒楃殑绫诲瀷鏄笉鍚堟硶鐨勩傛牴鎹垪鐨勭被鍨嬶紝璇ュ垪琚缃埌鐩稿簲鐨勯浂鍊笺

濡傛灉鎮ㄦ鍦ㄤ娇鐢C API锛屽垯鍙互閫氳繃璋冪敤mysql_info()鍑芥暟鑾峰彇淇℃伅瀛楃涓层傝鍙傝25.2.3.34鑺傦紝鈥渕ysql_info()鈥

13.2.4.1.聽INSERT ... SELECT璇硶

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锛岀敤浜庡拷鐣ヤ細瀵艰嚧閲嶅鍏抽敭瀛楅敊璇殑璁板綍銆

         涓嶈鍚屾椂浣跨敤DELAYEDINSERT...SELECT

         INSERT璇彞鐨勭洰鏍囪〃浼氭樉绀哄湪鏌ヨ鐨SELECT閮ㄥ垎鐨FROM瀛愬彞涓傦紙鍦ㄦ湁浜涙棫鐗堟湰鐨MySQL涓笉浼氬嚭鐜拌繖绉嶆儏鍐点傦級

         AUTO_INCREMENT鍒楃収甯歌繍琛屻

         涓轰簡纭繚浜岃繘鍒舵棩蹇楀彲浠ヨ鐢ㄤ簬鍐嶆鍒涘缓鍘熻〃锛MySQL涓嶅厑璁稿湪INSERT...SELECT杩愯鏈熼棿鍚屾椂杩涜鎻掑叆鎿嶄綔銆

         鐩墠锛屾偍涓嶈兘鍦ㄥ悜涓涓〃鎻掑叆鐨勫悓鏃讹紝鍙堝湪涓涓瓙鏌ヨ涓粠鍚屼竴涓〃涓夋嫨銆

ON DUPLICATE KEY UPDATE鐨勫奸儴鍒嗕腑锛屽彧瑕佹偍涓嶄娇鐢SELECT閮ㄥ垎涓殑GROUP BY锛屾偍灏卞彲浠ュ紩鐢ㄥ湪鍏跺畠琛ㄤ腑鐨勫垪銆傛湁涓涓壇浣滅敤鏄紝鎮ㄥ繀椤讳娇鍊奸儴鍒嗕腑鐨勯潪鍞竴鍒楃殑鍚嶇О绗﹀悎瑕佹眰銆

鎮ㄥ彲浠ヤ娇鐢REPLACE鏇夸唬INSERT锛屾潵瑕嗙洊鏃ц銆傚浜庡寘鍚敮涓鍏抽敭瀛楀硷紝骞跺鍒朵簡鏃ц鐨勬柊琛岋紝鍦ㄨ繘琛屽鐞嗘椂锛REPLACE鍙互浣滀负INSERT IGNORE鐨勫悓绫诲瓙鍙ワ細鏂拌琚敤浜庢浛鎹㈡棫琛岋紝鑰屼笉鏄涓㈠純銆

13.2.4.2.聽INSERT DELAYED璇硶

INSERT DELAYED ...

鐢ㄤ簬INSERT璇彞鐨DELAYED閫夐」鏄MySQL鐩稿浜庢爣鍑SQL鐨勬墿灞曘傚鏋滄偍鐨勫鎴风涓嶈兘绛夊緟INSERT瀹屾垚锛屽垯杩欎釜閫夐」鏄潪甯告湁鐢ㄧ殑銆傚綋鎮ㄤ娇鐢MySQL杩涜鏃ュ織缂栧啓鏃讹紝杩欐槸闈炲父甯歌鐨勯棶棰樸傛偍涔熷彲浠ュ畾鏈熻繍琛SELECTUPDATE璇彞锛岃繖浜涜鍙ヨ姳璐圭殑鏃堕棿杈冮暱銆

褰撲竴涓鎴风浣跨敤INSERT DELAYED鏃讹紝浼氱珛鍒讳粠鏈嶅姟鍣ㄥ寰楀埌涓涓‘瀹氥傚苟涓旇琚帓鍏ラ槦鍒楋紝褰撹〃娌℃湁琚叾瀹冪嚎绋嬩娇鐢ㄦ椂锛屾琛岃鎻掑叆銆

浣跨敤INSERT DELAYED鐨勫彟涓涓噸瑕佺殑濂藉鏄紝鏉ヨ嚜璁稿瀹㈡埛绔殑鎻掑叆琚泦涓湪涓璧凤紝骞惰缂栧啓鍏ヤ竴涓潡銆傝繖姣旀墽琛岃澶氱嫭绔嬬殑鎻掑叆瑕佸揩寰堝銆

浣跨敤DELAYED鏃舵湁涓浜涢檺鍒讹細

         INSERT DELAYED浠呴傜敤浜MyISAM, MEMORYARCHIVE琛ㄣ傚浜MyISAM琛紝濡傛灉鍦ㄦ暟鎹枃浠剁殑涓棿娌℃湁绌洪棽鐨勫潡锛屽垯鏀寔鍚屾椂閲囩敤SELECTINSERT璇彞銆傚湪杩欎簺鎯呭喌涓嬶紝鍩烘湰涓嶉渶瑕佸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鎰忓鍋滄锛屽垯鎵鏈夋病鏈夎鍐欏叆纾佺洏鐨勮閮戒細涓㈠け銆

浠ヤ笅璇︾粏鎻忚堪浜嗗綋鎮ㄥINSERTREPLACE浣跨敤DELAYED閫夐」鏃朵細鍙戠敓浠涔堟儏鍐点傚湪杩欎簺鎻忚堪涓紝鈥滅嚎绋嬧濇寚鐨勬槸宸叉帴鍙椾簡涓涓INSERT DELAYED璇彞鐨勭嚎绋嬶紝鈥滅鐞嗙▼搴忊濇寚鐨勬槸涓烘煇涓壒瀹氱殑琛ㄥ鐞嗘墍鏈INSERT DELAYED璇彞鐨勭嚎绋嬨

         褰撲竴涓嚎绋嬪涓涓〃鎵цDELAYED璇彞鏃讹紝浼氬垱寤哄嚭涓涓鐞嗙▼搴忕嚎绋嬶紙濡傛灉鍘熸潵涓嶅瓨鍦級锛屽鐢ㄤ簬鏈〃鐨勬墍鏈DELAYED璇彞杩涜澶勭悊銆

         绾跨▼浼氭鏌ユ槸鍚︾鐞嗙▼搴忎互鍓嶅凡鑾峰彇浜DELAYED閿佸畾锛涘鏋滄病鏈夎幏鍙栵紝鍒欏憡鐭ョ鐞嗙▼搴忕嚎绋嬭繘琛屾椤规搷浣溿傚嵆浣垮叾瀹冪嚎绋嬪琛ㄦ湁READWRITE閿佸畾锛屼篃鍙互鑾峰緱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

13.2.5.聽LOAD DATA INFILE璇硶

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湁鍏INSERTLOAD DATA INFILE鐨勬晥鐜囩殑瀵规瘮鍜屾湁鍏LOAD DATA INFILE鍔犻熺殑鏇村淇℃伅锛岃鍙傝7.2.16鑺傦紝鈥淚NSERT璇彞鐨勯熷害鈥

character_set_database绯荤粺鍙橀噺鎸囩ず鐨勫瓧绗﹂泦琚敤浜庤В閲婃枃浠朵腑鐨勪俊鎭SET NAMEScharacter_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鏉冮檺銆

5.7.3鑺傦紝鈥淢ySQL鎻愪緵鐨勬潈闄愨

涓庤鏈嶅姟鍣ㄧ洿鎺ヨ鍙栨枃浠剁浉姣旓紝浣跨敤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

鏈変簺杈撳叆璁板綍鎶婂師鏈夌殑璁板綍澶嶅埗鍒板敮涓鍏抽敭瀛楀间笂銆REPLACEIGNORE鍏抽敭瀛楃敤浜庢帶鍒惰繖浜涜緭鍏ヨ褰曠殑鎿嶄綔銆

濡傛灉鎮ㄦ寚瀹氫簡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 INFILESELECT...INTO OUTFILE鐨勮ˉ璇傦紙瑙13.2.7鑺傦紝鈥淪ELECT璇硶鈥銆傦級瑕佷粠涓涓〃涓妸鏁版嵁鍐欏叆涓涓枃浠朵腑锛屽簲浣跨敤SELECT...INTO OUTFILE銆傝璇诲彇鏂囦欢锛屾斁鍥炲埌琛ㄤ腑锛屽簲浣跨敤LOAD DATA INFILEFIELDSLINES瀛愬彞鐨勮娉曞浜庝袱涓鍙ユ槸涓鏍风殑銆備袱涓瓙鍙ラ兘鏄嚜閫夌殑锛屼絾鏄鏋滀袱涓兘琚寚瀹氫簡锛FIELDS蹇呴』浣嶄簬LINES鐨勫墠闈€

濡傛灉鎮ㄦ寚瀹氫簡涓涓FIELDS瀛愬彞锛屽垯姣忎釜浜氬瓙鍙ワ紙TERMINATED BY, [OPTIONALLY] ENCLOSED BYESCAPED 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 OUTFILELOAD 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 BYFIELDS ESCAPED BY鍊煎繀椤讳负鍗曚竴瀛楃銆FIELDS TERMINATED BY, LINES STARTING BYLINES 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, TEXTENUM锛夌殑鍒椾腑鐨勫硷細

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 BYLINES TERMINATED BY鍊肩殑绗竴涓瓧绗

         ASCII 0锛堝湪杞箟绗︿箣鍚庣紪鍐欑殑瀛楃瀹為檯涓婃槸ASCII0鈥欙紝鑰屼笉鏄竴涓间负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 BYFIELDS ENCLOSED BY鍊煎潎涓虹┖鍊('')锛屽垯浣跨敤鍥哄畾琛岋紙鏃犲垎闅旓級鏍煎紡銆備娇鐢ㄥ浐瀹氳鏍煎紡鏃讹紝鍦ㄥ瓧娈典箣闂翠笉浣跨敤鍒嗛殧绗︼紙浣嗘槸鎮ㄤ粛鐒跺彲浠ユ湁琛岀粓姝㈢锛夈傚垪鍊间娇鐢ㄥ垪鐨勬樉绀哄搴﹁繘琛屽啓鍏ュ拰璇诲彇銆備緥濡傦紝濡傛灉鏌愬垪琚畾涔変负INT(7)锛屽垯浣跨敤7瀛楃瀛楁鍐欏叆鍒楀笺傝緭鍑烘椂锛岄氳繃璇诲彇7涓瓧绗﹁幏鍙栧垪鍊笺

LINES TERMINATED BY浠嶇劧鐢ㄤ簬鍒嗛殧琛屻傚鏋滄煇琛屼笉鍖呭惈鎵鏈夊瓧娈碉紝鍒欏叾浣欑殑鍚勫垪琚缃埌榛樿鍊笺傚鏋滄偍娌℃湁琛岀粓姝㈢锛屾偍搴旇鎶婄粓姝㈢璁剧疆涓''銆傚湪姝ゆ儏鍐典笅锛屾枃鏈枃浠跺繀椤诲寘鍚瘡琛岀殑鎵鏈夊瓧娈点

鍥哄畾琛屾牸寮忎篃浼氬奖鍝NULL鍊肩殑鎿嶄綔锛岃繖灏嗗湪浠ュ悗杩涜浠嬬粛銆傛敞鎰忥紝濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓瀛楄妭瀛楃闆嗭紝鍒欏浐瀹氳鏍兼牸寮忎笉浼氳繍琛屻

鏍规嵁姝e湪浣跨敤涓殑FIELDSLINES閫夐」鐨勪笉鍚岋紝NULL鍊肩殑鎿嶄綔鏈夋墍鍙樺寲锛

         瀵逛簬榛樿鐨FIELDSLINES鍊硷紝NULL琚綔涓\N鐨勫瓧娈靛肩紪鍐欙紝鐢ㄤ簬杈撳嚭锛\N瀛楁鍊艰浣滀负NULL璇诲彇锛岀敤浜庤緭鍏ワ紙鍋囪ESCAPED BY瀛楃涓衡\鈥欙級銆

         濡傛灉FIELDS ENCLOSED BY涓嶆槸绌哄硷紝鍒欏寘鍚互鏂囧瓧璇嶈NULL涓哄肩殑瀛楁琚綔涓NULL鍊艰鍙栥傝繖涓庤FIELDS ENCLOSED BY瀛楃鍖呭洿鐨勮瘝璇NULL涓嶅悓銆傝璇嶈琚綔涓哄瓧绗︿覆'NULL'璇诲彇銆

         濡傛灉FIELDS ESCAPED BY鏄┖鍊硷紝鍒NULL琚綔涓鸿瘝璇NULL鍐欏叆銆

         閲囩敤鍥哄畾琛屾牸寮忔椂锛堝綋FIELDS TERMINATED BYFIELDS ENCLOSED BY鍧囦负绌哄兼椂閲囩敤锛夛紝NULL琚綔涓轰竴涓┖瀛楃涓插啓鍏ャ傛敞鎰忥紝杩欎細瀵艰嚧鍦ㄨ鍐欏叆鏂囦欢鏃讹紝琛ㄤ腑鐨NULL鍊煎拰绌哄瓧绗︿覆鍧囨棤娉曡鲸鍒紝杩欐槸鍥犱负涓よ呴兘琚綔涓虹┖瀛楃涓插啓鍏ャ傚鏋滄偍闇瑕佸湪璇诲彇鏂囦欢骞惰繑鍥炴椂鑳藉鍒嗚鲸涓よ咃紝鍒欐偍涓嶅簲浣跨敤鍥哄畾琛屾牸寮忋

LOAD DATA INFILE涓嶆敮鎸佹湁浜涙儏鍐碉細

         鍥哄畾瑙勬牸琛岋紙FIELDS TERMINATED BYFIELDS ENCLOSED BY鍧囦负绌哄硷級鍜BLOBTEXT鍒椼

         濡傛灉鎮ㄦ寚瀹氫簡涓涓垎闅旂锛屽苟涓旇鍒嗛殧绗︿笌鍏跺畠鐨勫墠缂涓鏍凤紝鍒LOAD DATA INFILE涓嶈兘姝g‘鍦扮悊瑙h緭鍏ュ笺備緥濡傦紝涓嬮潰鐨FIELDS瀛愬彞浼氬鑷撮棶棰橈細

                FIELDS TERMINATED BY '"' ENCLOSED BY '"'

         濡傛灉FIELDS ESCAPED BY涓虹┖鍊硷紝鍒欏寘鍚FIELDS ENCLOSED BYLINES 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 INSERTAFTER INSERT瑙﹀彂鍣紝鍒欏湪鎻掑叆琛屼箣鍓嶅拰鎻掑叆琛屼箣鍚庡垎鍒惎鍔ㄨЕ鍙戝櫒銆

濡傛灉涓涓緭鍏ヨ鍚湁杩囧鐨勫瓧娈碉紝鍒欏浣欑殑瀛楁琚拷鐣ワ紝骞朵笖璀﹀憡鐨勬暟閲忓鍔犮

濡傛灉涓涓緭鍏ヨ鍚湁鐨勫瓧娈佃繃灏戯紝鍒欒緭鍏ュ瓧娈电己澶辩殑琛ㄤ腑鐨勫垪琚缃负榛樿鍊笺傞粯璁ゅ艰祴鍊煎湪13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥涓繘琛屼簡璇存槑銆

濡傛灉瀛楁鍊肩己澶憋紝鍒欏涓涓┖瀛楁鍊间細琚寜涓嶅悓鏂瑰紡鐞嗚В锛

         瀵逛簬瀛楃涓茬被鍨嬶紝鍒楄璁剧疆涓虹┖瀛楃涓层

         瀵逛簬鏁板瓧绫诲瀷锛屽垪琚缃负0

         瀵逛簬鏃ユ湡鍜屾椂闂寸被鍨嬶紝鍒楄璁剧疆涓鸿绫诲瀷鐩稿簲鐨勨zero鈥濄傝鍙傝11.3鑺傦紝鈥滄棩鏈熷拰鏃堕棿绫诲瀷鈥

濡傛灉鎮ㄦ槑纭湴鎶婁竴涓┖瀛楃涓茶祴浜堜竴涓INSERTUPDATE璇彞涓殑瀛楃涓茬被鍨嬨佹暟瀛楃被鍨嬫垨鏃ユ湡鎴栨椂闂寸被鍨嬶紝鍒欎骇鐢熺殑杩欎簺鍊肩浉鍚屻

鍙湁鍦ㄤ袱绉嶆儏鍐典笅TIMESTAMP鍒楄璁剧疆涓哄綋鍓嶆棩鏈熷拰鏃堕棿銆備竴绉嶆儏鍐垫椂褰撳垪鏈変竴涓NULL鍊硷紙涔熷氨鏄\N锛夋椂锛涘彟涓绉嶆儏鍐垫槸锛堜粎瀵逛簬绗竴涓TIMESTAMP鍒楋級锛屽綋涓涓瓧娈垫竻鍗曡鎸囧畾鏃讹紝TIMESTAMP鍒椾細浠庡瓧娈垫竻鍗曚腑琚暐鍘汇

LOAD DATA INFILE鎶婃墍鏈夌殑杈撳叆鍊煎綋浣滃瓧绗︿覆锛屾墍浠ユ偍涓嶈兘鎸夌収浣跨敤INSERT璇彞鐨勬柟寮忎娇鐢ENUMSET鍒楃殑鏁板瓧鍊笺傛墍鏈夌殑ENUMSET鍊煎繀椤昏鎸囧畾涓哄瓧绗︿覆銆

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璇硶鈥

13.2.6.聽REPLACE璇硶

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 KEYUNIQUE绱㈠紩锛屽惁鍒欙紝浣跨敤涓涓REPLACE璇彞娌℃湁鎰忎箟銆傝璇彞浼氫笌INSERT鐩稿悓锛屽洜涓烘病鏈夌储寮曡鐢ㄤ簬纭畾鏄惁鏂拌澶嶅埗浜嗗叾瀹冪殑琛屻

鎵鏈夊垪鐨勫煎潎鍙栬嚜鍦REPLACE璇彞涓鎸囧畾鐨勫笺傛墍鏈夌己澶辩殑鍒楄璁剧疆涓哄悇鑷殑榛樿鍊硷紝杩欏拰INSERT涓鏍枫傛偍涓嶈兘浠庡綋鍓嶈涓紩鐢ㄥ硷紝涔熶笉鑳藉湪鏂拌涓娇鐢ㄥ笺傚鏋滄偍浣跨敤涓涓緥濡傗SET col_name = col_name + 1鈥濈殑璧嬪硷紝鍒欏浣嶄簬鍙充晶鐨勫垪鍚嶇О鐨勫紩鐢ㄤ細琚綔涓DEFAULT(col_name)澶勭悊銆傚洜姝わ紝璇ヨ祴鍊肩浉褰撲簬SET col_name = DEFAULT(col_name) + 1

涓轰簡鑳藉浣跨敤REPLACE锛屾偍蹇呴』鍚屾椂鎷ユ湁琛ㄧ殑INSERTDELETE鏉冮檺銆

REPLACE璇彞浼氳繑鍥炰竴涓暟锛屾潵鎸囩ず鍙楀奖鍝嶇殑琛岀殑鏁扮洰銆傝鏁版槸琚垹闄ゅ拰琚彃鍏ョ殑琛屾暟鐨勫拰銆傚鏋滃浜庝竴涓崟琛REPLACE璇ユ暟涓1锛屽垯涓琛岃鎻掑叆锛屽悓鏃舵病鏈夎琚垹闄ゃ傚鏋滆鏁板ぇ浜1锛屽垯鍦ㄦ柊琛岃鎻掑叆鍓嶏紝鏈変竴涓垨澶氫釜鏃ц琚垹闄ゃ傚鏋滆〃鍖呭惈澶氫釜鍞竴绱㈠紩锛屽苟涓旀柊琛屽鍒朵簡鍦ㄤ笉鍚岀殑鍞竴绱㈠紩涓殑涓嶅悓鏃ц鐨勫硷紝鍒欐湁鍙兘鏄竴涓崟涓琛屾浛鎹簡澶氫釜鏃ц銆

鍙楀奖鍝嶇殑琛屾暟鍙互瀹规槗鍦扮‘瀹氭槸鍚REPLACE鍙坊鍔犱簡涓琛岋紝鎴栬呮槸鍚REPLACE涔熸浛鎹簡鍏跺畠琛岋細妫鏌ヨ鏁版槸鍚︿负1锛堟坊鍔狅級鎴栨洿澶э紙鏇挎崲锛夈

濡傛灉鎮ㄦ鍦ㄤ娇鐢C API锛屽垯鍙互浣跨敤mysql_affected_rows()鍑芥暟鑾峰緱鍙楀奖鍝嶇殑琛屾暟銆

鐩墠锛屾偍涓嶈兘鍦ㄤ竴涓瓙鏌ヨ涓紝鍚戜竴涓〃涓洿鎹紝鍚屾椂浠庡悓涓涓〃涓夋嫨銆

浠ヤ笅鏄墍鐢ㄧ畻娉曠殑鏇磋缁嗙殑璇存槑锛堣绠楁硶涔熺敤浜LOAD DATA...REPLACE锛夛細

1.    灏濊瘯鎶婃柊琛屾彃鍏ュ埌琛ㄤ腑

2.    褰撳洜涓哄浜庝富閿垨鍞竴鍏抽敭瀛楀嚭鐜伴噸澶嶅叧閿瓧閿欒鑰岄犳垚鎻掑叆澶辫触鏃讹細

a.    浠庤〃涓垹闄ゅ惈鏈夐噸澶嶅叧閿瓧鍊肩殑鍐茬獊琛

b.    鍐嶆灏濊瘯鎶婃柊琛屾彃鍏ュ埌琛ㄤ腑

13.2.7.聽SELECT璇硶

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 BYORDER BYHAVING瀛愬彞銆備緥濡傦細

                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 INDEXIGNORE INDEXFORCE 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_namedb_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_nametbl_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 BYGROUP 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 BYMySQLGROUP BY杩涜浜嗘墿灞曪紝鍥犳鎮ㄥ彲浠ュ湪鍚勫垪锛堝湪瀛愬彞涓繘琛屽懡鍚嶏級鐨勫悗闈㈡寚瀹ASCDESC

                SELECT a, COUNT(b) FROM test_table GROUP BY a DESC

         MySQLGROUP 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 nLIMIT 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 OUTFILELOAD DATA INFILE鐨勮ˉ璇紱鐢ㄤ簬璇彞鐨exort_options閮ㄥ垎鐨勮娉曞寘鎷儴鍒FIELDSLINES瀛愬彞锛岃繖浜涘瓙鍙ヤ笌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 BYLINES TERMINATED BY鍊肩殑绗竴涓瓧绗

o        ASCII 0锛堝湪缂栧啓鏃舵帴鍦ㄨ浆涔夌鍚庨潰鐨勬槸ASCII 0鈥欙紝鑰屼笉鏄竴涓浂鍊煎瓧鑺傦級

濡傛灉FIELDS ESCAPED BY瀛楃鏄┖瀛楃锛屽垯娌℃湁瀛楃琚浆涔夛紝骞朵笖NULL琚綔涓NULL杈撳嚭锛岃屼笉鏄綔涓\N杈撳嚭銆傛寚瀹氫竴涓┖鐨勮浆涔夌涓嶆槸涓涓ソ鐨勪富鎰忋傜壒鍒槸褰撴偍鐨勬暟鎹腑鐨勫瓧娈靛煎寘鍚垰琚粰浜堢殑娓呭崟涓殑瀛楃鏃讹紝鏇存槸濡傛銆

鍏跺師鍥犳槸鎮ㄥ繀椤诲鎵鏈FIELDS TERMINATED BY, ENCLOSED BY, ESCAPED BYLINES 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 OUTFILEINTO 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, DISTINCTDISTINCTROW閫夐」鎸囧畾鏄惁閲嶅琛屽簲琚繑鍥炪傚鏋滆繖浜涢夐」娌℃湁琚粰瀹氾紝鍒欓粯璁ゅ间负ALL锛堟墍鏈夌殑鍖归厤琛岃杩斿洖锛夈DISTINCTDISTINCTROW鏄悓涔夎瘝锛岀敤浜庢寚瀹氱粨鏋滈泦鍚堜腑鐨勯噸澶嶈搴旇鍒犻櫎銆

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 BYDISTINCT鍚屾椂浣跨敤锛屾潵鍛婄煡浼樺寲绗︾粨鏋滈泦鍚堟湁寰堝琛屻傚湪杩欑鎯呭喌涓嬶紝MySQL鐩存帴浣跨敤浠ョ鐩樹负鍩虹鐨勪复鏃惰〃锛堝鏋滈渶瑕佺殑璇濓級銆傚湪杩欑鎯呭喌涓嬶紝MySQL杩樹細浼樺厛杩涜鍒嗙被锛屼笉浼樺厛浣跨敤涓存椂琛ㄣ備复鏃惰〃瀵逛簬GROUP BY缁勫垎甯︽湁鍏抽敭瀛椼

         SQL_BUFFER_RESULT淇冧娇缁撴灉琚斁鍏ヤ竴涓复鏃惰〃涓傝繖鍙互甯姪MySQL鎻愬墠瑙e紑琛ㄩ攣瀹氾紝鍦ㄩ渶瑕佽姳璐硅緝闀挎椂闂寸殑鎯呭喌涓嬶紝涔熷彲浠ュ府鍔╂妸缁撴灉闆嗗悎鍙戦佸埌瀹㈡埛绔腑銆

         SQL_SMALL_RESULT鍙互涓GROUP BYDISTINCT鍚屾椂浣跨敤锛屾潵鍛婄煡浼樺寲绗︾粨鏋滈泦鍚堟槸杈冨皬鐨勩傚湪姝ゆ儏鍐典笅锛MySAL浣跨敤蹇熶复鏃惰〃鏉ュ偍瀛樼敓鎴愮殑琛紝鑰屼笉鏄娇鐢ㄥ垎绫汇傚湪MySQL 5.1涓紝閫氬父涓嶉渶瑕佽繖鏍枫

         SQL_CALC_FOUND_ROWS鍛婄煡MySQL璁$畻鏈夊灏戣搴斾綅浜庣粨鏋滈泦鍚堜腑锛屼笉鑰冭檻浠讳綍LIMIT瀛愬彞銆傝鐨勬暟鐩彲浠ヤ娇鐢SELECT FOUND_ROWS()鎭㈠銆傝鍙傝12.9.3鑺傦紝鈥滀俊鎭嚱鏁扳

         濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓query_cache_type鍊硷紝鍊间负2DEMAND锛屽垯SQL_CACHE鍛婄煡MySQL鎶婃煡璇㈢粨鏋滃瓨鍌ㄥ湪鏌ヨ缂撳瓨涓傚浜庝娇鐢UNION鐨勬煡璇㈡垨瀛愭煡璇紝鏈夐」浼氬奖鍝嶆煡璇腑鐨勬墍鏈SELECT銆傝鍙傝5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测

         SQL_NO_CACHE鍛婄煡MySQL涓嶈鎶婃煡璇㈢粨鏋滃瓨鍌ㄥ湪鏌ヨ缂撳瓨涓傝鍙傝5.13鑺傦紝鈥淢ySQL鏌ヨ楂橀熺紦鍐测銆傚浜庝竴涓娇鐢UNION鎴栧瓙鏌ヨ鐨勬煡璇紝鏈夐」浼氬奖鍝嶆煡璇腑鐨SELECT

13.2.7.1.聽JOIN璇硶

MySQL鏀寔浠ヤ笅JOIN璇硶銆傝繖浜涜娉曠敤浜SELECT璇彞鐨table_references閮ㄥ垎鍜屽琛DELETEUPDATE璇彞锛

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 JOINON瀛愬彞鍚屾椂浣跨敤锛CROSS JOIN浠ュ叾瀹冩柟寮忎娇鐢ㄣ

閫氬父锛屽湪鍙惈鏈夊唴閮ㄨ仈鍚堣繍琛岀殑鑱斿悎琛ㄨ揪寮忎腑锛屽渾鎷彿鍙互琚拷鐣ャMySQL涔熸敮鎸佸祵濂楃殑鑱斿悎锛堣7.2.10鑺傦紝鈥淢ySQL濡備綍浼樺寲宓屽Join鈥锛夈

閫氬父锛屾偍涓嶅簲瀵ON閮ㄥ垎鏈変换浣曟潯浠躲ON閮ㄥ垎鐢ㄤ簬闄愬畾鍦ㄧ粨鏋滈泦鍚堜腑鎮ㄦ兂瑕佸摢浜涜銆備絾鏄紝鎮ㄥ簲鍦WHERE瀛愬彞涓寚瀹氳繖浜涙潯浠躲傝繖鏉¤鍒欐湁涓浜涗緥澶栥

鍦ㄥ墠闈㈢殑娓呭崟涓樉绀虹殑{ OJ ... LEFT OUTER JOIN ...}璇硶鐨勭洰鐨勫彧鏄负浜嗕繚鎸佷笌ODBC鐨勫吋瀹规с傝娉曚腑鐨勮姳鎷彿搴旀寜瀛楅潰涔﹀啓锛涜鎷彿涓嶆槸涓棿璇硶銆備腑闂磋娉曠敤浜庤娉曟弿杩扮殑鍏跺畠鍦版柟銆

         琛ㄥ紩鐢ㄥ彲浠ヤ娇鐢tbl_name AS alias_nametbl_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涓殑ONUSING閮ㄥ垎涓殑鍙宠〃娌℃湁鍖归厤鐨勮褰曪紝鍒欐墍鏈夊垪琚缃负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, c2c3锛屽垯浠ヤ笅鑱斿悎浼氬姣旀潵鑷袱涓〃鐨勫搴旂殑鍒楋細

                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_JOINJOIN鐩稿悓銆傞櫎浜嗘湁涓鐐逛笉涓鏍凤紝宸﹁〃浼氬湪鍙宠〃涔嬪墠琚鍙栥STRAIGH_JOIN鍙互琚敤浜庤繖鏍风殑鎯呭喌锛屽嵆鑱斿悎浼樺寲绗︿互閿欒鐨勯『搴忔帓鍒楄〃銆

鎮ㄥ彲浠ユ彁渚涙彁绀猴紝褰撲粠涓涓〃涓仮澶嶄俊鎭椂锛MySQL搴斾娇鐢ㄥ摢涓储寮曘傞氳繃鎸囧畾USE INDEXkey_list锛夛紝鎮ㄥ彲浠ュ憡鐭MySQL鍙娇鐢ㄤ竴涓储寮曟潵鏌ユ壘琛ㄤ腑鐨勮銆傚彟涓绉嶈娉IGNORE INDEXkey_list锛夊彲浠ヨ鐢ㄤ簬鍛婄煡MySQL涓嶈浣跨敤鏌愪簺鐗瑰畾鐨勭储寮曘傚鏋EXPLAIN鏄剧ずMySQL姝e湪浣跨敤鏉ヨ嚜绱㈠紩娓呭崟涓殑閿欒绱㈠紩鏃讹紝杩欎簺鎻愮ず浼氭湁鐢ㄥ銆

鎮ㄤ篃鍙互浣跨敤FORCE INDEX锛屽叾浣滅敤鎺ヨ繎USE INDEXkey_list锛夛紝涓嶈繃澧炲姞浜嗕竴椤逛綔鐢紝涓娆¤〃鎵弿琚亣璁句负浠d环寰堥珮銆傛崲鍙ヨ瘽璇达紝鍙湁褰撴棤娉曚娇鐢ㄤ竴涓粰瀹氱殑绱㈠紩鏉ユ煡鎵捐〃涓殑琛屾椂锛屾墠浣跨敤琛ㄦ壂鎻忋

USE KEYIGNORE KEYFORCE KEYUSE INDEXIGNORE INDEXFORCE INDEX鐨勫悓涔夎瘝銆

娉ㄩ噴锛氬綋MySQL鍐冲畾濡備綍鍦ㄨ〃涓煡鎵捐骞跺喅瀹氬浣曡繘琛岃仈鍚堟椂锛屼娇鐢USE INDEXIGNORE INDEXFORCE INDEX鍙細褰卞搷浣跨敤鍝簺绱㈠紩銆傚綋鍒嗚В涓涓ORDER BYGROUP 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璇彞涓紝鍒jUSING瀛愬彞涓鍛藉悕锛屽簲鍦ㄨ緭鍑轰腑鍙嚭鐜颁竴娆°備絾鏄紝鍦ㄤ袱绉嶆儏鍐典笅锛屽啑浣欑殑鍒楀潎娌¤娑堥櫎銆傚彟澶栵紝渚濇嵁鏍囧噯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涓殑鍏辨湁鍒椼傚鏋t3t1鏈夊叡鏈夊垪锛岃繖浜涘垪涓嶈鐢ㄤ綔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瀛愬彞鐨勬搷浣滄暟鏄t2t3銆傚洜涓t1.i1涓嶆槸浠讳綍涓涓搷浣滄暟涓殑鍒楋紝鎵浠ョ粨鏋滄槸鍑虹幇鍦'on clause'涓湁鏈煡鍒't1.i1'鐨勯敊璇傝浣胯仈鍚堝彲浠ヨ澶勭悊锛岀敤浣跨敤鍦嗘嫭鍙锋妸鍓嶄袱涓〃鏄庣‘鍦板綊涓轰竴缁勶紝杩欐牱鐢ㄤ簬ON瀛愬彞鐨勬搷浣滄暟涓(t1,t2)t3

SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);

鏈彉鏇翠篃閫傜敤浜INNER JOINCROSS JOINLEFT JOINRIGHT 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'鐨勯敊璇傝繖鏄洜涓i3t3涓殑涓涓〃锛岃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.c1b.c1鐩稿悓锛屽洜涓轰袱鍒楀皢鍏锋湁鐩稿悓鐨勫笺備娇鐢ㄥ閮ㄨ仈鍚堟椂锛堟瘮濡LEFT JOIN锛夛紝涓ゅ垪涓湁涓鍒楀彲浠ヤ负NULL銆傝鍒楀皢浼氫粠缁撴灉涓蹇界暐銆

13.2.7.2.聽UNION璇硶
 

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 ALLUNION DISTINCT銆傝娣峰悎鐨UNION绫诲瀷鎸夌収杩欐牱鐨勬柟寮忓寰咃紝鍗DISTICT鍏辩敤浣撹鐩栦綅浜庡叾宸﹁竟鐨勬墍鏈ALL鍏辩敤浣撱DISTINCT鍏辩敤浣撳彲浠ヤ娇鐢UNION DISTINCT鏄庣‘鍦扮敓鎴愶紝鎴栦娇鐢UNION锛堝悗闈笉鍔DISTINCTALL鍏抽敭璇嶏級闅愬惈鍦扮敓鎴愩

濡傛灉鎮ㄦ兂浣跨敤ORDER BYLIMIT瀛愬彞鏉ュ鍏ㄩ儴UNION缁撴灉杩涜鍒嗙被鎴栭檺鍒讹紝鍒欏簲瀵瑰崟涓湴SELECT璇彞鍔犲渾鎷彿锛屽苟鎶ORDER BYLIMIT鏀惧埌鏈鍚庝竴涓殑鍚庨潰銆備互涓嬩緥瀛愬悓鏃朵娇鐢ㄤ簡杩欎袱涓瓙鍙ワ細

(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 BYLIMIT锛屽簲鎶婂瓙鍙ユ斁鍏ュ渾鎷彿涓傚渾鎷彿鍖呭惈浜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銆傦級

13.2.8. Subquery璇硶

瀛愭煡璇㈡槸鍙︿竴涓鍙ヤ腑鐨勪竴涓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, SETDO銆傝繕鏈変竴涓檺瀹氭槸锛岀洰鍓嶏紝鎮ㄤ笉鑳藉湪涓涓瓙鏌ヨ涓慨鏀逛竴涓〃锛屽張鍦ㄥ悓涓涓〃涓夋嫨銆傝繖閫傜敤浜DELETE, INSERT, REPLACEUPDATE璇彞銆傚湪闄勫綍I锛鐗规ч檺鍒涓粰鍑轰簡瀵瑰瓙鏌ヨ浣跨敤鐨勬洿缁煎悎鐨勮璁恒

13.2.8.1. 瀛愭煡璇綔涓烘爣閲忔搷浣滄暟

瀛愭煡璇㈡渶绠鍗曠殑褰㈠紡鏄繑鍥炲崟涓鍊肩殑鏍囬噺瀛愭煡璇€傛爣閲忓瓙鏌ヨ鏄竴涓崟涓鎿嶄綔鏁般傚彧瑕佸崟涓鍒楀兼垨鏂囧瓧鏄悎娉曠殑锛屽苟涓旀偍甯屾湜瀛愭煡璇㈠叿鏈夋墍鏈夋搷浣滄暟閮藉叿鏈夌殑鐗规э紝鍒欐偍灏卞彲浠ヤ娇鐢ㄥ瓙鏌ヨ銆傛搷浣滄暟鍏锋湁鐨勭壒鎬у寘鎷細涓涓暟鎹被鍨嬨佷竴涓暱搴︺佷竴涓寚绀烘槸鍚﹀彲浠ヤ负NULL鐨勬爣蹇楃瓑銆備妇渚嬭鏄庯細
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涓湁涓琛屽寘鍚s1s1鏈変竴涓间负2

涓涓爣閲忓瓙鏌ヨ鍙互涓轰竴涓〃杈惧紡鐨勪竴閮ㄥ垎銆備笉瑕佸繕璁板渾鎷彿銆傚嵆浣挎槸瀛愭煡璇㈡槸涓涓负鍑芥暟鎻愪緵鑷彉閲忕殑鎿嶄綔鏁版椂锛屼篃涓嶈蹇樿鍦嗘嫭鍙枫備妇渚嬭鏄庯細

SELECT UPPER((SELECT s1 FROM t1)) FROM t2;

13.2.8.2. 浣跨敤瀛愭煡璇㈣繘琛屾瘮杈

瀛愭煡璇㈡渶甯歌鐨勪竴绉嶄娇鐢ㄦ柟寮忓涓嬶細

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鑺傦紝鈥滆瀛愭煡璇⑩

13.2.8.3. 浣跨敤ANY, IN鍜孲OME杩涜瀛愭煡璇

璇硶锛

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鍖呭惈锛21147锛夛紝鍒欒〃杈惧紡涓TRUE锛屽洜涓t2涓湁涓涓间负7锛岃鍊煎皬浜10銆傚鏋滆〃t2鍖呭惈锛2010锛夛紝鎴栬呭鏋滆〃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杩涜瀛愭煡璇

璇嶈SOMEANY鐨勫埆鍚嶃傚洜姝わ紝杩欎袱涓鍙ユ槸涓鏍风殑锛

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鈥濈殑鎰忔濇槸鈥滄病鏈変竴涓ba鐩哥瓑鈥濓紝浣嗘槸鍦SQL璇硶涓笉鏄繖涓剰鎬濄傝璇硶鐨勬剰鎬濇槸鈥滄湁閮ㄥ垎ba涓嶇浉绛夆濄備娇鐢<> SOME鏈夊姪浜庣‘璁ゆ瘡涓汉閮界悊瑙h鏌ヨ鐨勭湡姝e惈涔夈

13.2.8.4. 浣跨敤ALL杩涜瀛愭煡璇

璇硶锛
operand comparison_operator ALL (subquery)

璇嶈ALL蹇呴』鎺ュ湪涓涓瘮杈冩搷浣滅鐨勫悗闈€ALL鐨勬剰鎬濇槸鈥滃浜庡瓙鏌ヨ杩斿洖鐨勫垪涓殑鎵鏈夊硷紝濡傛灉姣旇緝缁撴灉涓TRUE锛屽垯杩斿洖TRUE銆傗濅緥濡傦細

SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);

鍋囪琛1涓湁涓琛屽寘鍚紙10锛夈傚鏋滆〃t2鍖呭惈锛-50锛岋紜5锛夛紝鍒欒〃杈惧紡涓TRUE锛屽洜涓10t2涓殑鎵鏈変笁涓奸兘澶с傚鏋滆〃t2鍖呭惈锛126NULL锛岋紞100锛夛紝鍒欒〃杈惧紡涓FALSE锛屽洜涓鸿〃t2涓湁涓涓12澶т簬10銆傚鏋滆〃t2鍖呭惈锛0NULL1锛夛紝鍒欒〃杈惧紡涓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);

13.2.8.5. 琛屽瓙鏌ヨ

瀵逛簬鏈偣鐨勮璁哄睘浜庢爣閲忔垨鍒楀瓙鏌ヨ锛屽嵆杩斿洖涓涓崟涓鍊兼垨涓鍒楀肩殑瀛愭煡璇€傝瀛愭煡璇㈡槸涓涓兘杩斿洖涓涓崟涓琛岀殑瀛愭煡璇㈠彉閲忥紝鍥犳鍙互杩斿洖涓涓互涓婄殑鍒楀笺備互涓嬫槸涓や釜渚嬪瓙锛
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

琛ㄨ揪寮忥紙12锛夊拰ROW12锛夋湁鏃惰绉颁负琛屾瀯閫犵銆備袱鑰呮槸绛夊悓鐨勶紝鍦ㄥ叾瀹冪殑璇涓紝涔熸槸鍚堟硶鐨勩備緥濡傦紝浠ヤ笅涓や釜璇彞鍦ㄨ涔変笂鏄瓑鍚岀殑锛堜絾鏄洰鍓嶅彧鏈夌浜屼釜璇彞鍙互琚紭鍖栵級锛

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

13.2.8.6.聽EXISTS鍜孨OT EXISTS

濡傛灉涓涓瓙鏌ヨ杩斿洖浠讳綍鐨勮锛屽垯EXISTS subqueryFALSE銆備緥濡傦細
SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);

杩囧幓锛EXISTS瀛愭煡璇互SELECT *涓哄紑濮嬶紝浣嗘槸鍙互浠SELECT 5SELECT 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鍙互鍥炵瓟杩欐牱鐨勯棶棰橈紝鈥滄槸鍚﹀浜庢墍鏈夌殑yx閮戒负TRUE锛熲

13.2.8.7. 鍏宠仈瀛愭煡璇

鐩稿叧鑱旂殑瀛愭煡璇㈡槸涓涓寘鍚琛ㄧ殑寮曠敤鐨勫瓙鏌ヨ銆傝琛ㄤ篃鏄剧ず鍦ㄥ閮ㄦ煡璇腑銆備緥濡傦細
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锛堝洜涓猴紙56锛変笉绛変簬锛57锛夛級锛屾墍浠ュ瓙鏌ヨ鎬讳綋涓婁负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 ...鏄竴涓洿闈犲鐨勫閮ㄦ煡璇€

瀵逛簬HAVINGORDER BY瀛愬彞涓殑瀛愭煡璇紝MySQL涔熶細鍦ㄥ閮ㄩ夋嫨娓呭崟涓鎵惧垪鍚嶇О銆

瀵逛簬鐗瑰畾鐨勬儏鍐碉紝鐩稿叧鑱旂殑瀛愭煡璇㈣浼樺寲銆備緥濡傦細

val IN (SELECT key_val FROM tbl_name WHERE correlated_condition)

鍚﹀垯锛岃繖浜涘瓙鏌ヨ鏁堢巼涓嶉珮锛屽彲鑳介熷害浼氭參銆傛妸鏌ヨ浣滀负鑱斿悎杩涜鏀瑰啓鍙兘浼氭敼杩涙晥鐜囥

鐩稿叧鑱旂殑瀛愭煡璇笉鑳戒粠澶栭儴鏌ヨ涓紩鐢ㄦ昏鍑芥暟鐨勭粨鏋溿

13.2.8.8. FROM瀛愬彞涓殑瀛愭煡璇

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瀛愬彞涓殑瀛愭煡璇篃浼氳鎵ц銆傝繖鏄洜涓哄湪浼樺寲杩囩▼涓紝涓婁竴绾х殑鏌ヨ闇瑕佹湁鍏虫墍鏈夎〃鐨勪俊鎭

13.2.8.9. 瀛愭煡璇㈤敊璇

浠ヤ笅閿欒鍙傜敤浜庡瓙鏌ヨ銆傛湰鑺傛妸杩欎簺閿欒褰掑湪涓璧枫

         鏉ヨ嚜瀛愭煡璇㈢殑鍒楃殑鏁扮洰涓嶆纭

                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璇彞涓鏍凤紝鍦UPDATEDELETE璇彞涓紝瀛愭煡璇㈡槸鍚堟硶鐨勩傛墍浠ユ偍鍙互鍦UPDATE璇彞涓娇鐢ㄥ瓙鏌ヨ杩涜璧嬪笺備笉杩囷紝鎮ㄤ笉鑳芥妸鍚屼竴涓〃锛堝湪鏈緥涓负琛t1锛夋棦鐢ㄤ簬瀛愭煡璇㈢殑FROM瀛愬彞锛屽張鐢ㄤ簬鏇存柊鐩爣銆

瀵逛簬浜嬪姟瀛樺偍寮曟搸锛屽瓙鏌ヨ鐨勯敊璇細瀵艰嚧鏁翠釜璇彞澶辨晥銆傚浜庨潪浜嬪姟瀛樺偍寮曟搸锛屽湪閬囧埌閿欒涔嬪墠杩涜鐨勬暟鎹慨璁細琚繚鐣欍

13.2.8.10. 浼樺寲瀛愭煡璇

寮鍙戣繃绋嬩笉鏂繘灞曪紝鎵浠ヤ粠闀胯繙鏉ョ湅锛屾病鏈変竴涓彲闈犵殑浼樺寲鎶宸с傛湁浜涙妧宸ф偍鍙兘浼氭劅鍏磋叮锛屽苟鍘熸剰閲囩敤锛

         鏈変簺瀛愬彞浼氬奖鍝嶅湪瀛愭煡璇腑鐨勮鐨勬暟閲忓拰椤哄簭銆備娇鐢ㄨ繖绫诲瓙鍙ャ備緥濡傦細

                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, ANYSOME瀛愭煡璇紝鐩殑鏄鏋滃瓙鏌ヨ涓殑select-list鍒楀凡缂栧埗绱㈠紩锛屽垯鑳藉彂鎸ュ嚭姝や紭鍔裤

         MySQL浣跨敤index-lookup鍑芥暟浠f浛浠ヤ笅鏍煎紡鐨勫瓙鏌ヨ銆EXPLAIN鎶婃鍑芥暟鎻忚堪涓虹壒娈婄殑鑱斿悎绫诲瀷锛unique_subqueryindex_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/鑾峰彇銆

13.2.8.11. 鎶婂瓙鏌ヨ浣滀负鐢ㄤ簬鏃╂湡MySQL鐗堟湰鐨勮仈鍚堣繘琛屾敼鍐

鍦ㄨ緝鏃╃増鏈殑MySQL涓紙鏃╀簬MySQL 4.1锛夛紝鍙敮鎸INSERT...SELECTREPLACE...SELECT...鏍煎紡鐨勫甫宓屽鐨勬煡璇€傝櫧鐒跺湪MySQL 5.1涓病鏈夎繖绉嶆儏鍐碉紝浣嗘湁鏃讹紝浠嶇劧鏈夊叾瀹冪殑鏂规硶娴嬭瘯涓缁勫肩殑浠庡睘鍏崇郴銆傚苟涓旓紝鍦ㄦ湁浜涙儏鍐典笅锛屼笉浠呭彲浠ュ湪娌℃湁瀛愭煡璇㈡椂瀵规煡璇㈣繘琛屾敼鍐欙紝鑰屼笖鏈夋椂浣跨敤杩欎簺鏂规硶姣斾娇鐢ㄥ瓙鏌ヨ鏁堢巼鏇撮珮銆傝繖浜涙柟娉曚箣涓鏄IN()缁撴瀯锛

涓句緥璇存槑锛屾湰鏌ヨ锛

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璇彞銆

13.2.9.聽TRUNCATE璇硶

TRUNCATE [TABLE] tbl_name

TRUNCATE TABLE鐢ㄤ簬瀹屽叏娓呯┖涓涓〃銆備粠閫昏緫涓婅锛岃璇彞涓庣敤浜庡垹闄ゆ墍鏈夎鐨DELETE璇彞绛夊悓锛屼絾鏄湪鏈変簺鎯呭喌涓嬶紝涓よ呭湪浣跨敤涓婃湁鎵涓嶅悓銆

瀵逛簬InnoDB琛紝濡傛灉鏈夐渶瑕佸紩鐢ㄨ〃鐨勫閿檺鍒讹紝鍒TRUNCATE TABLE琚槧灏勫埌DELETE涓婏紱鍚﹀垯浣跨敤蹇熷垹鍑忥紙鍙栨秷鍜岄噸鏂板垱寤鸿〃锛夈備娇鐢TRUNCATE TABLE閲嶆柊璁剧疆AUTO_INCREMENT璁℃暟鍣紝璁剧疆鏃朵笉鑰冭檻鏄惁鏈夊閿檺鍒躲

瀵逛簬鍏跺畠瀛樺偍寮曟搸锛屽湪MySQL 5.1涓紝TRUNCATE TABLEDELETE FROM鏈変互涓嬪嚑澶勪笉鍚岋細

         鍒犲噺鎿嶄綔浼氬彇娑堝苟閲嶆柊鍒涘缓琛紝杩欐瘮涓琛屼竴琛岀殑鍒犻櫎琛岃蹇緢澶氥

         鍒犲噺鎿嶄綔涓嶈兘淇濊瘉瀵逛簨鍔℃槸瀹夊叏鐨勶紱鍦ㄨ繘琛屼簨鍔″鐞嗗拰琛ㄩ攣瀹氱殑杩囩▼涓皾璇曡繘琛屽垹鍑忥紝浼氬彂鐢熼敊璇

         琚垹闄ょ殑琛岀殑鏁扮洰娌℃湁琚繑鍥炪

         鍙琛ㄥ畾涔夋枃浠tbl_name.frm鏄悎娉曠殑锛屽垯鍙互浣跨敤TRUNCATE TABLE鎶婅〃閲嶆柊鍒涘缓涓轰竴涓┖琛紝鍗充娇鏁版嵁鎴栫储寮曟枃浠跺凡缁忚鐮村潖銆

         琛ㄧ鐞嗙▼搴忎笉璁板緱鏈鍚庤浣跨敤鐨AUTO_INCREMENT鍊硷紝浣嗘槸浼氫粠澶村紑濮嬭鏁般傚嵆浣垮浜MyISAMInnoDB涔熸槸濡傛銆MyISAMInnoDB閫氬父涓嶅啀娆′娇鐢ㄥ簭鍒楀笺

         褰撹鐢ㄤ簬甯﹀垎鍖虹殑琛ㄦ椂锛TRUNCATE TABLE浼氫繚鐣欏垎鍖猴紱鍗筹紝鏁版嵁鍜岀储寮曟枃浠惰鍙栨秷骞堕噸鏂板垱寤猴紝鍚屾椂鍒嗗尯瀹氫箟锛.par锛夋枃浠朵笉鍙楀奖鍝嶃

TRUNCATE TABLE鏄湪MySQL涓噰鐢ㄧ殑涓涓Oracle SQL鎵╁睍銆

13.2.10.聽UPDATE璇硶

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 BYLIMITmultiple-table UPDATE鍚屾椂浣跨敤銆

鍦ㄤ竴涓鏇存敼鐨multiple-table UPDATE涓紝鏈変簺鍒楄寮曠敤銆傛偍鍙渶瑕佽繖浜涘垪鐨UPDATE鏉冮檺銆傛湁浜涘垪琚鍙栦簡锛屼絾鏄病琚慨鏀广傛偍鍙渶瑕佽繖浜涘垪鐨SELECT鏉冮檺銆

濡傛灉鎮ㄤ娇鐢ㄧ殑multiple-table UPDATE璇彞涓寘鍚甫鏈夊閿檺鍒剁殑InnoDB琛紝鍒MySQL浼樺寲绗﹀鐞嗚〃鐨勯『搴忓彲鑳戒笌涓婁笅灞傜骇鍏崇郴鐨勯『搴忎笉鍚屻傚湪姝ゆ儏鍐典笅锛岃鍙ユ棤鏁堝苟琚 鍥炴粴銆傚悓鏃讹紝鏇存柊涓涓崟涓琛紝骞朵笖渚濋潬ON UPDATE鍔熻兘銆傝鍔熻兘鐢InnoDB鎻愪緵锛岀敤浜庡鍏跺畠琛ㄨ繘琛岀浉搴旂殑淇敼銆傝鍙傝15.2.6.4鑺傦紝鈥淔OREIGN KEY绾︽潫鈥

鐩墠锛屾偍涓嶈兘鍦ㄤ竴涓瓙鏌ヨ涓洿鏂颁竴涓〃锛屽悓鏃朵粠鍚屼竴涓〃涓夋嫨銆

13.3. MySQL瀹炵敤宸ュ叿璇彞

13.3.1.聽DESCRIBE璇硶锛堣幏鍙栨湁鍏冲垪鐨勪俊鎭級

{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 TABLESHOW TABLE STATUS璇彞涔熷彲浠ユ彁渚涙湁鍏宠〃鐨勪俊鎭傝鍙傝13.5.4鑺傦紝鈥淪HOW璇硶鈥

13.3.2.聽USE璇硶

USE db_name

USE db_name璇彞鍙互閫氬憡MySQLdb_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鐩稿吋瀹广

13.4. MySQL浜嬪姟澶勭悊鍜岄攣瀹氳鍙

MySQL閫氳繃SET AUTOCOMMIT, START TRANSACTION, COMMITROLLBACK绛夎鍙ユ敮鎸佹湰鍦颁簨鍔★紙鍦ㄧ粰瀹氱殑瀹㈡埛绔繛鎺ヤ腑锛夈傝鍙傝13.4.1鑺傦紝鈥淪TART TRANSACTION, COMMIT鍜孯OLLBACK璇硶鈥XA浜嬪姟鏀寔杩樺彲浠ュ厑璁MySQL鍙備笌鍒嗗竷寮忎簨鍔°傝鍙傝13.4.7鑺傦紝鈥淴A浜嬪姟鈥

13.4.1.聽START TRANSACTION, COMMIT鍜孯OLLBACK璇硶

START TRANSACTION | BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET AUTOCOMMIT = {0 | 1}

START TRANSACTIONBEGIN璇彞鍙互寮濮嬩竴椤规柊鐨勪簨鍔°COMMIT鍙互鎻愪氦褰撳墠浜嬪姟锛屾槸鍙樻洿鎴愪负姘镐箙鍙樻洿銆ROLLBACK鍙互 鍥炴粴褰撳墠浜嬪姟锛屽彇娑堝叾鍙樻洿銆SET AUTOCOMMIT璇彞鍙互绂佺敤鎴栧惎鐢ㄩ粯璁ょ殑autocommit妯″紡锛岀敤浜庡綋鍓嶈繛鎺ャ

鑷夌殑WORK鍏抽敭璇嶈鏀寔锛岀敤浜COMMITRELEASE锛屼笌CHAINRELEASE瀛愬彞銆CHAINRELEASE鍙互琚敤浜庡浜嬪姟瀹屾垚杩涜闄勫姞鎺у埗銆Completion_type绯荤粺鍙橀噺鐨勫煎喅瀹氫簡榛樿瀹屾垚鐨勬ц川銆傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

AND CHAIN瀛愬彞浼氬湪褰撳墠浜嬪姟缁撴潫鏃讹紝绔嬪埢鍚姩涓涓柊浜嬪姟锛屽苟涓旀柊浜嬪姟涓庡垰缁撴潫鐨勪簨鍔℃湁鐩稿悓鐨勯殧绂荤瓑绾сRELEASE瀛愬彞鍦ㄧ粓姝簡褰撳墠浜嬪姟鍚庯紝浼氳鏈嶅姟鍣ㄦ柇寮涓庡綋鍓嶅鎴风鐨勮繛鎺ャ傚寘鍚NO鍏抽敭璇嶅彲浠ユ姂鍒CHAINRELEASE瀹屾垚銆傚鏋completion_type绯荤粺鍙橀噺琚缃负涓瀹氱殑鍊硷紝浣胯繛閿佹垨閲婃斁瀹屾垚鍙互榛樿杩涜锛屾鏃NO鍏抽敭璇嶆湁鐢ㄣ

榛樿鎯呭喌涓嬶紝MySQL閲囩敤autocommit妯″紡杩愯銆傝繖鎰忓懗鐫锛屽綋鎮ㄦ墽琛屼竴涓敤浜庢洿鏂帮紙淇敼锛夎〃鐨勮鍙ヤ箣鍚庯紝MySQL绔嬪埢鎶婃洿鏂板瓨鍌ㄥ埌纾佺洏涓

濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ竴涓簨鍔″畨鍏ㄥ瀷鐨勫瓨鍌ㄥ紩鎿庯紙濡InnoDB, BDBNDB绨囷級锛屽垯鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬭鍙ョ鐢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 TRANSACTIONautocommit浠嶇劧琚鐢紝鐩村埌鎮ㄤ娇鐢COMMITROLLBACK缁撴潫浜嬪姟涓烘銆傜劧鍚autocommit妯″紡鎭㈠鍒板師鏉ョ殑鐘舵併

BEGINBEGIN 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琚墽琛屻

涓轰簡鑾峰緱鏈濂界殑缁撴灉锛屼簨鍔″簲鍙娇鐢ㄧ敱鍗曚竴浜嬪姟瀛樺偍寮曟搸绠$悊鐨勮〃鎵ц銆傚惁鍒欙紝浼氬嚭鐜颁互涓嬮棶棰橈細

         濡傛灉鎮ㄤ娇鐢ㄧ殑琛ㄦ潵鑷涓簨鍔″畨鍏ㄥ瀷瀛樺偍寮曟搸锛堜緥濡InnoDBBDB锛夛紝骞朵笖浜嬪姟闅旂绛夌骇涓嶆槸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锛岀敤浜庤繛鎺ャ

13.4.2. 涓嶈兘鍥炴粴鐨勮鍙

鏈変簺璇彞涓嶈兘琚洖婊氥傞氬父锛岃繖浜涜鍙ュ寘鎷暟鎹畾涔夎瑷锛DDL锛夎鍙ワ紝姣斿鍒涘缓鎴栧彇娑堟暟鎹簱鐨勮鍙ワ紝鍜屽垱寤恒佸彇娑堟垨鏇存敼琛ㄦ垨瀛樺偍鐨勫瓙绋嬪簭鐨勮鍙ャ

鎮ㄥ湪璁捐浜嬪姟鏃讹紝涓嶅簲鍖呭惈杩欑被璇彞銆傚鏋滄偍鍦ㄤ簨鍔$殑鍓嶉儴涓彂甯冧簡涓涓笉鑳借鍥炴粴鐨勮鍙ワ紝鍒欏悗閮ㄧ殑鍏跺畠璇彞浼氬彂鐢熼敊璇紝鍦ㄨ繖浜涙儏鍐典笅锛岄氳繃鍙戝竷ROLLBACK璇彞涓嶈兘 鍥炴粴浜嬪姟鐨勫叏閮ㄦ晥鏋溿

13.4.3. 浼氶犳垚闅愬紡鎻愪氦鐨勮鍙

浠ヤ笅璇彞锛堜互鍙婂悓涔夎瘝锛夊潎闅愬惈鍦扮粨鏉熶竴涓簨鍔★紝浼间箮鏄湪鎵ц鏈鍙ュ墠锛屾偍宸茬粡杩涜浜嗕竴涓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 FUNCTIONDROP PROCEDURE绛夎鍙ヤ細瀵艰嚧涓涓殣鍚彁浜ゃ

         InnoDB涓殑CREATE TABLE璇彞琚綔涓轰竴涓崟涓浜嬪姟杩涜澶勭悊銆傝繖鎰忓懗鐫锛屾潵鑷敤鎴风殑ROLLBACK涓嶄細鎾ら攢鐢ㄦ埛鍦ㄤ簨鍔″鐞嗚繃绋嬩腑鍒涘缓鐨CREATE TABLE璇彞銆

浜嬪姟涓嶈兘琚祵濂椼傝繖鏄殣鍚COMMIT鐨勭粨鏋溿傚綋鎮ㄥ彂甯冧竴涓START TRANSACTION璇彞鎴栧叾鍚屼箟璇嶆椂锛岃COMMIT琚墽琛岋紝鐢ㄤ簬浠讳綍褰撳墠浜嬪姟銆

13.4.4.聽SAVEPOINT鍜孯OLLBACK TO SAVEPOINT璇硶

SAVEPOINT identifier
ROLLBACK [WORK] TO SAVEPOINT identifier
RELEASE SAVEPOINT identifier

InnoDB鏀寔SQL璇彞SAVEPOINT, ROLLBACK TO SAVEPOINT, RELEASE SAVEPOINT鍜岃嚜閫夌殑鐢ㄤ簬ROLLBACKWORK鍏抽敭璇嶃

SAVEPOINT璇彞鐢ㄤ簬璁剧疆涓涓簨鍔′繚瀛樼偣锛屽甫涓涓爣璇嗙鍚嶇О銆傚鏋滃綋鍓嶄簨鍔℃湁涓涓悓鏍峰悕绉扮殑淇濆瓨鐐癸紝鍒欐棫鐨勪繚瀛樼偣琚垹闄わ紝鏂扮殑淇濆瓨鐐硅璁剧疆銆

ROLLBACK TO SAVEPOINT璇彞浼氬悜浠ュ懡鍚嶇殑淇濆瓨鐐瑰洖婊氫竴涓簨鍔°傚鏋滃湪淇濆瓨鐐硅璁剧疆鍚庯紝褰撳墠浜嬪姟瀵硅杩涜浜嗘洿鏀癸紝鍒欒繖浜涙洿鏀逛細鍦 鍥炴粴涓鎾ら攢銆備絾鏄紝InnoDB涓嶄細閲婃斁琚瓨鍌ㄥ湪淇濆瓨鐐逛箣鍚庣殑瀛樺偍鍣ㄤ腑鐨勮閿佸畾銆傦紙娉ㄦ剰锛屽浜庢柊鎻掑叆鐨勮锛岄攣瀹氫俊鎭瀛樺偍鍦ㄨ涓殑浜嬪姟ID鎵胯浇锛涢攣瀹氭病鏈夎鍒嗗紑瀛樺偍鍦ㄥ瓨鍌ㄥ櫒涓傚湪杩欑鎯呭喌涓嬶紝琛岄攣瀹氬湪鎾ら攢涓閲婃斁銆傦級鍦ㄨ鍛藉悕鐨勪繚瀛樼偣涔嬪悗璁剧疆鐨勪繚瀛樼偣琚垹闄ゃ

濡傛灉璇彞杩斿洖浠ヤ笅閿欒锛屽垯鎰忓懗鐫涓嶅瓨鍦ㄥ甫鏈夋寚瀹氬悕绉扮殑淇濆瓨鐐癸細

ERROR 1181: Got error 153 during ROLLBACK

RELEASE SAVEPOINT璇彞浼氫粠褰撳墠浜嬪姟鐨勪竴缁勪繚瀛樼偣涓垹闄ゅ凡鍛藉悕鐨勪繚瀛樼偣銆備笉鍑虹幇鎻愪氦鎴 鍥炴粴銆傚鏋滀繚瀛樼偣涓嶅瓨鍦紝浼氬嚭鐜伴敊璇

濡傛灉鎮ㄦ墽琛COMMIT鎴栨墽琛屼笉鑳藉懡鍚嶄繚瀛樼偣鐨ROLLBACK锛屽垯褰撳墠浜嬪姟鐨勬墍鏈変繚瀛樼偣琚垹闄ゃ

13.4.5.聽LOCK TABLES鍜孶NLOCK TABLES璇硶

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 LOCALREAD涔嬮棿鐨勫尯鍒槸锛READ LOCAL鍏佽鍦ㄩ攣瀹氳淇濇寔鏃讹紝鎵ц闈炲啿绐佹INSERT璇彞锛堝悓鏃舵彃鍏ワ級銆備絾鏄紝濡傛灉鎮ㄦ鎵撶畻鍦MySQL澶栭潰鎿嶄綔鏁版嵁搴撴枃浠讹紝鍚屾椂鎮ㄤ繚鎸侀攣瀹氾紝鍒欎笉鑳戒娇鐢READ LOCAL銆傚浜InnoDB琛紝READ LOCALREAD鐩稿悓銆

褰撴偍浣跨敤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涓殑涓涓笉鏀寔浜嬪姟鐨勫瓨鍌ㄥ紩鎿庯紝鍒欏鏋滄偍鎯宠纭畾鍦SELECTUPDATE涔嬮棿娌℃湁鍏跺畠绾跨▼锛屾偍蹇呴』浣跨敤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锛屾湁鍙兘鍙︿竴涓嚎绋嬩細鍦ㄦ墽琛SELECTUPDATE璇彞涔嬮棿鍦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鏈夊叧鐨勯棶棰樷

13.4.6.聽SET TRANSACTION璇硶

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鍛戒护琛岄夐」鈥

13.4.7. XA浜嬪姟

瀵逛簬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锛夈備緥濡傦紝褰撲竴涓簨鍔$鐞嗗櫒鍙戠幇锛屼竴涓叏灞浜嬪姟鍙敱涓涓簨鍔¤祫婧愮粍鎴愶紙鍗筹紝鍗曚竴鍒嗘敮锛夛紝鍒欒璧勬簮鍙互琚憡鐭ュ悓鏃惰繘琛岄澶囧拰鎻愪氦銆

13.4.7.1. XA浜嬪姟SQL璇硶

瑕佸湪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 STARTJOINRESUME瀛愬彞涓嶈鏀寔銆

瀵逛簬XA ENDSUSPEND [FOR MIGRATE]瀛愬彞涓嶈鏀寔銆

姣忎釜XA璇彞浠XA鍏抽敭璇嶄负寮澶达紝澶氭暟璇彞瑕佹眰涓涓xid鍊笺 xid鏄竴涓XA浜嬪姟鏍囪瘑绗︺傚畠鎸囩ず璇ヨ鍙ラ傜敤浜庡摢涓簨鍔°xid鍊肩敱瀹㈡埛绔彁渚涳紝鎴栫敱MySQL鏈嶅姟鍣ㄧ敓鎴愩xid鍊煎寘鍚竴鍒颁笁涓儴鍒嗭細

xid: gtrid [, bqual [, formatID ]]

gtrid鏄竴涓叏灞浜嬪姟鏍囪瘑绗︼紝bqual鏄竴涓垎鏀檺瀹氱锛formatID鏄竴涓暟瀛楋紝鐢ㄤ簬鏍囪瘑鐢gtridbqual鍊间娇鐢ㄧ殑鏍煎紡銆傛牴鎹娉曠殑琛ㄧず锛bqualformatID鏄嚜閫夌殑銆傚鏋滄病鏈夌粰瀹氾紝榛樿鐨bqual鍊兼槸''銆傚鏋滄病鏈夌粰瀹氾紝榛樿鐨fromatID鍊兼槸1

gtridbqual蹇呴』涓哄瓧绗︿覆鏂囧瓧锛屾瘡涓殑闀垮害鏈澶氫负64瀛楄妭锛堜笉鏄瓧绗︼級銆gtridbqual鍙互鐢ㄥ绉嶆柟娉曟寚瀹氥傛偍鍙互浣跨敤甯﹀紩鍙风殑瀛楃涓('ab')锛屽崄鍏繘鍒跺瓧绗︿覆(0x6162, X'ab')锛屾垨浣嶅(b'nnnn')

formatID鏄竴涓棤绗﹀彿鐨勬暣鏁般

閫氳繃MySQL鏈嶅姟鍣ㄧ殑甯︿笅鍒掔嚎鐨XA鏀寔瀛愮▼搴忥紝gtridbqual鍊艰鐞嗚В涓轰互瀛楄妭涓哄崟浣嶃備絾鏄紝鍦ㄥ寘鍚XA璇彞鐨SQL璇彞姝e湪琚垎鏋愮殑鍚屾椂锛屾湇鍔″櫒浼氬幓鎿嶄綔涓浜涚壒瀹氱殑瀛楃闆嗐備负浜嗗畨鍏紝鎶gtridbqual浣滀负鍗佸叚杩涘埗瀛楃涓插啓鍏ャ

閫氬父锛xid鍊肩敱浜嬪姟绠$悊鍣ㄧ敓鎴愩傜敱涓涓TM鐢熸垚鐨勫煎繀椤讳笌鐢卞叾瀹TMs鐢熸垚鐨勫间笉鍚屻備竴涓粰瀹氱殑TM蹇呴』鑳借瘑鍒嚜宸辩殑xid鍊笺傝繖浜涘间綅浜庣敱XA RECOVER璇彞杩斿洖鐨勫兼竻鍗曚腑銆

XA START xid鐢ㄤ簬鍚姩涓涓甫缁欏畾xid鍊肩殑XA浜嬪姟銆傛瘡涓XA浜嬪姟蹇呴』鏈変竴涓敮涓鐨xid鍊硷紝鍥犳璇ュ煎綋鍓嶄笉鑳借鍏跺畠鐨XA浜嬪姟浣跨敤銆備娇鐢gtridbqual鍊艰瘎浼板敮涓鎬с傛墍鏈変笅鍒楃殑鐢ㄤ簬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鏄簨鍔xidformatID閮ㄥ垎

         gtrid_lengthxidgtrid閮ㄥ垎鐨勯暱搴︼紝浠ュ瓧鑺備负鍗曚綅

         bqual_lengthxidbqual閮ㄥ垎鐨勯暱搴︼紝浠ュ瓧鑺備负鍗曚綅

         dataxidgtrid閮ㄥ垎鍜bqual閮ㄥ垎鐨勪覆鑱

13.4.7.2. XA浜嬪姟鐘舵

XA浜嬪姟鍦ㄤ互涓嬬姸鎬佷笅杩涘睍锛

1.    浣跨敤XA START鏉ュ惎鍔ㄤ竴涓XA浜嬪姟锛屽苟鎶婂畠鏀惧叆ACTIVE鐘舵併

2.    瀵逛簬涓涓ACTIVE XA浜嬪姟锛屽彂甯冩瀯鎴愪簨鍔$殑SQL璇彞锛岀劧鍚庡彂甯冧竴涓XA END璇彞銆XA END鎶婁簨鍔℃斁鍏IDLE鐘舵併

3.    瀵逛簬涓涓IDLE XA浜嬪姟锛屾偍鍙互鍙戝竷涓涓XA PREPARE璇彞鎴栦竴涓XA COMMITONE PHASE璇彞锛

         XA PREPARE鎶婁簨鍔℃斁鍏PREPARED鐘舵併傚湪姝ょ偣涓婄殑XA RECOVER璇彞灏嗗湪鍏惰緭鍑轰腑鍖呮嫭浜嬪姟鐨xid鍊硷紝鍥犱负XA RECOVER浼氬垪鍑哄浜PREPARED鐘舵佺殑鎵鏈XA浜嬪姟銆

         XA COMMITONE 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璇彞涓嶈兘琚娇鐢紝鐩村埌璇ヤ簨鍔¤鎻愪氦鎴栬 鍥炴粴涓烘銆

13.5. 鏁版嵁搴撶鐞嗚鍙

13.5.1. 璐︽埛绠$悊璇彞

13.5.1.1.聽CREATE USER璇硶

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璇硶鈥

13.5.1.2.聽DROP USER璇硶

DROP USER user [, user] ...

DROP USER璇彞鐢ㄤ簬鍒犻櫎涓涓垨澶氫釜MySQL璐︽埛銆傝浣跨敤DROP USER锛屾偍蹇呴』鎷ユ湁mysql鏁版嵁搴撶殑鍏ㄥ眬CREATE USER鏉冮檺鎴DELETE鏉冮檺銆備娇鐢ㄤ笌GRANTREVOKE鐩稿悓鐨勬牸寮忎负姣忎釜 璐︽埛鍛藉悕锛涗緥濡傦紝'jeffrey'@'localhost'銆 璐︽埛鍚嶇О鐨勭敤鎴峰拰涓绘満閮ㄥ垎涓庣敤鎴疯〃璁板綍鐨UserHost鍒楀肩浉瀵瑰簲銆

浣跨敤DROP USER锛屾偍鍙互鍙栨秷涓涓处鎴峰拰鍏舵潈闄愶紝鎿嶄綔濡備笅锛

DROP USER user;

璇ヨ鍙ュ彲浠ュ垹闄ゆ潵鑷墍鏈夋巿鏉冭〃鐨勫笎鎴锋潈闄愯褰曘

瑕佺偣锛DROP USER涓嶈兘鑷姩鍏抽棴浠讳綍鎵撳紑鐨勭敤鎴峰璇濄傝屼笖锛屽鏋滅敤鎴锋湁鎵撳紑鐨勫璇濓紝姝ゆ椂鍙栨秷鐢ㄦ埛锛屽垯鍛戒护涓嶄細鐢熸晥锛岀洿鍒扮敤鎴峰璇濊鍏抽棴鍚庢墠鐢熸晥銆備竴鏃﹀璇濊鍏抽棴锛岀敤鎴蜂篃琚彇娑堬紝姝ょ敤鎴峰啀娆¤瘯鍥剧櫥褰曟椂灏嗕細澶辫触銆傝繖鏄湁鎰忚璁$殑銆

13.5.1.3.聽GRANT鍜孯EVOKE璇硶

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

GRANTREVOKE璇彞鍏佽绯荤粺绠$悊鍛樺垱寤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.dbmysql.host琛ㄤ腑銆GRANT ALL ON db_name.*REVOKE ALL ON db_name.*鍙巿浜堝拰鎾ら攢鏁版嵁搴撴潈闄愩

         琛ㄥ眰绾

琛ㄦ潈闄愰傜敤浜庝竴涓粰瀹氳〃涓殑鎵鏈夊垪銆傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.talbes_priv琛ㄤ腑銆GRANT ALL ON db_name.tbl_nameREVOKE ALL ON db_name.tbl_name鍙巿浜堝拰鎾ら攢琛ㄦ潈闄愩

         鍒楀眰绾

鍒楁潈闄愰傜敤浜庝竴涓粰瀹氳〃涓殑鍗曚竴鍒椼傝繖浜涙潈闄愬瓨鍌ㄥ湪mysql.columns_priv琛ㄤ腑銆傚綋浣跨敤REVOKE鏃讹紝鎮ㄥ繀椤绘寚瀹氫笌琚巿鏉冨垪鐩稿悓鐨勫垪銆

         瀛愮▼搴忓眰绾

CREATE ROUTINE, ALTER ROUTINE, EXECUTEGRANT鏉冮檺閫傜敤浜庡凡瀛樺偍鐨勫瓙绋嬪簭銆傝繖浜涙潈闄愬彲浠ヨ鎺堜簣涓哄叏灞灞傜骇鍜屾暟鎹簱灞傜骇銆傝屼笖锛岄櫎浜CREATE ROUTINE澶栵紝杩欎簺鏉冮檺鍙互琚巿浜堜负瀛愮▼搴忓眰绾э紝骞跺瓨鍌ㄥ湪mysql.procs_priv琛ㄤ腑銆

褰撳悗缁洰鏍囨槸涓涓〃銆佷竴涓凡瀛樺偍鐨勫嚱鏁版垨涓涓凡瀛樺偍鐨勮繃绋嬫椂锛object_type瀛愬彞搴旇鎸囧畾涓TABLEFUNCTIONPROCEDURE銆傚綋浠庢棫鐗堟湰鐨MySQL鍗囩骇鏃讹紝瑕佷娇鐢ㄦ湰瀛愬彞锛屾偍蹇呴』鍗囩骇鎮ㄧ殑鎺堟潈琛ㄣ傝鍙傝2.10.2鑺傦紝鈥滃崌绾ф巿鏉冭〃鈥

瑕佷娇鐢GRANTREVOKE锛屾偍蹇呴』鎷ユ湁GRANT OPTION鏉冮檺锛屽苟涓旀偍蹇呴』鐢ㄤ簬鎮ㄦ鍦ㄦ巿浜堟垨鎾ら攢鐨勬潈闄愩

瑕佹挙閿鎵鏈夋潈闄愶紝闇浣跨敤浠ヤ笅璇硶銆傛璇硶鐢ㄤ簬鍙栨秷瀵逛簬宸插懡鍚嶇殑鐢ㄦ埛鐨勬墍鏈夊叏灞灞傜骇銆佹暟鎹簱灞傜骇銆佽〃灞傜骇鍜屽垪灞傜骇鐨勬潈闄愩

REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...

瑕佷娇鐢ㄦ湰REVOKE璇硶锛屾偍蹇呴』鎷ユ湁mysql鏁版嵁搴撶殑鍏ㄥ眬CREATE USER鏉冮檺鎴UPDATE鏉冮檺銆

瀵逛簬GRANTREVOKE璇彞锛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 USERREVOKE ALL PRIVILEGES

CREATE VIEW

鍏佽浣跨敤CREATE VIEW

DELETE

鍏佽浣跨敤DELETE

DROP

鍏佽浣跨敤DROP TABLE

EXECUTE

鍏佽鐢ㄦ埛杩愯宸插瓨鍌ㄧ殑瀛愮▼搴

FILE

鍏佽浣跨敤SELECT...INTO OUTFILELOAD DATA INFILE

INDEX

鍏佽浣跨敤CREATE INDEXDROP 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 LOGSSET GLOBAL璇彞锛mysqladmin debug鍛戒护锛涘厑璁告偍杩炴帴锛堜竴娆★級锛屽嵆浣垮凡杈惧埌max_connections

UPDATE

鍏佽浣跨敤UPDATE

USAGE

鈥滄棤鏉冮檺鈥濈殑鍚屼箟璇

GRANT OPTION

鍏佽鎺堜簣鏉冮檺

褰撲粠鏃х増鏈殑MySQL鍗囩骇鏃讹紝瑕佷娇鐢EXECUTE, CREATE VIEW, SHOW VIEW, CREATE USER, CREATE ROUTINEALTER 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, SHUTDOWNSUPER鏉冮檺鏄鐞嗘ф潈闄愶紝鍙兘杩涜鍏ㄥ眬鎺堟潈锛堜娇鐢ON *.*璇硶锛夈

鍏跺畠鏉冮檺鍙互琚叏灞鎺堟潈锛屾垨琚祴浜堜负鍏跺畠灞傜骇銆

瀵逛簬涓涓〃锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEXALTER

瀵逛簬涓涓垪锛堜篃灏辨槸锛屽綋鎮ㄤ娇鐢ㄤ竴涓column_list瀛愬彞鏃讹級锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸SELECT, INSERTUPDATE

鍦ㄥ瓙绋嬪簭灞傜骇锛屾偍鍙互鎸囧畾鐨priv_type鍊煎彧鑳芥槸ALTER ROUTINE, EXECUTEGRANT OPTIONCREATE 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_namehost_name涓庝竴涓笉鍔犲紩鍙风殑鏍囪瘑绗︿竴鏍锋槸鍚堟硶鐨勶紝閭d箞鎮ㄤ笉闇瑕佸瀹冨姞寮曞彿銆備笉杩囷紝瑕佹寚瀹氫竴涓寘鍚壒娈婂瓧绗︼紙濡傗-鈥欙級鐨user_name瀛楃涓诧紝鎴栦竴涓寘鍚壒娈婂瓧绗︽垨閫氶厤瀛楃锛堝鈥%鈥欙級锛屽垯寮曞彿鏄繀瑕佺殑锛涗緥濡傦紝'test-user'@'test-hostname'銆傚垎鍒usernamehostname鍔犲紩鍙枫

鎮ㄥ彲浠ュ湪hostname涓寚瀹氶氶厤绗︺備緥濡user_name@'%.loc.gov'閫傜敤浜庡湪loc.gov鍩熶腑鐨勪换浣曚富鏈虹殑user_name銆傚悓鏃user_name@'144.155.166.%'閫傜敤浜144.155.166 C绾у瓙缃戜腑鐨勪换浣曚富鏈虹殑user_name

绠鍗曞舰寮user_nameuser_name@'%'鐨勫悓涔夎瘝銆

MySQL涓嶆敮鎸usernames涓殑閫氶厤绗︺傞氳繃鎶婂甫鏈User=''鐨勭櫥褰曢」鎻掑叆鍒mysql.user琛ㄤ腑锛屾垨閫氳繃浣跨敤GRANT璇彞鍒涘缓涓涓甫鏈夌┖鍚嶇О鐨勭敤鎴凤紝鍙互瀹氫箟鍖垮悕鐢ㄦ埛锛

mysql> GRANT ALL ON test.* TO ''@'localhost' ...

褰撴妸甯﹀紩鍙风殑鍊兼槸锛岄渶浣跨敤鍙嶅嬀鍙(`)涓烘暟鎹簱銆佽〃銆佸垪鍜屽瓙绋嬪簭鍚嶇О鍔犲紩鍙枫備娇鐢ㄥ崟寮曞彿(')hostnamesusernames鍜 瀵嗙爜鍔犲紩鍙枫

璀﹀憡锛氬鏋滄偍鍏佽鍖垮悕鐢ㄦ埛杩炴帴鍒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 USERDELETE鏄庣‘鍦版搷浣溿

濡傛灉鍒涘缓浜嗕竴涓柊鐨勭敤鎴凤紝鎴栬呭鏋滄偍鎷ユ湁鍏ㄥ眬鎺堟潈鏉冮檺锛屽垯鐢ㄦ埛瀵嗙爜琚缃负鐢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, SELECTUPDATE

鎮ㄤ笉搴旇鍚戜竴涓父瑙勭敤鎴锋巿浜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 countMAX_CONNECTIONS_PER_HOUR count閫夐」闄愬埗浜嗗湪浠讳綍缁欏畾鐨勪竴灏忔椂鏈熼棿锛岀敤鎴峰彲浠ユ墽琛岀殑鏌ヨ銆佹洿鏂板拰鐧诲綍鐨勬暟鐩傚鏋count0锛堥粯璁ゅ硷級锛岃繖鎰忓懗鐫锛屽璇ョ敤鎴锋病鏈夐檺鍒躲

MAX_USER_CONNECTIONS count閫夐」闄愬埗浜嗚处鎴峰彲浠ュ悓鏃惰繘琛岀殑杩炴帴鐨勬渶澶ф暟鐩傚鏋count0锛堥粯璁ゅ硷級锛屽垯max_user_connections绯荤粺鍙互鍐冲畾璇 璐︽埛鍚屾椂杩炴帴鐨勬暟鐩

娉ㄩ噴锛氳瀵逛竴涓師鏈夌殑鐢ㄦ埛鎸囧畾浠讳綍杩欑被璧勬簮闄愬埗鍨嬮夐」锛屽悓鏃跺張涓嶅奖鍝嶅師鏈夌殑鏉冮檺锛岄渶浣跨敤GRANT USAGE ON *.* ... WITH MAX_...

5.8.4鑺傦紝鈥滈檺鍒惰处鎴疯祫婧愨

闄や簡鏍规嵁username鍜屽瘑鐮佽繘琛屽父瑙勯壌瀹氬锛MySQL杩樺彲浠ユ鏌X509璇佹槑灞炴с傝涓MySQL璐︽埛鎸囧畾涓SSL鏈夊叧鐨勯夐」锛岄渶浣跨敤GRANT璇彞鐨REQUIRE瀛愬彞銆傦紙瑕佷簡瑙f湁鍏冲湪MySQL涓娇鐢SSL鐨勮儗鏅俊鎭紝璇峰弬瑙5.8.7鑺傦紝鈥滀娇鐢ㄥ畨鍏ㄨ繛鎺モ銆傦級

瀵逛簬涓涓粰瀹氱殑璐︽埛锛屾湁澶氱鍙兘鎬у彲浠ラ檺鍒惰繛鎺ョ被鍨嬶細

         濡傛灉璐︽埛娌℃湁SSLX509瑕佹眰锛屽苟涓斿鏋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'鐢ㄤ簬瀵硅繛鎺ュ皾璇曡繘琛岄檺瀹氾紝瀹㈡埛绔繀椤诲嚭绀轰竴涓敱CAissuer鈥欏彂甯冪殑鏈夋晥鐨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, ISSUERCIPHER閫夐」鍙互鍦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';

娉ㄦ剰锛SUBJECTISSUER鍊煎悇鑷簲琚綔涓轰竴涓崟涓瀛楃涓茶緭鍏ャ

REQUIRE鍚勯夐」涔嬮棿锛AND鍏抽敭璇嶆槸鑷夌殑銆

閫夐」鐨勯『搴忔棤鎵璋擄紝浣嗘槸閫夐」涓嶈兘琚寚瀹氫袱娆°

mysqld鍚姩鍚庯紝鎵鏈夌殑鏉冮檺琚鍏ュ瓨鍌ㄥ櫒涓傝浜嗚В璇︾粏璇存槑锛岃鍙傝5.7.7鑺傦紝鈥滄潈闄愭洿鏀逛綍鏃剁敓鏁堚

娉ㄦ剰锛屽鏋滄偍姝e湪浣跨敤琛ㄦ潈闄愭垨鍒楁潈闄愶紝鍗充娇鍙涓涓敤鎴蜂娇鐢紝鏈嶅姟鍣ㄤ篃浼氬鎵鏈夌敤鎴锋鏌ヨ〃鏉冮檺鍜屽垪鏉冮檺锛岃繖浼氱暐寰檷浣MySQL鐨勯熷害銆備笌姝ょ被浼硷紝濡傛灉鎮ㄥ鏌愪簺鐢ㄦ埛闄愬埗鏌ヨ銆佹洿鏂版垨杩炴帴鐨勬暟鐩紝鍒欐湇鍔″櫒蹇呴』鐩戞祴杩欎簺鍊笺

鏍囧噯SQL鐗堟湰鍜MySQL鐗堟湰鐨GRANT涔嬮棿鐨勬渶澶у尯鍒槸锛

         MySQL涓紝鏉冮檺涓hostnameusername鐨勭粍鍚堟湁鍏筹紝涓 鍗曚竴鐨username鏃犲叧銆

         鏍囧噯SQL涓嶆嫢鏈夊叏灞灞傜骇鎴栨暟鎹簱灞傜骇鏉冮檺锛屼篃涓嶆敮鎸MySQL鏀寔鐨勬墍鏈夋潈闄愮被鍨嬨

         MySQL涓嶆敮鎸佹爣鍑SQL TRIGGERUNDER鏉冮檺銆

         鏍囧噯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璇彞灏嗚鎷掔粷銆

13.5.1.4.聽RENAME USER璇硶

RENAME USER old_user TO new_user
    [, old_user TO new_user] ...

RENAME USER璇彞鐢ㄤ簬瀵瑰師鏈MySQL璐︽埛杩涜閲嶅懡鍚嶃傝浣跨敤RENAME USER锛屾偍蹇呴』鎷ユ湁鍏ㄥ眬CREATE USER鏉冮檺鎴mysql鏁版嵁搴UPDATE鏉冮檺銆傚鏋滄棫 璐︽埛涓嶅瓨鍦ㄦ垨鑰呮柊璐︽埛宸插瓨鍦紝鍒欎細鍑虹幇閿欒銆old_usernew_user鍊肩殑缁欏畾鏂规硶涓GRANT璇彞涓鏍枫

13.5.1.5.聽SET PASSWORD璇硶

SET PASSWORD = PASSWORD('some password')
SET PASSWORD FOR user = PASSWORD('some password')

SET PASSWORD璇彞鐢ㄤ簬鍚戜竴涓師鏈MySQL鐢ㄦ埛 璐︽埛璧嬩簣涓涓瘑鐮併

绗竴涓娉曚负褰撳墠鐢ㄦ埛璁剧疆瀵嗙爜銆傚凡浣跨敤涓涓潪鍖垮悕璐︽埛杩炴帴鍒版湇鍔″櫒涓婄殑浠讳綍瀹㈡埛鍗抽兘鍙互鏇存敼璇ヨ处鎴风殑瀵嗙爜銆

绗簩涓娉曚负褰撳墠鏈嶅姟鍣ㄤ富鏈轰笂鐨勪竴涓壒瀹氳处鎴疯缃瘑鐮併傚彧鏈夋嫢鏈mysql鏁版嵁搴UPDATE鏉冮檺鐨勫鎴风鍙互杩欎箞鍋氥user鍊煎簲浠user_name@host_name鐨勬牸寮忚缁欏畾锛屾澶user_namehost_namemysql.user琛ㄧ櫥褰曢」鐨UserHost鍒椾腑鍒楀嚭鐨勫畬鍏ㄤ竴鏍枫備妇渚嬭鏄庯紝濡傛灉鎮ㄦ湁涓涓櫥褰曢」锛UserHost鍒楀间负'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.1MySQL 4.1浠ュ悗鐨勬湇鍔″櫒锛屽垯鍦ㄩ槄璇5.7.9鑺傦紝鈥淢ySQL 4.1涓殑瀵嗙爜鍝堝笇澶勭悊鈥涔嬪墠锛屼笉鑳戒娇鐢ㄥ墠闈㈢殑SET PASSWORDUPDATE璇彞銆 瀵嗙爜鏍煎紡鍦MySQL 4.1涓彉鏇翠簡锛屽苟涓斿湪鐗瑰畾鎯呭喌涓嬶紝濡傛灉鎮ㄦ洿鏀瑰瘑鐮侊紝鎮ㄥ彲鑳芥棤娉曞湪杩炴帴鍒版湇鍔″櫒涓娿

鎮ㄥ彲浠ラ氳繃鎵цSELECT CURRENT_USER()瑙傜湅鎮ㄥ綋鍓嶇殑閴村畾user@host鐧诲綍椤广

13.5.2. 琛ㄧ淮鎶よ鍙

13.5.2.1.聽ANALYZE TABLE璇硶

ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

鏈鍙ョ敤浜庡垎鏋愬拰瀛樺偍琛ㄧ殑鍏抽敭瀛楀垎甯冦傚湪鍒嗘瀽鏈熼棿锛屼娇鐢ㄤ竴涓鍙栭攣瀹氬琛ㄨ繘琛岄攣瀹氥傝繖瀵逛簬MyISAM, BDBInnoDB琛ㄦ湁浣滅敤銆傚浜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锛夈

13.5.2.2.聽BACKUP TABLE璇硶

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

娑堟伅

13.5.2.3.聽CHECK TABLE璇硶

CHECK TABLE tbl_name [, tbl_name] ... [option] ...
 
option = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}

妫鏌ヤ竴涓垨澶氫釜琛ㄦ槸鍚︽湁閿欒銆CHECK TABLEMyISAMInnoDB琛ㄦ湁浣滅敤銆傚浜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, MEDIUMEXTENDED閫夐」锛屽垯瀵逛簬鍔ㄦ佹牸寮MyISAM琛紝榛樿妫鏌ョ被鍨嬫槸MEDIUM銆傝繖涓庡琛ㄨ繍琛myisamchk --medium-check tbl_name鐨勭粨鏋滅浉鍚屻傚浜庨潤鎬佹牸寮MyISAM琛紝榛樿妫鏌ョ被鍨嬩篃鏄MEDIUM锛岄櫎闈CHANGEDFAST宸茶鎸囧畾銆傚湪姝ゆ儏鍐典笅锛岄粯璁ゅ间负QUICK銆傚浜CHANGEDFAST锛岃鎵弿琚烦杩囷紝鍥犱负琛屾瀬灏戣鐮村潖銆

鎮ㄥ彲浠ョ粍鍚堟鏌ラ夐」锛屽涓嬮潰鐨勪緥瀛愭墍绀恒傝渚嬪瓙瀵硅〃杩涜浜嗕竴涓揩閫熸鏌ワ紝鏉ユ煡鐪嬭琛ㄦ槸鍚﹁姝g‘鍏抽棴锛

CHECK TABLE test_table FAST QUICK;

娉ㄩ噴锛氬湪鏈変簺鎯呭喌涓嬶紝CHECK TABLE浼氭洿鏀硅〃銆傚鏋滆〃琚爣璁颁负鈥corrupted鈥濇垨鈥not closed properly鈥濓紝鍒欏嚭鐜拌繖绉嶆儏鍐点備絾鏄CHECK TABLE涓嶄細鎵惧嚭琛ㄤ腑鐨勯棶棰樸傚湪杩欑鎯呭喌涓嬶紝CHECK TABLE浼氭妸琛ㄦ爣璁颁负鑹ソ銆

濡傛灉涓涓〃琚牬鍧忥紝寰堟湁鍙兘闂鍦ㄧ储寮曚腑锛岃屼笉鍦ㄦ暟鎹儴鍒嗕腑銆傛墍鏈夊墠杩扮殑妫鏌ョ被鍨嬮兘鍙互褰诲簳鍦版鏌ョ储寮曪紝鍥犳锛屽彲浠ユ壘鍑哄鏁扮殑閿欒銆

濡傛灉鎮ㄥ彧鎯宠妫鏌ユ偍鍋囧畾鐨勮〃鏄壇濂界殑锛屾偍搴旇涓嶄娇鐢ㄦ鏌ラ夐」鎴QUICK閫夐」銆傚綋鎮ㄦ椂闂村寙蹇欐椂锛屽簲浣跨敤QUICKQUICK鏃犳硶鎵惧嚭鏁版嵁鏂囦欢涓殑閿欒鐨勯闄╅潪甯稿皬銆傦紙鍦ㄥ鏁版儏鍐典笅锛屽湪姝e父浣跨敤涓紝MySQL搴旇兘鍦ㄦ暟鎹枃浠朵腑鎵惧嚭閿欒銆傚鏋滄壘鍑轰簡閿欒锛岃〃琚爣璁颁负鈥corrupted鈥濓紝骞朵笉鑳借浣跨敤锛岀洿鍒颁慨澶嶄负姝€傦級

濡傛灉鎮ㄦ兂瑕佹椂甯告鏌ヨ〃锛FASTCHANGED澶氭暟鎯呭喌涓嬩粠鍘熸湰涓浣跨敤锛堜緥濡傦紝浠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浠ュ鐨勫笺

13.5.2.4.聽CHECKSUM TABLE璇硶

CHECKSUM TABLE tbl_name [, tbl_name] ... [ QUICK | EXTENDED ]

鎶ュ憡涓涓〃鏍¢獙鍜屻

濡傛灉鎸囧畾浜QUICK锛屽垯鎶ュ憡娲绘ц〃鏍¢獙鍜岋紝鍚﹀垯鎶ュ憡NULL銆傝繖鏄潪甯稿揩鐨勩傛椿鎬ц〃閫氳繃鎸囧畾CHECKSUM1琛ㄩ夐」鍚敤锛岀洰鍓嶅彧鏀寔鐢ㄤ簬MyISAM琛ㄣ傝鍙傝13.1.5鑺傦紝鈥淐REATE TABLE璇硶鈥

EXTENDED妯″紡涓嬶紝鏁翠釜琛ㄨ涓琛屼竴琛屽湴璇诲彇锛屽苟璁$畻鏍¢獙鍜屻傚浜庡ぇ鍨嬭〃锛岃繖鏄潪甯告參鐨勩

榛樿鎯呭喌涓嬶紝濡傛灉鏃㈡病鏈夋寚瀹QUICK锛屼篃娌℃湁鎸囧畾EXTENDED锛屽苟涓斿鏋滆〃瀛樺偍寮曟搸鏀寔锛屽垯MySQL杩斿洖涓涓椿鎬ф牎楠屽拰锛屽惁鍒欎細瀵硅〃杩涜鎵弿銆

CHECKSUM TABLE瀵逛簬涓嶅瓨鍦ㄧ殑琛ㄤ細杩斿洖NULL銆傚浜庤繖绉嶆儏鍐碉紝浼氱敓鎴愪竴涓鍛娿

13.5.2.5.聽OPTIMIZE TABLE璇硶

OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

濡傛灉鎮ㄥ凡缁忓垹闄や簡琛ㄧ殑涓澶ч儴鍒嗭紝鎴栬呭鏋滄偍宸茬粡瀵瑰惈鏈夊彲鍙橀暱搴﹁鐨勮〃锛堝惈鏈VARCHAR, BLOBTEXT鍒楃殑琛級杩涜浜嗗緢澶氭洿鏀癸紝鍒欏簲浣跨敤OPTIMIZE TABLE銆傝鍒犻櫎鐨勮褰曡淇濇寔鍦ㄩ摼鎺ユ竻鍗曚腑锛屽悗缁殑INSERT鎿嶄綔浼氶噸鏂颁娇鐢ㄦ棫鐨勮褰曚綅缃傛偍鍙互浣跨敤OPTIMIZE TABLE鏉ラ噸鏂板埄鐢ㄦ湭浣跨敤鐨勭┖闂达紝骞舵暣鐞嗘暟鎹枃浠剁殑纰庣墖銆

鍦ㄥ鏁扮殑璁剧疆涓紝鎮ㄦ牴鏈笉闇瑕佽繍琛OPTIMIZE TABLE銆傚嵆浣挎偍瀵瑰彲鍙橀暱搴︾殑琛岃繘琛屼簡澶ч噺鐨勬洿鏂帮紝鎮ㄤ篃涓嶉渶瑕佺粡甯歌繍琛岋紝姣忓懆涓娆℃垨姣忔湀涓娆″嵆鍙紝鍙鐗瑰畾鐨勮〃杩愯銆

OPTIMIZE TABLE鍙MyISAM, BDBInnoDB琛ㄨ捣浣滅敤銆

瀵逛簬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鍛戒护鐨勪綔鐢ㄧ浉褰撲簬涓涓鍒朵富鏈嶅姟鍣紝鍦ㄩ粯璁ゆ儏鍐典笅锛岃繖浜涘懡浠ゅ皢琚鍒跺埌澶嶅埗浠庡睘鏈嶅姟鍣ㄤ腑銆

13.5.2.6.聽REPAIR TABLE璇硶

REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE
    tbl_name [, tbl_name] ... [QUICK] [EXTENDED] [USE_FRM]

REPAIR TABLE鐢ㄤ簬淇琚牬鍧忕殑琛ㄣ傞粯璁ゆ儏鍐典笅锛REPAIR TABLEmyisamchk --recover tbl_name鍏锋湁鐩稿悓鐨勬晥鏋溿REPAIR TABLEMyISAMARCHIVE琛ㄨ捣浣滅敤銆傝鍙傝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璇彞銆傦紙閫氳繃鍒朵綔涓涓浠芥潵鍚姩鏄竴涓ソ鍔炴硶銆傦級鍐嶆渶涓嶅埄鎯呭喌涓嬶紝鎮ㄥ彲浠ユ湁涓涓柊鐨勫共鍑鐨勭储寮曟枃浠讹紝涓嶅惈鏈夊叧鏁版嵁鏂囦欢鐨勪俊鎭傜劧鍚庯紝鎮ㄦ墽琛岀殑涓嬩竴涓搷浣滀細瑕嗙洊鏁版嵁鏂囦欢銆傝繖寰堝皯鍙戠敓锛屼絾鏄槸鏈夊彲鑳界殑銆

13.5.2.7.聽RESTORE 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

娑堟伅

13.5.3.聽SET璇硶

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@@localSESSION@@session鍦板悓涔夎瘝銆傚鏋滄病鏈変慨鏀圭锛屽垯SET璁剧疆浼氳瘽鍙橀噺銆

鏀寔绯荤粺鍙橀噺鐨@@var_name璇硶锛屼互渚夸娇MySQL璇硶涓庡叾瀹冩暟鎹簱绯荤粺鐩稿吋瀹广

濡傛灉鎮ㄥ湪鍚屼竴涓鍙ヤ腑璁剧疆澶氫釜绯荤粺鍙橀噺锛屽垯鏈鍚庝竴涓GLOBALSESSION閫夐」琚敤浜庢病鏈夋寚瀹氭ā寮忕殑鍙橀噺銆

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 SETSET NAMES锛夈備緥濡傦細

mysql> SELECT @@AUTOCOMMIT;
+--------------+
| @@AUTOCOMMIT |
+--------------+
|            1 |
+--------------+

         AUTOCOMMIT = {0 | 1}

璁剧疆autocommit妯″紡銆傚鏋滆缃负1锛屽垯鎵鏈夊琛ㄧ殑鏇存敼浼氱珛鍒荤敓鏁堛傚鏋滆缃负0锛屽垯鎮ㄥ繀椤讳娇鐢COMMIT鏉ユ帴鍙椾竴涓簨鍔★紝鎴栦娇鐢ROLLBACK鏉ュ彇娑堝畠銆傚鏋滄偍鎶AUTOCOMMIT妯″紡浠0鏀逛负1锛屽垯MySQL浼氬寮鏀句簨鍔℃墽琛屼竴涓嚜鍔COMMIT銆傚紑濮嬩竴涓簨鍔$殑鍙︿竴绉嶆柟娉曟槸浣跨敤涓涓START TRANSACTIONBEGIN璇彞銆傝鍙傝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_clientcharacter_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

鐢ㄤ簬璁剧疆灏嗚浠ヤ笅INSERTALTER 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_connectioncharacter_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瀛愬彞涓笉浣跨敤鍏抽敭瀛楃殑UPDATEDELETE璇彞銆傝繖鏍凤紝褰撳叧閿瓧浣跨敤涓嶆纭椂锛屼篃鏈夊彲鑳界悊瑙UPDATEDELETE璇彞銆傝繖鏍峰氨鍙互鏇存敼鎴栧垹闄ゅぇ閲忕殑琛屻

         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...SELECTINSERT 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瀵煎叆澶у瀷琛ㄧ殑閫熷害銆

13.5.4.聽SHOW璇硶

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 %鈥欏拰鈥_鈥欓氶厤绗︾殑瀛楃涓层傚浜庢妸璇彞杈撳嚭鍊奸檺瀹氫负鍖归厤鍊硷紝鏈牱寮忔槸鏈夌敤鐨勩

13.5.4.1.聽SHOW CHARACTER SET璇硶

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鍒楁樉绀虹敤浜庡瓨鍌ㄤ竴涓瓧绗︾殑鏈澶х殑瀛楄妭鏁扮洰銆

13.5.4.2.聽SHOW COLLATION璇硶

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涓庡瀛楃涓诧紙鍦ㄥ瓧绗﹂泦涓〃杈撅級鍒嗙被鎵闇鐨勫瓨鍌ㄥ櫒鐨勬暟閲忔湁鍏炽

13.5.4.3.聽SHOW COLUMNS璇硶

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 FIELDSSHOW COLUMNS鐨勫悓涔夎瘝銆傛偍涔熷彲浠ヤ娇鐢mysqlshow db_name tbl_name鍛戒护鍒椾妇琛ㄧ殑鍚勫垪銆

DESCRIBE璇彞鎻愪緵涓SHOW COLUMNS鐩歌繎鐨勪俊鎭傝鍙傝13.3.1鑺傦紝鈥淒ESCRIBE璇硶锛堣幏鍙栧叧浜庡垪鐨勪俊鎭級鈥

13.5.4.4.聽SHOW CREATE DATABASE璇硶

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

13.5.4.5.聽SHOW CREATE TABLE璇硶

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璇硶鈥

13.5.4.6.聽SHOW DATABASES璇硶

SHOW {DATABASES | SCHEMAS} [LIKE 'pattern']

SHOW DATABASES鍙互鍦MySQL鏈嶅姟鍣ㄤ富鏈轰笂鍒椾妇鏁版嵁搴撱傛偍涔熷彲浠ヤ娇鐢mysqlshow鍛戒护寰楀埌姝ゆ竻鍗曘傛偍鍙兘鐪嬪埌鎮ㄦ嫢鏈夋煇浜涙潈闄愮殑鏁版嵁搴擄紝闄ら潪鎮ㄦ嫢鏈夊叏灞SHOW DATABASES鏉冮檺銆

濡傛灉鏈嶅姟鍣ㄤ互--skip-show-database閫夐」涓鸿捣濮嬶紝鍒欐偍鏍规湰涓嶈兘浣跨敤鏈鍙ワ紝闄ら潪鎮ㄦ嫢鏈SHOW DATABASES鏉冮檺銆

涔熷彲浠ヤ娇鐢SHOW SCHEMAS

13.5.4.7.聽SHOW ENGINE璇硶

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] LOGSSHOW INNODB STATUS

SHOW ENGINE鍙互浠MySQL 4.1.2璧蜂娇鐢ㄣ

13.5.4.8.聽SHOW ENGINES璇硶

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绔狅細瀛樺偍寮曟搸鍜岃〃绫诲瀷

13.5.4.9.聽SHOW ERRORS璇硶

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璇硶鈥

13.5.4.10.聽SHOW GRANTS璇硶

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();

13.5.4.11.聽SHOW INDEX璇硶

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 TABLEmyisamchk -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 KEYSSHOW INDEX鐨勫悓涔夎瘝銆傛偍涔熷彲浠ヤ娇鐢mysqlshow -k db_name tbl_name鍛戒护鍒椾妇涓涓〃鐨勭储寮曘

13.5.4.12.聽SHOW INNODB STATUS璇硶

SHOW INNODB STATUS

MySQL 5.1涓紝杩欐槸SHOW ENGINE INNODB STATUS鐨勫悓涔夎瘝锛屼絾涓嶈禐鎴愪娇鐢ㄣ傝鍙傝13.5.4.7鑺傦紝鈥淪HOW ENGINE璇硶鈥

13.5.4.13.聽SHOW LOGS璇硶

SHOW [BDB] LOGS

MySQL 5.1涓紝杩欐槸SHOW ENGINE BDB LOGS鐨勫悓涔夎瘝锛屼絾鏄笉璧炴垚浣跨敤銆傝鍙傝13.5.4.7鑺傦紝鈥淪HOW ENGINE璇硶鈥

13.5.4.14.聽SHOW OPEN TABLES璇硶

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

琛ㄥ悕绉版槸鍚﹁閿佸畾銆傚悕绉伴攣瀹氱敤浜庡彇娑堣〃鎴栧琛ㄨ繘琛岄噸鍛藉悕绛夋搷浣溿

13.5.4.15.聽SHOW PRIVILEGES璇硶

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

13.5.4.16.聽SHOW PROCESSLIST璇硶

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 TABLEOPTIMIZE TABLE锛涘垯浼氬嚭鐜伴氱煡銆

         waiting for handler insert

INSERT DELAYED绠$悊绋嬪簭宸茬粡澶勭悊浜嗘墍鏈夊浜庣瓑寰呯姸鎬佺殑鎻掑叆锛屽苟姝e湪绛夊緟鏂版彃鍏ャ

澶氭暟鐘舵佸搴斾簬闈炲父蹇殑鎿嶄綔銆傚鏋滀竴涓嚎绋嬪湪杩欎簺鐘舵佷笅鍋滅暀浜嗘暟绉掞紝鍒欏彲鑳芥槸鏈夐棶棰橈紝闇瑕佽繘琛岃皟鏌ャ

鏈変竴浜涘叾瀹冪殑鐘舵侊紝鍦ㄥ墠闈㈢殑娓呭崟涓病鏈夋彁鍙婏紝浣嗘槸鍏朵腑鏈夊緢澶氱姸鎬佸浜庢煡鎵炬湇鍔″櫒涓殑绋嬪簭閿欒鏄湁鐢ㄧ殑銆

13.5.4.17.聽SHOW STATUS璇硶

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锛屾偍鍙互寰楀埌鎵鏈夊綋鍓嶈繛鎺ョ殑鐘舵佸笺傚鏋滄偍涓や釜閫夐」閮戒笉浣跨敤锛屽垯榛樿鍊间负SESSIONLOCALSESSION鐨勫悓涔夎瘝銆

娉ㄦ剰锛屾湁浜涚姸鎬佸彉閲忓彧鏈変竴涓叏灞鍊笺傚浜庤繖浜涘彉閲忥紝浣跨敤GLOBALSESSION浼氬緱鍒板悓鏍风殑鍊笺

13.5.4.18.聽SHOW TABLE STATUS璇硶

SHOW TABLE STATUS [FROM db_name] [LIKE 'pattern']

SHOW TABLE STATUS鐨勬ц川涓SHOW TABLE绫讳技锛屼笉杩囷紝鍙互鎻愪緵姣忎釜琛ㄧ殑澶ч噺淇℃伅銆傛偍涔熷彲浠ヤ娇鐢mysqlshow --status db_name鍛戒护寰楀埌姝ゆ竻鍗曘

鏈鍙ヤ篃鏄剧ず瑙嗗浘淇℃伅銆

瀵逛簬NDB Cluster琛紝鏈鍙ョ殑杈撳嚭鏄剧ずAvg_row_lengthData_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琛ㄧ殑鏍煎紡琚姤鍛婁负RedundantCompact

         Rows

琛岀殑鏁扮洰銆傞儴鍒嗗瓨鍌ㄥ紩鎿庯紝濡MyISAM锛屽瓨鍌ㄧ簿纭殑鏁扮洰銆

瀵逛簬鍏跺畠瀛樺偍寮曟搸锛屾瘮濡InnoDB锛屾湰鍊兼槸涓涓ぇ绾︾殑鏁帮紝涓庡疄闄呭肩浉宸彲杈4050锛呫傚湪杩欎簺鎯呭喌涓嬶紝浣跨敤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_lengthIndex_length鍊艰繎浼间簬琚暣搴忕殑瀛樺偍鍣ㄧ殑瀹為檯鍊笺傛暣搴忕畻娉曢鐣欎簡澶ч噺鐨勫瓨鍌ㄥ櫒锛屼互鍑忓皯鏁村簭鎿嶄綔鐨勬暟閲忋

瀵逛簬瑙嗗浘锛岀敱SHOW TABLE STATUS鏄剧ず鐨勬墍鏈夊瓧娈靛潎涓NULL銆備緥澶栨儏鍐垫槸Name鎸囩ず涓鸿鍥惧悕绉板悓鏃Comment绉颁负瑙嗗浘銆

13.5.4.19.聽SHOW TABLES璇硶

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杈撳嚭涓樉绀恒

13.5.4.20.聽SHOW TRIGGERS璇硶

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 TRIGGERSLIKE瀛愬彞鏃讹紝寰呭尮閰嶇殑琛ㄨ揪寮忥紙expr锛変細涓庤Е鍙戠▼搴忓畾涔夋椂鎵鍦ㄧ殑琛ㄧ殑鍚嶇О鐩告瘮杈冿紝鑰屼笉涓庤Е鍙戠▼搴忕殑鍚嶇О鐩告瘮杈冿細

mysql> SHOW TRIGGERS LIKE 'ins%';
Empty set (0.01 sec)

瀵规湰璇彞杈撳嚭涓殑鍚勫垪鐨勭畝瑕佽В閲婂涓嬶細

         Trigger: 瑙﹀彂绋嬪簭鐨勫悕绉般

         Event: 璋冪敤瑙﹀彂绋嬪簭鐨勬椂闂淬傚繀椤讳负'INSERT', 'UPDATE''DELETE'.涔嬩竴銆

         Table: 瑙﹀彂绋嬪簭瀹氫箟鏃跺搴旂殑琛ㄣ

         Statement: 褰撹Е鍙戠▼搴忚璋冪敤鏃舵墽琛岀殑璇彞銆傝繖涓庡湪INFORMATION_SCHEMA.TRIGGERSACTION_STATEMENT鍒椾腑鏄剧ず鐨勬枃鏈竴鏍枫

         Timing: 'BEFORE''AFTER'涓や釜鍊间箣涓銆

         Created: 鐩墠锛屾湰鍒楃殑鍊间负NULL

涓轰簡鎵цSHOW TRIGGERS锛屾偍蹇呴』鎷ユ湁SUPER鏉冮檺銆

鍚屾椂涔熻23.1.16鑺傦紝鈥淚NFORMATION_SCHEMA TRIGGERS琛ㄢ

13.5.4.21.聽SHOW VARIABLES璇硶

SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern']

SHOW VARIABLES鏄剧ず浜嗛儴闂MySQL绯荤粺鍙橀噺鐨勫笺傛湰淇℃伅涔熷彲浠ヤ娇鐢mysqladmin variables鍛戒护鑾峰緱銆

浣跨敤GLOBAL閫夐」锛屾偍鍙互鑾峰緱琚敤浜MySQL鏂拌繛鎺ョ殑鍊笺備娇鐢SESSION锛屾偍鍙互寰楀埌瀵逛簬褰撳墠杩炴帴璧锋晥鐨勫笺傚鏋滄偍涓や釜閫夐」閮戒笉浣跨敤锛岄粯璁ゅ间负SESSION

LOCALSESSION鐨勫悓涔夎瘝銆

濡傛灉榛樿鍊间笉鍚堥傦紝褰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      |
+-----------------------+----------+

13.5.4.22.聽SHOW WARNINGS璇硶

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 TABLEALTER TABLEDML璇彞锛屼細鐢熸垚璀﹀憡銆

浠ヤ笅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锛屼娇鈥滄敞鎰忊濈骇鍒殑璀﹀憡涓嶈璁板綍銆

13.5.5. 鍏跺畠绠$悊璇彞

13.5.5.1.聽CACHE INDEX璇硶

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, t2t3鍒嗛厤鍒板悕涓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'

榛樿鎯呭喌涓嬶紝琛ㄧ储寮曡鍒嗛厤缁欏湪鏈嶅姟鍣ㄥ惎鍔ㄦ椂琚垱寤虹殑涓伙紙榛樿锛夐敭缂撳瓨銆傚綋涓涓敭楂橀熺紦鍐茶鐮村潖鏃讹紝鎵鏈夎鍒嗛厤鍒版缂撳瓨涓殑绱㈠紩浼氬啀娆¤鍒嗛厤缁欓粯璁ょ殑 閿珮閫熺紦鍐层

绱㈠紩鐨勫垎閰嶄細瀵规湇鍔″櫒浜х敓鍏ㄥ眬鎬у奖鍝嶏細濡傛灉涓涓鎴风鎶婁竴涓储寮曞垎閰嶇粰涓涓粰瀹氱殑缂撳瓨锛屽垯涓嶈浠涔堝鎴风鍙戝竷鏌ヨ锛屾湰缂撳瓨閮借鐢ㄤ簬鎵鏈夋秹鍙婄储寮曠殑鏌ヨ銆

13.5.5.2.聽FLUSH璇硶

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蹇界暐SIGHUPSIGQUIT锛夈

濡傛灉鏈嶅姟鍣ㄤ娇鐢--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 SLAVEFLUSH TABLES WITH READ LOCK閮戒笉浼氳璁板叆鏃ュ織锛屽洜涓哄鏋滃畠浠澶嶅埗鍒颁竴涓粠灞炴湇鍔″櫒涓婏紝浼氬鑷村嚭鐜伴棶棰樸

鎮ㄤ篃鍙互浣跨敤flush-hosts, flush-logs, flush-privileges, flush-statusflush-tables鍛戒护璁块棶鍚湁mysqladmin搴旂敤绋嬪簭鐨勮鍙ャ

娉ㄩ噴锛氬湪MySQL 5.1.2-alpha涓紝涓嶅彲鑳藉湪宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑鍙戝竷FLUSH璇彞銆備笉杩囷紝鎮ㄥ彲浠ュ湪宸插瓨鍌ㄧ殑杩囩▼涓娇鐢FLUSH锛屽彧瑕佸畠浠笉浼氫粠宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑琚皟鐢ㄣ傝鍙傝I.1鑺傦紝鈥滃瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勯檺鍒垛

瑕佷簡瑙f湁鍏RESET璇彞涓庡鍒跺悓鏃朵娇鐢ㄧ殑淇℃伅锛屼篃鍙互瑙13.5.5.5鑺傦紝鈥淩ESET璇硶鈥

13.5.5.3.聽KILL璇硶

KILL [CONNECTION | QUERY] thread_id

姣忎釜涓mysqld鐨勮繛鎺ラ兘鍦ㄤ竴涓嫭绔嬬殑绾跨▼閲岃繍琛岋紝鎮ㄥ彲浠ヤ娇鐢SHOW PROCESSLIST璇彞鏌ョ湅鍝簺绾跨▼姝e湪杩愯锛屽苟浣跨敤KILL thread_id璇彞缁堟涓涓嚎绋嬨

KILL鍏佽鑷夌殑CONNECTIONQUERY淇敼绗︼細

         KILL CONNECTION涓庝笉鍚慨鏀圭鐨KILL涓鏍凤細瀹冧細缁堟涓庣粰瀹氱殑thread_id鏈夊叧鐨勮繛鎺ャ

         KILL QUERY浼氱粓姝㈣繛鎺ュ綋鍓嶆鍦ㄦ墽琛岀殑璇彞锛屼絾鏄細淇濇寔杩炴帴鐨勫師鐘躲

濡傛灉鎮ㄦ嫢鏈PROCESS鏉冮檺锛屽垯鎮ㄥ彲浠ユ煡鐪嬫墍鏈夌嚎绋嬨傚鏋滄偍鎷ユ湁SUPER鏉冮檺锛屾偍鍙互缁堟鎵鏈夌嚎绋嬪拰璇彞銆傚惁鍒欙紝鎮ㄥ彧鑳芥煡鐪嬪拰缁堟鎮ㄨ嚜宸辩殑绾跨▼鍜岃鍙ャ

鎮ㄤ篃鍙互浣跨敤mysqladmin processlistmysqladmin kill鍛戒护鏉ユ鏌ュ拰缁堟绾跨▼銆

娉ㄩ噴锛氭偍涓嶈兘鍚屾椂浣跨敤KILLEmbedded MySQL Server搴擄紝鍥犱负鍐呮鐨勬湇鍔″櫒鍙繍琛屼富鏈哄簲鐢ㄧ▼搴忕殑绾跨▼銆傚畠涓嶈兘鍒涘缓浠讳綍鑷韩鐨勮繛鎺ョ嚎绋嬨

褰撴偍杩涜涓涓KILL鏃讹紝瀵圭嚎绋嬭缃竴涓壒鏈夌殑缁堟鏍囪銆傚湪澶氭暟鎯呭喌涓嬶紝绾跨▼缁堟鍙兘瑕佽姳涓浜涙椂闂达紝杩欐槸鍥犱负缁堟鏍囪鍙細鍦ㄥ湪鐗瑰畾鐨勯棿闅旇妫鏌ワ細

         SELECT, ORDER BYGROUP BY寰幆涓紝鍦ㄨ鍙栦竴缁勮鍚庢鏌ユ爣璁般傚鏋滆缃簡缁堟鏍囪锛屽垯璇ヨ鍙ヨ鏀惧純銆

         ALTER TABLE杩囩▼涓紝鍦ㄦ瘡缁勮浠庡師鏉ョ殑琛ㄤ腑琚鍙栧墠锛屾鏌ョ粓姝㈡爣璁般傚鏋滆缃簡缁堟鏍囪锛屽垯璇彞琚斁寮冿紝涓存椂琛ㄨ鍒犻櫎銆

         UPDATEDELETE杩愯鏈熼棿锛屽湪姣忎釜缁勮鍙栦箣鍚庝互鍙婃瘡涓凡鏇磋鎴栧凡鍒犻櫎鐨勮涔嬪悗锛屾鏌ョ粓姝㈡爣璁般傚鏋滅粓姝㈡爣璁拌璁剧疆锛屽垯璇ヨ鍙ヨ鏀惧純銆傛敞鎰忥紝濡傛灉鎮ㄦ鍦ㄤ娇鐢ㄤ簨鍔★紝鍒欏彉鏇翠笉浼氳 鍥炴粴銆

         GET_LOCK()浼氭斁寮冨拰杩斿洖NULL

         INSERT DELAYED绾跨▼浼氬揩閫熷湴鍒锋柊锛堟彃鍏ワ級瀹冨湪瀛樺偍鍣ㄤ腑鐨勬墍鏈夌殑琛岋紝鐒跺悗缁堟銆

         濡傛灉绾跨▼鍦ㄨ〃閿佸畾绠$悊绋嬪簭涓紙鐘舵侊細閿佸畾锛夛紝鍒欒〃閿佸畾琚揩閫熷湴鏀惧純銆

         濡傛灉鍦ㄥ啓鍏ヨ皟鐢ㄤ腑锛岀嚎绋嬫鍦ㄧ瓑寰呯┖闂茬殑纾佺洏绌洪棿锛屽垯鍐欏叆琚斁寮冿紝骞朵即闅"disk full"閿欒娑堟伅銆

         璀﹀憡锛氬MyISAM琛ㄧ粓姝竴涓REPAIR TABLEOPTIMIZE TABLE鎿嶄綔浼氬鑷村嚭鐜颁竴涓鎹熷潖鐨勬病鏈夌敤鐨勮〃銆傚杩欐牱鐨勮〃鐨勪换浣曡鍙栨垨鍐欏叆閮戒細澶辫触锛岀洿鍒版偍鍐嶆浼樺寲鎴栦慨澶嶅畠锛堜笉涓柇锛夈

13.5.5.4.聽LOAD INDEX INTO CACHE璇硶

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淇敼绗﹀彧浼氬鑷寸储寮曠殑闈炲彾瀛愯妭鐐硅棰勮浇鍏ャ

瀵逛簬琛t1t2锛屼互涓嬭鍙ヤ細棰勮浇鍏ョ储寮曠殑鑺傜偣锛堢储寮曠粍锛夛細

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璇硶鍏佽鎮ㄦ寚瀹氾紝鍙湁鏉ヨ嚜琛ㄧ殑鐗瑰畾鐨勭储寮曞簲琚杞藉叆銆備絾鏄紝褰撳墠瀹炴柦浼氭妸鎵鏈夌殑琛ㄧ储寮曢杞藉叆缂撳瓨涓紝鎵浠ヤ竴瀹氳鎸囧畾琛ㄥ悕绉帮紝涓嶈兘鎸囧畾鍏跺畠鐨勩

13.5.5.5.聽RESET璇硶

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璇彞鈥

13.6. 澶嶅埗璇彞

鏈妭鍙欒堪浜嗕笌澶嶅埗鏈夊叧鐨SQL璇彞銆備竴缁勮鍙ヨ鐢ㄤ簬鎺у埗涓绘湇鍔″櫒銆傚叾瀹冪殑琚敤浜庢帶鍒朵粠灞炴湇鍔″櫒銆

13.6.1. 鐢ㄤ簬鎺у埗涓绘湇鍔″櫒鐨凷QL璇彞

鍙互閫氳繃SQL鐣岄潰鎺у埗澶嶅埗銆傛湰鑺傝璁轰簡鐢ㄤ簬绠$悊涓诲鍒舵湇鍔″櫒鐨勮鍙ャ13.6.2鑺傦紝鈥滅敤浜庢帶鍒朵粠鏈嶅姟鍣ㄧ殑SQL璇彞鈥璁ㄨ浜嗙敤浜庣鐞嗕粠灞炴湇鍔″櫒鐨勮鍙ャ

13.6.1.1.聽PURGE MASTER LOGS璇硶

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'鏍煎紡銆MASTERBINARY鏄悓涔夎瘝銆

濡傛灉鎮ㄦ湁涓涓椿鎬х殑浠庡睘鏈嶅姟鍣紝璇ユ湇鍔″櫒褰撳墠姝e湪璇诲彇鎮ㄦ鍦ㄨ瘯鍥惧垹闄ょ殑鏃ュ織涔嬩竴锛屽垯鏈鍙ヤ笉浼氳捣浣滅敤锛岃屾槸浼氬け璐ワ紝骞朵即闅忎竴涓敊璇備笉杩囷紝濡傛灉浠庡睘鏈嶅姟鍣ㄦ槸浼戞鐨勶紝骞朵笖鎮ㄧ宸ф竻鐞嗕簡鍏舵兂瑕佽鍙栫殑鏃ュ織涔嬩竴锛屽垯浠庡睘鏈嶅姟鍣ㄥ惎鍔ㄥ悗涓嶈兘澶嶅埗銆傚綋浠庡睘鏈嶅姟鍣ㄦ鍦ㄥ鍒舵椂锛屾湰璇彞鍙互瀹夊叏杩愯銆傛偍涓嶉渶瑕佸仠姝㈠畠浠

瑕佹竻鐞嗘棩蹇楋紝闇鎸夌収浠ヤ笅姝ラ锛

1.    鍦ㄦ瘡涓粠灞炴湇鍔″櫒涓婏紝浣跨敤SHOW SLAVE STATUS鏉ユ鏌ュ畠姝e湪璇诲彇鍝釜鏃ュ織銆

2.    浣跨敤SHOW MASTER LOGS鑾峰緱涓绘湇鍔″櫒涓婄殑涓绯诲垪鏃ュ織銆

3.    鍦ㄦ墍鏈夌殑浠庡睘鏈嶅姟鍣ㄤ腑鍒ゅ畾鏈鏃╃殑鏃ュ織銆傝繖涓槸鐩爣鏃ュ織銆傚鏋滄墍鏈夌殑浠庡睘鏈嶅姟鍣ㄦ槸鏇存柊鐨勶紝杩欐槸娓呭崟涓婄殑鏈鍚庝竴涓棩蹇椼

4.    鍒朵綔鎮ㄥ皢瑕佸垹闄ょ殑鎵鏈夋棩蹇楃殑澶囦唤銆傦紙杩欎釜姝ラ鏄嚜閫夌殑锛屼絾鏄缓璁噰鐢ㄣ傦級

5.    娓呯悊鎵鏈夌殑鏃ュ織锛屼絾鏄笉鍖呮嫭鐩爣鏃ュ織銆

13.6.1.2.聽RESET MASTER璇硶

RESET MASTER

鍙互鍒犻櫎鍒椾簬绱㈠紩鏂囦欢涓殑鎵鏈変簩杩涘埗鏃ュ織锛屾妸浜岃繘鍒舵棩蹇楃储寮曟枃浠堕噸鏂拌缃负绌猴紝骞跺垱寤轰竴涓柊鐨勪簩杩涘埗鏃ュ織鏂囦欢銆

13.6.1.3.聽SET SQL_LOG_BIN璇硶

SET SQL_LOG_BIN = {0|1}

濡傛灉瀹㈡埛绔娇鐢ㄤ竴涓湁SUPER鏉冮檺鐨勮处鎴疯繛鎺ワ紝鍒欏彲浠ョ鐢ㄦ垨鍚敤褰撳墠杩炴帴鐨勪簩杩涘埗鏃ュ織璁板綍銆傚鏋滃鎴风娌℃湁姝ゆ潈闄愶紝鍒欒鍙ヨ鎷掔粷锛屽苟浼撮殢鏈夐敊璇

13.6.1.4.聽SHOW BINLOG EVENTS璇硶

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锛氱敤浜庡鐞嗕簩杩涘埗鏃ュ織鏂囦欢鐨勫疄鐢ㄥ伐鍏封

13.6.1.5.聽SHOW MASTER LOGS璇硶

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 LOGSSHOW MASTER LOGS鐩稿綋銆

13.6.1.6.聽SHOW MASTER STATUS璇硶

SHOW MASTER STATUS

鐢ㄤ簬鎻愪緵涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楁枃浠剁殑鐘舵佷俊鎭備緥濡傦細

mysql > SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73       | test         | manual,mysql     |
+---------------+----------+--------------+------------------+

13.6.1.7.聽SHOW SLAVE HOSTS璇硶

SHOW SLAVE HOSTS

鐢ㄤ簬鏄剧ず褰撳墠浣跨敤涓绘湇鍔″櫒娉ㄥ唽鐨勫鍒朵粠灞炴湇鍔″櫒鐨勬竻鍗曘備笉浠--report-host=slave_name閫夐」涓哄紑澶寸殑浠庡睘鏈嶅姟鍣ㄤ笉浼氭樉绀哄湪鏈竻鍗曚腑銆

13.6.2. 鐢ㄤ簬鎺у埗浠庢湇鍔″櫒鐨凷QL璇彞

澶嶅埗鎿嶄綔鍙互閫氳繃SQL鐣岄潰鎺у埗銆傛湰鑺傝璁轰簡鐢ㄤ簬绠$悊浠庡睘澶嶅埗鏈嶅姟鍣ㄧ殑璇彞銆13.6.1鑺傦紝鈥滅敤浜庢帶鍒朵富鏈嶅姟鍣ㄧ殑SQL璇彞鈥璁ㄨ浜嗙敤浜庣鐞嗕富鏈嶅姟鍣ㄧ殑璇彞銆

13.6.2.1.聽CHANGE MASTER TO璇硶

  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_KEYMASTER_SSL_CIPHER鐢ㄤ簬鍚戜粠灞炴湇鍔″櫒鎻愪緵鏈夊叧濡備綍涓庝富鏈嶅姟鍣ㄨ繛鎺ョ殑淇℃伅銆

鍗充娇瀵逛簬鍦ㄧ紪璇戞椂娌℃湁SSL鏀寔鐨勪粠灞炴湇鍔″櫒锛SSL閫夐」(MASTER_SSL, MASTER_SSL_CA, MASTER_SSL_CAPATH, MASTER_SSL_CERT, MASTER_SSL_KEYMASTER_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_HOSTMASTER_PORT鏄富鏈嶅姟鍣ㄤ富鏈哄拰鍏TCP/IP鎺ュ彛鐨勪富鏈哄悕锛堟垨IP鍦板潃锛夈傛敞鎰忥紝濡傛灉MASTER_HOSTlocalhost鐩哥瓑锛岄偅涔堬紝鍜MySQL鐨勫叾瀹冮儴鍒嗕竴鏍凤紝鎺ュ彛鍙互琚拷鐣ワ紙渚嬪锛屽鏋滃彲浠ヤ娇鐢Unix鎻掓Ы鏂囦欢锛夈

濡傛灉鎮ㄦ寚瀹氫簡MASTER_HOSTMASTER_PORT锛屽垯浠庡睘鏈嶅姟鍣ㄤ細鍋囧畾涓绘湇鍔″櫒涓庝互鍓嶄笉涓鏍凤紙鍗充娇鎮ㄦ寚瀹氱殑涓绘満鎴栨帴鍙e间笌褰撳墠鍊兼槸涓鏍风殑銆傦級鍦ㄦ鎯呭喌涓嬶紝涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楃殑鍚嶇О鍜屼綅缃殑鍘熸湁鍊间笉鍐嶉傜敤锛屽洜姝わ紝濡傛灉鎮ㄤ笉鎸囧畾璇彞涓殑MASTER_LOG_FILEMASTER_LOG_POSMASTER_LOG_FILE=''MASTER_LOG_POS=4浼氳闈欓粯鍦版坊鍔犮

MASTER_LOG_FILEMASTER_LOG_POS鍧愭爣鐐癸紝浠庡睘鏈嶅姟鍣I/O绾跨▼鍦ㄥ惎鍔ㄤ箣鍚庝粠涓绘湇鍔″櫒璇诲彇銆傚鏋滄偍鍙寚瀹氫簡鍏朵腑涓涓紝鍒欎粠灞炴湇鍔″櫒涓嶈兘鎸囧畾RELAY_LOG_FILERELAY_LOG_POS銆傚鏋MSATER_LOG_FILEMASTER_LOG_POS閮芥病鏈夎鎸囧畾锛屽垯浠庡睘鏈嶅姟鍣ㄤ細浣跨敤鍦CHANGE MASTER琚彂甯冨墠鐨勬渶鍚庝竴涓slave SQL thread鍧愭爣銆傚綋鎮ㄥ彧鎯虫敼鍙樿浣跨敤鐨 瀵嗙爜鏃讹紝杩欏彲浠ョ‘淇濆鍒剁殑杩炵画鎬с傚嵆浣夸粠灞炴湇鍔″櫒SQL绾跨▼钀藉悗浜庝粠灞炴湇鍔″櫒I/O绾跨▼锛屼篃鍙互纭繚澶嶅埗鐨勮繛缁с

CHANGE MASTER浼氬垹闄ゆ墍鏈夌殑涓户鏃ュ織鏂囦欢骞跺惎鍔ㄤ竴涓柊鐨勬棩蹇楋紝闄ら潪鎮ㄦ寚瀹氫簡RELAY_LOG_FILERELAY_LOG_POS銆傚湪姝ゆ儏鍐典笅锛屼腑缁ф棩蹇楄淇濇寔锛relay_log_purge鍏ㄥ眬鍙橀噺琚潤榛樺湴璁剧疆涓0

CHANGE MASTER TO鍙互鏇存柊master.inforelay-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.inforelay-log.info锛屽苟浣跨敤鍘熸潵鐨勯夐」閲嶆柊鍚姩鏈嶅姟鍣ㄣ

瑕佽鏈嶅姟鍣ㄨ涓哄畠鏄竴涓粠灞炴湇鍔″櫒锛岄渶瑕佹寚瀹MASTER_HOST锛堢敋鑷充娇鐢ㄥ亣鍊硷級銆

13.6.2.2.聽LOAD DATA FROM MASTER璇硶

LOAD DATA FROM MASTER

鏈懡浠ょ敤浜庡涓绘湇鍔″櫒杩涜蹇収锛屽苟鎷疯礉鍒颁粠灞炴湇鍔″櫒涓娿傚畠鍙互鏇存柊MASTER_LOG_FILEMASTER_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_timeoutnet_write_timeout鍊笺傝鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

娉ㄦ剰锛LOAD DATA FROM MASTER涓嶄粠mysql鏁版嵁搴撴嫹璐濅换浣曡〃銆傝繖鍙互鏇村鏄撳湴璁╀富鏈嶅姟鍣ㄥ拰浠庡睘鏈嶅姟鍣ㄦ嫢鏈変笉鍚岀殑鐢ㄦ埛鍜屾潈闄愩

LOAD DATA FROM MASTER璇彞瑕佹眰鐢ㄤ簬杩炴帴涓绘湇鍔″櫒鐨勫鍒跺笎鎴凤紝浠ヤ究璁╀富鏈嶅姟鍣ㄦ嫢鏈RELOADSUPER鏉冮檺锛屽苟璁╂墍鏈夋偍鎯宠杞藉叆鐨勪富鏈嶅姟鍣ㄨ〃鎷ユ湁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鐨勭敤鎴峰簲鎷ユ湁鎺堟潈锛屼互鍙栨秷鎴栧垱寤鸿澶嶅埗鐨勬暟鎹簱鍜岃〃銆

13.6.2.3.聽LOAD TABLE tbl_name FROM MASTER璇硶

LOAD TABLE tbl_name FROM MASTER

鐢ㄤ簬鎶婅〃鐨勬嫹璐濅粠涓绘湇鍔″櫒杞Щ鍒颁粠灞炴湇鍔″櫒銆傛湰璇彞鐨勪富瑕佷綔鐢ㄦ槸璋冭瘯LOAD DATA FROM MASTER銆傚畠瑕佹眰鐢ㄤ簬杩炴帴涓绘湇鍔″櫒鐨勫笎鎴锋嫢鏈夊涓绘湇鍔″櫒鐨RELOADSUPER鏉冮檺锛屽苟鎷ユ湁瀵硅杞藉叆鐨勪富鏈嶅姟鍣ㄨ〃鐨SELECT鏉冮檺銆傚湪浠庡睘鏈嶅姟鍣ㄦ柟闈紝鍙戝竷LOAD TABLE FROM MASTER鐨勭敤鎴峰簲鎷ユ湁鍙栨秷鍜屽垱寤鸿〃鐨勬潈闄愩

鐢ㄤ簬LOAD DATA FROM MASTER鐨勬潯浠朵篃閫傜敤浜庤繖閲屻備妇渚嬭鏄庯紝LOAD TABLE FROM MASTER浠呭浜MyISAM琛ㄨ捣浣滅敤銆傚LOAD DATA FROM MASTER鐨勬殏鍋滄敞鎰忎篃閫傜敤銆

13.6.2.4.聽MASTER_POS_WAIT()璇硶

SELECT MASTER_POS_WAIT('master_log_file', master_log_pos)
杩欏疄闄呬笂鏄竴涓嚱鏁帮紝鑰屼笉鏄竴涓鍙ャ傚畠琚敤浜庣‘璁わ紝浠庡睘鏈嶅姟鍣ㄥ凡璇诲彇骞舵墽琛屼簡鍒拌揪涓绘湇鍔″櫒浜岃繘鍒舵棩蹇楃殑缁欏畾浣嶇疆銆傝浜嗚В瀹屾暣鐨勬弿杩帮紝璇峰弬瑙12.9.4鑺傦紝鈥滃叾浠栧嚱鏁扳

13.6.2.5.聽RESET SLAVE璇硶

RESET SLAVE

鐢ㄤ簬璁╀粠灞炴湇鍔″櫒蹇樿鍏跺湪涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織涓殑澶嶅埗浣嶇疆銆傛湰璇彞琚敤浜庤繘琛屼竴涓槑纭殑鍚姩锛氬畠浼氬垹闄master.inforelay-log.info鏂囦欢锛屼互鍙婃墍鏈夌殑涓户鏃ュ織锛屽苟鍚姩涓涓柊鐨勪腑缁ф棩蹇椼

娉ㄩ噴锛氭墍鏈夌殑涓户鏃ュ織琚垹闄わ紝鍗充娇瀹冧滑娌℃湁琚粠灞炴湇鍔″櫒SQL绾跨▼瀹屽叏鐨勬墽琛屻傦紙濡傛灉鎮ㄥ凡缁忓彂甯冧簡涓涓SLAVE璇彞鎴栧鏋滀粠灞炴湇鍔″櫒鐨勮浇鍏ラ噺寰堝ぇ锛屽垯杩欏浜庝竴涓鍒朵粠灞炴湇鍔″櫒鏄竴涓緢鍙兘鍑虹幇鐨勬儏鍐点傦級

瀛樺偍鍦master.info鏂囦欢涓殑杩炴帴淇℃伅閫氳繃浣跨敤鍦ㄥ搴旂殑鍚姩閫夐」涓寚瀹氱殑鍊硷紝琚珛鍗抽噸鏂拌缃簡銆傛淇℃伅鍖呮嫭涓绘湇鍔″櫒涓绘満銆佷富鏈嶅姟鍣ㄦ帴鍙c佷富鏈嶅姟鍣ㄧ敤鎴峰拰涓绘湇鍔″櫒 瀵嗙爜绛夊笺傚綋浠庡睘鏈嶅姟鍣SQL绾跨▼琚腑姝㈡椂锛屽畠浣嶄簬姝e湪澶嶅埗鐨勪复鏃惰〃鐨勪腑闂达紝骞朵笖鍙戝竷浜RESET SLAVE锛屽垯宸茶澶嶅埗鐨勪复鏃惰〃鍦ㄤ粠灞炴湇鍔″櫒涓鍒犻櫎銆

13.6.2.6.聽SET GLOBAL SQL_SLAVE_SKIP_COUNTER璇硶

SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n

浠庝富鏈嶅姟鍣ㄤ腑璺宠繃鍚庨潰鐨n涓簨浠躲傝澶嶅師鐢辫鍙ュ鑷寸殑澶嶅埗涓锛岃繖鏄湁鐢ㄧ殑銆

浠呭綋浠庡睘绾跨▼娌℃湁姝e湪杩愯鏃讹紝鏈鍙ユ椂鏈夋晥鐨勩傚惁鍒欙紝浼氫骇鐢熶竴涓敊璇

13.6.2.7.聽SHOW SLAVE STATUS璇硶

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.145.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_FileUntil_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_MasterNULL锛堟剰涔変负鈥滄湭鐭モ濓級銆備妇渚嬭鏄庯紝濡傛灉鍦ㄩ噸鏂拌繛鎺ヤ箣鍓嶏紝浠庡睘鏈嶅姟鍣I/O绾跨▼浼戠湢浜master-connect-retry绉掞紝鍒欐樉绀NULL锛屽洜涓轰粠灞炴湇鍔″櫒涓嶇煡閬撲富鏈嶅姟鍣ㄦ鍦ㄥ仛浠涔堬紝涔熶笉鑳芥湁鎶婃彙鍦拌钀藉悗澶氬皯銆

鏈瓧娈垫湁涓涓檺鍒躲傛椂闂存爣璁伴氳繃澶嶅埗琚繚鐣欙紝杩欐剰鍛崇潃锛屽鏋滀竴涓富鏈嶅姟鍣M1鏈韩鏄竴涓粠灞炴湇鍔″櫒M0锛屽垯鏉ヨ嚜M1binlog鐨勪换浣曚簨浠讹紙閫氳繃澶嶅埗鏉ヨ嚜M0binlog鐨勪簨浠惰屼骇鐢燂級锛屼笌鍘熶簨浠跺叿鏈夌浉鍚岀殑鏃堕棿鏍囪銆傝繖鍙互浣MySQL鎴愬姛鍦板鍒TIMESTAMP銆備絾鏄紝Seconds_Behind_Master鐨勭己鐐规槸锛屽鏋M1涔熸敹鍒版潵鑷鎴风鐨勭洿鎺ユ洿鏂帮紝鍒欏间細闅忔満鍙樺寲锛屽洜涓烘湁鏃舵渶杩戠殑M1鏃堕棿鏉ヨ嚜M0锛屾湁鏃舵潵鑷洿鎺ユ洿鏂帮紝鏈杩戠殑鏃堕棿鏍囪涔熸槸濡傛銆

13.6.2.8.聽START SLAVE璇硶

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_THREADSQL_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涓粛鐒舵帴鍙楄繖绉嶇敤娉曪紝浠ヤ究涓庝互鍓嶇増鏈吋瀹广備絾鐜板湪涓嶈禐鎴愪娇鐢ㄣ

13.6.2.9.聽STOP SLAVE璇硶
 

STOP SLAVE [thread_type [, thread_type] ... ]
 
thread_type: IO_THREAD | SQL_THREAD

鐢ㄤ簬涓浠庡睘鏈嶅姟鍣ㄧ嚎绋嬨STOP SLAVE瑕佹眰SUPER鏉冮檺銆

START SLAVE鐩镐技锛屾湰璇彞鍦ㄤ娇鐢ㄦ椂鍙互鍔IO_THREADSQL_THREAD閫夐」锛屾寚鏄庡皢琚腑姝㈢殑绾跨▼銆

鍦ㄤ互鍓嶇増鏈殑MySQL涓紝鏈鍙ヨ绉颁负SLAVE STOP銆傚湪MySQL 5.1涓粛鐒舵帴鍙楄繖绉嶇敤娉曪紝浠ヤ究涓庝互鍓嶇増鏈吋瀹广備絾鏄幇鍦ㄤ笉璧炴垚浣跨敤銆

13.7. 鐢ㄤ簬棰勫鐞嗚鍙ョ殑SQL璇硶

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, EXECUTEDEALLOCATE PREPARE璇彞銆

棰勫埗璇彞鐨SQL璇硶涓庝娇鐢ㄩ鍒惰鍙API璋冪敤涓嶅悓銆備緥濡傦紝鎮ㄤ笉鑳戒娇鐢mysql_stmt_prepare() C API鍑芥暟鏉ラ澶囦竴涓PREPARE, EXECUTEDEALLOCATE PREPARE璇彞銆

棰勫埗璇彞鐨SQL璇硶鍙互鍦ㄥ凡瀛樺偍鐨勮繃绋嬩腑浣跨敤锛屼絾鏄笉鑳藉湪宸插瓨鍌ㄧ殑鍑芥暟鎴栬Е鍙戠▼搴忎腑浣跨敤銆

褰撲娇鐢ㄩ鍒惰鍙ユ椂锛屽彲浠ュ湪LIMIT瀛愬彞涓娇鐢ㄥ崰浣嶇銆傝鍙傝13.2.7鑺傦紝鈥淪ELECT璇硶鈥


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