postgresql命令操作

1.連接數據庫
psql -h Server -p Port -U Username DatabaseName

2.創建數據庫
postgres=# create database testdb;

3.查看數據庫
postgres=# \l

4.刪除數據庫
postgres=# drop database testdb;

5.進入數據庫
postgres=# \c testdb;

6.列出當前庫所有表
testdb=# \dt

7.創建表
testdb=# create table account(
testdb(# user_id serial primary key,
testdb(# username varchar(50) unique not null,
testdb(# password varchar(50) not null);

create table products (
testdb(# product_no integer,
testdb(# name varchar(20),
testdb(# price numeric);

create table account (
username varchar(50) unique not null,
count integer);

create table company(
ID int primary key not null,
name TEXT not null,
age int not null,
address char(50),
salary real
);

create table audit(
emp_id int not null,
entry_date text not null
);

8.刪除表
testdb=# drop table account;

9.創建模式
testdb=# CREATE SCHEMA myschema;

10.指定模式創建表
create table myschema.mytable(
user_id serial primary key,
username varchar(50) unique not null,
password varchar(50) not null);

11.刪除模式裏的對象
drop schema myschema CASCADE;

12.刪除模式
drop schema myschema

13.插入數據
testdb=# insert into products values (1, 'chiness', 9.99);

14.查詢數據
testdb=# select * from products;

15.更新數據
testdb=# update products set name = 'hyh' where product_no = 1;

16.刪除表數據
testdb=# delete from products where name = 'hyh'; 字符串必須單引號

17.order by 升序,降序排列
testdb=# select from products order by price asc;
testdb=# select
from products order by price desc;

18.order by 多列排序
select * from products order by price,product_no asc;

19.group by分組
testdb=# select name, sum(price) from products group by name;
按名字分組統計每個名字下的價格總額

20.HAVING子句與GROUP BY子句組合使用,用於選擇函數結果滿足某些條件的特定行
testdb=# select name from products group by name having name != 'hyh';

21.條件查詢
AND 條件
testdb=# select * from products where name = 'hyh' and price = 10;

OR 條件
testdb=# select * from products where name = 'hyh' or price = 10;

AND & OR 條件
testdb=# select * from products where name = 'hyh' and price = 10 or product_no = 2;

NOT 條件
testdb=# select from products where name is not null;
testdb=# select
from products where name not in ('hyh','chiness');

LIKE 條件
testdb=# select * from products where name like '%ch%';

IN 條件
testdb=# select * from products where price in (13,15);

NOT IN 條件
testdb=# select * from products where name not in ('hyh','chiness');

BETWEEN 條件
testdb=# select * from products where price between 10 and 15;

22.連接
內連接(INNER JOIN)
testdb=# select products.name,account.count from products inner join account on products.name = account.username;

左外連接(LEFT OUTER JOIN)
左外連接返回從“ON”條件中指定的左側表中的所有行,只返回滿足條件的另一個表中的行
testdb=# select products.name,account.username,account.count from products left outer join account on products.price = account.count;

右外連接(RIGHT OUTER JOIN)
右外連接返回從“ON”條件中指定的右側表中的所有行,只返回滿足條件的另一個表中的行
testdb=# select products.name,account.count from products right outer join account on products.name = account.username;

全連接(FULL OUTER JOIN)跨連接(CROSS JOIN)
全外連接從左表和右表中返回所有行。 它將NULL置於不滿足連接條件的位置
testdb=# select products.name,account.count from products full outer join account on products.name = account.username;

23.創建函數
create or replace function totalRecords()
returns integer as $total$
declare
total integer;
begin
select count(*) into total from products;
return total;
end;
$total$ language plpgsql;

24.調用函數
select totalRecords();

25.觸發器

PostgreSQL觸發器是一組動作或數據庫回調函數,它們在指定的表上執行指定的數據庫事件(即,INSERT,UPDATE,DELETE或TRUNCATE語句)時自動運行。 觸發器用於驗證輸入數據,執行業務規則,保持審計跟蹤等
1.PostgreSQL在以下情況下執行/調用觸發器:在嘗試操作之前(在檢查約束並嘗試INSERT, UPDATE或DELETE之前)。或者在操作完成後(在檢查約束並且INSERT,UPDATE或DELETE完成後)。或者不是操作(在視圖中INSERT,UPDATE或DELETE的情況下)
2.對於操作修改的每一行,都會調用一個標記爲FOR EACH ROWS的觸發器。 另一方面,標記爲FOR EACH STATEMENT的觸發器只對任何給定的操作執行一次,而不管它修改多少行。
3.您可以爲同一事件定義同一類型的多個觸發器,但條件是按名稱按字母順序觸發。
4.當與它們相關聯的表被刪除時,觸發器被自動刪除

例子:
    創建兩個表
    create table company(
    ID int primary key not null,
    name TEXT not null,
    age int not null,
    address char(50),
    salary real
   );

create table audit(
    emp_id int not null,
    entry_date text not null
);

    創建函數
    create or replace function auditlogfunc() returns trigger as $example_table$
begin
    insert into audit(emp_id, entry_date) values (new.id,current_timestamp);
    return new;
end;
$example_table$ language plpgsql;

    創建觸發器
    create trigger example_trigger after insert on company
for each row execute procedure auditlogfunc();

    insert into company values(1,'小米科技',2,'北京清河',1234);
    #向company插入數據後,自動觸發向audit表插入數據

26.索引
索引是用於加速從數據庫檢索數據的特殊查找表。數據庫索引類似於書的索引(目錄)。 索引爲出現在索引列中的每個值創建一個條目
數據庫索引的重要特點:
索引使用SELECT查詢和WHERE子句加速數據輸出,但是會減慢使用INSERT和UPDATE語句輸入的數據
您可以在不影響數據的情況下創建或刪除索引
可以通過使用CREATE INDEX語句創建索引,指定創建索引的索引名稱和表或列名稱
還可以創建一個唯一索引,類似於唯一約束,該索引防止列或列的組合上有一個索引重複的索引

索引類型PostgreSQL中有幾種索引類型,如B-tree,Hash,GiST,SP-GiST和GIN等。每種索引類型根據不同的查詢使用不同的算法。 默認情況下,CREATE INDEX命令使用B樹索引

在products表name字段創建索引(單列索引)
create index products_index on products (name);

多列索引
create index account_index on account (username,count);

唯一索引,不允許表中出現重複的值
create unique index account_index on account (username);

刪除索引
drop index account_index;

以下情況避免使用索引
應該避免在小表上使用索引。
不要爲具有頻繁,大批量更新或插入操作的表創建索引。
索引不應用於包含大量NULL值的列。
不要在經常操作(修改)的列上創建索引
  1. 查詢表結構
    \d products;

28.union子句
PostgreSQL UNION子句/運算符用於組合兩個或多個SELECT語句的結果,而不返回任何重複的行。
要使用UNION,每個SELECT必須具有相同的列數,相同數量的列表達式,相同的數據類型,並且具有相同的順序,但不一定要相同

SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID
UNION
SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

union all 子句
SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID
UNION ALL
SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;

29.alter table
PostgreSQL ALTER TABLE命令用於添加,刪除或修改現有表中的列。您還可以使用ALTER TABLE命令在現有表上添加和刪除各種約束

增加字段
alter table company add gender char(1);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章