{"id":2679,"date":"2025-03-12T23:44:24","date_gmt":"2025-03-12T15:44:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2679"},"modified":"2025-03-12T23:44:24","modified_gmt":"2025-03-12T15:44:24","slug":"postgresql-view%ef%bc%88%e8%a7%86%e5%9b%be%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-view%ef%bc%88%e8%a7%86%e5%9b%be%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL View\uff08\u89c6\u56fe\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>PostgreSQL <strong>\u89c6\u56fe<\/strong>\uff08View\uff09\u662f\u4e00\u4e2a\u865a\u62df\u7684\u8868\uff0c\u5b9e\u9645\u4e0a\u5b83\u5e76\u4e0d\u5b58\u50a8\u6570\u636e\uff0c\u800c\u662f\u4e00\u4e2a\u4fdd\u5b58\u67e5\u8be2\u7684 SQL \u8bed\u53e5\u3002\u5f53\u6211\u4eec\u67e5\u8be2\u89c6\u56fe\u65f6\uff0cPostgreSQL \u4f1a\u6839\u636e\u89c6\u56fe\u5b9a\u4e49\u7684 SQL \u8bed\u53e5\u52a8\u6001\u751f\u6210\u7ed3\u679c\u96c6\u3002\u89c6\u56fe\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u67e5\u8be2\u3001\u589e\u5f3a\u5b89\u5168\u6027\u5e76\u63d0\u5347\u6570\u636e\u8bbf\u95ee\u7684\u53ef\u7ba1\u7406\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u521b\u5efa\u89c6\u56fe<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u57fa\u672c\u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW view_name AS\nSELECT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>view_name<\/code>\uff1a\u8981\u521b\u5efa\u7684\u89c6\u56fe\u540d\u79f0\u3002<\/li>\n\n\n\n<li><code>SELECT<\/code> \u67e5\u8be2\uff1a\u7528\u4e8e\u5b9a\u4e49\u89c6\u56fe\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW employee_view AS\nSELECT id, name, department\nFROM employees\nWHERE status = 'active';<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a <code>employee_view<\/code> \u7684\u89c6\u56fe\uff0c\u5b83\u53ea\u8fd4\u56de <code>employees<\/code> \u8868\u4e2d\u72b6\u6001\u4e3a <code>'active'<\/code> \u7684\u5458\u5de5\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u67e5\u8be2\u89c6\u56fe<\/strong><\/h2>\n\n\n\n<p>\u521b\u5efa\u89c6\u56fe\u540e\uff0c\u67e5\u8be2\u89c6\u56fe\u7684\u8bed\u6cd5\u4e0e\u67e5\u8be2\u8868\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employee_view;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u89c6\u56fe\u7684\u7279\u6027<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u89c6\u56fe\u672c\u8eab\u4e0d\u5b58\u50a8\u6570\u636e\uff0c\u5b83\u53ea\u4fdd\u5b58\u67e5\u8be2\u8bed\u53e5\u3002<\/li>\n\n\n\n<li>\u89c6\u56fe\u7684\u5185\u5bb9\u662f<strong>\u52a8\u6001\u751f\u6210<\/strong>\u7684\uff0c\u6bcf\u6b21\u67e5\u8be2\u89c6\u56fe\u65f6\uff0c\u90fd\u4f1a\u6267\u884c\u89c6\u56fe\u5b9a\u4e49\u7684\u67e5\u8be2\u8bed\u53e5\u3002<\/li>\n\n\n\n<li>\u89c6\u56fe\u53ef\u4ee5\u5305\u542b\u590d\u6742\u7684 <code>JOIN<\/code>\u3001<code>GROUP BY<\/code>\u3001<code>ORDER BY<\/code> \u7b49\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u66f4\u65b0\u89c6\u56fe\u7684\u6570\u636e<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ef\u66f4\u65b0\u89c6\u56fe<\/strong>\uff1a\u5982\u679c\u89c6\u56fe\u662f\u57fa\u4e8e\u5355\u4e2a\u8868\u4e14\u6ca1\u6709\u590d\u6742\u7684\u64cd\u4f5c\uff08\u5982 <code>JOIN<\/code>\u3001<code>GROUP BY<\/code>\uff09\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u66f4\u65b0\u89c6\u56fe\u4e2d\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u66f4\u65b0\u89c6\u56fe<\/strong>\uff1a\u5982\u679c\u89c6\u56fe\u5305\u542b\u591a\u4e2a\u8868\u7684\u8054\u5408\uff0c\u6216\u8005\u5305\u542b\u805a\u5408\u51fd\u6570\u3001\u5206\u7ec4\u7b49\u590d\u6742\u64cd\u4f5c\uff0c\u901a\u5e38\u4e0d\u53ef\u76f4\u63a5\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u66f4\u65b0\u53ef\u66f4\u65b0\u89c6\u56fe<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE employee_view\nSET department = 'HR'\nWHERE id = 5;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u8bed\u53e5\u4f1a\u66f4\u65b0 <code>employee_view<\/code> \u4e2d\u5458\u5de5 <code>id = 5<\/code> \u7684\u90e8\u95e8\u4fe1\u606f\uff08\u5047\u8bbe\u89c6\u56fe\u662f\u57fa\u4e8e <code>employees<\/code> \u8868\u7684\u7b80\u5355\u67e5\u8be2\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u5bf9\u4e0d\u53ef\u66f4\u65b0\u89c6\u56fe\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u89c6\u56fe\u662f\u4e0d\u53ef\u66f4\u65b0\u7684\uff08\u5982\u6709 <code>JOIN<\/code>\uff09\uff0c\u5219\u9700\u8981\u4f7f\u7528 <code>INSTEAD OF<\/code> \u89e6\u53d1\u5668\u6765\u5b9e\u73b0\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5220\u9664\u89c6\u56fe<\/strong><\/h2>\n\n\n\n<p>\u5220\u9664\u89c6\u56fe\u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP VIEW view_name;<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP VIEW employee_view;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u4fee\u6539\u89c6\u56fe<\/strong><\/h2>\n\n\n\n<p>\u8981\u4fee\u6539\u89c6\u56fe\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>CREATE OR REPLACE VIEW<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE OR REPLACE VIEW view_name AS\nSELECT new_columns\nFROM new_table\nWHERE condition;<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE OR REPLACE VIEW employee_view AS\nSELECT id, name, department, hire_date\nFROM employees\nWHERE status = 'active';<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4f1a\u91cd\u65b0\u5b9a\u4e49 <code>employee_view<\/code> \u89c6\u56fe\uff0c\u65b0\u589e\u4e86 <code>hire_date<\/code> \u5b57\u6bb5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u89c6\u56fe\u7684\u4f18\u52bf<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1 \u7b80\u5316\u590d\u6742\u67e5\u8be2<\/strong><\/h3>\n\n\n\n<p>\u89c6\u56fe\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u67e5\u8be2\u5c01\u88c5\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u67e5\u8be2\uff0c\u907f\u514d\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684 SQL \u4ee3\u7801\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u590d\u6742\u67e5\u8be2\nSELECT employees.name, departments.department_name\nFROM employees\nJOIN departments ON employees.department_id = departments.id\nWHERE employees.status = 'active';<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u6765\u5c01\u88c5\u8be5\u67e5\u8be2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW active_employees AS\nSELECT employees.name, departments.department_name\nFROM employees\nJOIN departments ON employees.department_id = departments.id\nWHERE employees.status = 'active';<\/code><\/pre>\n\n\n\n<p>\u4ee5\u540e\u53ea\u9700\u67e5\u8be2\u89c6\u56fe\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM active_employees;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.2 \u6570\u636e\u5b89\u5168<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7\u89c6\u56fe\u53ef\u4ee5\u9650\u5236\u7528\u6237\u8bbf\u95ee\u67d0\u4e9b\u654f\u611f\u6570\u636e\uff0c\u53ea\u66b4\u9732\u9700\u8981\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u4ec5\u663e\u793a\u5458\u5de5\u7684 <code>id<\/code> \u548c <code>name<\/code>\uff0c\u800c\u4e0d\u663e\u793a\u5de5\u8d44\u7b49\u654f\u611f\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.3 \u7edf\u4e00\u63a5\u53e3<\/strong><\/h3>\n\n\n\n<p>\u89c6\u56fe\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u67e5\u8be2\u63d0\u4f9b\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u7b80\u5316\u5ba2\u6237\u7aef\u64cd\u4f5c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u89c6\u56fe\u7684\u9650\u5236<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u5f71\u54cd<\/strong>\uff1a\u6bcf\u6b21\u67e5\u8be2\u89c6\u56fe\u65f6\uff0c\u90fd\u4f1a\u6267\u884c\u89c6\u56fe\u5b9a\u4e49\u7684\u67e5\u8be2\uff0c\u8fd9\u53ef\u80fd\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\uff0c\u7279\u522b\u662f\u5f53\u89c6\u56fe\u975e\u5e38\u590d\u6742\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u66f4\u65b0\u89c6\u56fe<\/strong>\uff1a\u5982\u679c\u89c6\u56fe\u6d89\u53ca\u591a\u4e2a\u8868\u3001\u805a\u5408\u51fd\u6570\u7b49\uff0c\u66f4\u65b0\u6570\u636e\u65f6\u4f1a\u53d7\u5230\u9650\u5236\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u80fd\u7d22\u5f15\u89c6\u56fe<\/strong>\uff1a\u89c6\u56fe\u672c\u8eab\u4e0d\u80fd\u521b\u5efa\u7d22\u5f15\uff0c\u67e5\u8be2\u89c6\u56fe\u65f6\u7684\u6027\u80fd\u65e0\u6cd5\u901a\u8fc7\u7d22\u5f15\u6765\u4f18\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u89c6\u56fe\u7684\u793a\u4f8b\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.1 \u521b\u5efa\u89c6\u56fe<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW employee_view AS\nSELECT id, name, department\nFROM employees\nWHERE status = 'active';<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.2 \u67e5\u8be2\u89c6\u56fe<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM employee_view;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.3 \u5220\u9664\u89c6\u56fe<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DROP VIEW employee_view;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9.4 \u66f4\u65b0\u89c6\u56fe\uff08\u57fa\u4e8e\u5355\u8868\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE employee_view\nSET department = 'HR'\nWHERE id = 5;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u89c6\u56fe\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u7b80\u5316\u6570\u636e\u64cd\u4f5c\u5e76\u63d0\u9ad8\u6570\u636e\u7ba1\u7406\u7684\u7075\u6d3b\u6027\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u89c6\u56fe\uff08View\uff09\u662f\u4e00\u4e2a\u865a\u62df\u7684\u8868\uff0c\u5b9e\u9645\u4e0a\u5b83\u5e76\u4e0d\u5b58\u50a8\u6570\u636e\uff0c\u800c\u662f\u4e00\u4e2a\u4fdd\u5b58\u67e5\u8be2\u7684 SQL \u8bed\u53e5\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2680,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2679"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2681,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2679\/revisions\/2681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2680"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}