openGauss每日一练第2天 | 学习openGauss客户端工具gsql的使用

一、学习目标

学习openGauss数据库客户端工具gsql的使用。

二、课程学习

gsql是openGauss提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。

三、课程作业

1. 使用gsql命令连接数据库
--使用omm用户连接到本机omm数据库的5432端口,命令中的-r选项提供了对gsql命令的历史版本支持。
su - omm
gsql -r
或
gsql -d omm  -p 5432  -r

实训环境示例:

root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# \q
omm@modb:~$ gsql -d omm -p 5432 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# 
2. 在gsql中查看数据库的版本、pg基础版本和版权信息
select version();
show server_version;
 \copyright

omm=# select version();
                                                                        version                
                                                        
-----------------------------------------------------------------------------------------------
--------------------------------------------------------
 (openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr   on aarch64
-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)

omm=# show server_version;
 server_version 
----------------
 9.2.4
(1 row)

omm=# \copyright
GaussDB Kernel Database Management System
Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.
3. 常见元命令的使用
--\l命令,元命令\l的作用是显示openGauss数据库集簇中,目前有哪些数据库。

\l

--\conninfo命令,元命令\conninfo的作用是在gsql中,显示会话的连接信息。

\conninfo

--\c[onnect] [DBNAME]命令,元命令\ c[onnect] [DBNAME]的作用是在gsql中,切换连接的数据库postgres。

\c postgres



--\du命令和\dg命令,元命令\dg命令与元命令\du命令的作用类似,都是显示openGauss数据库集簇中,目前有哪些用户和角色。

\du

\dg

--\db命令,元命令\db的作用是显示openGauss数据库集簇中,目前有哪些表空间。

\db

--\dn命令,元命令\dn的作用是显示当前数据库有哪些数据库模式。

\dn

--创建表
CREATE TABLE customer_t
(  c_customer_sk             integer,   
 c_customer_id             char(5),    
 c_first_name              char(6),    
 c_last_name               char(8) 
) ;
--插入数据
INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (3769, 5, 'Grace','White');

--\dt命令,命令\dt的作用是显示数据库中所有的表。
\dt

--\d TableName命令,元命令\d TableName的作用是查看某个表的信息。
\d customer_t

--\di IndexName命令,查看索引信息,元命令\di IndexName的作用是查看某个索引的信息。
create index idx_customer_id on customer_t(c_customer_id);
\di                 

--可以用\pset命令以不同的方法显示表:
\pset命令---用于设置输出的格式
\pset border 0:  表示输出内容无表框;
\pset border 1:  表示内部显示边框
\pset border 2:  表示内部都有边框

\pset border 2
SELECT * FROM customer_t;


--打开扩展表格式模式。把表中每一行的每列数据都拆分为单行显示
\x

omm@modb:~$ gsql -d omm -p 5432 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 omm       | omm   | UTF8     | C       | C     | 
omm=#  postgres  | omm   | UTF8     | C       | C     | 
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(4 rows)


omm=# \conninfo
You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432".
omm=# \c postgres
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".
openGauss=# \du
 omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Ope
ratoradmin, Policyadmin, UseFT | {}

                                                              List of roles
 Role name |                                                    Attributes                     
                               | Member of 
-----------+-----------------------------------------------------------------------------------
-------------------------------+-----------
 gaussdb   | Sysadmin                                                                          
                               | {}
openGauss=# \dg
                                                              List of roles
 Role name |                                                    Attributes                     
                               | Member of 
-----------+-----------------------------------------------------------------------------------
-------------------------------+-----------
 gaussdb   | Sysadmin                                                                          
                               | {}
 omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Ope
ratoradmin, Policyadmin, UseFT | {}

openGauss=# \db
      List of tablespaces
    Name    | Owner | Location 
------------+-------+----------
 pg_default | omm   | 
 pg_global  | omm   | 
(2 rows)

openGauss=# \dn
      List of schemas
      Name       |  Owner  
-----------------+---------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 gaussdb         | gaussdb
 pkg_service     | omm
 public          | omm
 snapshot        | omm
 sqladvisor      | omm
(11 rows)

openGauss=# 
openGauss=# CREATE TABLE customer_t
openGauss-# (  c_customer_sk             integer,   
openGauss(#  c_customer_id             char(5),    
openGauss(#  c_first_name              char(6),    
openGauss(#  c_last_name               char(8) 
openGauss(# ) ;
CREATE TABLE
openGauss=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (3769, 5, 'Grace','White');
INSERT 0 1
openGauss=# \dt
                           List of relations
 Schema |    Name    | Type  | Owner |             Storage              
--------+------------+-------+-------+----------------------------------
 public | customer_t | table | omm   | {orientation=row,compression=no}
(1 row)

openGauss=# \d customer_t
        Table "public.customer_t"
    Column     |     Type     | Modifiers 
---------------+--------------+-----------
 c_customer_sk | integer      | 
 c_customer_id | character(5) | 
 c_first_name  | character(6) | 
 c_last_name   | character(8) | 

openGauss=# create index idx_customer_id on customer_t(c_customer_id);
CREATE INDEX
openGauss=# \di
                        List of relations
 Schema |      Name       | Type  | Owner |   Table    | Storage 
--------+-----------------+-------+-------+------------+---------
 public | idx_customer_id | index | omm   | customer_t | 
(1 row)

openGauss=# \pset border 2
Border style is 2.
openGauss=# select * from customer_t;
+---------------+---------------+--------------+-------------+
|          3769 | 5             | Grace        | White       |
+---------------+---------------+--------------+-------------+
(1 row)

openGauss=# +---------------+---------------+--------------+-------------+
| c_customer_sk | c_customer_id | c_first_name | c_last_name |

openGauss=# \x
Expanded display is on.
4. gsql中的事务:测试gsql中的默认事务自动提交功能
--查看gsql中事务是否默认为自动提交
show AUTOCOMMIT;
--测试gsql中事务默认为自动提交功能
create  table customer_new as select * from customer_t;
--退出登录
\q
--重新登录后看到之前创建的表customer_new:
gsql -d postgres  -p 5432  -r
\dt
openGauss=# show AUTOCOMMIT;
 autocommit 
------------
 on
(1 row)

openGauss=# create  table customer_new as select * from customer_t;
INSERT 0 1
openGauss=# \q
omm@modb:~$ gsql -d postgres omm -p 5432 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

openGauss=# \dt
                            List of relations
 Schema |     Name     | Type  | Owner |             Storage              
--------+--------------+-------+-------+----------------------------------
 public | customer_new | table | omm   | {orientation=row,compression=no}
 public | customer_t   | table | omm   | {orientation=row,compression=no}
(2 rows)

openGauss=# 
5. gsql中的事务:测试gsql中的事务手动提交功能
–测试gsql手动提交
#Opengauss默认执行完一条语句后,立即提交。可以关闭自动提交功能:
#注意:此处设置ATUOCOMMIT必须用大写!

\set AUTOCOMMIT off
   --插入一些数据
INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES    
(6885, 1, 'Joes', 'Hunter'),    
(4321, 2, 'Lily','Carter'),    
(9527, 3, 'James', 'Cook'),
(9500, 4, 'Lucy', 'Baker');
  --查看表中数据
select * from customer_t;
--执行回滚
ROLLBACK;
    --检查是否回滚成功
SELECT * FROM customer_t;

image.png

6. gsql相关的帮助
--连接数据库时,可以使用如下命令获取帮助信息。
gsql --help
--\h获取和SQL语法有关的帮助信息
\h
--\? 获取和元命令有关的帮助信息
 \?
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值