CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

数据库 | Oracle | DB2 | SQL Server | MySQL | 商业智能 | BI | DBA | Sybase| SQL Server 2008

您现在的位置: IT专家网 > 数据库子站 > 数据库技巧

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

作者: ,  出处:IT专家网社区, 责任编辑: 李书琴, 
2007-11-01 09:31
  本文祥解了用视图自定义PostgreSQL数据库查询的方法……

  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.time
  test-# FROM stories AS s, authors AS a, stories_authors_link AS sa
  test-# WHERE s.id = sa.story
  test-# AND a.id = sa.author
  test-# ORDER BY s.time
  test-# DESC;
  title|name|time
  ------------------------+-------------+---------------------
  The Oxford Blues| John Doe| 2005-06-12 18:01:43
  Memories Of Malgudi| Jane Ki| 2005-06-09 23:35:57
  The Big Surprise| Ellen Sue| 2005-05-30 08:21:02
  Indians and The Cowboy | Jane Ki| 2005-04-16 11:19:28
  Into Thin Air...| James White | 2005-04-02 06:54:12
  All Tied Up| John Doe| 2005-04-01 12:37:00
  Crash!| 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;
共2页。 1 2 :

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅


    
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i