update/delete .. order by .. limit ..:
coalesce:
array:
按时间进行分区的分区表demo:
pg_sleep(seconds):让session睡眠多久。
EXTRACT的使用方法:
SELECT EXTRACT(YEAR FROM INTIME) FROM RK 从INTIME字段中提取年份 SELECT EXTRACT(MONTH FROM INTIME) FROM RK 从INTIME字段中提取月份 SELECT EXTRACT(DAY FROM INTIME) FROM RK 从INTIME字段中提取日 SELECT EXTRACT(HOUR FROM INTIME) FROM RK 从INTIME字段中提取时 SELECT EXTRACT(MINUTE FROM INTIME) FROM RK 从INTIME字段中提取分 SELECT EXTRACT(SECOND FROM INTIME) FROM RK 从INTIME字段中提取秒
select...into.../create table...as select...
创建一个表从语句中:
CREATE
TABLE
weather_temp (
LIKE
weather INCLUDING CONSTRAINTS);(
http://www.postgresql.org/docs/9.3/static/sql-createtable.html
)
digoal=# \z Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+----------------------+-------+-------------------+-------------------------- public | aa | table | | public | bb | table | | public | capitals | table | | public | cities | table | | public | foo | table | | public | hhj | table | | public | measurement | table | | public | measurement_y2005m02 | table | | public | measurement_y2006m02 | table | | public | measurement_y2006m03 | table | | public | measurement_y2007m12 | table | | public | vvc | table | | public | xxv | table | | (13 rows)
digoal=# \d+ foo Table "public.foo" Column | Type | Modifiers | Storage | Stats target | Description ----------+---------+-----------+----------+--------------+------------- fooid | integer | | plain | | foosubid | integer | | plain | | fooname | text | | extended | |
Indexes: "fooid_pkey" PRIMARY KEY, btree (fooid) Has OIDs: no
digoal=# create table nna as select * from foo;
SELECT 2
digoal=# select * into aax2 from foo ;
SELECT 2digoal=# \z Access privileges Schema | Name | Type | Access privileges | Column access privileges --------+----------------------+-------+-------------------+-------------------------- public | aa | table | | public | aax2 | table | | public | bb | table | | public | capitals | table | | public | cities | table | | public | foo | table | | public | hhj | table | | public | measurement | table | | public | measurement_y2005m02 | table | | public | measurement_y2006m02 | table | | public | measurement_y2006m03 | table | | public | measurement_y2007m12 | table | | public | nna | table | | public | vvc | table | | public | xxv | table | | (15 rows)
digoal=# \d+ aax2 Table "public.aax2" Column | Type | Modifiers | Storage | Stats target | Description ----------+---------+-----------+----------+--------------+------------- fooid | integer | | plain | | foosubid | integer | | plain | | fooname | text | | extended | | Has OIDs: no digoal=# \d+ nna Table "public.nna" Column | Type | Modifiers | Storage | Stats target | Description ----------+---------+-----------+----------+--------------+------------- fooid | integer | | plain | | foosubid | integer | | plain | | fooname | text | | extended | | Has OIDs: no
select into ... * form ...
postgres=# \d+ testtb
Table "public.testtb"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+----------+--------------+-------------
id | integer | | plain | |
info | text | | extended | |
Indexes:
"idx_1" btree (id)
Has OIDs: no
postgres=# CREATE OR REPLACE FUNCTION tx() RETURNS text AS
postgres-# $BODY$
postgres$# DECLARE
postgres$# re RECORD;
postgres$# begin
postgres$# SELECT INTO re * FROM testtb WHERE id=3;
postgres$# return re.info;
postgres$# END
postgres$# $BODY$
postgres-# LANGUAGE plpgsql;
postgres=# update testtb setinfo='llllllll' where id=3;
postgres=# select tx();
tx
----------
llllllll
(1 row)
rowtype:
添加新列:
删除重复行:
改变字符编码:
SELECT convert(‘foobar_utf8′,’UTF8′,’LATIN1′); #Converts foobar from utf8 to latin1. SELECT convert_from(‘foobar_utf8′,’LATIN1′); #Converts foobar to latin1. SELECT convert_to(‘foobar’,'UTF8′); #Converts foobar to utf8. SELECT to_ascii(‘foobar’,'LATIN1′); #Converts foobar to latin1.
一些PG的小技巧:
digoal=# select 'abc' ~ '(b|d)'; ?column? ---------- t (1 row)
regexp_matches:匹配字符串digoal=# select substring('foobar' from 'o(.)b(a)') ; substring ----------- o (1 row)
digoal=# SELECT regexp_matches('foobarbequebaz', '(bar)(beque)');--返回一个 text[] regexp_matches ---------------- {bar,beque} (1 row)
digoal=# select pg_typeof(regexp_matches('foobarbequebaz', '(bar)(beque)')); pg_typeof ----------- text[] (1 row)
digoal=# select 'bar'= any(regexp_matches('foobarbequebaz', '(bar)(beque)')); ERROR: op ANY/ALL (array) does not support set arguments digoal=# select 'bar'= any(t) from regexp_matches('foobarbequebaz', '(bar)(beque)') as t; ?column? ---------- t (1 row)
regexp_split_to_table 字符串切割(通过固定字符):digoal=# select regexp_replace('foobarbaz', 'b..', 'X'); regexp_replace ---------------- fooXbaz (1 row)
切割成一个array:digoal=# SELECT foo FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', E'\\s+') AS foo; foo ------- the quick brown fox jumps over the lazy dog (9 rows)
digoal=# SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', E'\\s+'); regexp_split_to_array ----------------------------------------------- {the,quick,brown,fox,jumps,over,the,lazy,dog} (1 row)
postgres=# select * from ct;
id | rowid | attribute | value
----+-------+-----------+-------
1 | test1 | att1 | val1
2 | test1 | att2 | val2
3 | test1 | att3 | val3
4 | test1 | att4 | val4
5 | test2 | att1 | val5
6 | test2 | att2 | val6
7 | test2 | att3 | val7
8 | test2 | att4 | val8
(8 rows)
postgres=# select string_agg(attribute,',') from ct;
string_agg
-----------------------------------------
att1,att2,att3,att4,att1,att2,att3,att4
(1 row)
cast:
copy:
copy...from...: 拷贝线下文件到表中。此表必须先建立(空表即可)。注意字段间的分隔号。digoal=# copy foo to '99909'; ERROR: relative path not allowed for COPY to file digoal=# copy foo to '/home/pg93/ms/master/99909'; COPY 5
44404文本的内容如下:[pg93@localhost master]$ pwd /home/pg93/ms/master [pg93@localhost master]$ vi 44404
23432,234245,asdfg
68967,23452,asfergwet
1343245,242524,agtrgewt
digoal=# create table ttbv (a int, b int, c text); CREATE TABLE digoal=# copy ttbv from '/home/pg93/ms/master/44404' with delimiter ','; COPY 3