[转帖]PG语法解剖--基本sql语句用法入门
PG语法解剖--基本sql语句用法入门
https://www.toutiao.com/i6710897833953722894/ COPY 命令挺好的 需要学习一下.
概述
今天主要对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语句用法入门的更多相关文章
- sql 语句用法
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 ...
- MySQL常见SQL语句用法
标签(linux): mysql 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 表字段类型 TINYINT 微小整数类型,可存储的容量为1字节 INT 整数类型 ...
- python中的 sql语句用法
函数中应用sql语句def _get_cust_number(self,cr,uid,ids,field_name,args,context=None): res={} for order in se ...
- 自己不懂的SQL语句用法
left join:是SQL语言中的查询类型,即连接查询.它的全称为左外连接,是外连接的一种. 连接通常可以在select语句的from子句或where子句中建立,其语法格式为: select c ...
- sql语句用法大全
https://www.w3school.com.cn/sql/sql_in.asp .substr函数格式 (俗称:字符截取函数) 格式1: substr(string string, int ...
- 基础SQL语句用法
1.插入数据:Insert 2.更新数据:update 每行金额增加100 3.删除数据:delete 4.查询:select 1)精确查询 2)模糊查询:like 模糊查询 % 匹配 3)Betw ...
- 动态sql语句基本语法--Exec与Exec sp_executesql 的区别
http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1 :普通SQL语句可以用Exec执行 ...
- 数据库基本----SQL语句大全
学会数据库是很实用D~~记录一些常用的sql语句...有入门有提高有见都没见过的...好全...收藏下...其实一般用的就是查询,插入,删除等语句而已....但学学存储过程是好事...以后数据方面的东 ...
- linux之SQL语句简明教程---UNION ALL
UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起. UNION ALL 和UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复 ...
随机推荐
- python3 中的bytes类型
- TensorFlow(十):卷积神经网络实现手写数字识别以及可视化
上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = inpu ...
- 欢迎使用CSDN的markdown编辑器
以下是蒻鞫第一次打开CSDN-markdown编译器的温馨提示,感觉CSDN好贴心,不作任何用途,仅为纪念,若存在违法侵权行为,请联系留言,立即删除. List item 这里写 欢迎使用Markdo ...
- springboot开启gzip压缩
springboot 2.x开启gzip压缩 1.application.yml配置 server: compression: enabled: true min-response-size: mim ...
- RK3288 修改ddr频率
转载请注明出处:https://www.cnblogs.com/lialong1st/p/10912334.html CPU:RK3288 系统:Android 5.1 RK3288 的 ddr 频率 ...
- 阿里云OSS的 存储包、下行流量包、回流流量包 三者有啥关系
阿里云OSS的 存储包.下行流量包.回流流量包 三者有啥关系 一.总结 一句话总结: 你把文件放 oss,会占用存储空间,存储包覆盖这部分费用 你访问存储在 oss 里面的文件,会产生下行流量,就是从 ...
- 安卓APP在线升级
安卓APP在线升级 通过IDHTTP组件在线下载APP到手机中,然后自动安装这个APP程序. 1)在线下载APP程序 需引用单元: {$IFDEF ANDROID} FMX.Helpers.Andro ...
- sql查询表的所有字段和表字段对应的类型
1.查询表的所有字段 select syscolumns.name from syscolumns where id=object_id('写上要查询的表名') 2.查询表的所有字段+表字段对应的类型 ...
- C#中 Dictionary<>的使用及注意事项
1,如果在主体代码中使用,直接在初始化中生成就行 2如果在其他层,比如逻辑层,要注意在事件内部定义,在外部的话,重复调用就会提示“”“已经定义了相同的KEY”,见例子 (例子是转的) Dictiona ...
- java说明文档制作
A:对工具类加入文档注释 B:通过javadoc命令生成说明书 * @author(提取作者内容) * @version(提取版本内容) * javadoc -d 指定的文件目录 -author -v ...