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. P2891 [USACO07OPEN]吃饭Dining

    漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...

  2. linux系列(四):mkdir命令

    1.命令格式: mkdir [选项] 目录名 2.命令功能: 通过 mkdir 命令可以实现在指定位置创建以 DirName(指定的文件名)命名的文件夹或目录.要创建文件夹或目录的用户必须对所创建的文 ...

  3. ZR#997

    ZR#997 解法: 找找规律就出来了,全场最简单的一道题. CODE: #include<iostream> #include<cstdio> #include<cst ...

  4. Go --- 七牛云 上传文件 & Token demo

    package main import ( "bytes" "crypto/hmac" "crypto/sha1" "encodi ...

  5. OpenJudge计算概论-自整除数

    /*=================================================== 自整除数 总时间限制: 1000ms 内存限制: 65536kB 描述 对一个整数n,如果其 ...

  6. 在 Java 应用程序中加一些 Groovy 进来

    如果您一直在阅读这个系列,那么您应该已经看到有各种各样使用 Groovy 的有趣方式,Groovy 的主要优势之一就是它的生产力.Groovy 代码通常要比 Java 代码更容易编写,而且编写起来也更 ...

  7. sklearn里计算roc_auc_score,报错ValueError: bad input shape

    用sklearn的DecisionTreeClassifer训练模型,然后用roc_auc_score计算模型的auc.代码如下 clf = DecisionTreeClassifier(criter ...

  8. CardView 简介和使用

    CardView 简介 本文链接:https://blog.csdn.net/ShawnXiaFei/article/details/81568537CardView 是 Google 官方发布 MD ...

  9. 创建Bitmap之Bitmap静态方法使用示例

    package com.loaderman.customviewdemo; import android.app.Activity; import android.content.Intent; im ...

  10. ARM 之一 ELF文件、镜像(Image)文件、可执行文件、对象文件 详解

    [转]https://blog.csdn.net/ZCShouCSDN/article/details/100048461 ELF 文件规范   ELF(Executable and Linking ...