postgresql-视图、删除记录

本文提供了PostgreSQL数据库的基本操作实例,包括记录的删除、插入、查询及视图的创建等。通过具体SQL语句展示了左外连接的使用,并创建了一个视图来统计不同城市的人员数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:本PostgreSQl实用指南系列为刘兴(http://deepfuture.iteye.com/)原创,未经笔者授权,任何人和机构不能转载

D:\pgsql>psql mydb
psql (8.4.2)
Type "help" for help.

删除记录
mydb=# delete from citys where name='北京';
DELETE 1
基本查询

mydb=# select * from citys
mydb-# ;
 name | id
------+----
 长沙 |  1
 湛江 |  2
(2 rows)

 

mydb=# select * from student
mydb-# ;
    name    | age | city
------------+-----+------
 deepfuture |  20 |    1
 未来       |  20 |    2
 张三       |  21 |    1
(3 rows)


mydb=# insert into citys values('上海',3)
mydb-# ;
INSERT 0 1
mydb=# select * from citys;
 name | id
------+----
 长沙 |  1
 湛江 |  2
 上海 |  3
(3 rows)


mydb=# select citys.name, count(*) from citys left outer join student on (studen
t.city=citys.id) group by citys.name,student.city;
 name | count
------+-------
 上海 |     1
 湛江 |     1
 长沙 |     2
(3 rows)


mydb=# select citys.name, count(student.city) from citys left outer join student
 on (student.city=citys.id) group by citys.name,student.city;
 name | count
------+-------
 上海 |     0
 湛江 |     1
 长沙 |     2
(3 rows)

创建视图

mydb=# create view citylist as select citys.name as 城市, count(student.city) as
 人数 from citys left outer join student on (student.city=citys.id) group by cit
ys.name,student.city;
CREATE VIEW

调用视图
mydb=# select * from citylist
mydb-# ;
 城市 | 人数
------+------
 上海 |    0
 湛江 |    1
 长沙 |    2
(3 rows)


mydb=#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值