如何在 PostgreSQL 中查看视图的 CREATE VIEW 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14634322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to see the CREATE VIEW code for a view in PostgreSQL?
提问by Elias Dorneles
Is there an easy way to see the code used to create a view using the PostgreSQL command-line client?
是否有一种简单的方法可以查看用于使用 PostgreSQL 命令行客户端创建视图的代码?
Something like the SHOW CREATE VIEW
from MySQL.
像SHOW CREATE VIEW
来自 MySQL 的东西。
回答by EoghanM
Kept having to return here to look up pg_get_viewdef
(how to remember that!!), so searched for a more memorable command... and got it:
一直不得不返回这里查找pg_get_viewdef
(如何记住!!),所以搜索了一个更令人难忘的命令......并得到了它:
\d+ viewname
You can see similar sorts of commands by typing \?
at the pgsql command line.
您可以通过\?
在 pgsql 命令行中键入来查看类似的命令。
Bonus tip: The emacs command sql-postgres
makes pgsql a lot more pleasant (edit, copy, paste, command history).
额外提示:emacs 命令sql-postgres
使 pgsql 更加愉快(编辑、复制、粘贴、命令历史)。
回答by a_horse_with_no_name
select pg_get_viewdef('viewname', true)
A list of all those functions is available in the manual:
手册中提供了所有这些功能的列表:
http://www.postgresql.org/docs/current/static/functions-info.html
http://www.postgresql.org/docs/current/static/functions-info.html
回答by Clodoaldo Neto
select definition from pg_views where viewname = 'my_view'
回答by Steve Judd
If you want an ANSI SQL-92 version:
如果您想要 ANSI SQL-92 版本:
select view_definition from information_schema.views where table_name = 'view_name';
回答by Brain90
GoodNews from v.9.6 and above, View editing are now native from psql. Just invoke \ev
command. View definitions will show in your configured editor.
来自 v.9.6 及更高版本的好消息,视图编辑现在是 psql 原生的。只需调用\ev
命令。视图定义将显示在您配置的编辑器中。
julian@assange=# \ev {your_view_names}
julian@assange=# \ev {your_view_names}
Bonus. Some useful command to interact with query buffer.
奖金。一些与查询缓冲区交互的有用命令。
Query Buffer
\e [FILE] [LINE] edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\ev [VIEWNAME [LINE]] edit view definition with external editor
\p show the contents of the query buffer
\r reset (clear) the query buffer
\s [FILE] display history or save it to file
\w FILE write query buffer to file
回答by Gianluca Rossini
These is a little thing to point out.
Using the function pg_get_viewdef or pg_views or information_schema.views you will always get a rewrited versionof your original DDL.
The rewited version may or not be the same as your originl DDL script.
If the Rule Manager rewrite your view definition your original DLL will be lost and you will able to read the only the rewrited version of your view definition.
Not all views are rewrited but if you use sub-select or joins probably your views will be rewrited.
这些是需要指出的一点。
使用 pg_get_viewdef 或 pg_views 或 information_schema.views 函数,您将始终获得原始 DDL的重写版本。
重新编写的版本可能与您的原始 DDL 脚本相同,也可能不同。
如果规则管理器重写您的视图定义,您的原始 DLL 将丢失,您将能够读取视图定义的唯一重写版本。
并非所有视图都被重写,但如果您使用子选择或连接,您的视图可能会被重写。