天天看點

openGauss資料庫-gsql入門

作者:liwt

gsql是GaussDB(for openGauss)提供在指令行下運作的資料庫連接配接工具,可以通過此工具連接配接伺服器并對其進行操作和維護,除了具備操作資料庫的基本功能,gsql還提供了若幹進階特性,便于使用者使用。

基本功能

  • 連接配接資料庫:可以通過gsql遠端連接配接資料庫執行個體。如何使用gsql連接配接資料庫請參考連接配接執行個體。
  • 執行SQL語句:支援互動式地鍵入并執行SQL語句,也可以執行一個檔案中指定的SQL語句。
  • 執行元指令:元指令可以幫助管理者檢視資料庫對象的資訊、查詢緩存區資訊、格式化SQL輸出結果,以及連接配接到新的資料庫等。

使用指導

步驟 1 使用gsql連接配接到GaussDB(for openGauss)執行個體。

gsql工具使用-d參數指定目标資料庫名、-U參數指定資料庫使用者名、-h參數指定主機名、-p參數指定端口号資訊。

說明:

若未指定資料庫名稱,則使用初始化時預設生成的資料庫名稱;若未指定資料庫使用者名,則預設使用目前作業系統使用者作為資料庫使用者名;當某個值沒有前面的參數(-d、-U等)時,若連接配接的指令中沒有指定資料庫名(-d)則該參數會被解釋成資料庫名;如果已經指定資料庫名(-d)而沒有指定資料庫使用者名(-U)時,該參數則會被解釋成資料庫使用者名。

示例,使用jack使用者連接配接到遠端主機postgres資料庫的8000端口。

gsql -h 10.180.123.163 -d postgres -U jack -p 8000

詳細的gsql參數請參見指令參考。

步驟 2 執行SQL語句。

以建立資料庫human_staff為例。

CREATE DATABASE human_staff;

CREATE DATABASE

通常,輸入的指令行在遇到分号的時候結束。如果輸入的指令行沒有錯誤,結果就會輸出到螢幕上。

步驟 3 執行gsql元指令。

以列出GaussDB(for openGauss)中所有的資料庫和描述資訊為例。

postgres=# \l

List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges

----------------+----------+-----------+---------+-------+-----------------------

human_resource | root | SQL_ASCII | C | C |

postgres | root | SQL_ASCII | C | C |

template0 | root | SQL_ASCII | C | C | =c/root +

| | | | | root=CTc/root

template1 | root | SQL_ASCII | C | C | =c/root +

| | | | | root=CTc/root

human_staff | root | SQL_ASCII | C | C |

(5 rows)

更多gsql元指令請參見元指令參考。

示例

以把一個查詢分成多行輸入為例。注意提示符的變化:

postgres=# CREATE TABLE HR.areaS(

postgres(# area_ID NUMBER,

postgres(# area_NAME VARCHAR2(25)

postgres-# )tablespace EXAMPLE;

CREATE TABLE

檢視表的定義:

postgres=# \d HR.areaS

Table "hr.areas"

Column | Type | Modifiers

-----------+-----------------------+-----------

area_id | numeric | not null

area_name | character varying(25) |

向HR.areaS表插入四行資料:

postgres=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (1, 'Europe');

INSERT 0 1

postgres=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (2, 'Americas');

INSERT 0 1

postgres=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (3, 'Asia');

INSERT 0 1

postgres=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (4, 'Middle East and Africa');

INSERT 0 1

切換提示符:

postgres=# \set PROMPT1 '%n@%m %~%R%#'

root@[local] postgres=#

檢視表:

root@[local] postgres=#SELECT * FROM HR.areaS;

area_id | area_name

---------+------------------------

1 | Europe

4 | Middle East and Africa

2 | Americas

3 | Asia

(4 rows)

可以用\pset指令以不同的方法顯示表:

root@[local] postgres=#\pset border 2

Border style is 2.

root@[local] postgres=#SELECT * FROM HR.areaS;

+---------+------------------------+

| area_id | area_name |

+---------+------------------------+

| 1 | Europe |

| 2 | Americas |

| 3 | Asia |

| 4 | Middle East and Africa |

+---------+------------------------+

(4 rows)

root@[local] postgres=#\pset border 0

Border style is 0.

root@[local] postgres=#SELECT * FROM HR.areaS;

area_id area_name

------- ----------------------

1 Europe

2 Americas

3 Asia

4 Middle East and Africa

(4 rows)

使用元指令:

root@[local] postgres=#\a \t \x

Output format is unaligned.

Showing only tuples.

Expanded display is on.

root@[local] postgres=#SELECT * FROM HR.areaS;

area_id|2

area_name|Americas

area_id|1

area_name|Europe

area_id|4

area_name|Middle East and Africa

area_id|3

area_name|Asia

openGauss資料庫-gsql入門

繼續閱讀