{"id":6248,"date":"2024-08-14T19:01:01","date_gmt":"2024-08-14T11:01:01","guid":{"rendered":""},"modified":"2024-08-14T19:01:01","modified_gmt":"2024-08-14T11:01:01","slug":"mysql\u89c6\u56fe\u64cd\u4f5c\u547d\u4ee4\u8be6\u89e3_\u6570\u636e\u5e93\u89c6\u56fe\u521b\u5efa","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6248.html","title":{"rendered":"mysql\u89c6\u56fe\u64cd\u4f5c\u547d\u4ee4\u8be6\u89e3_\u6570\u636e\u5e93\u89c6\u56fe\u521b\u5efa"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e0a\u7bc7\u535a\u6587\u5206\u4eab\u4e86\u89c6\u56fe\u7684\u597d\u5904mysql\u89c6\u56fe\u7684\u4f5c\u7528\uff08\u8be6\u7ec6\uff09
\u4e58\u70ed\u6253\u94c1\uff0c\u770b\u5230\u4e00\u7bc7\u89c6\u56fe\u521b\u5efa\u7684\u535a\u6587\u89c9\u5f97\u4e0d\u9519<\/p>\n

\u521b\u5efa\u89c6\u56fe<\/h4>\n

\u5728MySQL\u4e2d\uff0c\u521b\u5efa\u89c6\u56fe\u7684\u5b8c\u6574\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION] <\/code><\/pre>\n

\u5176\u5bf9\u5e94\u7684\u8bed\u6cd5\u53d8\u91cf\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n

[OR REPLACE]<\/code><\/h6>\n

\u4e2d\u62ec\u53f7\u4e2d\u7684 OR REPLACE\u5173\u952e\u5b57\u662f\u53ef\u9009\u7684\u3002\u5982\u679c\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u5b58\u5728\u6307\u5b9a\u540d\u79f0\u7684\u89c6\u56fe\u65f6\uff0c\u6ca1\u6709\u8be5\u5173\u952e\u5b57\uff0c\u5c06\u4f1a\u63d0\u793a\u9519\u8bef\u4fe1\u606f\uff1b\u5982\u679c\u4f7f\u7528\u4e86 OR REPLACE\u5173\u952e\u5b57\uff0c\u5219\u5f53\u524d\u6b63\u5728\u521b\u5efa\u7684\u89c6\u56fe\u4f1a\u8986\u76d6\u6389\u539f\u6765\u540c\u540d\u7684\u89c6\u56fe\u3002<\/p>\n

[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]<\/code><\/h6>\n

ALGORITHM\u5b50\u53e5\u662f\u53ef\u9009\u7684\uff0c\u5b83\u8868\u793a\u4f7f\u7528\u4f55\u79cd\u7b97\u6cd5\u6765\u5904\u7406\u89c6\u56fe\u3002\u6b64\u5916\uff0c\u5b83\u5e76\u4e0d\u5c5e\u4e8e\u6807\u51c6SQL\u7684\u4e00\u90e8\u5206\uff0c\u800c\u662fMySQL\u5bf9\u6807\u51c6SQL\u8fdb\u884c\u7684\u529f\u80fd\u6269\u5c55\u3002 ALGORITHM\u53ef\u4ee5\u8bbe\u7f6e\u4e09\u4e2a\u503c\uff1a MERGE\u3001 TEMPTABLE\u6216 UNDEFINED\u3002\u5982\u679c\u6ca1\u6709 ALGORITHM\u5b50\u53e5\uff0c\u5219\u9ed8\u8ba4\u503c\u4e3a UNDEFINED(\u672a\u5b9a\u4e49\u7684)\u3002 \u5bf9\u4e8e MERGE\uff0c\u4f1a\u5c06\u5f15\u7528\u89c6\u56fe\u7684\u8bed\u53e5\u7684\u6587\u672c\u4e0e\u89c6\u56fe\u5b9a\u4e49\u5408\u5e76\u8d77\u6765\uff0c\u4f7f\u5f97\u89c6\u56fe\u5b9a\u4e49\u7684\u67d0\u4e00\u90e8\u5206\u53d6\u4ee3\u8bed\u53e5\u7684\u5bf9\u5e94\u90e8\u5206\u3002
\u5bf9\u4e8eTEMPTABLE\uff0c\u89c6\u56fe\u7684\u7ed3\u679c\u5c06\u88ab\u7f6e\u4e8e\u4e34\u65f6\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u6267\u884c\u8bed\u53e5\u3002<\/p>\n

\u5bf9\u4e8eUNDEFINED\uff0cMySQL\u5c06\u9009\u62e9\u6240\u8981\u4f7f\u7528\u7684\u7b97\u6cd5\u3002\u5982\u679c\u53ef\u80fd\uff0c\u5b83\u503e\u5411\u4e8eMERGE\u800c\u4e0d\u662fTEMPTABLE\uff0c\u8fd9\u662f\u56e0\u4e3aMERGE\u901a\u5e38\u66f4\u6709\u6548\uff0c\u800c\u4e14\u5982\u679c\u4f7f\u7528\u4e86\u4e34\u65f6\u8868\uff0c\u89c6\u56fe\u662f\u4e0d\u53ef\u66f4\u65b0\u7684\u3002<\/p>\n

\u4e4b\u6240\u4ee5\u63d0\u4f9bTEMPTABLE\u9009\u9879\uff0c\u662f\u56e0\u4e3aTEMPTABLE\u5728\u521b\u5efa\u4e34\u65f6\u8868\u4e4b\u540e\u3001\u5e76\u5728\u5b8c\u6210\u8bed\u53e5\u5904\u7406\u4e4b\u524d\uff0c\u80fd\u591f\u91ca\u653e\u57fa\u8868\u4e0a\u7684\u9501\u5b9a\u3002\u4e0eMERGE\u7b97\u6cd5\u76f8\u6bd4\uff0c\u9501\u5b9a\u91ca\u653e\u7684\u901f\u5ea6\u66f4\u5feb\uff0c\u8fd9\u6837\uff0c\u4f7f\u7528\u89c6\u56fe\u7684\u5176\u4ed6\u5ba2\u6237\u7aef\u4e0d\u4f1a\u88ab\u5c4f\u853d\u8fc7\u957f\u65f6\u95f4\u3002<\/p>\n

\u6b64\u5916\uff0cMERGE\u7b97\u6cd5\u8981\u6c42\u89c6\u56fe\u4e2d\u7684\u884c\u548c\u57fa\u8868\u4e2d\u7684\u884c\u5177\u6709\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\u3002\u5982\u679c\u89c6\u56fe\u5305\u542b\u805a\u5408\u51fd\u6570(SUM(), MIN(), MAX(), COUNT()\u7b49)\u3001DISTINCT\u3001GROUP BY\u3001HAVING\u3001UNION\u6216UNION ALL\u3001\u6ca1\u6709\u57fa\u8868\u7684\u5f15\u7528\u6587\u5b57\u503c(\u4f8b\u5982\uff1aSELECT \u2018hello\u2019;)\u7b49\u7ed3\u6784\u4e2d\u7684\u4efb\u4f55\u4e00\u79cd\uff0c\u5c06\u5931\u53bb\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u6b64\u65f6\u5fc5\u987b\u4f7f\u7528\u4e34\u65f6\u8868\u53d6\u800c\u4ee3\u4e4b\u3002<\/p>\n

[(column_list)]<\/code><\/h6>\n

(column_list)\u7528\u4e8e\u81ea\u5b9a\u4e49\u89c6\u56fe\u4e2d\u5404\u4e2a\u5b57\u6bb5\u7684\u540d\u79f0\u3002\u5982\u679c\u6ca1\u6709\u8be5\u547d\u4ee4\u9009\u9879\uff0c\u90a3\u4e48\u901a\u8fc7\u89c6\u56fe\u67e5\u8be2\u5230\u7684\u5404\u4e2a\u5b57\u6bb5\u7684\u540d\u79f0\u548c\u89c6\u56fe\u6240\u4f7f\u7528\u5230\u7684\u6570\u636e\u8868\u7684\u5b57\u6bb5\u540d\u79f0\u4fdd\u6301\u4e00\u81f4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u7528\u4e8e\u521b\u5efa\u89c6\u56fe\u7684SQL\u8bed\u53e5\uff1a<\/p>\n

CREATE<\/span> OR<\/span> REPLACE<\/span> VIEW<\/span> v_user AS<\/span> SELECT<\/span> id,<\/span> username FROM<\/span> user<\/span>;<\/span> <\/code><\/pre>\n

\u7531\u4e8euser\u8868\u4e2d\u7684\u5b57\u6bb5\u540d\u79f0\u4e3aid\u548cusername\uff0c\u56e0\u6b64\u89c6\u56fev_user\u4e2d\u7684\u4e24\u4e2a\u5b57\u6bb5\u540d\u79f0\u4e5f\u5206\u522b\u9ed8\u8ba4\u4e3aid\u548cusername\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u89c6\u56fev_user\u4e2d\u7684\u5b57\u6bb5\u540d\u79f0\u5206\u522b\u81ea\u5b9a\u4e49\u4e3auid\u548cuname\u3002<\/p>\n

CREATE<\/span> OR<\/span> REPLACE<\/span> VIEW<\/span> v_user (<\/span>uid,<\/span> uname)<\/span> AS<\/span> SELECT<\/span> id,<\/span> username FROM<\/span> user<\/span>;<\/span> <\/code><\/pre>\n

select_statement<\/code>
select_statement\u7528\u4e8e\u6307\u5b9a\u89c6\u56fe\u7684\u5185\u5bb9\u5b9a\u4e49\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u8fd9\u91cc\u5c31\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4e00\u4e2aSELECT\u8bed\u53e5\u3002<\/p>\n

[WITH [CASCADED | LOCAL] CHECK OPTION]<\/code><\/h6>\n

\u8be5\u9009\u9879\u4e2d\u7684 CASCADED\u4e3a\u9ed8\u8ba4\u503c\uff0c LOCAL CHECK OPTION\u7528\u4e8e\u5728\u53ef\u66f4\u65b0\u89c6\u56fe\u4e2d\u9632\u6b62\u63d2\u5165\u6216\u66f4\u65b0\u884c\u3002\u7531\u4e8e\u6b64\u9009\u9879\u4e00\u822c\u4e0d\u4f7f\u7528\uff0c\u56e0\u6b64\u4e0d\u518d\u8d58\u8ff0\uff0c\u5177\u4f53\u4fe1\u606f\u8bf7\u53c2\u8003MySQL\u5b98\u65b9\u7f51\u7ad9\u4e0a\u7684\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n

\u5220\u9664\u89c6\u56fe<\/h4>\n

\u5728MySQL\u4e2d\u5220\u9664\u89c6\u56fe\u7684\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u5176\u8be6\u7ec6\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

--\u5220\u9664\u6307\u5b9a\u540d\u79f0\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u89c6\u56fe<\/span> DROP<\/span> VIEW<\/span> [<\/span>IF<\/span> EXISTS<\/span>]<\/span> view_name [<\/span>,<\/span> view_name2]<\/span>.<\/span>.<\/span>.<\/span> <\/code><\/pre>\n

\u5176\u4e2d\uff0c\u5173\u952e\u5b57IF EXISTS\u7528\u4e8e\u9632\u6b62\u56e0\u89c6\u56fe\u4e0d\u5b58\u5728\u800c\u63d0\u793a\u51fa\u9519\uff0c\u6b64\u65f6\uff0c\u53ea\u6709\u5b58\u5728\u8be5\u89c6\u56fe\u624d\u4f1a\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002DROP VIEW\u8bed\u53e5\u53ef\u4ee5\u4e00\u6b21\u6027\u5220\u9664\u591a\u4e2a\u89c6\u56fe\uff0c\u53ea\u9700\u8981\u5728\u591a\u4e2a\u89c6\u56fe\u540d\u79f0\u4e4b\u95f4\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u5373\u53ef\u3002\u5982\u679c\u591a\u4e2a\u89c6\u56fe\u5b58\u5728\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u4e0d\u6570\u636e\u5f53\u524d\u6570\u636e\u5e93\u7684\u89c6\u56fe\u540d\u79f0\u4e4b\u524d\u8fd8\u5fc5\u987b\u52a0\u4e0adb_name.\u524d\u7f00\u3002<\/p>\n

--\u5220\u9664\u89c6\u56fev_user<\/span> DROP<\/span> VIEW<\/span> v_user;<\/span> <\/code><\/pre>\n

\u4fee\u6539\u89c6\u56fe<\/h4>\n

\u8bf7\u53c2\u8003\u521b\u5efa\u89c6\u56fe\u8bed\u6cd5\u4e2d\u7684OR REPLACE\u5173\u952e\u5b57\uff0c\u53ea\u8981\u5177\u5907\u8be5\u5173\u952e\u5b57\u7684\u89c6\u56fe\u521b\u5efa\u8bed\u53e5\u5c31\u662f\u4fee\u6539\u89c6\u56fe\u7684SQL\u8bed\u53e5\u3002<\/p>\n

\u67e5\u770b\u89c6\u56fe<\/h4>\n

\u5728MySQL\u4e2d\uff0cshow tables\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u54ea\u4e9b\u6570\u636e\u8868\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u67e5\u770b\u5230\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u54ea\u4e9b\u89c6\u56fe\u3002<\/p>\n

--\u6267\u884cshow tables<\/span> mysql><\/span> show<\/span> tables<\/span>;<\/span> --\u4ee5\u4e0b\u662f\u8f93\u51fa\u7ed3\u679c<\/span> +<\/span>----------------+<\/span> |<\/span> Tables_in_test |<\/span> +<\/span>----------------+<\/span> |<\/span> user<\/span> |<\/span> |<\/span> v_user |<\/span> +<\/span>----------------+<\/span> 2<\/span> rows<\/span> in<\/span> set<\/span> (<\/span>0.00<\/span> sec)<\/span> <\/code><\/pre>\n

\u4e0d\u8fc7\uff0c\u4ec5\u4ec5\u4f7f\u7528show tables\u8bed\u53e5\uff0c\u5728\u8f93\u51fa\u7ed3\u679c\u4e2d\uff0c\u6211\u4eec\u6839\u672c\u65e0\u6cd5\u533a\u5206\u5230\u5e95\u54ea\u4e9b\u624d\u662f\u89c6\u56fe\u54ea\u4e9b\u624d\u662f\u771f\u5b9e\u7684\u6570\u636e\u8868(\u5f53\u7136\uff0c\u89c6\u56fe\u7684\u547d\u540d\u6211\u4eec\u53ef\u4ee5\u7edf\u4e00\u7ea6\u5b9a\u4ee5\"v_\u201c\u5f00\u5934)\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u547d\u4ee4show full tables\uff0c\u8be5\u547d\u4ee4\u53ef\u4ee5\u5217\u51fa\u989d\u5916\u7684table_type\u5217\uff0c\u5982\u679c\u5bf9\u5e94\u8f93\u51fa\u884c\u4e0a\u8be5\u5217\u7684\u503c\u4e3a\"VIEW\u201d\uff0c\u5219\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u89c6\u56fe\u3002<\/p>\n

--\u6267\u884cshow full tables<\/span> mysql><\/span> show<\/span> full<\/span> tables<\/span>;<\/span> --\u4ee5\u4e0b\u662f\u8f93\u51fa\u7ed3\u679c<\/span> +<\/span>----------------+------------+<\/span> |<\/span> Tables_in_test |<\/span> Table_type |<\/span> +<\/span>----------------+------------+<\/span> |<\/span> user<\/span> |<\/span> BASE TABLE<\/span> |<\/span> |<\/span> v_user |<\/span> VIEW<\/span> |<\/span> +<\/span>----------------+------------+<\/span> 2<\/span> rows<\/span> in<\/span> set<\/span> (<\/span>0.00<\/span> sec)<\/span> <\/code><\/pre>\n

\u5f53\u6211\u4eec\u901a\u8fc7\u4e0a\u8ff0\u547d\u4ee4\u627e\u5230\u4e86\u6211\u4eec\u6240\u9700\u8981\u7684\u89c6\u56fe\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u67e5\u770b\u521b\u5efa\u8be5\u89c6\u56fe\u7684\u8be6\u7ec6\u8bed\u53e5\uff1a<\/p>\n

show<\/span> create<\/span> view<\/span> view_name <\/code><\/pre>\n

\u4f8b\u5982\uff0c\u6211\u4eec\u4f7f\u7528\u8be5\u547d\u4ee4\u67e5\u770b\u521b\u5efa\u89c6\u56fev_user\u7684SQL\u8bed\u53e5\uff1a<\/p>\n

--\u7531\u4e8e\u8be5\u8bed\u53e5\u7684\u8f93\u51fa\u7ed3\u679c\u8f83\u4e3a\u6742\u4e71\uff0c\u56e0\u6b64\u4f7f\u7528\\G\u547d\u4ee4\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa<\/span> mysql><\/span> show<\/span> create<\/span> view<\/span> v_user \\G;<\/span> --\u4ee5\u4e0b\u662f\u683c\u5f0f\u5316\u7684\u8f93\u51fa\u7ed3\u679c<\/span> *<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span> 1.<\/span> row<\/span> *<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span>*<\/span> View<\/span>: v_user Create<\/span> View<\/span>: CREATE<\/span> ALGORITHM<\/span>=<\/span>UNDEFINED<\/span> DEFINER<\/span>=<\/span>`<\/span>root`<\/span>@`localhost`<\/span> SQL<\/span> SECURITY DEFINER<\/span> VIEW<\/span> `<\/span>v_user`<\/span> AS<\/span> select<\/span> `<\/span>user<\/span>`<\/span>.<\/span>`<\/span>id`<\/span> AS<\/span> `<\/span>uid`<\/span>,<\/span>`<\/span>user<\/span>`<\/span>.<\/span>`<\/span>username`<\/span> AS<\/span> `<\/span>uname`<\/span> from<\/span> `<\/span>user<\/span>`<\/span> character_set_client: utf8 collation_connection: utf8_general_ci 1<\/span> row<\/span> in<\/span> set<\/span> (<\/span>0.00<\/span> sec)<\/span> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"mysql\u89c6\u56fe\u64cd\u4f5c\u547d\u4ee4\u8be6\u89e3_\u6570\u636e\u5e93\u89c6\u56fe\u521b\u5efa\u4e0a\u7bc7\u535a\u6587\u5206\u4eab\u4e86\u89c6\u56fe\u7684\u597d\u5904mysql\u89c6\u56fe\u7684\u4f5c\u7528\uff08\u8be6\u7ec6\uff09\u4e58\u70ed\u6253\u94c1\uff0c\u770b\u5230\u4e00\u7bc7\u89c6\u56fe\u521b\u5efa\u7684\u535a\u6587\u89c9...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6248"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6248"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6248\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}