PostgreSQL簡介以及簡單使用


1.簡介

PostgreSQL 是一個免費的對象-關系數據庫服務器(ORDBMS),在靈活的BSD許可證下發行。

PostgreSQL 開發者把它念作 post-gress-Q-L。

PostgreSQL 的 Slogan 是 "世界上最先進的開源關系型數據庫"。

 

關於與mysql的區別參考:https://www.cnblogs.com/geekmao/p/8541817.html

2. 安裝

1. 下載安裝包:這里

 2. 安裝

 直接下一步即可,需要注意:

(1) postgresql的默認賬號是postgres

(2)postgresql的默認端口是5432

3.簡單使用

 1. 登錄

psql -U postgres

2.查看存在的庫

\l

3.創建數據庫並且進入以及查看

postgres=# CREATE DATABASE test;
CREATE DATABASE
postgres=# \c test;
您現在已經連接到數據庫 "test",用戶 "postgres".

4.創建表以及查看信息

test-# CREATE TABLE DEPARTMENT(
test(#    ID INT PRIMARY KEY      NOT NULL,
test(#    DEPT           CHAR(50) NOT NULL,
test(#    EMP_ID         INT      NOT NULL
test(# );
錯誤:  語法錯誤 在 "CREATE" 或附近的
第2行CREATE TABLE DEPARTMENT(
     ^
test=# CREATE TABLE DEPARTMENT(
test(#    ID INT PRIMARY KEY      NOT NULL,
test(#    DEPT           CHAR(50) NOT NULL,
test(#    EMP_ID         INT      NOT NULL
test(# );
CREATE TABLE
test=# \d
                 關聯列表
 架構模式 |    名稱    |  類型  |  擁有者
----------+------------+--------+----------
 public   | department | 數據表 | postgres
(1 行記錄)

test=# \d department
               數據表 "public.department"
  欄位  |     類型      | Collation | Nullable | Default
--------+---------------+-----------+----------+---------
 id     | integer       |           | not null |
 dept   | character(50) |           | not null |
 emp_id | integer       |           | not null |
索引:
    "department_pkey" PRIMARY KEY, btree (id)

5. 插入一條數據以及搜索

test=# INSERT INTO department values(1, '測試', 1);
INSERT 0 1
test=# select * from department;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  1 | 測試                                                 |      1
(1 行記錄)

6. 在插入一條進行分頁查詢(limit是取的行數,offset是偏移量,從0開始且包含offset指定的下標值)

test=# INSERT INTO department values(2, '測試2', 2);
INSERT 0 1
test=# select * from department limit 1 offset 1;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  2 | 測試2                                                |      2
(1 行記錄)

7. explain簡單分析(列出的信息少於mysql的explain分析)

test=# explain select * from department where id = 1;
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Index Scan using department_pkey on department  (cost=0.15..8.17 rows=1 width=212)
   Index Cond: (id = 1)
(2 行記錄)

 

  其他事務類似於mysql,鎖也有共享鎖和排它鎖。也有JSON類型的字段,mysql新版本(應該是5.7開始)也是支持JSON類型的字段。也有全文索引的功能,mysql5.7之后也支持全文索引。

  只是公司用到了postgresql,所以簡單的使用下postgresql。

 

補充:postgreSQL的schema模式

一個模式可以包含視圖、索引、據類型、函數和操作符等。

相同的對象名稱可以被用於不同的模式中而不會出現沖突,例如 schema1 和 myschema 都可以包含名為 mytable 的表。

使用模式的優勢:

(1)允許多個用戶使用一個數據庫並且不會互相干擾。

(2)將數據庫對象組織成邏輯組以便更容易管理。

(3)第三方應用的對象可以放在獨立的模式中,這樣它們就不會與其他對象的名稱發生沖突。

模式類似於操作系統層的目錄,但是模式不能嵌套。

postgres=# create schema myschema;  #創建模式
CREATE SCHEMA
postgres=# create table myschema.company(    #創建表且指定模式
postgres(#    ID   INT              NOT NULL,
postgres(#    NAME VARCHAR (20)     NOT NULL,
postgres(#    AGE  INT              NOT NULL,
postgres(#    ADDRESS  CHAR (25),
postgres(#    SALARY   DECIMAL (18, 2),
postgres(#    PRIMARY KEY (ID)
postgres(# );
CREATE TABLE
postgres=# select * from myschema.company;    #查詢指定模式的表
 id | name | age | address | salary
----+------+-----+---------+--------
(0 行記錄)


postgres=# DROP SCHEMA myschema CASCADE;    刪除模式級聯刪除數據
注意:  遞歸刪除 表 myschema.company
DROP SCHEMA

  默認的schema是public模式。

 

補充:PG索引用法

PG創建索引:
CREATE INDEX index_name ON table_name(colname);
刪除索引:
DROP INDEX index_name;
查看索引:
select * from pg_indexes where tablename='tableName';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM