20.2.1. CREATE PROCEDURE鍜孋REATE FUNCTION
20.2.2. ALTER PROCEDURE鍜孉LTER FUNCTION
20.2.3. DROP PROCEDURE鍜屽拰DROP FUNCTION
20.2.4. SHOW CREATE
PROCEDURE鍜孲HOW CREATE FUNCTION
20.2.5. SHOW
PROCEDURE STATUS鍜孲HOW FUNCTION STATUS
MySQL
5.1鐗堟敮鎸佸瓨鍌ㄧ▼搴忓拰鍑芥暟銆備竴涓瓨鍌ㄧ▼搴忔槸鍙互琚瓨鍌ㄥ湪鏈嶅姟鍣ㄤ腑鐨勪竴濂SQL璇彞銆備竴鏃﹀畠琚瓨鍌ㄤ簡锛屽鎴风涓嶉渶瑕佸啀閲嶆柊鍙戝竷鍗曠嫭鐨勮鍙ワ紝鑰屾槸鍙互寮曠敤瀛樺偍绋嬪簭鏉ユ浛浠銆
涓嬮潰涓浜涙儏鍐典笅瀛樺偍绋嬪簭灏ゅ叾鏈夌敤锛
· 褰撶敤涓嶅悓璇█缂栧啓澶氬鎴峰簲鐢ㄧ▼搴忥紝鎴栧瀹㈡埛搴旂敤绋嬪簭鍦ㄤ笉鍚屽钩鍙颁笂杩愯涓旈渶瑕佹墽琛岀浉鍚岀殑鏁版嵁搴撴搷浣滀箣鏃躲
·
瀹夊叏鏋佷负閲嶈涔嬫椂銆傛瘮濡傦紝閾惰瀵规墍鏈夋櫘閫氭搷浣滀娇鐢ㄥ瓨鍌ㄧ▼搴忋傝繖鎻愪緵涓涓潥鍥鸿屽畨鍏ㄧ殑鐜锛岀▼搴忓彲浠ョ‘淇濇瘡涓涓搷浣滈兘琚Ε鍠勮鍏ユ棩蹇椼傚湪杩欐牱涓涓缃腑锛屽簲鐢ㄧ▼搴忓拰鐢ㄦ埛涓嶅彲鑳界洿鎺ヨ闂暟鎹簱琛紝浣嗘槸浠呭彲浠ユ墽琛屾寚瀹氱殑瀛樺偍绋嬪簭銆
瀛鍌ㄧ▼搴忓彲浠ユ彁渚涙敼鑹悗鐨勬ц兘锛屽洜涓哄彧鏈夎緝灏戠殑淇℃伅闇瑕佸湪鏈嶅姟鍣ㄥ拰瀹㈡埛绠椾箣闂翠紶閫併備唬浠锋槸澧炲姞鏁版嵁搴撴湇鍔″櫒绯荤粺鐨勮礋鑽凤紝鍥犱负鏇村鐨勫伐浣滃湪鏈嶅姟鍣ㄨ繖杈瑰畬鎴愶紝鏇村皯鐨勫湪瀹㈡埛绔紙搴旂敤绋嬪簭锛夐偅杈瑰畬鎴愪笂銆傚鏋滆澶氬鎴风鏈哄櫒锛堟瘮濡傜綉椤垫湇鍔″櫒锛夊彧鐢变竴涓垨灏戞暟鍑犱釜鏁版嵁搴撴湇鍔″櫒鎻愪緵鏈嶅姟锛屽彲浠ヨ冭檻涓涓嬪瓨鍌ㄧ▼搴忋
瀛樺偍绋嬪簭涔熷厑璁镐綘鍦ㄦ暟鎹簱鏈嶅姟鍣ㄤ笂鏈夊嚱鏁板簱銆傝繖鏄竴涓鐜颁唬搴旂敤绋嬪簭璇█鍏变韩鐨勭壒寰侊紝瀹冨厑璁歌繖鏍风殑鍐呴儴璁捐锛屾瘮濡傞氳繃浣跨敤绫汇備娇鐢ㄨ繖浜涘鎴风搴旂敤绋嬪簭璇█鐗瑰緛瀵圭敋鑷充簬鏁版嵁搴撲娇鐢ㄨ寖鍥翠互澶栫殑缂栫▼浜哄憳閮芥湁濂藉銆
MySQL涓哄瓨鍌ㄧ▼搴忛伒寰猄QL:2003璇硶锛岃繖涓娉曚篃琚敤鍦IBM鐨凞B2鏁版嵁搴撲笂銆
MySQL瀵瑰瓨鍌ㄧ▼搴忕殑瀹炵幇杩樺湪杩涘害涓傛墍鏈夋湰绔犲彊杩扮殑璇硶閮借鏀寔锛屽湪鏈夐檺鍒舵垨鎵╁睍鐨勫湴鏂逛細鎭板綋鍦版寚鍑烘潵銆傛湁鍏充娇鐢
瀛樺偍绋嬪簭鐨勯檺鍒剁殑鏇村璁ㄨ鍦闄勫綍 I,
鐗规ч檺鍒閲屾彁鍒般
濡20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”閲屾墍璇寸殑锛
瀛樺偍瀛愮▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳藉凡缁忓畬鎴愩
瀛樺偍绋嬪簭闇瑕佸湪mysql鏁版嵁搴撲腑鏈塸roc琛ㄣ傝繖涓〃鍦∕ySQL
5.1瀹夎杩囩▼涓垱寤恒傚鏋滀綘浠庢棭鏈熺殑鐗堟湰鍗囩骇鍒癕ySQL 5.1 锛岃纭畾鏇存柊浣犵殑鎺堟潈琛ㄤ互纭繚proc琛ㄧ殑瀛樺湪銆傝鍙傞槄2.10.2鑺 “鍗囩骇鎺堟潈琛”銆
鍦∕ySQL 5.1涓紝鎺堟潈绯荤粺濡備笅鑰冭檻瀛樺偍瀛愮▼搴忥細
· 鍒涘缓瀛樺偍瀛愮▼搴忛渶瑕丆REATE ROUTINE鏉冮檺銆
·
鎻愰啋鎴栫Щ闄ゅ瓨鍌ㄥ瓙绋嬪簭闇瑕丄LTER
ROUTINE鏉冮檺銆傝繖涓潈闄愯嚜鍔ㄦ巿浜堝瓙绋嬪簭鐨勫垱寤鸿呫
·
鎵ц瀛愮▼搴忛渶瑕丒XECUTE鏉冮檺銆傜劧鑰岋紝杩欎釜鏉冮檺鑷姩鎺堜簣
瀛愮▼搴忕殑鍒涘缓鑰呫傚悓鏍凤紝瀛愮▼搴忛粯璁ょ殑SQL
SECURITY 鐗瑰緛鏄疍EFINER锛屽畠鍏佽鐢ㄨ瀛愮▼搴忚闂暟鎹簱鐨勭敤鎴蜂笌鎵ц瀛愮▼搴忚仈绯诲埌涓璧枫
20.2.1. CREATE
PROCEDURE鍜孋REATE FUNCTION
20.2.2. ALTER
PROCEDURE鍜孉LTER FUNCTION
20.2.3. DROP
PROCEDURE鍜孌ROP FUNCTION
20.2.4. SHOW
CREATE PROCEDURE鍜孲HOW CREATE FUNCTION
瀛樺偍绋嬪簭鍜屽嚱鏁版槸鐢–REATE PROCEDURE鍜孋REATE
FUNCTION璇彞鍒涘缓鐨勫瓙绋嬪簭銆備竴涓瓙绋嬪簭瑕佷箞鏄竴涓▼搴忚涔堟槸涓涓嚱鏁般備娇鐢–ALL璇彞鏉ヨ皟鐢
绋嬪簭锛岀▼搴忓彧鑳界敤杈撳嚭鍙橀噺浼犲洖鍊笺傚氨鍍忓埆鍏跺畠鍑芥暟璋冪敤涓鏍凤紝鍑芥暟鍙互琚粠璇彞澶栬皟鐢紙鍗抽氳繃寮曠敤鍑芥暟鍚嶏級锛屽嚱鏁拌兘杩斿洖鏍囬噺鍊笺
瀛樺偍瀛愮▼搴忎篃鍙互璋冪敤鍏跺畠瀛樺偍瀛愮▼搴忋
鍦∕ySQL 5.1涓紝涓涓瓨鍌ㄥ瓙绋嬪簭鎴栧嚱鏁颁笌鐗瑰畾鐨勬暟鎹簱鐩歌仈绯汇傝繖閲屾湁鍑犱釜鎰忔濓細
· 褰撲竴涓瓙绋嬪簭琚皟鐢ㄦ椂锛屼竴涓殣鍚殑USE db_name 琚墽琛岋紙褰撳瓙绋嬪簭缁堟鏃跺仠姝㈡墽琛岋級銆瀛樺偍瀛愮▼搴忓唴鐨USE璇彞鏃朵笉鍏佽鐨勩
· 浣犲彲浠ヤ娇鐢ㄦ暟鎹簱鍚嶉檺瀹氬瓙绋嬪簭鍚嶃傝繖鍙互琚敤鏉ュ紩鐢ㄤ竴涓笉鍦ㄥ綋鍓嶆暟鎹簱涓殑瀛愮▼搴忋傛瘮濡傦紝瑕佸紩鐢ㄤ竴涓笌test鏁版嵁搴撳叧鑱旂殑瀛樺偍绋嬪簭p鎴栧嚱鏁癴锛屼綘鍙互璇碈ALL test.p()鎴杢est.f()銆
·
鏁版嵁搴撶Щ闄ょ殑鏃跺欙紝涓庡畠鍏宠仈鐨勬墍鏈夊瓨鍌ㄥ瓙绋嬪簭涔熼兘琚Щ闄ゃ
MySQL 鏀寔闈炲父鏈夌敤鐨勬墿灞曪紝鍗冲畠鍏佽鍦ㄥ瓨鍌ㄧ▼搴忎腑浣跨敤甯歌鐨凷ELECT璇彞锛堥偅灏辨槸璇达紝涓嶄娇鐢ㄥ厜鏍囨垨 灞閮ㄥ彉閲忥級銆傝繖涓竴涓煡璇㈢殑缁撴灉鍖呰绠鍗曞湴鐩存帴閫佸埌瀹㈡埛绔傚SELECT璇彞鐢熸垚澶氫釜缁撴灉鍖咃紝鎵浠ュ鎴风蹇呴』浣跨敤鏀寔澶氱粨鏋滃寘鐨凪ySQL瀹㈡埛绔簱銆傝繖鎰忓懗杩欏鎴风蹇呴』 浣跨敤鑷冲皯MySQL 4.1浠ユ潵鐨勮繎鏈熺増鏈笂鐨勫鎴风搴撱
涓嬮潰涓鑺傛弿杩扮敤鏉ュ垱寤猴紝鏀瑰彉锛岀Щ闄ゅ拰鏌ヨ瀛樺偍绋嬪簭鍜屽嚱鏁扮殑璇硶銆
CREATE PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
routine_body:
Valid SQL procedure statement or statements
杩欎簺璇彞鍒涘缓瀛樺偍瀛愮▼搴忋傝鍦MySQL
5.1涓垱寤哄瓙绋嬪簭锛屽繀椤诲叿鏈CREATE
ROUTINE鏉冮檺锛屽苟涓擜LTER ROUTINE鍜孍XECUTE鏉冮檺琚嚜鍔ㄦ巿浜堝畠鐨勫垱寤鸿呫傚鏋滀簩杩涘埗鏃ュ織鍔熻兘琚厑璁革紝浣犱篃鍙兘闇瑕SUPER鏉冮檺锛岃鍙傞槄20.4鑺锛鈥滃瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戠▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳”銆
榛樿鍦帮紝瀛愮▼搴忎笌褰撳墠鏁版嵁搴撳叧鑱斻傝鏄庣‘鍦版妸瀛愮▼搴忎笌涓涓粰瀹氭暟鎹簱鍏宠仈璧锋潵锛屽彲浠ュ湪鍒涘缓瀛愮▼搴忕殑鏃跺欐寚瀹氬叾鍚嶅瓧涓db_name.sp_name銆
濡傛灉瀛愮▼搴忓悕鍜屽唴寤虹殑SQL鍑芥暟鍚嶄竴鏍凤紝瀹氫箟瀛愮▼搴忔椂锛屼綘闇瑕佸湪杩欎釜鍚嶅瓧鍜岄殢鍚庢嫭鍙蜂腑闂存彃鍏ヤ竴涓┖鏍硷紝鍚﹀垯鍙戠敓璇硶閿欒銆傚綋浣犻殢鍚庤皟鐢ㄥ瓙绋嬪簭鐨勬椂鍊欎篃瑕佹彃鍏ャ備负姝わ紝鍗充娇鏈夊彲鑳藉嚭鐜拌繖绉嶆儏鍐碉紝鎴戜滑杩樻槸寤鸿鏈濂介伩鍏嶇粰浣犺嚜宸辩殑 瀛樺偍瀛愮▼搴忓彇涓庡瓨鍦ㄧ殑SQL鍑芥暟涓鏍风殑鍚嶅瓧銆
鐢辨嫭鍙峰寘鍥寸殑鍙傛暟鍒楀繀椤绘绘槸瀛樺湪銆傚鏋滄病鏈夊弬鏁帮紝涔熻浣跨敤涓涓┖鍙傛暟鍒()銆傛瘡涓弬鏁 榛樿閮芥槸涓涓狪N鍙傛暟銆傝鎸囧畾涓哄叾瀹冨弬鏁帮紝鍙湪鍙傛暟鍚嶄箣鍓嶄娇鐢ㄥ叧閿瘝 OUT鎴朓NOUT
娉ㄦ剰: 鎸囧畾鍙傛暟涓篒N,
OUT, 鎴朓NOUT 鍙PROCEDURE鏄悎娉曠殑銆傦紙FUNCTION鍙傛暟鎬绘槸琚涓烘槸IN鍙傛暟锛
RETURNS瀛楀彞鍙兘瀵笷UNCTION鍋鎸囧畾锛屽鍑芥暟鑰岃█杩欐槸寮哄埗鐨勩傚畠鐢ㄦ潵鎸囧畾鍑芥暟鐨勮繑鍥炵被鍨嬶紝鑰屼笖鍑芥暟浣撳繀椤诲寘鍚竴涓RETURN value璇彞銆
routine_body
鍖呭惈鍚堟硶鐨凷QL杩囩▼璇彞銆傚彲浠ヤ娇鐢ㄥ鍚堣鍙ヨ娉曪紝璇峰弬闃20.2.7鑺傦紝鈥淏EGIN ... END澶嶅悎璇彞”銆傚鍚堣鍙ュ彲浠ュ寘鍚
澹版槑锛屽惊鐜拰鍏跺畠鎺у埗缁撴瀯璇彞銆傝繖浜涜鍙ョ殑璇硶鍦ㄦ湰绔犲悗鍏嶄粙缁嶏紝涓句緥锛岃鍙傞槄20.2.8鑺傦紝鈥淒ECLARE璇彞”鍜20.2.12鑺傦紝鈥娴佺▼鎺у埗鏋勯犫銆
CREATE FUNCTION璇彞琚敤鍦ㄦ洿鏃╃殑MySQL鐗堟湰涓浠鏀寔UDF 锛堣嚜瀹氫箟鍑芥暟锛夈傝鍙傞槄27.2鑺傦紝鈥缁橫ySQL娣诲姞鏂板嚱鏁”銆 UDF缁х画琚敮鎸侊紝鍗充娇鐜板湪
鏈変簡瀛樺偍鍑芥暟銆俇DF浼氳璁や负涓涓閮ㄥ瓨鍌ㄥ嚱鏁般傜劧鑰岋紝涓嶈璁╁瓨鍌ㄥ嚱鏁颁笌UDF鍑芥暟鍏变韩鍚嶅瓧绌洪棿銆
澶栭儴瀛樺偍绋嬪簭鐨勬鏋跺皢鍦ㄤ笉涔呯殑灏嗘潵寮曞叆銆傝繖灏嗗厑璁镐綘鐢SQL涔嬪鐨勮瑷缂栧啓瀛樺偍绋嬪簭銆傛渶鍙兘鐨勬槸锛岀涓涓鏀寔璇█鏄疨HP锛屽洜涓烘牳蹇働HP寮曟搸寰堝皬锛岀嚎绋嬪畨鍏紝涓斿彲浠ヨ鏂逛究鍦板祵鍏ャ傚洜涓烘鏋舵槸鍏紑鐨勶紝瀹冨笇鏈涜澶氬叾瀹冭瑷涔熻兘琚敮鎸併
濡傛灉绋嬪簭鎴栫嚎绋嬫绘槸瀵瑰悓鏍风殑杈撳叆鍙傛暟浜х敓鍚屾牱鐨勭粨鏋滐紝鍒欒璁や负瀹冩槸“纭畾鐨”锛屽惁鍒欏氨鏄“闈炵‘瀹”鐨勩傚鏋滄棦娌℃湁缁欏畾DETERMINISTIC涔熸病鏈夌粰瀹歂OT
DETERMINISTIC锛岄粯璁ょ殑灏辨槸NOT DETERMINISTIC銆
涓鸿繘琛屽鍒讹紝浣跨敤NOW()鍑芥暟锛堟垨瀹冪殑鍚屼箟璇嶏級鎴
褰撳墠鏉ヨ锛孌ETERMINISTIC鐗瑰緛琚帴鍙楋紝浣嗚繕娌℃湁琚紭鍖栫▼搴忔墍浣跨敤銆傜劧鑰屽鏋滀簩杩涘埗鏃ュ織鍔熻兘琚厑璁镐簡锛岃繖涓壒寰佸奖鍝嶅埌MySQL鏄惁浼氭帴鍙楀瓙绋嬪簭瀹氫箟銆璇峰弬闃20.4鑺锛屸瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”銆
涓浜涚壒寰佹彁渚涘瓙绋嬪簭浣跨敤鏁版嵁鐨勫唴鍦ㄤ俊鎭侰ONTAINS SQL琛ㄧず瀛愮▼搴忎笉鍖呭惈璇绘垨鍐欐暟鎹殑璇彞銆侼O SQL琛ㄧず瀛愮▼搴忎笉鍖呭惈SQL璇彞銆俁EADS
SQL DATA琛ㄧず瀛愮▼搴忓寘鍚鏁版嵁鐨勮鍙ワ紝浣嗕笉鍖呭惈鍐欐暟鎹殑璇彞銆侻ODIFIES SQL DATA琛ㄧず瀛愮▼搴忓寘鍚啓鏁版嵁鐨勮鍙ャ傚鏋杩欎簺鐗瑰緛娌℃湁鏄庣‘缁欏畾锛榛樿鐨勬槸CONTAINS SQL銆
SQL SECURITY鐗瑰緛鍙互鐢ㄦ潵鎸囧畾
瀛愮▼搴忚鐢ㄥ垱寤哄瓙绋嬪簭鑰呯殑璁稿彲鏉ユ墽琛岋紝杩樻槸浣跨敤璋冪敤鑰呯殑璁稿彲鏉ユ墽琛屻傞粯璁ゅ兼槸DEFINER銆傚湪SQL:2003涓呮槸涓涓柊鐗规с傚垱寤鸿呮垨璋冪敤鑰呭繀椤荤敱璁块棶
瀛愮▼搴忓叧鑱旂殑鏁版嵁搴撶殑璁稿彲銆傚湪MySQL 5.1涓紝蹇呴』鏈塃XECUTE鏉冮檺鎵嶈兘鎵ц瀛愮▼搴忋傚繀椤绘嫢鏈夎繖涓潈闄愮殑鐢ㄦ埛瑕佷箞鏄畾涔夎咃紝瑕佷箞鏄皟鐢ㄨ咃紝杩欏彇鍐充簬SQL SECURITY鐗瑰緛鏄浣曡缃殑銆
MySQL瀛樺偍sql_mode绯荤粺鍙橀噺璁剧疆锛岃繖涓缃湪瀛愮▼搴忚鍒涘缓鐨勬椂鍊欒捣浣滅敤锛孧ySQL鎬绘槸寮哄埗浣跨敤杩欎釜璁剧疆鏉ユ墽琛
瀛愮▼搴忋
COMMENT瀛愬彞鏄竴涓狹ySQL鐨勬墿灞曪紝瀹冨彲浠ヨ鐢ㄦ潵鎻忚堪
瀛樺偍绋嬪簭銆傝繖涓俊鎭SHOW CREATE PROCEDURE鍜 SHOW CREATE
FUNCTION璇彞鏉ユ樉绀恒
MySQL鍏佽瀛愮▼搴忓寘鍚獶DL璇彞锛屽CREATE鍜孌ROP銆侻ySQL涔熷厑璁稿瓨鍌ㄧ▼搴忥紙浣嗕笉鏄
瀛樺偍鍑芥暟锛夊寘鍚玈QL 浜や簰璇彞锛屽COMMIT銆傚瓨鍌ㄥ嚱鏁颁笉鍙互鍖呭惈閭d簺鍋氭槑纭殑鍜岀粷瀵圭殑鎻愪氦鎴栬呭仛鍥炴粴鐨勮銆係QL鏍囧噯涓嶈姹傚杩欎簺璇彞鐨勬敮鎸侊紝SQL鏍囧噯澹版槑姣忎釜DBMS鎻愪緵鍟嗗彲浠ュ喅瀹氭槸鍚﹀厑璁告敮鎸佽繖浜涜鍙ャ
瀛樺偍瀛愮▼搴忎笉鑳戒娇鐢↙OAD DATA
INFILE銆
杩斿洖缁撴灉鍖呯殑璇彞涓嶈兘琚敤鍦ㄥ瓨鍌ㄥ嚱鏁扮銆傝繖鍖呮嫭涓嶄娇鐢↖NTO缁欏彉閲忚鍙
鍒楀肩殑SELECT璇彞锛孲HOW
璇彞锛屽強鍏跺畠璇稿EXPLAIN杩欐牱鐨勮鍙ャ傚浜庡彲鍦ㄥ嚱鏁板畾涔夋椂闂磋鍐冲畾瑕佽繑鍥炰竴涓粨鏋滃寘鐨勮鍙ワ紝鍙戠敓涓涓厑璁镐粠鍑芥暟閿欒杩斿洖缁撴灉鍖呯殑Not锛圗R_SP_NO_RETSET_IN_FUNC锛夈傚浜庡彧鍙湪杩愯鏃跺喅瀹氳杩斿洖涓涓粨鏋滃寘鐨勮鍙ワ紝
鍙戠敓涓涓笉鑳藉湪缁欏畾涓婁笅鏂囬敊璇繑鍥炵粨鏋滃寘鐨凱ROCEDURE %s
锛圗R_SP_BADSELECT)銆
涓嬮潰鏄竴涓娇鐢∣UT鍙傛暟鐨勭畝鍗曠殑瀛樺偍绋嬪簭鐨勪緥瀛愩備緥瀛愪负锛屽湪
绋嬪簭琚畾涔夌殑鏃跺欙紝鐢mysql瀹㈡埛绔痙elimiter鍛戒护鏉ユ妸璇彞瀹氱晫绗︿粠 ;鍙樹负//銆傝繖灏卞厑璁哥敤鍦
绋嬪簭浣撲腑鐨;瀹氱晫绗﹁浼犻掑埌鏈嶅姟鍣ㄨ屼笉鏄mysql鑷繁鏉ヨВ閲娿
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
褰撲娇鐢╠elimiter鍛戒护鏃讹紝浣犲簲璇ラ伩鍏嶄娇鐢ㄥ弽鏂滄潬(‘\’)瀛楃锛屽洜涓洪偅鏄疢ySQL鐨
杞箟瀛楃銆
涓嬪垪鏄竴涓緥瀛愶紝涓涓噰鐢ㄥ弬鏁扮殑鍑芥暟浣跨敤涓涓猄QL鍑芥暟鎵ц涓涓搷浣滐紝骞惰繑鍥炵粨鏋滐細
mysql> delimiter //
mysql> CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50)
-> RETURN CONCAT('Hello, ',s,'!');
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world! |
+----------------+
1 row in set (0.00 sec)
濡傛灉鍦ㄥ瓨鍌ㄥ嚱鏁颁腑鐨凴ETURN璇彞杩斿洖涓涓被鍨嬩笉鍚屼簬鍦ㄥ嚱鏁扮殑RETURNS瀛愬彞涓寚瀹氱被鍨嬬殑鍊硷紝杩斿洖鍊艰寮哄埗涓烘伆褰撶殑绫诲瀷銆傛瘮濡傦紝濡傛灉涓涓嚱鏁拌繑鍥炰竴涓狤NUM鎴朣ET鍊硷紝浣嗘槸RETURN璇彞杩斿洖涓涓暣鏁帮紝瀵逛簬SET鎴愬憳闆嗙殑鐩稿簲鐨凟NUM鎴愬憳锛屼粠鍑芥暟杩斿洖鐨勫兼槸瀛楃涓层
ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
characteristic:
{ CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
杩欎釜璇彞鍙互琚敤鏉ユ敼鍙樹竴涓瓨鍌ㄧ▼搴忔垨鍑芥暟鐨勭壒寰併傚湪MySQL
5.1涓紝浣犲繀椤荤敤ALTER ROUTINE鏉冮檺鎵嶅彲鐢ㄦ瀛愮▼搴忋傝繖涓潈闄愯鑷姩鎺堜簣瀛愮▼搴忕殑鍒涘缓鑰呫傚20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”涓墍杩帮紝
濡傛灉浜岃繘鍒舵棩蹇楀姛鑳借鍏佽浜嗭紝浣犲彲鑳戒篃闇瑕丼UPER鏉冮檺銆
鍦ˋLTER PROCEDURE鍜孉LTER FUNCTION璇彞涓紝鍙互鎸囧畾瓒呰繃涓涓殑鏀瑰彉銆
DROP {PROCEDURE | FUNCTION} [IF EXISTS] sp_name
杩欎釜璇彞琚敤鏉ョЩ闄や竴涓瓨鍌ㄧ▼搴忔垨鍑芥暟銆傚嵆锛屼粠鏈嶅姟鍣ㄧЩ闄や竴涓埗瀹氱殑瀛愮▼搴忋傚湪MySQL
5.1涓紝浣犲繀椤绘湁ALTER ROUTINE鏉冮檺鎵嶅彲鐢ㄦ瀛愮▼搴忋傝繖涓潈闄愯鑷姩鎺堜簣瀛愮▼搴忕殑鍒涘缓鑰呫
IF EXISTS 瀛愬彞鏄竴涓狹ySQL鐨勬墿灞曘傚鏋滅▼搴忔垨鍑芥暟涓嶅瓨鍌紝瀹冮槻姝㈠彂鐢熼敊璇備骇鐢熶竴涓彲浠ョ敤SHOW
WARNINGS鏌ョ湅鐨勮鍛娿
SHOW CREATE {PROCEDURE | FUNCTION} sp_name
杩欎釜璇彞鏄竴涓狹ySQL鐨勬墿灞曘傜被浼间簬SHOW CREATE TABLE锛屽畠杩斿洖涓涓彲鐢ㄦ潵閲嶆柊鍒涘缓宸插懡鍚
瀛愮▼搴忕殑纭垏瀛楃涓层
mysql> SHOW CREATE FUNCTION test.hello\G
*************************** 1. row ***************************
Function: hello
sql_mode:
Create Function: CREATE FUNCTION `test`.`hello`(s CHAR(20)) RETURNS CHAR(50)
RETURN CONCAT('Hello, ',s,'!')
SHOW {PROCEDURE | FUNCTION} STATUS [LIKE 'pattern']
杩欎釜璇彞鏄竴涓狹ySQL鐨勬墿灞曘傚畠杩斿洖瀛愮▼搴忕殑鐗瑰緛锛屽鏁版嵁搴擄紝鍚嶅瓧锛岀被鍨嬶紝鍒涘缓鑰呭強鍒涘缓鍜屼慨鏀规棩鏈熴傚鏋滄病鏈夋寚瀹氭牱寮忥紝鏍规嵁浣犱娇鐢ㄧ殑璇彞锛屾墍鏈
瀛樺偍绋嬪簭鍜屾墍鏈夊瓨鍌ㄥ嚱鏁扮殑淇℃伅閮借鍒楀嚭銆
mysql> SHOW FUNCTION STATUS LIKE 'hello'\G
*************************** 1. row ***************************
Db: test
Name: hello
Type: FUNCTION
Definer: testuser@localhost
Modified: 2004-08-03 15:29:37
Created: 2004-08-03 15:29:37
Security_type: DEFINER
Comment:
浣犲彲浠ヤ粠INFORMATION_SCHEMA涓殑ROUTINES琛ㄨ幏寰楁湁鍏冲瓨鍌ㄥ瓙绋嬪簭鐨勪俊鎭傝鍙傞槄23.1.14鑺傦紝鈥淚NFORMATION_SCHEMA
ROUTINES 琛”銆
CALL sp_name([parameter[,...]])
CALL璇彞璋冪敤涓涓厛鍓嶇敤CREATE
PROCEDURE鍒涘缓鐨勭▼搴忋
CALL璇彞鍙互鐢 澹版槑涓篛UT鎴栫殑INOUT鍙傛暟鐨勫弬鏁扮粰瀹冪殑璋冪敤鑰呬紶鍥炲笺傚畠涔“杩斿洖”鍙楀奖鍝嶇殑琛屾暟锛屽鎴风绋嬪簭鍙互鍦SQL绾у埆閫氳繃璋冪敤ROW_COUNT()鍑芥暟鑾峰緱杩欎釜鏁帮紝浠嶤涓槸璋冪敤the mysql_affected_rows() C API鍑芥暟鏉鑾峰緱銆
[begin_label:] BEGIN
[statement_list]
END [end_label]
瀛樺偍瀛愮▼搴忓彲浠ヤ娇鐢BEGIN ... END澶嶅悎璇彞鏉ュ寘鍚涓鍙ャstatement_list
浠h〃涓涓垨澶氫釜璇彞鐨勫垪琛ㄣstatement_list涔嬪唴姣忎釜璇彞閮藉繀椤荤敤鍒嗗彿锛堬紱锛夋潵缁撳熬銆
澶嶅悎璇彞鍙互琚爣璁般傞櫎闈begin_label瀛樺湪,鍚﹀垯end_label涓嶈兘琚粰鍑,骞朵笖濡傛灉浜岃呴兘瀛樺湪,浠栦滑蹇呴』鏄悓鏍风殑銆
璇锋敞鎰忥紝鍙夌殑[NOT] ATOMIC瀛愬彞鐜板湪杩樹笉琚敮鎸併傝繖鎰忓懗鐫鍦ㄦ寚浠ゅ潡鐨勫紑濮嬫病鏈変氦浜掔殑瀛樺偍鐐硅璁剧疆锛屽苟涓斿湪涓婁笅鏂囦腑鐢ㄥ埌鐨凚EGIN瀛愬彞瀵瑰綋鍓嶄氦浜掑姩浣滄病鏈夊奖鍝嶃
浣跨敤澶氶噸璇彞闇瑕佸鎴风鑳藉彂閫佸寘鍚鍙ュ畾鐣岀;鐨勬煡璇㈠瓧绗︿覆銆傝繖涓鍙峰湪鍛戒护琛屽鎴风琚敤delimiter鍛戒护鏉ュ鐞嗐傛敼鍙樻煡璇㈢粨灏惧畾鐣岀;锛堟瘮濡傛敼鍙樹负//锛変娇寰; 鍙鐢ㄥ湪瀛愮▼搴忎綋涓
DECLARE璇彞琚敤鏉ユ妸涓嶅悓椤圭洰灞鍩熷埌涓涓
瀛愮▼搴忥細灞閮ㄥ彉閲忥紙璇峰弬闃20.2.9鑺傦紝鈥瀛樺偍绋嬪簭涓殑鍙橀噺”)锛屾潯浠跺拰
澶勭悊绋嬪簭锛堣鍙傞槄20.2.10鑺傦紝鈥鏉′欢鍜屽鐞嗙▼搴”)
鍙婂厜鏍囷紙璇峰弬闃20.2.11鑺傦紝鈥鍏夋爣”)銆係IGNAL鍜孯ESIGNAL璇彞褰撳墠杩樹笉琚敮鎸併
DECLARE浠呰鐢ㄥ湪BEGIN ... END澶嶅悎璇彞閲岋紝骞朵笖蹇呴』鍦ㄥ鍚堣鍙ョ殑寮澶达紝鍦ㄤ换浣曞叾瀹冭鍙ヤ箣鍓嶃
鍏夋爣蹇呴』鍦ㄥ0鏄庡鐞嗙▼搴忎箣鍓嶈澹版槑锛屽苟涓斿彉閲忓拰鏉′欢蹇呴』鍦ㄥ0鏄庡厜鏍囨垨澶勭悊绋嬪簭涔嬪墠琚0鏄庛
浣犲彲浠ュ湪瀛愮▼搴忎腑澹版槑骞朵娇鐢ㄥ彉閲忋
DECLARE var_name[,...] type [DEFAULT value]
杩欎釜璇彞琚敤鏉ュ0鏄庡眬閮ㄥ彉閲忋傝缁欏彉閲忔彁渚涗竴涓粯璁ゅ硷紝璇峰寘鍚竴涓狣EFAULT瀛愬彞銆傚煎彲浠ヨ鎸囧畾涓轰竴涓〃杈惧紡锛屼笉闇瑕佷负涓涓父鏁般傚鏋滄病鏈塂EFAULT瀛愬彞锛屽垵濮嬪间负NULL銆
灞閮ㄥ彉閲忕殑浣滅敤鑼冨洿鍦ㄥ畠琚0鏄庣殑BEGIN ... END鍧楀唴銆傚畠鍙互琚敤鍦ㄥ祵濂楃殑鍧椾腑锛岄櫎浜嗛偅浜涚敤鐩稿悓鍚嶅瓧
澹版槑鍙橀噺鐨勫潡銆
SET var_name = expr [, var_name = expr] ...
鍦ㄥ瓨鍌ㄧ▼搴忎腑鐨凷ET璇彞鏄竴鑸琒ET璇彞鐨勬墿灞曠増鏈傝鍙傝冨彉閲忓彲鑳芥槸瀛愮▼搴忓唴澹版槑鐨勫彉閲忥紝鎴栬呮槸鍏ㄥ眬鏈嶅姟鍣ㄥ彉閲忋
鍦ㄥ瓨鍌ㄧ▼搴忎腑鐨凷ET璇彞浣滀负棰勫厛瀛樺湪鐨凷ET璇硶鐨勪竴閮ㄥ垎鏉ュ疄鐜般傝繖鍏佽SET a=x, b=y, ...杩欐牱鐨勬墿灞曡娉曘傚叾涓笉鍚岀殑鍙橀噺绫诲瀷锛堝眬鍩 澹版槑鍙橀噺鍙婂叏灞鍜岄泦浣撳彉閲忥級鍙互琚贩鍚堣捣鏉ャ傝繖涔熷厑璁告妸灞閮ㄥ彉閲忓拰涓浜涘彧瀵圭郴缁熷彉閲忔湁鎰忎箟鐨勯夐」鍚堝苟璧锋潵銆傚湪閭g鎯呭喌涓嬶紝姝ら夐」琚瘑鍒紝浣嗘槸琚拷鐣ヤ簡銆
SELECT col_name[,...] INTO var_name[,...] table_expr
杩欎釜SELECT璇硶鎶婇夊畾鐨勫垪鐩存帴瀛樺偍鍒板彉閲忋傚洜姝わ紝鍙湁鍗曚竴鐨勮鍙互琚彇鍥炪
SELECT id,data INTO x,y FROM test.t1 LIMIT 1;
娉ㄦ剰锛岀敤鎴峰彉閲忓悕鍦∕ySQL 5.1涓槸瀵瑰ぇ灏忓啓涓嶆晱鎰熺殑銆傝鍙傞槄9.3鑺傦紝鈥鐢ㄦ埛鍙橀噺”銆
閲嶈: SQL鍙橀噺鍚嶄笉鑳藉拰鍒楀悕涓鏍枫傚鏋淪ELECT ... INTO杩欐牱鐨凷QL璇彞鍖呭惈涓涓鍒楃殑鍙傝冿紝骞跺寘鍚竴涓笌鍒楃浉鍚屽悕瀛楃殑
灞閮ㄥ彉閲忥紝MySQL褰撳墠鎶婂弬鑰冭В閲婁负涓涓彉閲忕殑鍚嶅瓧銆備緥濡傦紝鍦ㄤ笅闈㈢殑璇彞涓紝xname
琚В閲婁负鍒皒name variable
鐨勫弬鑰冭屼笉鏄埌xname column鐨勶細
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE newname VARCHAR(5);
DECLARE xid INT;
SELECT xname,id INTO newname,xid
FROM table1 WHERE xname = xname;
SELECT newname;
END;
褰撹繖涓▼搴忚璋冪敤鐨勬椂鍊欙紝鏃犺table.xname鍒楃殑鍊兼槸浠涔堬紝鍙橀噺newname灏嗚繑鍥炲‘bob’銆
鐗瑰畾鏉′欢闇瑕佺壒瀹氬鐞嗐傝繖浜涙潯浠跺彲浠ヨ仈绯诲埌閿欒锛屼互鍙婂瓙绋嬪簭涓殑涓鑸祦绋嬫帶鍒躲
DECLARE condition_name CONDITION FOR condition_value
condition_value:
SQLSTATE [VALUE] sqlstate_value
| mysql_error_code
杩欎釜璇彞鎸囧畾闇瑕佺壒娈婂鐞嗙殑鏉′欢銆傚畠灏嗕竴涓悕瀛楀拰鎸囧畾鐨勯敊璇潯浠跺叧鑱旇捣鏉ャ傝繖涓悕瀛楀彲浠ラ殢鍚庤鐢ㄥ湪DECLARE HANDLER璇彞涓傝鍙傞槄20.2.10.2鑺傦紝鈥淒ECLARE澶勭悊绋嬪簭”銆
闄や簡SQLSTATE鍊硷紝涔熸敮鎸丮ySQL閿欒浠g爜銆
DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
handler_type:
CONTINUE
| EXIT
| UNDO
condition_value:
SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| mysql_error_code
杩欎釜璇彞鎸囧畾姣忎釜鍙互澶勭悊涓涓垨澶氫釜鏉′欢鐨勫鐞嗙▼搴忋傚鏋滀骇鐢熶竴涓垨澶氫釜鏉′欢锛屾寚瀹氱殑璇彞琚墽琛屻
瀵逛竴涓狢ONTINUE澶勭悊绋嬪簭锛屽綋鍓嶅瓙绋嬪簭鐨勬墽琛屽湪鎵ц 澶勭悊绋嬪簭璇彞涔嬪悗缁х画銆傚浜嶦XIT澶勭悊绋嬪簭锛屽綋鍓岯EGIN...END澶嶅悎璇彞鐨勬墽琛岃缁堟銆俇NDO 澶勭悊绋嬪簭绫诲瀷璇彞杩樹笉琚敮鎸併
· SQLWARNING鏄鎵鏈変互01寮澶寸殑SQLSTATE浠g爜鐨勯熻銆
· NOT FOUND鏄鎵鏈変互02寮澶寸殑SQLSTATE浠g爜鐨勯熻銆
· SQLEXCEPTION鏄鎵鏈夋病鏈夎SQLWARNING鎴朜OT FOUND鎹曡幏鐨凷QLSTATE浠g爜鐨勯熻銆
闄や簡SQLSTATE鍊硷紝MySQL閿欒浠g爜涔熶笉琚敮鎸併
渚嬪:
mysql> CREATE TABLE test.t (s1 int,primary key (s1));
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> CREATE PROCEDURE handlerdemo ()
-> BEGIN
-> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
-> SET @x = 1;
-> INSERT INTO test.t VALUES (1);
-> SET @x = 2;
-> INSERT INTO test.t VALUES (1);
-> SET @x = 3;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL handlerdemo()//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
+------+
| @x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
娉ㄦ剰鍒帮紝@x鏄3锛岃繖琛ㄦ槑MySQL琚墽琛屽埌绋嬪簭鐨勬湯灏俱傚鏋淒ECLARE
CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1; 杩欎竴琛屼笉鍦紝绗簩涓狪NSERT鍥燩RIMARY KEY寮哄埗鑰屽け璐ヤ箣鍚,MySQL鍙兘宸茬粡閲囧彇
榛樿(EXIT)璺緞锛屽苟涓擲ELECT @x鍙兘宸茬粡杩斿洖2銆
绠鍗曞厜鏍囧湪瀛樺偍绋嬪簭鍜屽嚱鏁板唴琚敮鎸併傝娉曞鍚屽湪宓屽叆鐨凷QL涓傚厜鏍囧綋鍓嶆槸涓嶆晱鎰熺殑锛屽彧璇荤殑鍙婁笉婊氬姩鐨勩備笉鏁忔劅鎰忎负鏈嶅姟鍣ㄥ彲浠ユ椿涓嶅彲浠ュ鍒跺畠鐨勭粨鏋滆〃銆
鍏夋爣蹇呴』鍦ㄥ0鏄庡鐞嗙▼搴忎箣鍓嶈澹版槑锛屽苟涓斿彉閲忓拰鏉′欢蹇呴』鍦ㄥ0鏄庡厜鏍囨垨澶勭悊绋嬪簭涔嬪墠琚0鏄庛
渚嬪锛
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
OPEN cur2;
REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
CLOSE cur2;
END
DECLARE cursor_name CURSOR FOR select_statement
杩欎釜璇彞澹版槑涓涓厜鏍囥備篃鍙互鍦ㄥ瓙绋嬪簭涓畾涔夊涓厜鏍囷紝浣嗘槸涓涓潡涓殑姣忎竴涓厜鏍囧繀椤绘湁鍞竴鐨勫悕瀛椼
SELECT璇彞涓嶈兘鏈塈NTO瀛愬彞銆
IF, CASE, LOOP,
WHILE, ITERATE, 鍙 LEAVE 鏋勯犺瀹屽叏瀹炵幇銆
杩欎簺鏋勯犲彲鑳芥瘡涓寘鍚涔堜竴涓崟鐙鍙ワ紝瑕佷箞鏄娇鐢˙EGIN ... END澶嶅悎璇彞鐨勪竴鍧楄鍙ャ傛瀯閫犲彲浠ヨ宓屽銆
鐩墠杩樹笉鏀寔FOR寰幆銆
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF
IF瀹炵幇浜嗕竴涓熀鏈殑鏉′欢鏋勯犮傚鏋search_condition姹傚间负鐪燂紝鐩稿簲鐨凷QL璇彞鍒楄〃琚墽琛屻傚鏋滄病鏈search_condition鍖归厤锛屽湪ELSE瀛愬彞閲岀殑璇彞鍒楄〃琚墽琛屻statement_list鍙互鍖呮嫭涓涓垨澶氫釜璇彞銆
璇锋敞鎰忥紝涔熸湁涓涓狪F()
鍑芥暟锛屽畠涓嶅悓浜庤繖閲屾弿杩扮殑IF璇彞銆傝鍙傞槄12.2鑺傦紝鈥鎺у埗娴佺▼鍑芥暟”銆
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
Or:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
瀛樺偍绋嬪簭鐨凜ASE璇彞瀹炵幇涓涓鏉傜殑鏉′欢鏋勯犮傚鏋search_condition 姹傚间负鐪燂紝鐩稿簲鐨凷QL琚墽琛屻傚鏋滄病鏈夋悳绱㈡潯浠跺尮閰嶏紝鍦‥LSE瀛愬彞閲岀殑璇彞琚墽琛屻
娉ㄦ剰锛杩欓噷浠嬬粛鐨勭敤鍦
瀛樺偍绋嬪簭閲岀殑CASE璇彞涓12.2鑺傦紝鈥鎺у埗娴佺▼鍑芥暟鈥閲屾弿杩扮殑SQL CASE琛ㄨ揪寮忕殑CASE璇彞鏈夎交寰笉鍚屻傝繖閲岀殑CASE璇彞涓嶈兘鏈塃LSE
NULL瀛愬彞锛屽苟涓旂敤END CASE鏇夸唬END鏉ョ粓姝€
[begin_label:] LOOP
statement_list
END LOOP [end_label]
LOOP鍏佽鏌愮壒瀹氳鍙ユ垨璇彞缇ょ殑閲嶅鎵ц锛屽疄鐜颁竴涓畝鍗曠殑寰幆鏋勯犮傚湪寰幆鍐呯殑璇彞涓鐩撮噸澶嶇洿寰幆琚鍑猴紝閫鍑洪氬父浼撮殢鐫涓涓LEAVE 璇彞銆
LOOP璇彞鍙互琚爣娉ㄣ傞櫎闈begin_label瀛樺湪锛屽惁鍒end_label涓嶈兘琚粰鍑猴紝骞朵笖濡傛灉涓よ呴兘鍑虹幇锛屽畠浠繀椤绘槸鍚屾牱鐨勩
ITERATE label
ITERATE鍙彲浠ュ嚭鐜板湪LOOP, REPEAT,
鍜學HILE璇彞鍐呫侷TERATE鎰忔濅负锛“鍐嶆寰幆銆”
渚嬪锛
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
label1: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN ITERATE label1; END IF;
LEAVE label1;
END LOOP label1;
SET @x = p1;
END
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
REPEAT璇彞鍐呯殑璇彞鎴栬鍙ョ兢琚噸澶嶏紝鐩磋嚦search_condition 涓虹湡銆
REPEAT
璇彞鍙互琚爣娉ㄣ 闄ら潪begin_label涔熷瓨鍦紝end_label鎵嶈兘琚敤锛屽鏋滀袱鑰呴兘瀛樺湪锛屽畠浠繀椤绘槸涓鏍风殑銆
渚嬪锛
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-> BEGIN
-> SET @x = 0;
-> REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @x//
+------+
| @x |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]
WHILE璇彞鍐呯殑璇彞鎴栬鍙ョ兢琚噸澶嶏紝鐩磋嚦search_condition 涓虹湡銆
WHILE璇彞鍙互琚爣娉ㄣ
闄ら潪begin_label涔熷瓨鍦紝end_label鎵嶈兘琚敤锛屽鏋滀袱鑰呴兘瀛樺湪锛屽畠浠繀椤绘槸涓鏍风殑銆
渚嬪锛
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
...
SET v1 = v1 - 1;
END WHILE;
END
鏄殑锛屽湪瀛樺偍绋嬪簭鍜屽嚱鏁颁腑琚墽琛屾爣鍑嗚涓鸿浠庝富MySQL鏈嶅姟鍣ㄥ鍒跺埌浠庢湇鍔″櫒銆傛湁灏戞暟闄愬埗锛屽畠浠湪20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰 瑙﹀彂绋嬪簭浜岃繘鍒舵棩蹇楀姛鑳解涓杩般
鏄殑锛岄氳繃涓鑸珼DL璇彞鎵ц鐨勫瓨鍌ㄧ▼搴忓拰鍑芥暟锛屽叾鍦ㄤ富鏈嶅姟鍣ㄤ笂鐨勫垱寤鸿澶嶅埗鍒颁粠鏈嶅姟鍣紝鎵浠ョ洰鏍囧皢瀛樺湪涓や釜鏈嶅姟鍣ㄤ笂銆傚瀛樺偍绋嬪簭鍜屽嚱鏁扮殑ALTER 鍜孌ROP璇彞涔熻澶嶅埗銆
MySQL绾綍姣忎釜鍙戠敓鍦ㄥ瓨鍌ㄧ▼搴忓拰鍑芥暟閲岀殑DML浜嬩欢锛屽苟澶嶅埗杩欎簺鍗曠嫭鐨勮涓哄埌浠庢湇鍔″櫒銆傛墽琛屽瓨鍌ㄧ▼搴忓拰鍑芥暟鐨勫垏瀹炶皟鐢ㄤ笉琚鍒躲
鏄殑锛屽洜涓轰竴涓粠鏈嶅姟鍣ㄦ湁鏉冮檺鏉ユ墽琛屼换浣曡鑷富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇楃殑璇彞锛屾寚瀹氱殑瀹夊叏绾︽潫鍥犱笌澶嶅埗涓璧蜂娇鐢ㄧ殑瀛樺偍绋嬪簭鍜屽嚱鏁拌屽瓨鍦ㄣ傚鏋滃鍒舵垨浜岃繘鍒舵棩蹇楀ぇ浣撲笂鏄縺娲荤殑锛堜负point-in-time鎭㈠鐨勭洰鐨勶級锛岄偅涔圡ySQL DBA 鏈変袱涓畨鍏ㄩ夐」鍙夛細
宓屽叆鍒板瓨鍌ㄧ▼搴忎腑鐨勪笉纭畾锛堥殢鏈猴級鎴栨椂鍩鸿涓嶈兘閫傚綋鍦板鍒躲傞殢鏈轰骇鐢熺殑缁撴灉锛屼粎鍥犲叾鏈э紝鏄綘鍙娴嬬殑鍜屼笉鑳借纭疄鍏嬮殕鐨勩傚洜姝わ紝澶嶅埗鍒颁粠鏈嶅姟鍣ㄧ殑闅忔満琛屼负灏嗕笉浼氶暅鍍忛偅浜涗骇鐢熷湪涓绘湇鍔″櫒涓婄殑銆傛敞鎰忥紝 澹版槑瀛樺偍绋嬪簭鎴栧嚱鏁颁负DETERMINISTIC鎴栬呭湪log_bin_trust_routine_creators涓缃郴缁熷彉閲忎负0 灏嗕細鍏佽闅忓嵆鍊兼搷浣滆璋冪敤銆
姝ゅ锛屾椂鍩鸿涓轰笉鑳藉湪浠庢湇鍔″櫒涓婇噸鏂颁骇鐢燂紝鍥犱负鍦ㄥ瓨鍌ㄧ▼搴忎腑閫氳繃瀵瑰鍒朵娇鐢ㄧ殑浜岃繘鍒舵棩蹇楁潵璁℃椂杩欐牱鐨勬椂鍩鸿涓烘槸涓嶅彲閲嶆柊浜х敓鐨勶紝鍥犱负璇ヤ簩杩涘埗鏃ュ織浠呯邯褰旸ML浜嬩欢涓斾笉鍖呮嫭璁℃椂绾︽潫銆
鏈鍚庯紝鍦ㄥぇ鍨婦ML琛屼负锛堝澶ф壒鎻掑叆锛変腑闈炰氦浜掕〃鍙戠敓閿欒锛岃闈炰氦浜掕〃鍙兘缁忓巻澶嶅埗锛屽湪澶嶅埗鐗堢殑闈炰氦浜掕〃涓富鏈嶅姟鍣ㄥ彲浠ヨ閮ㄥ垎鍦颁粠DML琛屼负鏇存柊銆備絾鏄洜涓哄彂鐢熺殑閭d釜閿欒锛屽浠庢湇鍔″櫒娌℃湁鏇存柊銆 瀵瑰嚱鏁扮殑DML琛屼负锛屽伐浣滃尯灏嗚鐢↖GNORE鍏抽敭璇嶆潵鎵ц锛屼互渚夸簬鍦ㄤ富鏈嶅姟鍣ㄤ笂瀵艰嚧閿欒鐨勬洿鏂拌蹇界暐锛屽苟涓斾笉浼氬鑷撮敊璇殑鏇存柊琚鍒跺埌浠庢湇鍔″櫒銆
褰卞搷澶嶅埗鐨勫悓涓闄愬埗浼氬奖鍝峱oint-in-time鎭㈠銆
灏嗘潵鍙戣鐨凪ySQL棰勬湡鏈変竴涓姛鑳藉幓閫夋嫨澶嶅埗璇ュ浣曡澶勭悊锛
MySQL 5.1涓殑瑙﹀彂绋嬪簭鍜屽鍒惰薄鍦ㄥぇ澶氭暟鍏跺畠鏁版嵁搴撳紩鎿庝腑涓鏍峰伐浣滐紝鍦ㄩ偅浜涘紩鎿庝腑锛岄氳繃瑙﹀彂绋嬪簭鍦ㄤ富鏈嶅姟鍣ㄤ笂鎵ц鐨勮涓轰笉琚鍒跺埌浠庢湇鍔″櫒銆傚彇鑰屼唬涔嬬殑鏄紝浣嶄簬涓籑ySQL鏈嶅姟鍣ㄧ殑琛ㄤ腑鐨 瑙﹀彂绋嬪簭闇瑕佸湪閭d簺瀛樺湪浜庝换浣昅ySQL浠庢湇鍔″櫒涓婄殑琛ㄥ唴琚垱寤猴紝浠ヤ究浜庤Е鍙戠▼搴忓彲浠ヤ篃鍙互鍦ㄤ粠鏈嶅姟鍣ㄤ笂琚縺娲汇
棣栧厛锛屼富鏈嶅姟鍣ㄤ笂鐨勮Е鍙戠▼搴忓繀椤诲湪浠庢湇鍔″櫒涓婇噸寤恒備竴鏃﹂噸寤轰簡锛屽鍒舵祦绋嬪氨璞″叾瀹冨弬涓庡埌澶嶅埗涓殑鏍囧噯DML璇彞涓鏍峰伐浣溿備緥濡傦細鑰冭檻涓涓凡缁忔彃鍏ヨЕ鍙戠▼搴廇FTER鐨凟MP琛紝瀹冧綅浜庝富MySQL鏈嶅姟鍣ㄤ笂銆傚悓鏍风殑EMP琛ㄥ拰AFTER鎻掑叆 瑙﹀彂绋嬪簭涔熷瓨鍦ㄤ簬浠庢湇鍔″櫒涓娿傚鍒舵祦绋嬪彲鑳芥槸锛
1.
瀵笶MP鍋氫竴涓狪NSERT璇彞銆
2. EMP涓婄殑AFTER瑙﹀彂绋嬪簭婵娲汇
3. INSERT璇彞琚啓杩涗簩杩涘埗鏃ュ織銆
4.
浠庢湇鍔″櫒涓婄殑澶嶅埗鎷捐捣INSERT璇彞缁橢MP琛紝骞跺湪浠庢湇鍔″櫒涓婃墽琛屽畠銆
5. 浣嶄簬浠庢湇鍔″櫒EMP涓婄殑AFTER瑙﹀彂绋嬪簭婵娲汇
锛岃繖涓鑺備粙缁峂ySQL 5.1濡備綍鑰冭檻浜岃繘鍒舵棩蹇楀姛鑳芥潵澶勭悊瀛樺偍瀛愮▼搴忥紙绋嬪簭鍜屽嚱鏁帮級
銆傝繖涓鑺備篃閫傜敤浜庤Е鍙戠▼搴忋
浜岃繘鍒舵棩蹇楀寘鍚慨鏀规暟鎹簱鍐呭鐨凷QL璇彞鐨勪俊鎭傝繖涓俊鎭互鎻忚堪淇敼鐨勪簨浠剁殑褰㈠紡淇濆瓨璧锋潵銆
浜岃繘鍒舵棩蹇楁湁涓や釜閲嶈鐩殑锛
·
澶嶅埗鐨勫熀纭鏄富鏈嶅姟鍣ㄥ彂閫佸寘鍚湪浜岃繘鍒舵棩蹇楅噷鐨勪簨浠跺埌浠庢湇鍔″櫒锛屼粠鏈嶅姟鍣ㄦ墽琛岃繖浜涗簨浠舵潵閫犳垚涓庡涓绘湇鍔″櫒閫犳垚鐨勫悓鏍风殑鏁版嵁鏀瑰彉锛岃鍙傞槄6.2鑺傦紝鈥澶嶅埗姒傝堪”銆
·
鐗瑰畾鐨勬暟鎹仮澶嶆搷浣滆瑕佷娇鐢ㄤ簩杩涘埗鏃ュ織銆傚浠界殑鏂囦欢琚仮澶嶄箣鍚庯紝澶囦唤鍚庣邯褰曠殑浜岃繘鍒舵棩蹇楅噷鐨勪簨浠惰閲嶆柊鎵ц銆傝繖浜涗簨浠舵妸鏁版嵁搴撳甫浠庡浠界偣鐨勬棩瀛愬甫鍒板綋鍓嶃傝鍙傞槄5.9.2.2鑺傦紝鈥浣跨敤澶囦唤鎭㈠”銆
MySQL涓紝浠ュ瓨鍌ㄥ瓙绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘寮曞彂浜嗗緢澶氶棶棰橈紝杩欎簺鍦ㄤ笅闈㈣璁轰腑鍒楀嚭锛屼綔涓哄弬鑰冧俊鎭
闄や簡瑕佸彟澶栨敞鎰忕殑涔嬪锛岃繖浜涜皥璁哄亣璁句綘宸茬粡閫氳繃鐢--log-bin閫夐」鍚姩鏈嶅姟鍣ㄥ厑璁镐簡浜岃繘鍒舵棩蹇楀姛鑳姐傦紙濡傛灉浜岃繘鍒舵棩蹇楀姛鑳戒笉琚厑璁革紝澶嶅埗灏嗕笉鍙兘锛屼负鏁版嵁鎭㈠鐨勪簩杩涘埗鏃ュ織涔熶笉瀛樺湪銆傦級璇峰弬闃5.11.3鑺傦紝鈥浜岃繘鍒舵棩蹇”銆
瀵瑰瓨鍌ㄥ瓙绋嬪簭璇彞鐨勪簩杩涘埗鏃ュ織鍔熻兘鐨勭壒寰佸湪涓嬮潰鍒楄〃涓弿杩般備竴浜涙潯鐩寚鍑轰綘搴旇娉ㄦ剰鍒扮殑闂銆備絾鏄湪涓浜涙儏鍐典笅锛屾湁浣犲彲浠ユ洿鏀圭殑濡囦簲涓冭缃垨浣犲彲浠ョ敤鏉ュ鐞嗗畠浠殑宸ヤ綔鍖恒
·
CREATE
PROCEDURE, CREATE FUNCTION, ALTER PROCEDURE,鍜孉LTER FUNCTION
璇彞琚啓杩涗簩杩涘埗鏃ュ織锛孋ALL, DROP PROCEDURE,
鍜孌ROP FUNCTION 涔熶竴鏍枫
灏界濡傛锛屽澶嶅埗鏈変竴涓畨鍏ㄦ殫绀猴細瑕佸垱寤轰竴涓瓙绋嬪簭锛岀敤鎴峰繀椤绘湁CREATE ROUTINE鏉冮檺,浣嗘湁杩欎釜鏉冮檺鐨勭敤鎴蜂笉鑳藉啓涓涓
瀛愮▼搴忓湪浠庢湇鍔″櫒涓婃墽琛屼换浣曟搷浣溿傚洜涓哄湪浠庢湇鍔″櫒涓婄殑SQL绾跨▼鐢ㄥ畬鍏ㄦ潈闄愭潵杩愯銆備緥濡傦紝濡傛灉涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄥ垎鍒湁鏈嶅姟鍣↖D鍊1鍜2锛屽湪涓绘湇鍔″櫒涓婄殑鐢ㄦ埛鍙兘鍒涘缓骞惰皟鐢ㄥ涓嬩竴涓
绋嬪簭锛
mysql> delimiter //
mysql> CREATE PROCEDURE mysp ()
-> BEGIN
-> IF @@server_id=2 THEN DROP DATABASE accounting; END IF;
-> END;
-> //
mysql> delimiter ;
mysql> CALL mysp();
CREATE PROCEDURE鍜孋ALL璇彞灏嗚鍐欒繘浜岃繘鍒舵棩蹇楋紝鎵浠ヤ粠鏈嶅姟鍣ㄥ皢鎵ц瀹冧滑銆傚洜涓轰粠SQL绾跨▼鏈夊畬鍏ㄦ潈闄愶紝瀹冨皢绉婚櫎accounting鏁版嵁搴撱
瑕佷娇鍏佽浜岃繘鍒舵棩蹇楀姛鑳界殑鏈嶅姟鍣ㄩ伩鍏嶈繖涓嵄闄╋紝MySQL 5.1宸茬粡瑕佹眰 瀛樺偍绋嬪簭鍜屽嚱鏁扮殑鍒涘缓鑰呴櫎浜嗛氬父闇瑕佺殑CREATE ROUTINE鐨勬潈闄愬锛岃繕蹇呴』鏈塖UPER 鏉冮檺銆傜被浼煎湴锛岃浣跨敤ALTER PROCEDURE鎴朅LTER FUNCTION锛岄櫎浜咥LTER ROUTINE鏉冮檺澶栦綘蹇呴』鏈塖UPER鏉冮檺銆傛病鏈塖UPER鏉冮檺锛屽皢浼氬彂鐢熶竴涓敊璇細
ERROR 1419 (HY000): You do not have the SUPER privilege and
binary logging is enabled (you *might* want to use the less safe
log_bin_trust_routine_creators variable)
浣犲彲鑳戒笉鎯冲己鍒惰姹傚瓙绋嬪簭鍒涘缓鑰呭繀椤绘湁SUPER鏉冮檺銆備緥濡傦紝浣犵郴缁熶笂鎵鏈夋湁CREATE ROUTINE鏉冮檺鐨勭敤鎴峰彲鑳芥槸鏈夌粡楠岀殑搴旂敤绋嬪簭寮鍙戣呫傝绂佹鎺夊SUPER鏉冮檺鐨勮姹傦紝璁剧疆log_bin_trust_routine_creators 鍏ㄥ眬绯荤粺鍙橀噺涓1銆傞粯璁ゅ湴锛岃繖涓彉閲忓间负0锛屼絾浣犲彲浠ヨ薄杩欐牱鏀瑰彉杩欐牱锛
mysql> SET GLOBAL log_bin_trust_routine_creators = 1;
浣犱篃鍙互鍦ㄥ惎鍔ㄦ湇鍔″櫒涔嬫椂鐢--log-bin-trust-routine-creators閫夐」鏉ヨ缃厑璁歌繖涓彉閲忋
濡傛灉浜岃繘鍒舵棩蹇楀姛鑳戒笉琚厑璁革紝log_bin_trust_routine_creators
娌℃湁琚敤涓婏紝瀛愮▼搴忓垱寤洪渶瑕丼UPER鏉冮檺銆
·
涓涓墽琛屾洿鏂扮殑闈炵‘瀹氬瓙绋嬪簭鏄笉鍙噸澶嶇殑锛屽畠鑳芥湁涓や釜涓嶅鎰忕殑褰卞搷锛
o 瀹冧細浣垮緱浠庢湇鍔″櫒涓嶅悓浜庝富鏈嶅姟鍣銆
- 鎭㈠鐨勬暟鎹笌鍘熷鏁版嵁涓嶅悓銆
瑕佽В鍐宠繖浜涢棶棰橈紝MySQL寮哄埗鍋氫笅闈㈣姹傦細鍦ㄤ富鏈嶅姟鍣ㄤ笂锛岄櫎闈炲瓙绋嬪簭琚0鏄庝负纭畾鎬х殑鎴栬呬笉鏇存敼鏁版嵁锛屽惁鍒欏垱寤烘垨鑰呮浛鎹㈠瓙绋嬪簭灏嗚鎷掔粷銆傝繖鎰忓懗鐫褰撲綘鍒涘缓涓涓瓙绋嬪簭鐨勬椂鍊欙紝浣犲繀椤昏涔堝0鏄庡畠鏄‘瀹氭х殑锛岃涔堝畠涓嶆敼鍙樻暟鎹備袱濂楀瓙绋嬪簭鐗瑰緛鍦ㄨ繖閲岄傜敤锛
-
DETERMINISTIC鍜孨OT
DETERMINISTIC鎸囧嚭涓涓瓙绋嬪簭鏄惁瀵圭粰瀹氱殑杈撳叆鎬绘槸浜х敓鍚屾牱鐨勭粨鏋溿傚鏋滄病鏈夌粰瀹氫换涓鐗瑰緛锛岄粯璁ゆ槸NOT DETERMINISTIC锛屾墍浠ヤ綘蹇呴』鏄庣‘鎸囧畾DETERMINISTIC鏉
澹版槑涓涓
瀛愮▼搴忔槸纭畾鎬х殑銆
浣跨敤NOW()
鍑芥暟锛堟垨瀹冪殑鍚屼箟锛夋垨鑰匯AND()
鍑芥暟涓嶆槸蹇呰鍦颁娇涔熶竴涓瓙绋嬪簭闈炵‘瀹氭с傚NOW()鑰岃█锛屼簩杩涘埗鏃ュ織鍖呮嫭鏃堕棿鎴冲苟姝g‘澶嶅埗銆俁AND()鍙鍦ㄤ竴涓
瀛愮▼搴忓唴琚皟鐢ㄤ竴娆′篃鍙互姝g‘澶嶅埗銆傦紙浣犲彲浠ヨ涓哄瓙绋嬪簭鎵ц鏃堕棿鎴冲拰闅忔満鏁扮瀛愪綔涓烘鏃犵枒闂湴杈撳叆锛屽畠浠湪涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂鏄竴鏍风殑銆傦級
-
CONTAINS SQL, NO
SQL, READS SQL DATA, 鍜 MODIFIES SQL鏁版嵁鎻愪緵瀛愮▼搴忔槸璇昏繕鏄啓鏁版嵁鐨勪俊鎭傛棤璁篘O
SQL 杩樻槸READS SQL DATA i閮芥寚鍑猴紝瀛愮▼搴忔病鏈夋敼鍙樻暟鎹紝浣嗕綘蹇呴』鏄庣櫧鍦版寚鏄庤繖浜涗腑鐨勪竴涓紝鍥犱负濡傛灉浠讳綍杩欎簺鐗瑰緛娌℃湁琚粰鍑猴紝
榛樿鐨勭壒寰佹槸CONTAINS SQL銆
榛樿鍦帮紝瑕佷竴涓狢REATE PROCEDURE 鎴 CREATE FUNCTION 璇彞琚帴鍙楋紝DETERMINISTIC 鎴 NO SQL涓嶳EADS SQL DATA 涓殑涓涓繀椤绘槑鐧藉湴鎸囧畾锛屽惁鍒欎細浜х敓濡備笅閿欒锛
ERROR 1418 (HY000): This routine has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled
(you *might* want to use the less safe log_bin_trust_routine_creators
variable)
濡傛灉璁剧疆log_bin_trust_routine_creators
涓1, 绉婚櫎瀵瑰瓙绋嬪簭蹇呴』鏄‘瀹氱殑鎴栦笉淇敼鏁版嵁鐨勮姹傘
娉ㄦ剰锛屽瓙绋嬪簭鏈х殑璇勪及鏄熀浜庡垱寤鸿呯殑“璇氬疄搴”
锛歁ySQL涓嶆鏌ュ0鏄庝负纭畾鎬х殑瀛愮▼搴忔槸鍚︿笉鍚骇鐢熼潪纭畾鎬х粨鏋滅殑璇彞銆
· 濡傛灉瀛愮▼搴忚繑鍥炴棤閿欙紝CALL璇彞琚啓杩涗簩杩涘埗鏃ュ織锛屽惁鍒欏氨涓嶅啓銆傚綋涓涓瓙绋嬪簭淇敼鏁版嵁澶辫触浜嗭紝浣犱細寰楀埌杩欐牱鐨勮鍛婏細
路 ERROR 1417 (HY000): A routine failed and has neither NO SQL nor
路 READS SQL DATA in its declaration and binary logging is enabled; if
路 non-transactional tables were updated, the binary log will miss their
路 changes
杩欎釜璁版棩蹇楄涓烘綔鍦ㄥ湴瀵艰嚧闂.濡傛灉涓涓瓙绋嬪簭閮ㄥ垎鍦颁慨鏀逛竴涓潪浜や簰琛紙姣斿涓涓狹yISAM琛╝ble锛夊苟涓旇繑鍥炰竴涓敊璇紝浜岃繘鍒舵棩蹇楀皢鍙嶆槧杩欎簺鍙樺寲銆傝闃叉杩欑鎯呭喌锛屼綘搴旇鍦
瀛愮▼搴忎腑浣跨敤浜や簰琛ㄥ苟涓斿湪浜や簰鍔ㄤ綔鍐呬慨鏀硅〃銆
鍦ㄤ竴涓瓙绋嬪簭鍐咃紝濡傛灉浣犲湪INSERT, DELETE, 鎴栬匲PDATE閲屼娇鐢↖GNORE鍏抽敭璇嶆潵蹇界暐閿欒锛屽彲鑳藉彂鐢熶竴涓儴鍒嗘洿鏂帮紝浣嗘病鏈夐敊璇骇鐢熴傝繖鏍风殑璇彞琚褰曟棩蹇楋紝涓旀甯稿鍒躲
· 濡傛灉涓涓瓨鍌ㄥ嚱鏁板湪涓涓SELECT杩欐牱涓嶄慨鏀规暟鎹殑璇彞鍐呰璋冪敤锛屽嵆浣垮嚱鏁版湰韬洿鏀规暟鎹紝鍑芥暟鐨勬墽琛屼篃灏嗕笉琚啓杩涗簩杩涘埗鏃ュ織閲屻傝繖涓褰曟棩蹇楃殑琛屼负娼滃湪鍦板鑷撮棶棰樸傚亣璁惧嚱鏁癿yfunc()濡備笅瀹氫箟锛
路 CREATE FUNCTION myfunc () RETURNS INT
路 BEGIN
路 INSERT INTO t (i) VALUES(1);
路 RETURN 0;
路 END;
鎸夌収涓婇潰瀹氫箟锛涓嬮潰鐨勮鍙ヤ慨鏀琛╰锛屽洜涓簃yfunc()淇敼琛╰,
浣嗘槸璇彞涓嶈鍐欒繘浜岃繘鍒舵棩蹇楋紝鍥犱负瀹冩槸涓涓猄ELECT璇彞锛
SELECT myfunc();
瀵硅繖涓棶棰樼殑宸ヤ綔鍖哄皢璋冪敤鍦ㄥ仛鏇存柊鐨勮鍙ラ噷鍋氭洿鏂扮殑鍑芥暟銆傛敞鎰忥紝铏界劧DO璇彞鏈夋椂涓轰簡鍏朵及绠楄〃杈惧紡鐨勫壇鏁堝簲鑰岃鎵ц锛孌O鍦ㄨ繖閲屼笉鏄竴涓伐浣滃尯锛屽洜涓哄畠涓嶈鍐欒繘浜岃繘鍒舵棩蹇椼
·
鍦ㄤ竴涓瓙绋嬪簭鍐呮墽琛岀殑璇彞涓嶈鍐欒繘浜岃繘鍒舵棩蹇椼傚亣濡備綘鍙戝竷涓嬪垪璇彞锛
路 CREATE PROCEDURE mysp INSERT INTO t VALUES(1);
路 CALL mysp;
瀵逛簬杩欎釜渚嬪瓙鏉ヨ锛孋REATE
PROCEDURE 鍜孋ALL璇彞鍑虹幇鍦ㄤ簩杩涘埗鏃ュ織閲岋紝浣咺NSERT璇彞骞舵湭鍑虹幇銆
·
鍦ㄤ粠鏈嶅姟鍣ㄤ笂锛屽綋鍐冲畾澶嶅埗鍝釜鏉ヨ嚜涓绘湇鍔″櫒鐨勪簨浠舵椂锛屼笅鍒楅檺鍒惰搴旂敤锛--replicate-*-table瑙勫垯涓嶉傜敤浜嶤ALL璇彞鎴栧瓙绋嬪簭鍐呯殑璇彞锛氬湪杩欎簺鎯呭喌涓嬶紝鎬绘槸杩斿洖鈥滃鍒讹紒”
瑙﹀彂绋嬪簭绫讳技浜庡瓨鍌ㄥ嚱鏁帮紝鎵浠ュ墠杩扮殑璇勮涔熼傜敤浜庤Е鍙戠▼搴忥紝闄や簡涓嬪垪鎯呭喌锛 CREATE
TRIGGER娌℃湁鍙夌殑DETERMINISTIC鐗瑰緛锛屾墍浠ヨЕ鍙戠▼搴忚鍋囧畾涓烘绘槸纭畾鎬х殑銆傜劧鑰岋紝杩欎釜鍋囪鍦ㄤ竴浜涙儏鍐典笅鏄潪娉曠殑銆傛瘮濡傦紝UUID()鍑芥暟鏄潪纭畾鎬х殑锛堜笉鑳藉鍒讹級銆備綘搴旇灏忓績鍦
瑙﹀彂绋嬪簭涓娇鐢ㄨ繖涓嚱鏁般
瑙﹀彂绋嬪簭鐩墠涓嶈兘鏇存柊琛紝浣嗘槸鍦ㄥ皢鏉ヤ細鏀寔銆傚洜涓鸿繖涓師鍥狅紝濡傛灉浣犳病鏈塖UPER鏉冮檺涓攍og_bin_trust_routine_creators 琚涓0锛屽緱鍒扮殑閿欒淇℃伅绫讳技浜庡瓨鍌ㄥ瓙绋嬪簭涓嶤REATE TRIGGER浜х敓鐨勯敊璇俊鎭
鍦ㄦ湰鑺備腑鍙欒堪鐨勯棶棰樻潵鑷彂鐢熷湪SQL璇彞绾у埆鐨勪簩杩涘埗鏃ュ織璁板綍鐨勪簨瀹炪傛湭鏉ュ彂琛岀殑MySQL鏈熸湜鑳藉疄鐜拌绾х殑浜岃繘鍒舵棩蹇楄褰曪紝璁板綍鍙戠敓鍦ㄦ洿
缁嗚嚧鐨勭骇鍒苟涓旀寚鍑哄摢涓敼鍙樹綔涓烘墽琛孲QL鐨勭粨鏋滃鍗曚釜璁板綍鑰屽仛銆
杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆