MySQL 前景好还是 PgSQL 前景好?一直是一个争论的话题。无论后面谁的市场占有率高,对我们来说,都学学也是不亏的。这节内容就让我们一起走进 PgSQL 的世界。
1 安装
配置 yum 源
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安装
yum install -y postgresql14-server
2 建库表
创建 DB
create database martin;
删库
drop database martin;
选择数据库(刚才删的库需要建回来)
\c martin
建表:
create table aaa(id int primary key not null,name char(50),age int,add char(50),score int);
查询所有表
\d
可以查看到刚才创建的表:
3 增删查改
写入数据:
insert into aaa values (1,'a',1,'a',90);
查询数据:
select * from aaa;
带条件的查询:
select * from aaa where id=1;
更新:
update aaa set name='martin' where id=1;
删除:
delete from aaa where id=1;
如果需要清空表,可以使用:
truncate table aaa;
4 limit/排序/分组
为了方便后面实验,我们再造一些数据:
insert into aaa values (1,'a',1,'a',90),(2,'b',2,'b',80),(3,'c',3,'b',92);
limit
select * from aaa limit 1;
正序排序:
select * from aaa order by score;
倒序排序:
select * from aaa order by score desc;
分组
select add,avg(score) from aaa group by add;
今天的内容就到这里,给大家推荐几个优质的公众号:

文章转载自悦专栏,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




