PG语法解剖--基本sql语句用法入门

https://www.toutiao.com/i6710897833953722894/

COPY 命令挺好的 需要学习一下. 
原创 波波说运维 2019-07-12 00:02:00

概述

今天主要对PG数据库的一些基本SQL语句用法做个介绍,做个简单了解,也做备忘!

下面主要用例子来说明。


1、建表语句

create table test (id int8 primary key,info text,crt_time timestamp);

注意保留字


2、select into & create table as

postgres=# select * into table new_tbl from pg_class;postgres=# create table tbl_1 as select * from pg_class;


3、插入\更新\删除\查询

insert into tbl (xx,xx) values (xx,xx);update tbl set xx=xx where xxx;delete from tbl where xxx=xxx;select xx from xx where xx...;

如果是delete|update limit,则:

update tbl set xx=xx where ctid = any ( array (select ctid from tbl where xx limit ? for update));delete from tbl where ctid = any ( array (select ctid from tbl where xx limit ? for update));

4、批量DML

insert into xx values (),(),...(); copy xx from stdin; copy xx from 'file'; pg_bulkloadupdate t set info=t1.info,crt_time=t1.crt_time from t1,t2 where (t.id=t1.id) and t1.id=t2.id;update tbl_1 set relname=tmp.rel from (values (1,'test1'),(2,'test2')) tmp (id, rel) where tmp.id=tbl_1.id;delete from t using t1 where t.id=t1.id;delete from tbl_1 using (values (1),(2)) tmp (rel) where tmp.rel=tbl_1.reltype;

注意update , delete 批量操作,JOIN不是一一对应时,更新目标可能会随机匹配。


5、DB端copy+客户端copy

• https://github.com/digoal/blog/blob/master/201805/20180516_03.md

• https://github.com/digoal/blog/blob/master/201805/20180510_01.md

5.1、copy为什么快?

协议:

5.2、DB 端copy

copy tbl to 'file';copy (SQL) to 'file';copy tbl from 'file';

5.3、客户端copy

copy tbl from stdin;copy (SQL) to stdout;copy tbl to stdout;psql (\copy to | from); -- copy协议

6、排序+ offset limit

select * from tbl_1 order by relname nulls first;select * from tbl_1 order by relname nulls last;select * from tbl_1 order by relname;select * from tbl_1 order by relname limit 10 offset 10;select * from tbl_1 order by relname::text collate "C";

7、聚合+解耦合

select string_agg(relname,',' order by xx) from tbl_1; select g,avg(c1) from tbl group by g;


8、distinct

select distinct relname,relnamespace from pg_class;SELECT id, COUNT_DISTINCT(val) FROM test_table GROUP BY 1;select count(distinct (relname,relnamespace)) from pg_class;select distinct on (c3) c2,c3 from tbl;

9、INNER|OUTER JOIN

•inner

select * from t1 join t2 on (t1.x=t2.x) where xxxx;

• left

1)scan filter

select t1.*,t2.* from t1 left join t2 on (t1.x=t2.x) where t1.x=x;

2)join filter

select t1.*,t2.* from t1 left join t2 on (t1.x=t2.x and t1.x=x);

• right

把上面的left join改成right join即可,这里就不多说了。

[转帖]PG语法解剖--基本sql语句用法入门的更多相关文章

  1. sql 语句用法

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 ...

  2. MySQL常见SQL语句用法

    标签(linux): mysql 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 表字段类型 TINYINT 微小整数类型,可存储的容量为1字节 INT 整数类型 ...

  3. python中的 sql语句用法

    函数中应用sql语句def _get_cust_number(self,cr,uid,ids,field_name,args,context=None): res={} for order in se ...

  4. 自己不懂的SQL语句用法

    left  join:是SQL语言中的查询类型,即连接查询.它的全称为左外连接,是外连接的一种. 连接通常可以在select语句的from子句或where子句中建立,其语法格式为: select  c ...

  5. sql语句用法大全

    https://www.w3school.com.cn/sql/sql_in.asp .substr函数格式   (俗称:字符截取函数) 格式1: substr(string string, int ...

  6. 基础SQL语句用法

    1.插入数据:Insert 2.更新数据:update 每行金额增加100 3.删除数据:delete 4.查询:select 1)精确查询 2)模糊查询:like 模糊查询  % 匹配 3)Betw ...

  7. 动态sql语句基本语法--Exec与Exec sp_executesql 的区别

    http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1   :普通SQL语句可以用Exec执行   ...

  8. 数据库基本----SQL语句大全

    学会数据库是很实用D~~记录一些常用的sql语句...有入门有提高有见都没见过的...好全...收藏下...其实一般用的就是查询,插入,删除等语句而已....但学学存储过程是好事...以后数据方面的东 ...

  9. linux之SQL语句简明教程---UNION ALL

    UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起. UNION ALL 和UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复 ...

随机推荐

  1. scrapy框架自定制命令

    写好自己的爬虫项目之后,可以自己定制爬虫运行的命令. 一.单爬虫 在项目的根目录下新建一个py文件,如命名为start.py,写入如下代码: from scrapy.cmdline import ex ...

  2. Linux下vim卡死原因

    使用vim的时候,偶尔会碰到vim莫名其妙的僵在那里. 解决方案: 经查,原来Ctrl+S在Linux里是锁定屏幕的快捷键,如果要解锁,按下Ctrl+Q就可以了. 经验总结: 牢记这两个VIM组合键 ...

  3. elasticsearch集群健康状态查看

    1. 查看ES集群健康状态 http://localhost:9200/_cluster/health?pretty 响应: { "cluster_name" : "if ...

  4. web目录

    Proj app controllers jobs models view user xxx.html init.go conf message public img js css html

  5. Apache Flink - Window

    Window: 在Streaming中,数据是无限且连续的,我们不可能等所有数据都到才进行处理,我们可以来一个就处理一下,但是有时我们需要做一些聚合类的处理,例如:在过去的1分钟内有多少用户点击了我们 ...

  6. Asis_2016_b00ks wp

    目录 程序基本信息 程序漏洞 利用思路 exp脚本 参考 程序基本信息 程序漏洞 有一个读入函数,程序的所有输入都靠它读取,这个程序有个很明显的off_by_one漏洞,在输入时多输入一个0字符. 利 ...

  7. java实现哈夫曼树进行文件加解压

    目录 1.哈夫曼树简述 2.构造树的节点 3.构造哈夫曼树的类(压缩) 4.构造哈夫曼树的类(解压) 5.整体工程文件(包括测试类) 6.结果 7.参考链接 1.哈夫曼树简述 给定n个权值作为n个叶子 ...

  8. HearthBuddy模拟对手的回合

    start calculations, current time: 10:29:48 V2019.09.01.002 Rush 10000 face 27 berserk:1 ets 200 secr ...

  9. MySQL实时监听——EPX 原创: 奕X 贝壳产品技术 今天

    MySQL实时监听——EPX 原创: 奕X 贝壳产品技术 今天

  10. nodejs做中间层,转发请求

    简述node中间层的优势 node中间层,可以解决前端的跨域问题,因为服务器端的请求是不涉及跨域的,跨域是浏览器的同源策略导致的,关于跨域可以查看跨域复习使用node坐中间层,方便前后端分离,后端只需 ...