新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > 数据库 > 数据库综合 > 正文:怎样用视图自定义PostgreSQL数据库查询

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

新客网 XKER.COM 2007-11-01 来源: 00796 收藏本文

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);

 

共2页: 上一页 [1] [2] 下一页
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐