1. 程式人生 > 實用技巧 >PostgreSQL資料庫的sql語法(整理版)

PostgreSQL資料庫的sql語法(整理版)

1.概述

  PostgreSQL是一個免費的關係型資料庫伺服器(ORDBMS)

2.登入資料庫

  以使用者的名義登入資料庫,這時使用的是psql命令

psql -h 127.0.0.1 -U dbuser -p 5832 -d database

  上面的命令的引數含義如下:

  • -h指定伺服器
  • -p指定埠
  • -U指定使用者
  • -d指定資料庫

  輸入上面的命令以後,系統會提示輸入dbuser使用者的密碼。輸入正確,就可以登入控制檯了。

3.控制檯命令

操作 命令

設定密碼

\password dbuser
退出控制檯 \q
檢視SQL命令的解釋 \hcommand ,如\h select
檢視psql命令列表 \?
列出所有資料庫 \l
進入其他資料庫 \c [database_name]
列出當前資料庫的所有表格 \d
列出某一張表的結構 \d [table_name]
列出所有使用者 \du
開啟文字編輯器 \e
列出當前資料庫和連線的資訊 \conninfo

4.PSQL資料庫操作命令

操作 命令
建立資料庫 create database [db_name];
刪除資料庫 drop database [db_name];
查詢所有資料庫 select datname from PG_DATABASE; /select * from PG_DATABASE;
查詢某一資料庫的所有表 select table_name FROM information_schema.tables where table_schema = 'public';
建立表

create table if not exists ke_p_role (
  id serial primary key, #唯一值,遞增
  name varchar (64) not null, #字串64位
  seq smallint not null, #最小int型
  description varchar(128) not null #字串128位
)

插入表資料 insert into ke_p_role values ('1', 'Administrator', '1', 'Have all permissions'), ('2', 'Devs', '2', 'Own add or delete'), ('3', 'Tourist', '3', 'Only viewer')
根據欄位,不存在則插入,存在則更新

create table if not exists ke_topic_rank (
  cluster varchar(64),
  topic varchar(128),
  tkey varchar(128),
  tvalue bigint,
  primary key (cluster, topic, tkey)
)

insert into ke_topic_rank values ('test1','test2','test3', 7) on conflict (cluster,topic,tkey) do update set tvalue=excluded.tvalue;

查詢記錄 select * from user_tbl;
查詢記錄,帶limit和offset偏移量 select * from ke_consumer_group_summary where cluster='cluster1' limit 2 offset 0;
遞迴查詢

with recursive summary as (
  (select topic, diffval, timespan from ke_logsize where diffval > 0 order by topic asc, timespan desc limit 1)
  union all
  select u.* from summary s, lateral( select topic,diffval,timespan from ke_logsize where diffval > 0 and topic > s.topic order by topic asc, timespan desc limit 1) u
)
select topic, diffval, timespan, '1' as rownum, '1' as rank from summary;

查詢,coalesce判斷是否為空

如果a.logsize為空,則返回右邊的0

select coalesce(sum(a.logsize),0) from (select logsize from ke_logsize where cluster='cluster1' and topic in ('phone') and tm='20200615' order by timespan desc limit 1) a

查詢,ifnull,為空則返回0

select ifnull(lag,0) from ke_consumer_bscreen where cluster='cluster1' and tm='20200713' and "group"='consumer' and topic='topic1' order by timespan desc limit 1

更新資料 update user_tblset name = '李四' where name = '張三';
刪除資料 delete fromuser_tbl where name = '李四' ;
新增表字段 alter tableuser_tbl add email varchar(40);
更新表字段 alter tableuser_tbl alter column signupdate set not null;
重命名錶欄位 alter tableuser_tbl rename column signupdate to signup;
刪除表字段 alter table user_tbl drop column email;
重命名錶名 alter tableuser_tbl rename to backup_tbl;
刪除表名 drop table if existsbackup_tbl;
清空表資料 truncate table ke_alarm_config;
查詢postgres當前的連線數 select * from pg_stat_activity;
postgres的最大連線數 show max_connections;

5.總結

【參考資料】

https://pg.sjk66.com/postgresql/create-table.htmlPostgreSQL 建立表CREATE TABLE

https://www.alibabacloud.com/help/zh/doc-detail/52951.htmPostgreSQL UPSERT的功能與用法

https://blog.csdn.net/u011402596/article/details/38510547postgresql的show databases、show tables、describe table操作

https://yanbin.blog/postgresql-unnest-batch-crud-merge/PostgreSQL 批量插入, 更新和合並操作

https://blog.csdn.net/qq_43639296/article/details/90667860postgresql中類似IFNULL用法

https://www.cnblogs.com/Paul-watermelon/p/10401344.htmlPostgreSQL入門教程(命令列)