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

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

原创 闫龙伟 2022-11-25
334

课程作业

1.gsql命令连到数据库omm

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.	

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

omm=# select 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.

omm=# 

3.常见元命令使用

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)

omm=# \conninfo
You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432".
omm=# omm-# CREATE TABLE customer_t
(  c_customer_sk             integer,   
omm(#  c_customer_id             char(5),    
omm(#  c_first_name              char(6),    
omm(#  c_last_name               char(8) 
omm(# ) ;
CREATE TABLE
omm=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) 		VALUES (3769, 5, 'Grace','White');
INSERT 0 1
omm=# \dt
                       List of relations
Schema |    Name    | Type  | Owner |             Storage              
--------+------------+-------+-------+----------------------------------
 public | customer_t | table | omm   | {orientation=row,compression=no}
(1 row)

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) | 

omm=# \di                 
No relations found.
omm=# Border style is 2.
omm=# \pset border 2
omm=# 

omm=# SELECT * FROM customer_t;
+---------------+---------------+--------------+-------------+
| c_customer_sk | c_customer_id | c_first_name | c_last_name |
+---------------+---------------+--------------+-------------+
|          3769 | 5             | Grace        | White       |
+---------------+---------------+--------------+-------------+
(1 row)

omm=# \x
Expanded display is on.
omm=# SELECT * FROM customer_t;
+-[ RECORD 1 ]--+----------+
| c_customer_sk | 3769     |
| c_customer_id | 5        |
| c_first_name  | Grace    |
| c_last_name   | White    |
+---------------+----------+

omm=# 

4.使用两种方法,连到postgres数据库中

omm=# \c postgres
Non-SSL connection (SSL connection is recommended when requiring high-security)
ou are now connected to database "postgres" as user "omm".

omm@modb:~$ gsql -d postgres  -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.

5.测试gsql中的默认事务自动提交功能

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=# show AUTOCOMMIT;
autocommit 
------------
on
(1 row)

omm=# create  table customer_new as select * from customer_t;

INSERT 0 1
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)
ype “help” for help.

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}
(2 rows)

omm=# 

6.测试gsql中的事务手动提交功能

omm=# \set AUTOCOMMIT off
omm=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES    
omm-# (6885, 1, 'Joes', 'Hunter'),    
omm-# (4321, 2, 'Lily','Carter'),    
omm-# (9527, 3, 'James', 'Cook'),
omm-# (9500, 4, 'Lucy', 'Baker');
omm=# INSERT 0 4
omm=# 
omm=# 

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  
	omm=#           4321 | 2             | Lily         | Carter  
      9527 | 3             | James        | Cook    
      9500 | 4             | Lucy         | Baker   
(5 rows)


omm=# ROLLBACK;
ROLLBACK
omm=# SELECT * FROM customer_t;
omm=#  c_customer_sk | c_customer_id | c_first_name | c_last_name 
---------------+---------------+--------------+-------------
      3769 | 5             | Grace        | White   
(1 row)

7.了解gsql相关帮助

omm=# \h
Available help:
ABORT                             BEGIN                             CREATE TABLESPACE                 			DROP TEXT SEARCH CONFIGURATION
ALTER APP WORKLOAD GROUP          CALL                              CREATE TEXT SEARCH 	CONFIGURATION  DROP TEXT SEARCH DICTIONARY
ALTER APP WORKLOAD GROUP MAPPING  CHECKPOINT                        CREATE TEXT SEARCH DICTIONARY     DROP TRIGGER
ALTER AUDIT POLICY                CLEAN CONNECTION                  CREATE TRIGGER                    DROP TYPE
ALTER DATA SOURCE                 CLOSE                             CREATE TYPE                       DROP USER
ALTER DATABASE                    CLUSTER                           CREATE USER                       DROP VIEW
ALTER DEFAULT PRIVILEGES          COMMENT                           CREATE VIEW                       DROP WEAK PASSWORD DICTIONARY
ALTER DIRECTORY                   COMMIT                            CREATE WEAK PASSWORD DICTIONARY   DROP WORKLOAD GROUP
ALTER EXTENSION                   COMMIT PREPARED                   CREATE WORKLOAD 


omm=# \?
General
\copyright             show openGauss usage and distribution terms
\g [FILE] or ;         execute query (and send results to file or |pipe)
\qecho [STRING]        write string to query output stream (see \o)
--More--  \h(\help) [NAME]              help on syntax of SQL commands, * for all commands
\parallel [on [num]|off] toggle status of execute (currently off)
\q                     quit gsql



omm@modb:~$ gsql --help
General options:
-c, --command=COMMAND    run only single command (SQL or internal) and exit
	gsql is the openGauss interactive terminal.

Usage:
 gsql [OPTION]... [DBNAME [USERNAME]]

-d, --dbname=DBNAME      database name to connect to (default: "omm")
-f, --file=FILENAME      execute commands from file, then exit
-l, --list               list available databases, then exit
-v, --set=, --variable=NAME=VALUE
                       set gsql variable NAME to VALUE
-V, --version            output version information, then exit
-X, --no-gsqlrc          do not read startup file (~/.gsqlrc)
-e, --echo-queries       echo commands sent to server
-E, --echo-hidden        display queries that internal commands generate
-k, --with-key=KEY       the key for decrypting the encrypted file
-L, --log-file=FILENAME  send session log to file
-m, --maintenance        can connect to cluster during 2-pc transaction recovery
-n, --no-libedit        disable enhanced command line editing (libedit)
-o, --output=FILENAME    send query results to file (or |pipe)
-q, --quiet              run quietly (no messages, only query output)
-1 ("one"), --single-transaction
                       execute command file as a single transaction
-?, --help               show this help, then exit
最后修改时间:2022-11-26 11:30:00
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论