{"id":2076,"date":"2023-04-01T21:46:17","date_gmt":"2023-04-01T13:46:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2076"},"modified":"2023-04-06T16:00:36","modified_gmt":"2023-04-06T08:00:36","slug":"mysql-view-details","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/mysql-view-details\/","title":{"rendered":"MySQL\u89c6\u56fe\u8be6\u89e3"},"content":{"rendered":"<h2>\u4ec0\u4e48\u662f\u89c6\u56fe\uff1f<\/h2>\n<p>\u89c6\u56fe\uff08view\uff09\u662f\u4e00\u79cd\u865a\u62df\u5b58\u5728\u7684\u8868\uff0c\u662f\u4e00\u4e2a\u903b\u8f91\u8868\uff0c\u672c\u8eab\u5e76\u4e0d\u5305\u542b\u6570\u636e\u3002\u4f5c\u4e3a\u4e00\u4e2aselect\u8bed\u53e5\u4fdd\u5b58\u5728\u6570\u636e\u5b57\u5178\u4e2d\u7684\u3002<\/p>\n<h2>\u89c6\u56fe\u662f\u5e72\u4ec0\u4e48\u7528\u7684\uff1f<\/h2>\n<p><!-- more --><\/p>\n<ul>\n<li>\u901a\u8fc7\u89c6\u56fe\uff0c\u53ef\u4ee5\u5c55\u73b0\u57fa\u8868\u7684\u90e8\u5206\u6570\u636e<\/li>\n<li>\u89c6\u56fe\u6570\u636e\u6765\u81ea\u5b9a\u4e49\u89c6\u56fe\u7684\u67e5\u8be2\u4e2d\u4f7f\u7528\u7684\u8868\uff0c\u4f7f\u7528\u89c6\u56fe\u52a8\u6001\u751f\u6210<\/li>\n<li>\u57fa\u8868\uff1a\u7528\u6765\u521b\u5efa\u89c6\u56fe\u7684\u8868\u53eb\u505a\u57fa\u8868<\/li>\n<\/ul>\n<h2>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u89c6\u56fe\uff1f<\/h2>\n<p>\u56e0\u4e3a\u89c6\u56fe\u7684\u8bf8\u591a\u4f18\u70b9\uff0c\u5982\u4e0b<\/p>\n<p>1\uff09\u7b80\u5355\uff1a\u4f7f\u7528\u89c6\u56fe\u7684\u7528\u6237\u5b8c\u5168\u4e0d\u9700\u8981\u5173\u5fc3\u540e\u9762\u5bf9\u5e94\u7684\u8868\u7684\u7ed3\u6784\u3001\u5173\u8054\u6761\u4ef6\u548c\u7b5b\u9009\u6761\u4ef6\uff0c\u5bf9\u7528\u6237\u6765\u8bf4\u5df2\u7ecf\u662f\u8fc7\u6ee4\u597d\u7684\u590d\u5408\u6761\u4ef6\u7684\u7ed3\u679c\u96c6\u3002<br \/>\n2\uff09\u5b89\u5168\uff1a\u4f7f\u7528\u89c6\u56fe\u7684\u7528\u6237\u53ea\u80fd\u8bbf\u95ee\u4ed6\u4eec\u88ab\u5141\u8bb8\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u5bf9\u8868\u7684\u6743\u9650\u7ba1\u7406\u5e76\u4e0d\u80fd\u9650\u5236\u5230\u67d0\u4e2a\u884c\u67d0\u4e2a\u5217\uff0c\u4f46\u662f\u901a\u8fc7\u89c6\u56fe\u5c31\u53ef\u4ee5\u7b80\u5355\u7684\u5b9e\u73b0\u3002<br \/>\n3\uff09\u6570\u636e\u72ec\u7acb\uff1a\u4e00\u65e6\u89c6\u56fe\u7684\u7ed3\u6784\u786e\u5b9a\u4e86\uff0c\u53ef\u4ee5\u5c4f\u853d\u8868\u7ed3\u6784\u53d8\u5316\u5bf9\u7528\u6237\u7684\u5f71\u54cd\uff0c\u6e90\u8868\u589e\u52a0\u5217\u5bf9\u89c6\u56fe\u6ca1\u6709\u5f71\u54cd\uff1b\u6e90\u8868\u4fee\u6539\u5217\u540d\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u89c6\u56fe\u6765\u89e3\u51b3\uff0c\u4e0d\u4f1a\u9020\u6210\u5bf9\u8bbf\u95ee\u8005\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u4f7f\u7528\u89c6\u56fe\u7684\u5927\u90e8\u5206\u60c5\u51b5\u662f\u4e3a\u4e86\u4fdd\u969c\u6570\u636e\u5b89\u5168\u6027\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>\n<h2>MySQL\u4e2d\u7684\u89c6\u56fe\u64cd\u4f5c<\/h2>\n<p>\u56e0\u4e3a\u89c6\u56fe\u662f\u9700\u8981\u57fa\u8868\u624d\u80fd\u6784\u5efa\uff0c\u56e0\u6b64\u5728\u8bb2\u89e3\u89c6\u56fe\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e24\u5f20\u6570\u636e\u8868\u7528\u4e8e\u540e\u9762\u6f14\u793a\u89c6\u56fe\u64cd\u4f5c\uff0c\u4e0b\u9762\u662f\u6d4b\u8bd5\u8868\u548c\u6d4b\u8bd5\u6570\u636e\u521b\u5efa\u7684SQL\u8bed\u53e5\u3002<\/p>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS `author`;\nCREATE TABLE `author` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `author_name` varchar(255) DEFAULT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE =InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;\nINSERT INTO `author` VALUES (&#039;1&#039;, &#039;Naaman&#039;);\nINSERT INTO `author` VALUES (&#039;2&#039;, &#039;Lucy&#039;);\nINSERT INTO `author` VALUES (&#039;3&#039;, &#039;Lily&#039;);\nINSERT INTO `author` VALUES (&#039;4&#039;, &#039;Jack&#039;);\nDROP TABLE IF EXISTS `blog`;\nCREATE TABLE `blog` (\n  `id` int(11) NOT NULL AUTO_INCREMENT,\n  `title` varchar(50) DEFAULT NULL,\n  `content` varchar(255) DEFAULT NULL,\n  `author_id` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `fk-author` (`author_id`),\n  CONSTRAINT `fk-author` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;\nINSERT INTO `blog` VALUES (&#039;1&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba21&#039;, &#039;\u535a\u5ba2\u5185\u5bb9111&#039;, &#039;1&#039;);\nINSERT INTO `blog` VALUES (&#039;2&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba22&#039;, &#039;\u6d4b\u8bd5\u5185\u5bb9222&#039;, &#039;2&#039;);\nINSERT INTO `blog` VALUES (&#039;3&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba23&#039;, &#039;\u6d4b\u8bd5\u5185\u5bb9333&#039;, &#039;4&#039;);<\/code><\/pre>\n<h3>\u521b\u5efa\u89c6\u56fe<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u6765\u770b\u770b\u521b\u5efa\u89c6\u56fe\u7684SQL\u8bed\u6cd5<\/p>\n<pre><code class=\"language-sql\">CREATE [OR REPLACE] [ALGORITHM = { UNDEFINED | MERGE | TEMPTABLE }]\n  VIEW view_name [(column_list)]\n  AS select_statement \n  [WITH [CASCADED | LOCAL] CHECK OPTION]<\/code><\/pre>\n<ul>\n<li><code>OR REPLACE<\/code>\uff1a\u8868\u793a\u5728\u521b\u5efa\u89c6\u56fe\u65f6\u5019\u4f1a\u66ff\u6362\u5df2\u6709\u89c6\u56fe<\/li>\n<li><code>ALGORITHM<\/code>\uff1a\u8868\u793a\u89c6\u56fe\u9009\u62e9\u7b97\u6cd5\uff0c\u5c06\u5728\u6587\u7ae0\u7684\u540e\u9762\u8be6\u7ec6\u8bb2\u89e3<\/li>\n<li><code>select_statement<\/code>\uff1a\u8868\u793aselect\u8bed\u53e5<\/li>\n<li><code>[WITH [CASCADED | LOCAL] CHECK OPTION]<\/code>\uff1a\u8868\u793a\u89c6\u56fe\u5728\u66f4\u65b0\u65f6\u4fdd\u8bc1\u5728\u89c6\u56fe\u7684\u6743\u9650\u8303\u56f4\u4e4b\u5185\uff0c\u8be6\u60c5\u5c06\u5728\u540e\u9762\u8bb2\u89e3<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a\u63a8\u8350\u4f7f\u7528<code>WHIT [CASCADED | LOCAL] CHECK OPTION<\/code>\u9009\u9879\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u6240\u4ee5\u5efa\u8bae\u52a0\u4e0a\u5b83\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u63a8\u8350\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-sql\">create view &lt;\u89c6\u56fe\u540d\u79f0&gt;[(column_list)]\n  as select\u8bed\u53e5\nwith check option;<\/code><\/pre>\n<h4>\u521b\u5efa\u5355\u8868\u89c6\u56fe<\/h4>\n<p>\u6267\u884c\u4e0b\u9762\u7684SQL\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u5355\u8868\u89c6\u56fe<\/p>\n<pre><code class=\"language-sql\">create view v_author(\u7f16\u53f7,\u59d3\u540d)\nas \nselect * from author \nwith check option;<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; create view v_author(\u7f16\u53f7,\u59d3\u540d)\n    -&gt; as \n    -&gt; select * from author \n    -&gt; with check option;\nQuery OK, 0 rows affected (0.01 sec)<\/code><\/pre>\n<p>\u4f7f\u7528<code>desc v_author<\/code>\u547d\u4ee4\u67e5\u770b\u89c6\u56fe\u4fe1\u606f\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; desc v_author;\n+--------+--------------+------+-----+---------+-------+\n| Field  | Type         | Null | Key | Default | Extra |\n+--------+--------------+------+-----+---------+-------+\n| \u7f16\u53f7   | int(11)      | NO   |     | 0       |       |\n| \u59d3\u540d   | varchar(255) | YES  |     | NULL    |       |\n+--------+--------------+------+-----+---------+-------+\n2 rows in set (0.00 sec)<\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c<code>select * from v_author<\/code>\u67e5\u770b\u89c6\u56fe\u91cc\u9762\u663e\u793a\u7684\u6570\u636e\uff0c\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; select * from v_author;\n+--------+--------+\n| \u7f16\u53f7   | \u59d3\u540d   |\n+--------+--------+\n|      1 | Naaman |\n|      2 | Lucy   |\n|      3 | Lily   |\n|      4 | Jack   |\n+--------+--------+\n4 rows in set (0.00 sec)<\/code><\/pre>\n<h4>\u521b\u5efa\u591a\u8868\u89c6\u56fe<\/h4>\n<p>\u6267\u884c\u4e0b\u9762\u7684SQL\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u591a\u8868\u89c6\u56fe<\/p>\n<pre><code class=\"language-sql\">create view v_blog (\u7f16\u53f7,\u6807\u9898,\u5185\u5bb9,\u4f5c\u8005)\nas \nselect b.id,b.title,b.content,a.author_name from author a,blog b \nwhere a.id=b.author_id \nwith check option;<\/code><\/pre>\n<pre><code class=\"language-sql\">mysql&gt; create view v_blog (\u7f16\u53f7,\u6807\u9898,\u5185\u5bb9,\u4f5c\u8005)\n    -&gt; as \n    -&gt; select b.id,b.title,b.content,a.author_name from author a,blog b \n    -&gt; where a.id=b.author_id \n    -&gt; with check option;\nQuery OK, 0 rows affected (0.01 sec)<\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c<code>select * from v_blog<\/code>\u67e5\u770b\u591a\u8868\u89c6\u56fe\u4e2d\u7684\u6570\u636e\uff0c\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; select * from v_blog;\n+--------+---------------+-----------------+--------+\n| \u7f16\u53f7   | \u6807\u9898          | \u5185\u5bb9            | \u4f5c\u8005   |\n+--------+---------------+-----------------+--------+\n|      1 | \u6d4b\u8bd5\u535a\u5ba21     | \u535a\u5ba2\u5185\u5bb9111     | Naaman |\n|      2 | \u6d4b\u8bd5\u535a\u5ba22     | \u6d4b\u8bd5\u5185\u5bb9222     | Lucy   |\n|      3 | \u6d4b\u8bd5\u535a\u5ba23     | \u6d4b\u8bd5\u5185\u5bb9333     | Jack   |\n+--------+---------------+-----------------+--------+\n3 rows in set (0.00 sec)<\/code><\/pre>\n<ul>\n<li>\u89c6\u56fe\u5c06\u6211\u4eec\u4e0d\u9700\u8981\u7684\u6570\u636e\u8fc7\u6ee4\u6389\uff0c\u5c06\u76f8\u5173\u7684\u5217\u540d\u7528\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u5217\u540d\u66ff\u6362\u3002\u89c6\u56fe\u4f5c\u4e3a\u4e00\u4e2a\u8bbf\u95ee\u63a5\u53e3\uff0c\u4e0d\u7ba1\u57fa\u8868\u7684\u8868\u7ed3\u6784\u548c\u8868\u540d\u6709\u591a\u590d\u6742<\/li>\n<li>\u5982\u679c\u521b\u5efa\u89c6\u56fe\u65f6\u4e0d\u660e\u786e\u6307\u5b9a\u89c6\u56fe\u7684\u5217\u540d\uff0c\u90a3\u4e48\u5217\u540d\u5c31\u548c\u5b9a\u4e49\u89c6\u56fe\u7684select\u5b50\u53e5\u4e2d\u7684\u5217\u540d\u5b8c\u5168\u76f8\u540c<\/li>\n<li>\u5982\u679c\u663e\u5f0f\u7684\u6307\u5b9a\u89c6\u56fe\u7684\u5217\u540d\u5c31\u6309\u7167\u6307\u5b9a\u7684\u5217\u540d<\/li>\n<\/ul>\n<p>\u6ce8\u610f\uff1a\u663e\u793a\u6307\u5b9a\u89c6\u56fe\u5217\u540d\uff0c\u8981\u6c42\u89c6\u56fe\u540d\u540e\u9762\u7684\u5217\u7684\u6570\u91cf\u5fc5\u987b\u5339\u914dselect\u5b50\u53e5\u4e2d\u7684\u5217\u7684\u6570\u91cf<\/p>\n<h3>\u67e5\u770b\u89c6\u56fe<\/h3>\n<p>\u4f7f\u7528<code>show create view<\/code>\u8bed\u53e5\u67e5\u770b\u89c6\u56fe\u4fe1\u606f\uff0c\u6bd4\u5982<\/p>\n<pre><code class=\"language-sql\">mysql&gt; show create view v_author \\G;\n*************************** 1. row ***************************\n                View: v_author\n         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_author` AS select `author`.`id` AS `\u7f16\u53f7`,`author`.`author_name` AS `\u59d3\u540d` from `author` WITH CASCADED CHECK OPTION\ncharacter_set_client: utf8\ncollation_connection: utf8_general_ci\n1 row in set (0.00 sec)<\/code><\/pre>\n<p>\u89c6\u56fe\u4e00\u65e6\u521b\u5efa\u5b8c\u6bd5\uff0c\u5c31\u53ef\u4ee5\u50cf\u4e00\u4e2a\u666e\u901a\u8868\u90a3\u6837\u4f7f\u7528\uff0c\u89c6\u56fe\u4e3b\u8981\u7528\u6765\u67e5\u8be2\uff0c\u6bd4\u5982<code>select * from v_blog where \u7f16\u53f7=1;<\/code>\uff0c\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; select * from v_blog where \u7f16\u53f7=1;\n+--------+---------------+-----------------+--------+\n| \u7f16\u53f7   | \u6807\u9898          | \u5185\u5bb9            | \u4f5c\u8005   |\n+--------+---------------+-----------------+--------+\n|      1 | \u6d4b\u8bd5\u535a\u5ba21     | \u535a\u5ba2\u5185\u5bb9111     | Naaman |\n+--------+---------------+-----------------+--------+\n1 row in set (0.00 sec)<\/code><\/pre>\n<p>\u6709\u5173\u89c6\u56fe\u7684\u4fe1\u606f\u8bb0\u5f55\u5728<code>information_schema<\/code>\u6570\u636e\u5e93\u4e2d\u7684views\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7SQL\u8bed\u53e5\u6765\u67e5\u770b\uff0c\u6bd4\u5982<code>select * from information_schema.views where TABLE_NAME=&#039;v_blog&#039;\\G;<\/code>\uff0c\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; select * from information_schema.views where TABLE_NAME=&#039;v_blog&#039; \\G;\n*************************** 1. row ***************************\n       TABLE_CATALOG: def\n        TABLE_SCHEMA: test\n          TABLE_NAME: v_blog\n     VIEW_DEFINITION: select `b`.`id` AS `\u7f16\u53f7`,`b`.`title` AS `\u6807\u9898`,`b`.`content` AS `\u5185\u5bb9`,`a`.`author_name` AS `\u4f5c\u8005` from `test`.`author` `a` join `test`.`blog` `b` where (`a`.`id` = `b`.`author_id`)\n        CHECK_OPTION: CASCADED\n        IS_UPDATABLE: YES\n             DEFINER: root@localhost\n       SECURITY_TYPE: DEFINER\nCHARACTER_SET_CLIENT: utf8\nCOLLATION_CONNECTION: utf8_general_ci\n1 row in set (0.00 sec)<\/code><\/pre>\n<h3>\u89c6\u56fe\u7684\u66f4\u6539<\/h3>\n<h4>CREATE OR REPLACE VIEW\u8bed\u53e5\u4fee\u6539\u89c6\u56fe<\/h4>\n<pre><code class=\"language-sql\">create or replace view view_name as select\u8bed\u53e5;<\/code><\/pre>\n<p>\u5728\u89c6\u56fe\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u53ef\u5bf9\u89c6\u56fe\u8fdb\u884c\u4fee\u6539\uff0c\u89c6\u56fe\u4e0d\u5728\u7684\u60c5\u51b5\u4e0b\u53ef\u521b\u5efa\u89c6\u56fe<\/p>\n<h4>ALTER\u8bed\u53e5\u4fee\u6539\u89c6\u56fe<\/h4>\n<pre><code class=\"language-sql\">ALTER \n  [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\n  [DEFINER = { user | CURRENT_USER}]\n  [SQL SECURITY { DEFINER | INVOKER }] \nVIEW view_name [(column_list)]\nAS select_statement\n  [WITH [CASCADED | LOCAL] CHECK OPTION ]<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4fee\u6539\u89c6\u56fe\u662f\u6307\u4fee\u6539\u6570\u636e\u5e93\u4e2d\u5df2\u5b58\u5728\u7684\u8868\u7684\u5b9a\u4e49\uff0c\u5f53\u57fa\u8868\u7684\u67d0\u4e9b\u5b57\u6bb5\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u89c6\u56fe\u6765\u4fdd\u6301\u89c6\u56fe\u548c\u57fa\u672c\u8868\u4e4b\u95f4\u4e00\u81f4<\/p>\n<h4>DML\u64cd\u4f5c\u66f4\u65b0\u89c6\u56fe<\/h4>\n<p>\u56e0\u4e3a\u89c6\u56fe\u672c\u8eab\u6ca1\u6709\u6570\u636e\uff0c\u56e0\u6b64\u5bf9\u89c6\u56fe\u8fdb\u884c\u7684dml\u64cd\u4f5c\u6700\u7ec8\u90fd\u4f53\u73b0\u5728\u57fa\u8868\u4e2d\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u89c6\u56fe\u7684DML\u64cd\u4f5c\uff0c\u4e0d\u662f\u6240\u6709\u7684\u89c6\u56fe\u90fd\u53ef\u4ee5\u505aDML\u64cd\u4f5c\u3002<\/p>\n<p>\u6709\u4e0b\u5217\u5185\u5bb9\u4e4b\u4e00\uff0c\u89c6\u56fe\u4e0d\u80fd\u505aDML\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>select\u5b50\u53e5\u4e2d\u5305\u542bdistinct<\/li>\n<li>select\u5b50\u53e5\u4e2d\u5305\u542b\u7ec4\u51fd\u6570<\/li>\n<li>select\u8bed\u53e5\u4e2d\u5305\u542b<code>group by<\/code>\u5b50\u53e5<\/li>\n<li>select\u8bed\u53e5\u4e2d\u5305\u542b<code>order by<\/code>\u5b50\u53e5<\/li>\n<li>select\u8bed\u53e5\u4e2d\u5305\u542b<code>union<\/code>\u3001<code>union all<\/code>\u7b49\u96c6\u5408\u8fd0\u7b97\u7b26<\/li>\n<li>where\u5b50\u53e5\u4e2d\u5305\u542b\u76f8\u5173\u5b50\u67e5\u8be2<\/li>\n<li>from\u5b50\u53e5\u4e2d\u5305\u542b\u591a\u4e2a\u8868<\/li>\n<li>\u5982\u679c\u89c6\u56fe\u4e2d\u6709\u8ba1\u7b97\u5217\uff0c\u5219\u4e0d\u80fd\u66f4\u65b0<\/li>\n<li>\u5982\u679c\u57fa\u8868\u4e2d\u6709\u67d0\u4e2a\u5177\u6709\u975e\u7a7a\u7ea6\u675f\u7684\u5217\u672a\u51fa\u73b0\u5728\u89c6\u56fe\u5b9a\u4e49\u4e2d\uff0c\u5219\u4e0d\u80fd\u505ainsert\u64cd\u4f5c<\/li>\n<\/ul>\n<h4>drop\u5220\u9664\u89c6\u56fe<\/h4>\n<p>\u5220\u9664\u89c6\u56fe\u662f\u6307\u5220\u9664\u6570\u636e\u5e93\u4e2d\u5df2\u5b58\u5728\u7684\u89c6\u56fe\uff0c\u5220\u9664\u89c6\u56fe\u65f6\uff0c\u53ea\u80fd\u5220\u9664\u89c6\u56fe\u7684\u5b9a\u4e49\uff0c\u4e0d\u4f1a\u5220\u9664\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u4f1a\u5f71\u54cd\u57fa\u8868\uff1a<\/p>\n<pre><code class=\"language-sql\">DROP VIEW [IF EXISTS]\nview_name [, view_name] ...<\/code><\/pre>\n<p>\u6bd4\u5982<code>drop view if exists v_student;<\/code><\/p>\n<h3>\u4f7f\u7528WITH CHECK OPTION\u7ea6\u675f<\/h3>\n<p>\u5bf9\u4e8e\u53ef\u4ee5\u6267\u884cDML\u64cd\u4f5c\u7684\u89c6\u56fe\uff0c\u5b9a\u4e49\u65f6\u53ef\u4ee5\u5e26\u4e0a<code>WITH CHECK OPTION<\/code>\u7ea6\u675f<br \/>\n\u4f5c\u7528\uff1a\u5bf9\u89c6\u56fe\u6240\u505a\u7684DML\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u4e0d\u80fd\u8fdd\u53cd\u89c6\u56fe\u7684WHERE\u6761\u4ef6\u7684\u9650\u5236<\/p>\n<p>\u9996\u5148\u5411\u535a\u5ba2\u8868\u4e2d\u63d2\u5165\u51e0\u6761\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">INSERT INTO `blog` VALUES (&#039;4&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba24&#039;, &#039;\u535a\u5ba2\u5185\u5bb9444&#039;, &#039;1&#039;);\nINSERT INTO `blog` VALUES (&#039;5&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba25&#039;, &#039;\u6d4b\u8bd5\u5185\u5bb9555&#039;, &#039;1&#039;);\nINSERT INTO `blog` VALUES (&#039;6&#039;, &#039;\u6d4b\u8bd5\u535a\u5ba26&#039;, &#039;\u6d4b\u8bd5\u5185\u5bb9666&#039;, &#039;1&#039;);<\/code><\/pre>\n<p>\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\uff0c\u83b7\u53d6\u6307\u5b9a\u7f16\u53f7\u4e3a1\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-sql\">create view v_blog_1(\u7f16\u53f7,\u6807\u9898,\u5185\u5bb9,\u4f5c\u8005\u7f16\u53f7)\nas\nselect id,title,content,author_id from blog\nwhere author_id = 1\nwith check option;<\/code><\/pre>\n<p>\u67e5\u8be2\u4e00\u4e0b\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">select * from v_blog_1;<\/code><\/pre>\n<pre><code class=\"language-sql\">mysql&gt; select * from v_blog_1;\n+--------+---------------+-----------------+--------------+\n| \u7f16\u53f7   | \u6807\u9898          | \u5185\u5bb9            | \u4f5c\u8005\u7f16\u53f7     |\n+--------+---------------+-----------------+--------------+\n|      1 | \u6d4b\u8bd5\u535a\u5ba21     | \u535a\u5ba2\u5185\u5bb9111     |            1 |\n|      4 | \u6d4b\u8bd5\u535a\u5ba24     | \u535a\u5ba2\u5185\u5bb9444     |            1 |\n|      5 | \u6d4b\u8bd5\u535a\u5ba25     | \u6d4b\u8bd5\u5185\u5bb9555     |            1 |\n|      6 | \u6d4b\u8bd5\u535a\u5ba26     | \u6d4b\u8bd5\u5185\u5bb9666     |            1 |\n+--------+---------------+-----------------+--------------+\n4 rows in set (0.00 sec)<\/code><\/pre>\n<p>\u518d\u4f7f\u7528update\u5bf9\u89c6\u56fe\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n<pre><code class=\"language-sql\">update v_blog_1 set \u4f5c\u8005\u7f16\u53f7=2 where \u7f16\u53f7=1;<\/code><\/pre>\n<p>\u8bed\u53e5\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; update v_blog_1 set \u4f5c\u8005\u7f16\u53f7=2 where \u7f16\u53f7=1;\nERROR 1369 (HY000): CHECK OPTION failed &#039;test.v_blog_1&#039;<\/code><\/pre>\n<p>\u56e0\u4e3a\u8fdd\u53cd\u4e86\u89c6\u56fe\u4e2d\u7684<code>where author_id = 1<\/code>\u5b50\u53e5\uff0c\u6240\u4ee5\u629b\u51fa\u5f02\u5e38<\/p>\n<p>\u5229\u7528<code>with check option<\/code>\u7ea6\u675f\u9650\u5236\uff0c\u4fdd\u8bc1\u66f4\u65b0\u89c6\u56fe\u662f\u5728\u8be5\u89c6\u56fe\u7684\u6743\u9650\u8303\u56f4\u4e4b\u5185<\/p>\n<p>\u4f7f\u7528<code>WITH CHECK OPTION<\/code>\u7ea6\u675f\u65f6(\u4e0d\u6307\u5b9a\u9009\u9879\u5219\u9ed8\u8ba4\u662f<code>CASCADED<\/code>)\uff0c\u53ef\u4ee5\u4f7f\u7528<code>CASCADED<\/code>\u6216\u8005<code>LOCAL<\/code>\u9009\u9879\u6307\u5b9a\u68c0\u67e5\u7684\u7a0b\u5ea6\uff1a<\/p>\n<ul>\n<li><code>CASCADED<\/code>\uff1a\u68c0\u67e5\u6240\u6709\u7684\u89c6\u56fe\uff0c\u4f1a\u68c0\u67e5\u5d4c\u5957\u89c6\u56fe\u53ca\u5176\u5e95\u5c42\u7684\u89c6\u56fe<\/li>\n<li><code>LOCAL<\/code>\uff1a\u53ea\u68c0\u67e5\u5c06\u8981\u66f4\u65b0\u7684\u89c6\u56fe\u672c\u8eab\uff0c\u5d4c\u5957\u89c6\u56fe\u4e0d\u68c0\u67e5\u5176\u5e95\u5c42\u7684\u89c6\u56fe<\/li>\n<\/ul>\n<h3>\u5b9a\u4e49\u89c6\u56fe\u65f6\u7684\u5176\u4ed6\u9009\u9879<\/h3>\n<p>\u89c6\u56fe\u7684\u5b8c\u6574\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE [OR REPLACE]\n  [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\n  [DEFINER = { user | CURRENT_USER }]\n  [SQL SECURITY { DEFINER | INVOKER }]\nVIEW view_name [(column_list)]\nAS select_statement \n  [WITH [CASCADED | LOCAL] CHECK OPTION]<\/code><\/pre>\n<h4>ALGORITHM\u9009\u9879<\/h4>\n<p>\u9009\u62e9\u5728\u5904\u7406\u5b9a\u4e49\u89c6\u56fe\u7684select\u8bed\u53e5\u4e2d\u4f7f\u7528\u7684\u65b9\u6cd5<\/p>\n<ul>\n<li>UNDEFINED\uff1aMySQL\u5c06\u81ea\u52a8\u9009\u62e9\u6240\u8981\u4f7f\u7528\u7684\u7b97\u6cd5<\/li>\n<li>MERGE\uff1a\u5c06\u89c6\u56fe\u7684\u8bed\u53e5\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<\/li>\n<li>TEMPTABLE\uff1a\u5c06\u89c6\u56fe\u7684\u7ed3\u679c\u5b58\u5165\u4e34\u65f6\u8868\uff0c\u7136\u540e\u4f7f\u7528\u4e34\u65f6\u8868\u6267\u884c\u8bed\u53e5<\/li>\n<\/ul>\n<p>\u7f3a\u7701ALGORITHM\u9009\u9879\u7b49\u540c\u4e8e<code>ALGORITHM = UNDEFINED<\/code><\/p>\n<h4>DEFINER\u9009\u9879<\/h4>\n<p>\u6307\u51fa\u8c01\u662f\u89c6\u56fe\u7684\u521b\u5efa\u8005\u6216\u5b9a\u4e49\u8005<\/p>\n<ul>\n<li>definer=&#8217;\u7528\u6237\u540d&#8217;@&#8217;\u767b\u5f55\u4e3b\u673a&#8217;<\/li>\n<li>\u5982\u679c\u4e0d\u6307\u5b9a\u8be5\u9009\u9879\uff0c\u5219\u521b\u5efa\u89c6\u56fe\u7684\u7528\u6237\u5c31\u662f\u5b9a\u4e49\u8005\uff0c\u6307\u5b9a\u5173\u952e\u5b57<code>CURRENT_USER<\/code>(\u5f53\u524d\u7528\u6237)\u548c\u4e0d\u6307\u5b9a\u8be5\u9009\u9879\u6548\u679c\u76f8\u540c<\/li>\n<\/ul>\n<h4>SQL SECURITY\u9009\u9879<\/h4>\n<p>\u8981\u67e5\u8be2\u4e00\u4e2a\u89c6\u56fe\uff0c\u9996\u5148\u5fc5\u987b\u8981\u5177\u6709\u5bf9\u89c6\u56fe\u7684select\u6743\u9650\uff0c\u5982\u679c\u540c\u4e00\u4e2a\u7528\u6237\u5bf9\u4e8e\u89c6\u56fe\u6240\u8bbf\u95ee\u7684\u8868\u6ca1\u6709select\u6743\u9650\uff0c\u90a3\u4f1a\u600e\u4e48\u6837\uff1f<\/p>\n<p>SQL SECURITY\u9009\u9879\u51b3\u5b9a\u6267\u884c\u7684\u7ed3\u679c\uff1a<\/p>\n<ul>\n<li>SQL SECURITY DEFINER\uff1a\u5b9a\u4e49(\u521b\u5efa)\u89c6\u56fe\u7684\u7528\u6237\u5fc5\u987b\u5bf9\u89c6\u56fe\u6240\u8bbf\u95ee\u7684\u8868\u5177\u6709select\u6743\u9650\uff0c\u4e5f\u5c31\u662f\u8bf4\u5c06\u6765\u5176\u4ed6\u7528\u6237\u8bbf\u95ee\u8868\u7684\u65f6\u5019\u4ee5\u5b9a\u4e49\u8005\u7684\u8eab\u4efd\uff0c\u6b64\u65f6\u5176\u4ed6\u7528\u6237\u5e76\u6ca1\u6709\u8bbf\u95ee\u6743\u9650\u3002<\/li>\n<li>SQL SECURITY INVOKER\uff1a\u8bbf\u95ee\u89c6\u56fe\u7684\u7528\u6237\u5fc5\u987b\u5bf9\u89c6\u56fe\u6240\u8bbf\u95ee\u7684\u8868\u5177\u6709select\u6743\u9650\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u7701SQL SECURITY\u9009\u9879\u7b49\u540c\u4e8e<code>SQL SECURITY DEFINER<\/code><\/p>\n<p>\u89c6\u56fe\u6743\u9650\u603b\u7ed3\uff1a<\/p>\n<p>\u4f7f\u7528root\u7528\u6237\u5b9a\u4e49\u4e00\u4e2a\u89c6\u56fe(\u63a8\u8350\u4f7f\u7528\u7b2c\u4e00\u79cd)\uff1au1\u3001u2<br \/>\n1\uff09u1\u4f5c\u4e3a\u5b9a\u4e49\u8005\u5b9a\u4e49\u4e00\u4e2a\u89c6\u56fe\uff0cu1\u5bf9\u57fa\u8868\u6709select\u6743\u9650\uff0cu2\u5bf9\u89c6\u56fe\u6709\u8bbf\u95ee\u6743\u9650\uff1au2\u662f\u4ee5\u5b9a\u4e49\u8005\u7684\u8eab\u4efd\u8bbf\u95ee\u53ef\u4ee5\u67e5\u8be2\u5230\u57fa\u8868\u7684\u5185\u5bb9\uff1b<br \/>\n2\uff09u1\u4f5c\u4e3a\u5b9a\u4e49\u8005\u5b9a\u4e49\u4e00\u4e2a\u89c6\u56fe\uff0cu1\u5bf9\u57fa\u8868\u6ca1\u6709select\u6743\u9650\uff0cu2\u5bf9\u89c6\u56fe\u6709\u8bbf\u95ee\u6743\u9650\uff0cu2\u5bf9\u57fa\u8868\u6709select\u6743\u9650\uff1au2\u8bbf\u95ee\u89c6\u56fe\u7684\u65f6\u5019\u662f\u4ee5\u8c03\u7528\u8005\u7684\u8eab\u4efd\uff0c\u6b64\u65f6\u8c03\u7528\u8005\u662fu2\uff0c\u53ef\u4ee5\u67e5\u8be2\u5230\u57fa\u8868\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u89c6\u56fe\uff1f \u89c6\u56fe\uff08view\uff09\u662f\u4e00\u79cd\u865a\u62df\u5b58\u5728\u7684\u8868\uff0c\u662f\u4e00\u4e2a\u903b\u8f91\u8868\uff0c\u672c\u8eab\u5e76\u4e0d\u5305\u542b\u6570\u636e\u3002\u4f5c\u4e3a\u4e00\u4e2aselect\u8bed\u53e5\u4fdd\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-2076","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2076"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2076\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}