鍦5.1鐗MySQL鏈嶅姟鍣ㄤ腑鎻愪緵浜嗚鍥惧姛鑳斤紙鍖呮嫭鍙洿鏂拌鍥撅級銆
鏈珷璁ㄨ浜嗕笅杩颁富棰橈細
路 浣跨敤CREATE VIEW鎴ALTER VIEW鍒涘缓鎴栨洿鏀硅鍥俱
路 浣跨敤DROP VIEW閿姣佽鍥俱
路 浣跨敤SHOW CREATE VIEW鏄剧ず瑙嗗浘鍏冩暟鎹
鍏充簬浣跨敤瑙嗗浘鏂归潰鐨勯檺鍒讹紝璇峰弬瑙闄勫綍I锛氱壒鎬ч檺鍒銆
濡傛灉浣犲凡浠庝笉鏀寔瑙嗗浘鐨勮緝鏃х増鏈崌绾у埌MySQL 5.1锛岃鎯充娇鐢ㄨ鍥撅紝搴斿崌绾ф巿鏉冭〃锛屼娇涔嬪寘鍚笌瑙嗗浘鏈夊叧鐨勬潈闄愩傝鍙傝2.10.2鑺傦紝鈥滃崌绾ф巿鏉冭〃鈥銆
ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
璇ヨ鍙ョ敤浜庢洿鏀瑰凡鏈夎鍥剧殑瀹氫箟銆傚叾璇硶涓CREATE VIEW绫讳技銆傝鍙傝22.2鑺傦紝鈥淐REATE VIEW璇硶鈥銆傝璇彞闇瑕佸叿鏈夐拡瀵硅鍥剧殑CREATE VIEW鍜DROP鏉冮檺锛屼篃闇瑕侀拡瀵SELECT璇彞涓紩鐢ㄧ殑姣忎竴鍒楃殑鏌愪簺鏉冮檺銆
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
璇ヨ鍙ヨ兘鍒涘缓鏂扮殑瑙嗗浘锛屽鏋滅粰瀹氫簡OR REPLACE瀛愬彞锛岃璇彞杩樿兘鏇挎崲宸叉湁鐨勮鍥俱select_statement鏄竴绉SELECT璇彞锛屽畠缁欏嚭浜嗚鍥剧殑瀹氫箟銆傝璇彞鍙粠鍩鸿〃鎴栧叾浠栬鍥捐繘琛岄夋嫨銆
璇ヨ鍙ヨ姹傚叿鏈夐拡瀵硅鍥剧殑CREATE VIEW鏉冮檺锛屼互鍙婇拡瀵圭敱SELECT璇彞閫夋嫨鐨勬瘡涓鍒椾笂鐨勬煇浜涙潈闄愩瀵逛簬鍦SELECT璇彞涓叾浠栧湴鏂逛娇鐢ㄧ殑鍒楋紝蹇呴』鍏锋湁SELECT鏉冮檺銆濡傛灉杩樻湁OR REPLACE瀛愬彞锛屽繀椤诲湪瑙嗗浘涓婂叿鏈DROP鏉冮檺銆
瑙嗗浘灞炰簬鏁版嵁搴撱傚湪榛樿鎯呭喌涓嬶紝灏嗗湪褰撳墠鏁版嵁搴撳垱寤烘柊瑙嗗浘銆傝鎯冲湪缁欏畾鏁版嵁搴撲腑鏄庣‘鍒涘缓瑙嗗浘锛屽垱寤烘椂锛屽簲灏嗗悕绉版寚瀹氫负db_name.view_name銆
mysql> CREATE VIEW test.v AS SELECT * FROM t;
琛ㄥ拰瑙嗗浘鍏变韩鏁版嵁搴撲腑鐩稿悓鐨勫悕绉扮┖闂达紝鍥犳锛屾暟鎹簱涓嶈兘鍖呭惈鍏锋湁鐩稿悓鍚嶇О鐨勮〃鍜岃鍥俱
瑙嗗浘蹇呴』鍏锋湁鍞竴鐨勫垪鍚嶏紝涓嶅緱鏈夐噸澶嶏紝灏卞儚鍩鸿〃閭f牱銆傞粯璁ゆ儏鍐典笅锛岀敱SELECT璇彞妫绱㈢殑鍒楀悕灏嗙敤浣滆鍥惧垪鍚嶃傝鎯充负瑙嗗浘鍒楀畾涔夋槑纭殑鍚嶇О锛屽彲浣跨敤鍙夌殑column_list瀛愬彞锛屽垪鍑虹敱閫楀彿闅斿紑鐨ID銆column_list涓殑鍚嶇О鏁扮洰蹇呴』绛変簬SELECT璇彞妫绱㈢殑鍒楁暟銆
SELECT璇彞妫绱㈢殑鍒楀彲浠ユ槸瀵硅〃鍒楃殑绠鍗曞紩鐢ㄣ備篃鍙互鏄娇鐢ㄥ嚱鏁般佸父閲忓笺佹搷浣滅绛夌殑琛ㄨ揪寮忋
瀵逛簬SELECT璇彞涓笉鍚堟牸鐨勮〃鎴栬鍥撅紝灏嗘牴鎹粯璁ょ殑鏁版嵁搴撹繘琛岃В閲娿傞氳繃鐢ㄦ伆褰撶殑鏁版嵁搴撳悕绉伴檺瀹氳〃鎴栬鍥惧悕锛岃鍥捐兘澶熷紩鐢ㄨ〃鎴栧叾浠栨暟鎹簱涓殑瑙嗗浘銆
鑳藉浣跨敤澶氱SELECT璇彞鍒涘缓瑙嗗浘銆傝鍥捐兘澶熷紩鐢ㄥ熀琛ㄦ垨鍏朵粬瑙嗗浘銆傚畠鑳戒娇鐢ㄨ仈鍚堛UNION鍜屽瓙鏌ヨ銆SELECT鐢氳嚦涓嶉渶寮曠敤浠讳綍琛ㄣ鍦ㄤ笅闈㈢殑绀轰緥涓紝瀹氫箟浜嗕粠鍙︿竴琛ㄩ夋嫨涓ゅ垪鐨勮鍥撅紝骞剁粰鍑轰簡鏍规嵁杩欎簺鍒楄绠楃殑琛ㄨ揪寮忥細
mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+
瑙嗗浘瀹氫箟鏈嶄粠涓嬭堪闄愬埗锛
路 SELECT璇彞涓嶈兘鍖呭惈FROM瀛愬彞涓殑瀛愭煡璇€
路 SELECT璇彞涓嶈兘寮曠敤绯荤粺鎴栫敤鎴峰彉閲忋
路 SELECT璇彞涓嶈兘寮曠敤棰勫鐞嗚鍙ュ弬鏁般
路 鍦ㄥ瓨鍌ㄥ瓙绋嬪簭鍐咃紝瀹氫箟涓嶈兘寮曠敤瀛愮▼搴忓弬鏁版垨灞閮ㄥ彉閲忋
路 鍦ㄥ畾涔変腑寮曠敤鐨勮〃鎴栬鍥惧繀椤诲瓨鍦ㄣ備絾鏄紝鍒涘缓浜嗚鍥惧悗锛岃兘澶熻垗寮冨畾涔夊紩鐢ㄧ殑琛ㄦ垨瑙嗗浘銆傝鎯虫鏌ヨ鍥惧畾涔夋槸鍚﹀瓨鍦ㄨ繖绫婚棶棰橈紝鍙娇鐢CHECK TABLE璇彞銆
路 鍦ㄥ畾涔変腑涓嶈兘寮曠敤TEMPORARY琛紝涓嶈兘鍒涘缓TEMPORARY瑙嗗浘銆
路 鍦ㄨ鍥惧畾涔変腑鍛藉悕鐨勮〃蹇呴』宸插瓨鍦ㄣ
路 涓嶈兘灏嗚Е鍙戠▼搴忎笌瑙嗗浘鍏宠仈鍦ㄤ竴璧枫
鍦ㄨ鍥惧畾涔変腑鍏佽浣跨敤ORDER BY锛屼絾鏄紝濡傛灉浠庣壒瀹氳鍥捐繘琛屼簡閫夋嫨锛岃岃瑙嗗浘浣跨敤浜嗗叿鏈夎嚜宸ORDER BY鐨勮鍙ワ紝瀹冨皢琚拷鐣ャ
瀵逛簬瀹氫箟涓殑鍏朵粬閫夐」鎴栧瓙鍙ワ紝瀹冧滑灏嗚澧炲姞鍒板紩鐢ㄨ鍥剧殑璇彞鐨勯夐」鎴栧瓙鍙ヤ腑锛屼絾鏁堟灉鏈畾涔夈備緥濡傦紝濡傛灉鍦ㄨ鍥惧畾涔変腑鍖呭惈LIMIT瀛愬彞锛岃屼笖浠庣壒瀹氳鍥捐繘琛屼簡閫夋嫨锛岃岃瑙嗗浘浣跨敤浜嗗叿鏈夎嚜宸LIMIT瀛愬彞鐨勮鍙锛岄偅涔堝浣跨敤鍝釜LIMIT鏈綔瀹氫箟銆傜浉鍚岀殑鍘熺悊涔熼傜敤浜庡叾浠栭夐」锛屽璺熷湪SELECT鍏抽敭瀛楀悗鐨ALL銆DISTINCT鎴SQL_SMALL_RESULT锛骞堕傜敤浜庡叾浠栧瓙鍙ワ紝濡INTO銆FOR UPDATE銆LOCK IN SHARE MODE銆佷互鍙PROCEDURE銆
濡傛灉鍒涘缓浜嗚鍥撅紝骞堕氳繃鏇存敼绯荤粺鍙橀噺鏇存敼浜嗘煡璇㈠鐞嗙幆澧冿紝浼氬奖鍝嶄粠瑙嗗浘鑾峰緱鐨勭粨鏋滐細
mysql> CREATE VIEW v AS SELECT CHARSET(CHAR(65)), COLLATION(CHAR(65));
Query OK, 0 rows affected (0.00 sec)
mysql> SET NAMES 'latin1';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM v;
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| latin1 | latin1_swedish_ci |
+-------------------+---------------------+
1 row in set (0.00 sec)
mysql> SET NAMES 'utf8';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM v;
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| utf8 | utf8_general_ci |
+-------------------+---------------------+
1 row in set (0.00 sec)
鍙夌殑ALGORITHM瀛愬彞鏄鏍囧噯SQL鐨MySQL鎵╁睍銆ALGORITHM鍙彇涓変釜鍊硷細MERGE銆TEMPTABLE鎴UNDEFINED銆傚鏋滄病鏈ALGORITHM瀛愬彞锛岄粯璁ょ畻娉曟槸UNDEFINED锛堟湭瀹氫箟鐨勶級銆傜畻娉曚細褰卞搷MySQL澶勭悊瑙嗗浘鐨勬柟寮忋
瀵逛簬MERGE锛屼細灏嗗紩鐢ㄨ鍥剧殑璇彞鐨勬枃鏈笌瑙嗗浘瀹氫箟鍚堝苟璧锋潵锛屼娇寰楄鍥惧畾涔夌殑鏌愪竴閮ㄥ垎鍙栦唬璇彞鐨勫搴旈儴鍒嗐
瀵逛簬TEMPTABLE锛岃鍥剧殑缁撴灉灏嗚缃簬涓存椂琛ㄤ腑锛岀劧鍚庝娇鐢ㄥ畠鎵ц璇彞銆
瀵逛簬UNDEFINED锛MySQL灏嗛夋嫨鎵瑕佷娇鐢ㄧ殑绠楁硶銆傚鏋滃彲鑳斤紝瀹冨惧悜浜MERGE鑰屼笉鏄TEMPTABLE锛岃繖鏄洜涓MERGE閫氬父鏇存湁鏁堬紝鑰屼笖濡傛灉浣跨敤浜嗕复鏃惰〃锛岃鍥炬槸涓嶅彲鏇存柊鐨勩
鏄庣‘閫夋嫨TEMPTABLE鐨1涓師鍥犲湪浜庯紝鍒涘缓涓存椂琛ㄤ箣鍚庛佸苟鍦ㄥ畬鎴愯鍙ュ鐞嗕箣鍓嶏紝鑳藉閲婃斁鍩鸿〃涓婄殑閿佸畾銆備笌MERGE绠楁硶鐩告瘮锛岄攣瀹氶噴鏀剧殑閫熷害鏇村揩锛岃繖鏍凤紝浣跨敤瑙嗗浘鐨勫叾浠栧鎴风涓嶄細琚睆钄借繃闀挎椂闂淬
瑙嗗浘绠楁硶鍙互鏄UNDEFINED锛屾湁涓夌鏂瑰紡锛
路 鍦CREATE VIEW璇彞涓病鏈ALGORITHM瀛愬彞銆
路 CREATE VIEW璇彞鏈1涓樉寮ALGORITHM = UNDEFINED瀛愬彞銆
路 涓轰粎鑳界敤涓存椂琛ㄥ鐞嗙殑瑙嗗浘鎸囧畾ALGORITHM = MERGE銆傚湪杩欑鎯呭喌涓嬶紝MySQL灏嗙敓鎴愬憡璀︼紝骞跺皢绠楁硶璁剧疆涓UNDEFINED銆
姝e鍓嶉潰鎵浠嬬粛鐨勯偅鏍凤紝閫氳繃灏嗚鍥惧畾涔変腑鐨勫搴旈儴鍒嗗悎骞跺埌寮曠敤瑙嗗浘鐨勮鍙ヤ腑锛屽MERGE杩涜澶勭悊銆傚湪涓嬮潰鐨勭ず渚嬩腑锛岀畝瑕佷粙缁嶄簡MERGE鐨勫伐浣滄柟寮忋傚湪璇ョず渚嬩腑锛屽亣瀹氭湁1涓叿鏈変笅杩板畾涔夌殑瑙嗗浘v_merge锛
CREATE ALGORITHM = MERGE VIEW v_merge (vc1, vc2) AS
SELECT c1, c2 FROM t WHERE c3 > 100;
绀轰緥1锛氬亣瀹氬彂鍑轰簡涓嬭堪璇彞锛
SELECT * FROM v_merge;
MySQL浠ヤ笅杩版柟寮忓鐞嗚鍙ワ細
路 v_merge鎴愪负t
路 *鎴愪负vc1銆vc2锛涓c1銆c2瀵瑰簲
路 澧炲姞瑙嗗浘WHERE瀛愬彞
鎵浜х敓鐨勫皢鎵ц鐨勮鍙ヤ负锛
SELECT c1, c2 FROM t WHERE c3 > 100;
绀轰緥2锛氬亣瀹氬彂鍑轰簡涓嬭堪璇彞锛
SELECT * FROM v_merge WHERE vc1 < 100;
璇ヨ鍙ョ殑澶勭悊鏂瑰紡涓庡墠闈粙缁嶇殑绫讳技锛屼絾vc1 < 100鍙樹负c1 < 100锛骞朵娇鐢AND杩炴帴璇嶅皢瑙嗗浘鐨WHERE瀛愬彞娣诲姞鍒拌鍙ョ殑WHERE瀛愬彞涓紙澧炲姞浜嗗渾鎷彿浠ョ‘淇濅互姝g‘鐨勪紭鍏堥『搴忔墽琛屽瓙鍙ラ儴鍒嗭級銆傛墍寰楃殑灏嗚鎵ц鐨勮鍙ュ彉涓猴細
SELECT c1, c2 FROM t WHERE (c3 > 100) AND (c1 < 100);
浜嬪疄涓婏紝灏嗚鎵ц鐨勮鍙ユ槸鍏锋湁涓嬭堪褰㈠紡鐨WHERE瀛愬彞锛
WHERE (select WHERE) AND (view WHERE)
MERGE绠楁硶瑕佹眰瑙嗗浘涓殑琛屽拰鍩鸿〃涓殑琛屽叿鏈変竴瀵逛竴鐨勫叧绯汇傚鏋滀笉鍏锋湁璇ュ叧绯汇傚繀椤讳娇鐢ㄤ复鏃惰〃鍙栬屼唬涔嬨傚鏋滆鍥惧寘鍚笅杩扮粨鏋勪腑鐨勪换浣曚竴绉嶏紝灏嗗け鍘讳竴瀵逛竴鐨勫叧绯伙細
路 鑱氬悎鍑芥暟锛SUM(), MIN(), MAX(), COUNT()绛夛級銆
路 DISTINCT
路 GROUP BY
路 HAVING
路 UNION鎴UNION ALL
路 浠呭紩鐢ㄦ枃瀛楀硷紙鍦ㄨ鎯呭喌涓嬶紝娌℃湁鍩烘湰琛級銆
鏌愪簺瑙嗗浘鏄彲鏇存柊鐨勩備篃灏辨槸璇达紝鍙互鍦ㄨ濡UPDATE銆DELETE鎴INSERT绛夎鍙ヤ腑浣跨敤瀹冧滑锛屼互鏇存柊鍩鸿〃鐨勫唴瀹广傚浜庡彲鏇存柊鐨勮鍥撅紝鍦ㄨ鍥句腑鐨勮鍜屽熀琛ㄤ腑鐨勮涔嬮棿蹇呴』鍏锋湁涓瀵逛竴鐨勫叧绯銆傝繕鏈変竴浜涚壒瀹氱殑鍏朵粬缁撴瀯锛岃繖绫荤粨鏋勪細浣垮緱瑙嗗浘涓嶅彲鏇存柊銆傛洿鍏蜂綋鍦拌锛濡傛灉瑙嗗浘鍖呭惈涓嬭堪缁撴瀯涓殑浠讳綍涓绉嶏紝閭d箞瀹冨氨鏄笉鍙洿鏂扮殑锛
路 鑱氬悎鍑芥暟锛SUM(), MIN(), MAX(), COUNT()绛夛級銆
路 DISTINCT
路 GROUP BY
路 HAVING
路 UNION鎴UNION ALL
路 浣嶄簬閫夋嫨鍒楄〃涓殑瀛愭煡璇
路 Join
路 FROM瀛愬彞涓殑涓嶅彲鏇存柊瑙嗗浘
路 WHERE瀛愬彞涓殑瀛愭煡璇紝寮曠敤FROM瀛愬彞涓殑琛ㄣ
路 浠呭紩鐢ㄦ枃瀛楀硷紙鍦ㄨ鎯呭喌涓嬶紝娌℃湁瑕佹洿鏂扮殑鍩烘湰琛級銆
路 ALGORITHM = TEMPTABLE锛堜娇鐢ㄤ复鏃惰〃鎬讳細浣胯鍥炬垚涓轰笉鍙洿鏂扮殑锛銆
鍏充簬鍙彃鍏ユэ紙鍙敤INSERT璇彞鏇存柊锛锛屽鏋滃畠涔熸弧瓒冲叧浜庤鍥惧垪鐨勪笅杩伴澶栬姹傦紝鍙洿鏂扮殑瑙嗗浘涔熸槸鍙彃鍏ョ殑锛
路 涓嶅緱鏈夐噸澶嶇殑瑙嗗浘鍒楀悕绉般
路 瑙嗗浘蹇呴』鍖呭惈娌℃湁榛樿鍊肩殑鍩鸿〃涓殑鎵鏈夊垪銆
路 瑙嗗浘鍒楀繀椤绘槸绠鍗曠殑鍒楀紩鐢ㄨ屼笉鏄鍑哄垪銆傚鍑哄垪涓嶆槸绠鍗曠殑鍒楀紩鐢紝鑰屾槸浠庤〃杈惧紡瀵煎嚭鐨勩備笅闈㈢粰鍑轰簡涓浜涘鍑哄垪绀轰緥锛
路 3.14159
路 col1 + 3
路 UPPER(col2)
路 col3 / col4
路 (subquery)
娣峰悎浜嗙畝鍗曞垪寮曠敤鍜屽鍑哄垪鐨勮鍥炬槸涓嶅彲鎻掑叆鐨勶紝浣嗘槸锛屽鏋滀粎鏇存柊闈炲鍑哄垪锛岃鍥炬槸鍙洿鏂扮殑銆傝冭檻涓嬭堪瑙嗗浘锛
CREATE VIEW v AS SELECT col1, 1 AS col2 FROM t;
璇ヨ鍥炬槸涓嶅彲鎻掑叆鐨勶紝杩欐槸鍥犱负col2鏄粠琛ㄨ揪寮忓鍑虹殑銆備絾鏄紝濡傛灉鏇存柊鏃朵笉鏇存柊col2锛屽畠鏄彲鏇存柊鐨勩傝繖绫绘洿鏂版槸鍏佽鐨勶細
UPDATE v SET col1 = 0;
涓嬭堪鏇存柊鏄笉鍏佽鐨勶紝鍘熷洜鍦ㄤ簬锛屽畠璇曞浘鏇存柊瀵煎嚭鍒楋細
UPDATE v SET col2 = 0;
鍦ㄦ煇浜涙儏鍐典笅锛岃兘澶熸洿鏂板琛ㄨ鍥撅紝鍋囧畾瀹冭兘浣跨敤MERGE绠楁硶杩涜澶勭悊銆備负姝わ紝瑙嗗浘蹇呴』浣跨敤鍐呴儴鑱斿悎锛堣屼笉鏄閮ㄨ仈鍚堟垨UNION锛夈姝ゅ锛屼粎鑳芥洿鏂拌鍥惧畾涔変腑鐨勫崟涓〃锛屽洜姝わ紝SET瀛愬彞蹇呴』浠呭懡鍚嶈鍥句腑鏌愪竴琛ㄧ殑鍒椼傚嵆浣夸粠鐞嗚涓婅涔熸槸鍙洿鏂扮殑锛屼笉鍏佽浣跨敤UNION ALL鐨勮鍥撅紝杩欐槸鍥犱负锛屽湪瀹炴柦涓皢浣跨敤涓存椂琛ㄦ潵澶勭悊瀹冧滑銆
瀵逛簬澶氳〃鍙洿鏂拌鍥撅紝濡傛灉鏄皢鍏舵彃鍏ュ崟涓〃涓紝INSERT鑳藉宸ヤ綔銆涓嶆敮鎸DELETE銆
瀵逛簬鍙洿鏂拌鍥撅紝鍙粰瀹WITH CHECK OPTION瀛愬彞鏉ラ槻姝㈡彃鍏ユ垨鏇存柊琛岋紝闄ら潪浣滅敤鍦ㄨ涓婄殑select_statement涓殑WHERE瀛愬彞涓衡滅湡鈥濄
鍦ㄥ叧浜庡彲鏇存柊瑙嗗浘鐨WITH CHECK OPTION瀛愬彞涓紝褰撹鍥炬槸鏍规嵁鍙︿竴涓鍥惧畾涔夌殑鏃讹紝LOCAL鍜CASCADED鍏抽敭瀛鍐冲畾浜嗘鏌ユ祴璇曠殑鑼冨洿銆LOCAL鍏抽敭瀛楀CHECK OPTION杩涜浜嗛檺鍒讹紝浣垮叾浠呬綔鐢ㄥ湪瀹氫箟鐨勮鍥句笂锛CASCADED浼氬灏嗚繘琛岃瘎浼扮殑鍩鸿〃杩涜妫鏌ャ傚鏋滄湭缁欏畾浠讳竴鍏抽敭瀛楋紝榛樿鍊间负CASCADED銆傝鑰冭檻涓嬭堪琛ㄥ拰瑙嗗浘闆嗗悎鐨勫畾涔夛細
mysql> CREATE TABLE t1 (a INT);
mysql> CREATE VIEW v1 AS SELECT * FROM t1 WHERE a < 2
-> WITH CHECK OPTION;
mysql> CREATE VIEW v2 AS SELECT * FROM v1 WHERE a > 0
-> WITH LOCAL CHECK OPTION;
mysql> CREATE VIEW v3 AS SELECT * FROM v1 WHERE a > 0
-> WITH CASCADED CHECK OPTION;
杩欓噷锛岃鍥v2鍜v3鏄牴鎹彟涓瑙嗗浘v1瀹氫箟鐨銆v2鍏锋湁LOCAL妫鏌ラ夐」锛屽洜姝わ紝浠呬細閽堝v2妫鏌ュ鎻掑叆椤硅繘琛屾祴璇曘v3鍏锋湁CASCADED妫鏌ラ夐」锛屽洜姝わ紝涓嶄粎浼氶拡瀵瑰畠鑷繁鐨勬鏌ュ鎻掑叆椤硅繘琛屾祴璇曪紝涔熶細閽堝鍩烘湰瑙嗗浘鐨勬鏌瀵规彃鍏ラ」杩涜娴嬭瘯銆傚湪涓嬮潰鐨勮鍙ヤ腑锛屼粙缁嶄簡杩欎簺宸紓锛
ql> INSERT INTO v2 VALUES (2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO v3 VALUES (2);
ERROR 1369 (HY000): CHECK OPTION failed 'test.v3'
瑙嗗浘鐨勫彲鏇存柊鎬у彲鑳戒細鍙楀埌绯荤粺鍙橀噺updatable_views_with_limit鐨勫肩殑褰卞搷銆璇峰弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥銆
INFORMATION_SCHEMA鍖呭惈1涓VIEWS琛紝浠庤琛ㄥ彲鑾峰彇鍏充簬瑙嗗浘瀵硅薄鐨勪俊鎭銆傝鍙傝23.1.15鑺傦紝鈥淚NFORMATION_SCHEMA VIEWS琛ㄢ銆
DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[RESTRICT | CASCADE]
DROP VIEW鑳藉鍒犻櫎1涓垨澶氫釜瑙嗗浘銆傚繀椤诲湪姣忎釜瑙嗗浘涓婃嫢鏈DROP鏉冮檺銆
鍙互浣跨敤鍏抽敭瀛IF EXISTS鏉ラ槻姝鍥犱笉瀛樺湪鐨勮鍥捐鍑洪敊銆缁欏畾浜嗚瀛愬彞鏃讹紝灏嗕负姣忎釜涓嶅瓨鍦ㄧ殑瑙嗗浘鐢熸垚NOTE銆璇峰弬瑙13.5.4.22鑺傦紝鈥淪HOW WARNINGS璇硶鈥銆
濡傛灉缁欏畾浜RESTRICT鍜CASCADE锛屽皢瑙f瀽骞跺拷鐣ュ畠浠
SHOW CREATE VIEW view_name
璇ヨ鍙ョ粰鍑轰簡1涓垱寤虹粰瀹氳鍥剧殑CREATE VIEW璇彞銆
mysql> SHOW CREATE VIEW v;
+------+----------------------------------------------------+
| View | Create View |
+------+----------------------------------------------------+
| v | CREATE VIEW `test`.`v` AS select 1 AS `a`,2 AS `b` |
+------+----------------------------------------------------+
杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆