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. 2018-2019 ACM-ICPC, Asia Dhaka Regional Contest

    目录 Contest Info Solutions B. Counting Inversion C. Divisors of the Divisors of An Integer E. Helping ...

  2. 爬虫(十四):scrapy下载中间件

    下载器中间件是介于Scrapy的request/response处理的钩子框架,是用于全局修改Scrapy request和response的一个轻量.底层的系统. 激活Downloader Midd ...

  3. Linux 下Mongdb数据库

    一.安装mongdb 1.创建安装目录 # mkdir /data/local # mkdir /data/local/mongodbdata 2.解压安装包 # tar -xvf /software ...

  4. 二分算法题目训练(一)——Shell Pyramid详解

    HDU2446——Shell Pyramid 详解 Shell Pyramid 题目描述(Google 翻译的) 在17世纪,由于雷鸣般的喧嚣,浓烟和炽热的火焰,海上的战斗与现代战争一样.但那时,大炮 ...

  5. SpatialHadoop的编译与运行

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/spatialhadoop_compile_and_run Spa ...

  6. ansible 主机正则

    ansible <pattern> -m <module_name> -a <arguments> 该功能主要针对Inventory的主机列表,案例如下: 1.AL ...

  7. 使用spring profile实现多环境切换

    第一步: applicationContext.xml <!--环境配置开始--> <beans profile="production"> <con ...

  8. MIPS 指令集(共31条)

    MIPS 指令集(共31条) MIPS 指令集(共31条) 助记符 指令格式 示例 示例含义 操作及其解释 Bit # 31..26 25..21 20..16 15..11 10..6 5..0 R ...

  9. Vue项目中的http请求统一管理

    module.exports = { dev: { // Paths assetsSubDirectory: '/', assetsPublicPath: '/', proxyTable: { /op ...

  10. 数据库连接池Flask-SQLAlchemy中多线程安全的问题

    使用flask-sqlalchemy写代码码到一半,突然想到,Session是否是线程安全的?于是上官方文档,答案是否! 那问题来了,怎么破?因为它会牵涉到多线程情况下,调用rollback导致的不可 ...