暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第2天

原创 2022-11-27
360

学习目标

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

gsql 是 openGauss 提供的在命令行下运行的数据库连接工具。
此工具除了具备操作数据库的基本功能,还提供了若干高级特性,便于用户使用。

gsql连接数据库时的参数选项:gsql -h 172.17.0.5 -p 5432 -U lvzz -d omm -W Test@123
-h, --host:指定运行数据库的服务器主机名或IP
-p, --port:指定数据库服务器的端口号,默认为5432
-U, --username:指定连接数据库的用户
-W, --passwrod:指定用户的密码
-d, --dbname:指定要连接的数据库名称

omm@modb:~$ gsql -h 172.17.0.5 -p 5432 -U lvzz -d omm -W Test@123
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=>

课程作业

1、gsql命令连接数据库omm

----本地方式连接
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=#

----远程方式连接
omm@modb:~$ gsql -h 172.17.0.5 -p 5432 -U lvzz -d omm -W Test@123
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=>

上述连接操作中,连接成功后分别显示“omm=#”和“omm=>”,通过管理员用户omm登录成功后,提示符是井号“#”,使用普通用户身份登录和连接数据库,提示符是大于号“>”。
通过gsql连接数据库时,如果没有通过-U指定用户名,默认和当前登录到OS上的用户名相同。

2、查看数据库的版本、版权信息

----通过查看version()函数,获取数据库的版本信息
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-li
nux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit
(1 row)

----通过查看server_version参数,获取PG基础版本信息
omm=# show server_version;
server_version
----------------
9.2.4
(1 row)

----通过gsql元命令 \copyright 查看版本信息
omm=# \copyright
GaussDB Kernel Database Management System
Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.

3、常见元命令使用

----\l 命令,显示数据库列表信息
omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
omm       | omm   | UTF8     | C       | C     |
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)

----\conninfo 命令,显示当前会话的连接信息
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=# \conninfo
You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432".


omm@modb:~$ gsql -h 172.17.0.5 -p 5432 -U lvzz -d omm -W Test@123
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=> \conninfo
You are connected to database "omm" as user "lvzz" on host "172.17.0.5" at port "5432".

----\c[onnect] [dbname] 命令,切换连接的数据库
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和\dg 命令,显示数据库中的用户和角色信息
----\db 命令,显示表空间信息
openGauss=# \db
      List of tablespaces
    Name    | Owner | Location
------------+-------+----------
pg_default | omm   |
pg_global  | omm   |
 
----\dn 命令,显示数据库中的模式信息
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
----\dt 命令,显示数据库中所有的表
----\d TableName 命令,查看某个表的信息
omm=# \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) |
Indexes:
    "idx_customer_id" btree (c_customer_id) TABLESPACE pg_default

----\di IndexName命令,查看索引信息
omm=# \di idx_customer_id
                        List of relations
Schema |      Name       | Type  | Owner |   Table    | Storage
--------+-----------------+-------+-------+------------+---------
public | idx_customer_id | index | omm   | customer_t |
(1 row)

4、测试gsql中的默认事务自动提交功能

----查看gsql中事务是否默认为自动提交
omm=# show autocommit;
autocommit
------------
on

----测试gsql中事务默认为自动提交功能
omm=# create  table customer_new as select * from customer_t;
INSERT 0 1
omm=# \q

----重新登录后看到之前创建的表customer_new
omm@modb:~$ gsql -d omm -p 5432  -r
omm=# \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}

5、测试gsql中的事务手动提交功能

----关闭自动提交功能,此处设置ATUOCOMMIT必须用大写
omm=# \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');

----查看表中数据
omm=# select * from customer_t;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------
          3769 | 5             | Grace        | White   
          6885 | 1             | Joes         | Hunter  
          4321 | 2             | Lily         | Carter  
          9527 | 3             | James        | Cook    
          9500 | 4             | Lucy         | Baker

----执行回滚
ROLLBACK;

----检查是否回滚成功
omm=# SELECT * FROM customer_t;
c_customer_sk | c_customer_id | c_first_name | c_last_name
---------------+---------------+--------------+-------------
          3769 | 5             | Grace        | White   
(1 row)

6、了解gsql相关帮助

omm=# help
You are using gsql, the command-line interface to gaussdb.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with gsql commands
       \g or terminate with semicolon to execute query
       \q to quit

\copyright 指令表示版本发布相关信息
\h 指令表示与标准查询语言SQL相关的问题 
\? 指令表示与gsql工具相关的命令
\g 指令用来查询与执行query相关的指令
\q 指令用来退出数据库连接
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论