加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PSQL Doc ...

发布时间:2020-12-13 17:20:38 所属栏目:百科 来源:网络整理
导读:内容结构 1 新用戶 2 sql查询语言环境,数据类型和函数,用户角色,性能调优 3 安装和服务端的管理 4 客户端程序的接口 5 服务端大可扩展性,包含用户自定义数据类型和函数. 6 SQL语句的参考信息,客户端和服务端的程序. 7 PostgreSQL开发者需要的信息 www.postgre
内容结构

1新用戶
2sql查询语言环境,数据类型和函数,用户角色,性能调优
3安装和服务端的管理
4客户端程序的接口
5服务端大可扩展性,包含用户自定义数据类型和函数.
6SQL语句的参考信息,客户端和服务端的程序.
7PostgreSQL开发者需要的信息



www.postgresql.org/docs/ 9.4/interactive/tutorial-advanced.html


1.新用户
PostgreSQL所对象关系数据库.
有些特性:复杂查询,外键,触发器,可更新的视图,事物的完整性,多并发控制
PostgreSQL可以被任何没有特权的用户安装,非root帐号也能访问.

切换到posgres用户(未进入数据库中),创建数据库:
createdbdb_name
createdb(默认创建postgres数据库)
dropdbdb_name
访问数据库
psqldb_name
dbname= #selectversion();(查看版本号)
hhelp
qquit

lshowdatabases

c dbname #change current database


2.SQL基本的使用

创建表
CREATETABLEweather(
cityvarchar( 80),
temp_lo int,--lowtemperature
temp_hi int,--hightemperature
prcpreal,--precipitation
datedate
);

CREATETABLEcities(
namevarchar( 80),
locationpoint
);

可以使用大数据类型有 int,smallint,real,doubleprecision,char(N),varchar(N),date,time,timestamp,andinterval等.
'--'表示后面的内容被注释掉了.

删除表
DROPTABLEtablename;
插入数据
INSERTINTOweatherVALUES( 'SanFrancisco',46,50,0.25,'1994-11-27');
INSERTINTOcitiesVALUES( 'SanFrancisco','(-194.0,53.0)');
更易读的语句
INSERTINTOweather(city,temp_lo,temp_hi,prcp,date)
VALUES( 'SanFrancisco',43,57,0.0,'1994-11-29');
插入的数据不需要使用特定的序列:
INSERTINTOweather(date,city,temp_lo)
VALUES( '1994-11-29','Hayward',54,37);
从文本框读取数据:
COPYweatherFROM '/home/user/weather.txt';

查询表
SELECT*FROMweather;
查询精准的数据列:
SELECTcity,dateFROMweather;
查询使用简单的数学表达式:
SELECTcity,(temp_hi+temp_lo)/ 2AStemp_avg,dateFROMweather;
筛选查询:
SELECT*FROMweatherWHEREcity= 'SanFrancisco'ANDprcp> 0.0;
排序查询:
SELECT*FROMweatherORDERBYcity;
多排序查询:
SELECT*FROMweatherORDERBYcity,temp_lo;
排重性查询:
SELECTDISTINCTcityFROMweather;
排重与排序查询:
SELECTDISTINCTcityFROMweatherORDERBYcity;

多表查询
SELECT*FROMweather,citiesWHEREcity=name;
精准而正确的查询语句:
SELECTweather.city,weather.temp_lo,weather.temp_hi,
weather.prcp,weather.date,cities.location
FROMweather,cities
WHEREcities.name=weather.city;
#大致等于SELECT*FROMweatherINNERJOINcitiesON(weather.city=cities.name);

INNERJOIN使用了内查询,但有时候我们更需要外查询如LEFTOUTERJOIN.
SELECT*FROMweatherLEFTOUTERJOINcitiesON(weather.city=cities.name);
#以wether表作为准则表,以cities作为辅助表,把符合条件的辅助表内容匹配到wether表中导出为结果
自连接,比如查询一个表中较小的两个数据和最大的两个数据:
SELECTW1.city,W1.temp_loASlow,W1.temp_hiAShigh,
W2.city,W2.temp_loASlow,W2.temp_hiAShigh
FROMweatherW1,weatherW2
WHEREW1.temp_lo<W2.temp_lo
ANDW1.temp_hi>W2.temp_hi;
使用别名查询:
SELECT*FROMweatherw,citiescWHEREw.city=c.name;

函数查询
SELECT max(temp_lo)FROMweather;
还有其它的函数,如avg(average),max(maximum),min(minimum)
查询最大值的数据:
SELECTcityFROMweatherWHEREtemp_lo=(SELECT max(temp_lo)FROMweather);
或者使用groupby:SELECTcity,max(temp_lo)FROMweatherGROUPBYcity;
过滤输出结果,用have函数:
SELECTcity,max(temp_lo)FROMweatherGROUPBYcityHAVING max(temp_lo)< 40;
使用like匹配内容:
SELECTcity,max(temp_lo)FROMweatherWHEREcityLIKE 'S%'( 1)GROUPBYcityHAVING max(temp_lo)< 40;
更新
UPDATEweatherSETtemp_hi=temp_hi- 2,temp_lo=temp_lo- 2WHEREdate> '1994-11-28';
删除
DELETEFROMweatherWHEREcity= 'Hayward';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读