您好,欢迎访问一九零五行业门户网

用视图自定义PostgreSQL数据库查询

欢迎进入oracle社区论坛,与200万技术人员互动交流 >>进入 postgresql的一个主要特点就是创建自定义“视图”,这些视图仅仅是预先定义的sql查询,它们存储在数据库中并可以在需要时重复使用。因此,以这种方式储存经常使用的sql查询比每次都手工输入要更有效
欢迎进入oracle社区论坛,与200万技术人员互动交流 >>进入
postgresql的一个主要特点就是创建自定义“视图”,这些视图仅仅是预先定义的sql查询,它们存储在数据库中并可以在需要时重复使用。因此,以这种方式储存经常使用的sql查询比每次都手工输入要更有效率而且更加灵活,因为通过视图生成的数据集本身就可以通过sql来操作。
这篇文章主要介绍了如何创建、使用和删除postgresql数据库中的视图。
示例表格
使用以下的sql命令来创建三个示例表格:
test=# create table stories (id int, title varchar, time timestamp);test=# create table authors (id int, name varchar);test=# create table stories_authors_link (story int, author int);
以上命令创建了三个表:一个用于小说标题、一个用于作者姓名,还有一个用于作者与小说的映射。使用列表a中的代码向表格中填充记录:
列表a:
test=# insert into authors values (1, 'john doe');test=# insert into authors values (2, 'james white');test=# insert into authors values (3, 'ellen sue');test=# insert into authors values (4, 'gina haggelstrom');test=# insert into authors values (5, 'jane ki');test=# insert into stories values (100, 'all tied up', '2005-04-01 12:37:00');test=# insert into stories values (112, 'into thin air...', '2005-04-02 06:54:12');test=# insert into stories values (127, 'the oxford blues', '2005-06-12 18:01:43');test=# insert into stories values (128, 'crash!', '2005-03-27 09:12:17');test=# insert into stories values (276, 'memories of malgudi', '2005-06-09 23:35:57');test=# insert into stories values (289, 'the big surprise', '2005-05-30 08:21:02');test=# insert into stories values (301, 'indians and the cowboy', '2005-04-16 11:19:28');test=# insert into stories_authors_link values (112, 2);test=# insert into stories_authors_link values (127, 1);test=# insert into stories_authors_link values (128, 5);test=# insert into stories_authors_link values (276, 5);test=# insert into stories_authors_link values (289, 3);test=# insert into stories_authors_link values (301, 5);test=# insert into stories_authors_link values (100, 1);
下一步,假设我们要获取一份关于小说及其作者的完整报告,这最好是通过连接三个表的公用字段来实现,如列表b所示:
列表b:
test=# select s.title, a.name, s.timetest-# from stories as s, authors as a, stories_authors_link as satest-# where s.id = sa.storytest-# and a.id = sa.authortest-# order by s.timetest-# desc;title|name|time------------------------+-------------+---------------------the oxford blues| john doe| 2005-06-12 18:01:43memories of malgudi| jane ki| 2005-06-09 23:35:57the big surprise| ellen sue| 2005-05-30 08:21:02indians and the cowboy | jane ki| 2005-04-16 11:19:28into thin air...| james white | 2005-04-02 06:54:12all tied up| john doe| 2005-04-01 12:37:00crash!| jane ki| 2005-03-27 09:12:17(7 rows)
很显然,如果一而再,再而三地输入这么长的查询是非常无效的,因此,将查询存储为视图是很有意义的,您可以这样做:
test=# create view myview as select s.title, a.name, s.time from stories as s, authors as a, stories_authors_link as sa where s.id = sa.story and a.id = sa.author order by s.time desc;
创建一个视图的语法是create view name as query,这将在数据库中以name为名称来存储query字符串的查询,您可以通过dv命令来检查输出,如下所示:
test=# dv list of relations schema | name | type | owner--------+--------+------+------- public | myview | view | pgsql(1 row)
如果要重复使用一个视图,可以运行一个select查询,就像一个正常的表一样,如列表c所示:
列表c:
test=# select * from myview;title|name|time------------------------+-------------+-------------the oxford blues| john doe| 2005-06-12 18:01:43memories of malgudi| jane ki| 2005-06-09 23:35:57the big surprise| ellen sue| 2005-05-30 08:21:02indians and the cowboy | jane ki| 2005-04-16 11:19:28into thin air...| james white | 2005-04-02 06:54:12all tied up| john doe| 2005-04-01 12:37:00crash!| jane ki| 2005-03-27 09:12:17(7 rows)
如列表c所示:从视图中进行选择实际上运行了原有的存储查询,很自然地,您可以在select语句中使用sql操作符来操作一个视图的输出,可以参考列表d中的示例。
列表d:
test=# select title, name from myview limit 3;title|name---------------------+-----------the oxford blues| john doememories of malgudi | jane kithe big surprise| ellen sue(3 rows)
验证原有的视图已经不存在可以通过dv命令的输出来检查:
test=# dvno relations found.
注释:与以上的例子相同,视图提供了一个简便快捷的方式来完成经常使用的select查询,而且还可以简单地获取相同数据的不同视角。
其它类似信息

推荐信息