[Reprint] PG grammar anatomy - Getting basic sql statement usage

PG grammar anatomy - Getting basic sql statement usage

HTTPS: // www.toutiao.com/i6710897833953722894/ 

COPY command is very good to learn about.

 

Gregg said that the original operation and maintenance 2019-07-12 00:02:00

Outline

Today mainly some basic SQL statements PG database usage to be introduced, to be simple to understand, to do the memo!

The following examples illustrate mainly used.

PG grammar anatomy - Getting basic sql statement usage

 


1, construction of the table statement

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

Note reserved words


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;
PG grammar anatomy - Getting basic sql statement usage

 


3, insert \ update \ delete \ query

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...;

If delete | update limit, then:

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, bulk DML

insert into xx values (),(),...(); 
copy xx from stdin;
copy xx from 'file';
pg_bulkload
update 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;

Note update, delete batch operation, not a one-time JOIN update target may randomly matches.


5, DB + client terminal 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 Why fast?

protocol:

PG grammar anatomy - Getting basic sql statement usage

 

5.2, DB-side copy

copy tbl to 'file';
copy (SQL) to 'file';
copy tbl from 'file';
PG grammar anatomy - Getting basic sql statement usage

 

5.3 client copy

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

6, sorting + 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, the polymerization decoupled +

select string_agg(relname,',' order by xx) from tbl_1; 
select g,avg(c1) from tbl group by g;
PG grammar anatomy - Getting basic sql statement usage

 


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

The top left join can be changed right join, there is not much to say.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11621358.html