首页  韩国资源  酷站加油  我的展厅  设计名站  古典元素  推荐下载  设计欣赏  每周专访  招募精英  人才专区  网页教程  平面设计  编程开发  设计竞赛
当前位置:首页 > 编程开发 > 数据库编程 > 正文
Google
如何创建使用和删除PostgreSQL数据库中的视图
来源:赛迪网 作者:李丝 2008年02月20日 09:24 网友评论:0条 点击:

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;

首页 上一页 [1] [2] 下一页 尾页
上一篇:解决如何轻松在表格里删除列   下一篇:一种处理不规范日期数据的好方法
收藏此页】【打印】【关闭
 相关文章  我要点评
·SQL Server2008数据库引擎功能的重大更改
·SQLite不同于其他大部分的SQL数据库引擎
·IBM DB2数据库的注意事项
·DB2数据库创建存储过程时遇到的错误现象
·使用SQL Server数据库嵌套子查询
·删除Oracle数据库10g垃圾表的最新方法
·MySQL数据库双机热备的配置方法
·解决启用数据库复制时出现的18483错误

免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请联络:68design#163.com
转载要求:作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印。



关于我们 | 在线反馈 | 广告报价 | 友情链接 | 联系我们 | 免责声明 | 在线投稿 | 网站地图
Copyright © 2003-2007 68design.net, All Rights Reserve 【找网页设计师,当然上网页设计师联盟】