1. 程式人生 > >PostgreSql 常用語句整理

PostgreSql 常用語句整理

一.常用語句

1.修改為postgresql使用者:  su - postgres

2.登入資料庫:    psql -p port -U username -d dbname

3.退出:  \q

4.檢視正在執行的語句:

    select * from pg_stat_activity;

5.殺死某個pid:

    SELECT pg_terminate_backend(pid);

6.批量殺死pid:

    SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state='idle';

7.B表資料匯入A表,A表存在:

    INSERT INTO table_a (欄位1, 欄位2, ...) SELECT 欄位1, 欄位2, ... FROM table_b;

8.B表資料匯入A表,A表不存在:

    select * into  table_a from table_b;

9. 多表關聯更新資料:

更新table_a中的code欄位,table_a關聯table_b,code欄位出自table_c表

update table_a as a set code = tmp.code
from (
      select b.id, c.code 
      from table_b as b 
      inner join table_c as c on b.id = c.id
     ) as tmp
where a.id = tmp.id 

10.存在則更新,不存在則插入:

INSERT INTO test_postgre(id,name,,age,InputTime)
VALUES('1','postgre','24','2018-01-10 22:00:00')
ON conflict(id) 
DO UPDATE SET name = 'postgreOk', InputTime ='2018-02-22 12:00:00'

注:id必須是唯一約束; 

 二.修改表常用語句

1.修改表名:

ALTER TABLE table_name RENAME TO new_name;  

2.修改列名:

ALTER TABLE table_name RENAME column_name to new_column_name;  

3.增加列:

ALTER TABLE table_name ADD column_name datatype; 

4.刪除列:

ALTER TABLE table_name DROP  column_name;  

 5.修改列資料型別:

ALTER TABLE table_name ALTER  column_name TYPE datatype; 

 6. 新增唯一約束:

alter table table_a add constraint unique_table_a_id unique(id);

注:必須保證id不重複 

7.新增主鍵:

alter table table_a add primary key(id);

三.常用字串函式 

1.position(substring in string):判斷string是否包含substring,包含返回大於0的位置,不包含返回0。

2.replace(string1,string2,string3):替換字串,將string1中的string2替換成string3。

3.btrim(string string1 [, characters string2]):刪除string1兩側的string2。

4.rtrim(string string1 [, characters string2]):刪除string1右側的string2。

5.ltrim(string string1 [, characters string2]):刪除string1左側的string2。

6.concat(str string1 [, str string2 [, ...] ]):拼接引數,個數不限,型別不限。

7.concat_ws(sep sep1, str string1 [,str string2 [, ...] ]):以sep1分隔符,拼接引數。

8.left(str string1, int n):返回string1前n個字元。

9.right(str string1,  int n):返回string1後n個字元。

10.substr(string string1, from [, count]):從from開始擷取string1字串共擷取count長度。

四.常用數值函式

1.ceil(num):取整數,取大值,例:ceil(35.7)=36。

2.floor(num):取整數,取小值,例:floor(35.7)=35。

3.round(numeric,int):四捨五入.後面的值為精度,如果後面的精度沒有則取整,例:round(36.567,2)=36.57。

4.trunc(numeric,int):截斷,後面的為精度,如果不設定精度則為取整,類似於floor,例:trunc(32.567,2)=32.56。

5.random():0.0到1.0之間的隨機數。

五.常用日期函式

1.age(timestamp1,timestamp2):返回timestamp1減去timestamp2時間差,timestamp1為空則為當前時間。

2.now():等同於current_timestamp,當前事務的開啟時間戳.在一個事務內時間保持不變。

3.date_trunc(text,timestamp):截斷指定精度,例:

select date_trunc('day',current_timestamp),date_trunc('hour',current_timestamp);
      date_trunc      |      date_trunc      
------------------------+------------------------
2018-09-16 00:00:00+08 | 2018-09-16 11:00:00+08

4.date_part(text,timestamp):獲取時間子域,例:

select date_part('hour',now()),date_part('minute',now()),date_part('month',now());
date_part | date_part | date_part 
-----------+-----------+-----------
        11 |        10 |        9

注:date_part函式常用引數:

  • year:年
  • quarter:第幾季度
  • month:月
  • week:第幾個星期
  • dow:星期幾,0是星期天
  • day:本月第幾天
  • doy:本年第幾天
  • hour:得到時間中的小時
  • minute:得到時間中的分鐘
  • second:得到時間中的秒
  • epoch:相對於1970-01-01 00:00:00以來的時間