有没有一种简单的方法来查看使用PostgreSQL命令行客户端创建视图的代码?
比如MySQL中的SHOW CREATE VIEW。
有没有一种简单的方法来查看使用PostgreSQL命令行客户端创建视图的代码?
比如MySQL中的SHOW CREATE VIEW。
当前回答
在psql cli中,可以使用
\d+ <yourViewName>
\sv <yourViewName>
输出如下:
\d+ v_ma_students
View "public.v_ma_students"
Column | Type | Collation | Nullable | Default | Storage | De
scription
--------+-----------------------+-----------+----------+---------+----------+---
SOMETHINGS HERE
View definition:
SELECT student.sno,
student.sname,
student.ssex,
student.sage,
student.sdept
FROM student
WHERE student.sdept::text = 'MA'::text;
Options: check_option=cascaded
\sv v_ma_students
CREATE OR REPLACE VIEW public.v_ma_students AS
SELECT student.sno,
student.sname,
student.ssex,
student.sage,
student.sdept
FROM student
WHERE student.sdept::text = 'MA'::text
WITH CASCADED CHECK OPTION
其他回答
select definition from pg_views where viewname = 'my_view'
如果你想要一个ANSI SQL-92版本:
select view_definition from information_schema.views where table_name = 'view_name';
select pg_get_viewdef('viewname', true)
手册中列出了所有这些功能:
http://www.postgresql.org/docs/current/static/functions-info.html
在psql cli中,可以使用
\d+ <yourViewName>
\sv <yourViewName>
输出如下:
\d+ v_ma_students
View "public.v_ma_students"
Column | Type | Collation | Nullable | Default | Storage | De
scription
--------+-----------------------+-----------+----------+---------+----------+---
SOMETHINGS HERE
View definition:
SELECT student.sno,
student.sname,
student.ssex,
student.sage,
student.sdept
FROM student
WHERE student.sdept::text = 'MA'::text;
Options: check_option=cascaded
\sv v_ma_students
CREATE OR REPLACE VIEW public.v_ma_students AS
SELECT student.sno,
student.sname,
student.ssex,
student.sage,
student.sdept
FROM student
WHERE student.sdept::text = 'MA'::text
WITH CASCADED CHECK OPTION
一直不得不返回这里查找pg_get_viewdef(如何记住!!),所以搜索一个更容易记住的命令…并得到了它:
\d+ viewname
你可以通过输入\?在PGSQL命令行。
额外提示:emacs命令sql-postgres使pgsql更加方便(编辑、复制、粘贴、命令历史)。