{"id":2015,"date":"2020-07-29T23:09:41","date_gmt":"2020-07-29T15:09:41","guid":{"rendered":"http:\/\/39.97.231.10\/?p=2015"},"modified":"2020-07-29T23:09:41","modified_gmt":"2020-07-29T15:09:41","slug":"mysql%e8%a7%86%e5%9b%beview%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=2015","title":{"rendered":"MySQL\u89c6\u56feview\u7684\u4f7f\u7528"},"content":{"rendered":"<div class=\\\"blog_content\\\">\n<div class=\\\"blog_detail\\\">\n<div>\nMySQL\u652f\u6301\u89c6\u56fe\u529f\u80fd\uff0c\u5305\u62ec\u53ef\u66f4\u65b0\u7684\u89c6\u56fe\u3002\u89c6\u56fe\u53ef\u7406\u89e3\u4e3a\u4e00\u4e2a\u865a\u62df\u8868\uff0c\u4f5c\u7528\u4e0e\u771f\u5b9e\u8868\u4e00\u6837\uff0c\u5728\u903b\u8f91\u4e0a\u53ef\u4ee5\u628a\u89c6\u56fe\u5f53\u4f5c\u4e00\u4e2a\u771f\u5b9e\u8868\u4f7f\u7528\uff0c\u89c6\u56fe\u5185\u90e8\u5305\u542b\u4e86\u771f\u6b63\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u8fd9\u4e2a\u67e5\u8be2\u8bed\u53e5\u53ef\u4ee5\u975e\u5e38\u590d\u6742\uff0c\u4f46\u662f\u7528\u6237\u5e76\u4e0d\u5173\u5fc3\u8fd9\u4e2a\u67e5\u8be2\u8bed\u53e5\uff0c\u7528\u6237\u53ea\u9700\u8981\u67e5\u8be2\u89c6\u56fe\uff0c\u5c31\u80fd\u8fd4\u56de\u60f3\u8981\u7684\u6570\u636e\u3002<\/p>\n<h4>1. \u89c6\u56fe\u4f18\u7f3a\u70b9<\/h4>\n<h5>1.1. \u4f18\u70b9<\/h5>\n<ul>\n<li>\u7b80\u5316\u7528\u6237\u7684\u4f7f\u7528\uff0c\u590d\u6742\u6027\u9690\u85cf\u5728\u5185\u90e8<\/li>\n<li>\u5b89\u5168\u6027\uff0c\u7528\u6237\u53ea\u80fd\u67e5\u8be2\u6216\u4fee\u6539\u4ed6\u4eec\u6240\u80fd\u89c1\u5230\u7684\u6570\u636e<\/li>\n<li>\u903b\u8f91\u72ec\u7acb\uff0c\u80fd\u591f\u5c4f\u853d\u8868\u7ed3\u6784\u53d8\u5316\u5e26\u6765\u7684\u5f71\u54cd<\/li>\n<\/ul>\n<h5>1.2 \u7f3a\u70b9<\/h5>\n<ul>\n<li>\u6027\u80fd\u8f83\u5dee\uff0c\u7b80\u5355\u7684\u67e5\u8be2\u4e5f\u4f1a\u53d8\u5f97\u6bd4\u8f83\u590d\u6742<\/li>\n<li>\u4fee\u6539\u4e0d\u65b9\u4fbf\uff0c\u590d\u6742\u7684\u89c6\u56fe\u5f88\u96be\u7ef4\u62a4<\/li>\n<\/ul>\n<h4>2. \u521b\u5efa\u89c6\u56fe<\/h4>\n<p>\u521b\u5efa\u89c6\u56fe\u8bed\u6cd5\uff1a<\/p>\n<pre><code>CREATE\n    [OR REPLACE]\n    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\n    [DEFINER = user]\n    [SQL SECURITY { DEFINER | INVOKER }]\n    VIEW view_name [(column_list)]\n    AS select_statement\n    [WITH [CASCADED | LOCAL] CHECK OPTION]\n<\/code><\/pre>\n<p>create view\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u89c6\u56fe\uff0c\u6216\u8005\u4f7f\u7528replace view \u66ff\u6362(\u4fee\u6539)\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u89c6\u56fe\u3002<\/p>\n<ul>\n<li>view_name\uff0c\u89c6\u56fe\u540d\u79f0\u3002<\/li>\n<li>select_statement\uff0c\u4e00\u4e2aselect\u8bed\u53e5\uff0c\u7528\u6765\u63d0\u4f9b\u89c6\u56fe\u7684\u5b9a\u4e49\uff0c\u89c6\u56fe\u4e2d\u7684\u6570\u636e\u7531\u8fd9\u4e2aselect\u8bed\u53e5\u63d0\u4f9b\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<br \/>\ncreate view v_tb as select * from tb;<\/p>\n<ul>\n<li>\u89c6\u56fe\u4f4d\u4e8eschema\u547d\u540d\u7a7a\u95f4\uff0c\u5728\u4e00\u4e2a\u6570\u636e\u5e93\u91cc\uff0c\u89c6\u56fe\u7684\u540d\u79f0\u5fc5\u987b\u552f\u4e00\u3002<\/li>\n<li>\u521b\u5efa\u89c6\u56fe\u9700\u8981\u6709CREATE VIEW\u6743\u9650\u4ee5\u53caSELECT\u8bed\u53e5\u4e2d\u5bf9\u6d89\u53ca\u5230\u7684\u8868\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u4f7f\u7528REPLACE VIEW\uff0c\u9700\u8981\u6709DROP\u6743\u9650\u3002<\/li>\n<li>\u89c6\u56fe\u4e00\u65e6\u521b\u5efa\u597d\uff0c\u90a3\u4e48\u5b83\u7684\u72b6\u6001\u5c31\u662f\u56fa\u5b9a\u7684\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u540e\u6765\u8868\u7684\u5b9a\u4e49\u4fee\u6539\u800c\u6539\u53d8\u3002\u6bd4\u5982\u89c6\u56fe\u7684select_statement\u4e3aselect * from t; \u5982\u679c\u8868t\u589e\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u6bb5\uff0c\u89c6\u56fe\u4e2d\u4e0d\u4f1a\u65b0\u589e\u8be5\u5b57\u6bb5\u3002\u540c\u6837\u5982\u679c\u8868t\u5220\u9664\u4e86\u4e00\u4e2a\u5b57\u6bb5\uff0c\u67e5\u8be2\u89c6\u56fe\u5219\u4f1a\u62a5\u9519\u3002<\/li>\n<\/ul>\n<h4>3. \u67e5\u770b\u89c6\u56fe<\/h4>\n<h5>3.1 \u67e5\u770b\u6570\u636e\u5e93\u4e2d\u6709\u54ea\u4e9b\u89c6\u56fe<\/h5>\n<p>select * from information_schema.views where table_schema=\\&#8217;db_name\\&#8217;\\\\G<\/p>\n<h5>3.2 \u67e5\u770b\u89c6\u56fe\u5b9a\u4e49<\/h5>\n<p>show create view view_name\\\\G<br \/>\n\u6216\u8005<br \/>\nshow create table view_name\\\\G<\/p>\n<h5>3.3 \u5220\u9664\u89c6\u56fe<\/h5>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre><code>DROP VIEW [IF EXISTS]\n    view_name [, view_name] ...\n    [RESTRICT | CASCADE]\n<\/code><\/pre>\n<p>\u6bd4\u5982\uff1a<br \/>\ndrop view view_tb;<\/p>\n<h4>4. \u89c6\u56fe\u4f7f\u7528\u6848\u4f8b<\/h4>\n<p>\u8868t\u4e3a\u5546\u54c1\u8868\uff0c\u8bb0\u5f55\u6570\u91cf\u548c\u5355\u4ef7\uff0c\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\uff0c\u5728\u83b7\u53d6\u6570\u91cf\u548c\u5355\u4ef7\u7684\u57fa\u7840\u4e0a\uff0c\u8ba1\u7b97\u603b\u4ef7\u3002\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; CREATE TABLE t (qty INT, price INT);\nmysql&gt; INSERT INTO t VALUES(3, 50);\nmysql&gt; CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;\nmysql&gt; SELECT * FROM v;\n+------+-------+-------+\n| qty  | price | value |\n+------+-------+-------+\n|    3 |    50 |   150 |\n+------+-------+-------+\n<\/code><\/pre>\n<h4>5. \u89c6\u56fe\u4f7f\u7528\u7684\u4e00\u4e9b\u9650\u5236<\/h4>\n<ul>\n<li>\u89c6\u56feselect\u8bed\u53e5\u4e0d\u80fd\u5f15\u7528\u7cfb\u7edf\u53d8\u91cf\u6216\u8005\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u5728\u5b58\u50a8\u8fc7\u7a0b\u5185\u90e8\uff0c\u89c6\u56feselect\u8bed\u53e5\u4e0d\u80fd\u5f15\u7528\u5b58\u50a8\u8fc7\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u548c\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li>\u89c6\u56feselect\u8bed\u53e5\u4e0d\u80fd\u5f15\u7528\u9884\u7f16\u8bd1\u53c2\u6570\u3002<\/li>\n<li>\u89c6\u56fe\u4e2d\u5f15\u7528\u7684\u8868\u6216\u8005\u89c6\u56fe\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u67e5\u8be2\u89c6\u56fe\u65f6\u5c06\u62a5\u9519\u3002<\/li>\n<li>\u89c6\u56fe\u4e0d\u80fd\u5f15\u7528\u4e34\u65f6\u8868\uff0c\u56e0\u6b64\u4e5f\u4e0d\u80fd\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u89c6\u56fe\u3002<\/li>\n<li>\u4e0d\u80fd\u4e3a\u89c6\u56fe\u5173\u8054\u89e6\u53d1\u5668\u3002<\/li>\n<li>\u89c6\u56feselect\u8bed\u53e5\u4e2d\u5b57\u6bb5\u522b\u540d\u6700\u592764\u4e2a\u5b57\u7b26\uff0c\u800c\u4e0d\u662f256\u3002<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u652f\u6301\u89c6\u56fe\u529f\u80fd\uff0c\u5305\u62ec\u53ef\u66f4\u65b0\u7684\u89c6\u56fe\u3002\u89c6\u56fe\u53ef\u7406\u89e3\u4e3a\u4e00\u4e2a\u865a\u62df\u8868\uff0c\u4f5c\u7528\u4e0e\u771f\u5b9e\u8868\u4e00\u6837\uff0c\u5728\u903b\u8f91\u4e0a\u53ef\u4ee5\u628a\u89c6\u56fe\u5f53\u4f5c\u4e00\u4e2a &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=2015\"> <span class=\"screen-reader-text\">MySQL\u89c6\u56feview\u7684\u4f7f\u7528<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"MySQL\u652f\u6301\u89c6\u56fe\u529f\u80fd\uff0c\u5305\u62ec\u53ef\u66f4\u65b0\u7684\u89c6\u56fe\u3002\u89c6\u56fe\u53ef\u7406\u89e3\u4e3a\u4e00\u4e2a\u865a\u62df\u8868\uff0c\u4f5c\u7528\u4e0e\u771f\u5b9e\u8868\u4e00\u6837\uff0c\u5728\u903b\u8f91\u4e0a\u53ef\u4ee5\u628a\u89c6\u56fe\u5f53\u4f5c\u4e00\u4e2a&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2015"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2015"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2015\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}