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

openGauss每日一练第2天 - 学习心得体会

原创 xplibin 2022-11-25
236

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

2.使用gsql命令连接数据库

root@modb:~# su - omm
gsql -r
omm@modb:~$ 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.

3.在gsql中查看数据库的版本、pg基础版本和版权信息

ERROR:  syntax error at or near "gsql"
LINE 1: gsql -d omm  -p 5432  -r
        ^
omm=# show server_version;
omm=#  server_version 
----------------
 9.2.4
(1 row)

 \copyright
GaussDB Kernel Database Management System
Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.

```


```


4.常见元命令的使用

```omm=# --\l命令,元命令\l的作用是显示openGauss数据库集簇中,目前有哪些数据库。
omm=# 
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
omm=# 
omm=#  template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(4 rows)

--\conninfo命令,元命令\conninfo的作用是在gsql中,显示会话的连接信息。
omm=# 
omm=# \conninfo
You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432".
omm=# 
omm=# --\c[onnect] [DBNAME]命令,元命令\ c[onnect] [DBNAME]的作用是在gsql中,切换连接的数据库postgres。
omm=# 
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=# 
openGauss=# 
openGauss=# 
�色。auss=# --\du命令和\dg命令,元命令\dg命令与元命令\du命令的作用类似,都是显示openGauss数据库集簇中,目前有哪些用户和角
openGauss=# 
openGauss=# \du
                                                              List of roles
 Role name |                                                    Attributes                                                    | Member of 
-----------+------------------------------------------------------------------------------------------------------------------+-----------
 gaussdb   | Sysadmin                                                                                                         | {}
 omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}

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

openGauss=# openGauss=# 
--\db命令,元命令\db的作用是显示openGauss数据库集簇中,目前有哪些表空间。
openGauss=# 
openGauss=# \db
      List of tablespaces
    Name    | Owner | Location 
------------+-------+----------
 pg_default | omm   | 
 pg_global  | omm   | 
(2 rows)

openGauss=# 
openGauss=# --\dn命令,元命令\dn的作用是显示当前数据库有哪些数据库模式。
openGauss=# 
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=# 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(# ) ;
ERROR:  relation "customer_t" already exists in schema "public"
DETAIL:  creating new table with existing name in the same schema
openGauss=# 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=# 
openGauss=# --\dt命令,命令\dt的作用是显示数据库中所有的表。
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=# 
openGauss=# --\d TableName命令,元命令\d TableName的作用是查看某个表的信息。
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) | 
Indexes:
    "idx_customer_id" btree (c_customer_id) TABLESPACE pg_default

openGauss=# 
openGauss=# openGauss=# --\di IndexName命令,查看索引信息,元命令\di IndexName的作用是查看某个索引的信息。
create index idx_customer_id on customer_t(c_customer_id);
ERROR:  relation "idx_customer_id" already exists
openGauss=# \di                 
                        List of relations
 Schema |      Name       | Type  | Owner |   Table    | Storage 
--------+-----------------+-------+-------+------------+---------
 public | idx_customer_id | index | omm   | customer_t | 
(1 row)

openGauss=# 
openGauss=# --可以用\pset命令以不同的方法显示表:
openGauss=# 
openGauss=# \pset border 2
Border style is 2.
openGauss=# SELECT * FROM customer_t;
+---------------+---------------+--------------+-------------+
| c_customer_sk | c_customer_id | c_first_name | c_last_name |
+---------------+---------------+--------------+-------------+
|          3769 | 5             | Grace        | White       |
|          3769 | 5             | Grace        | White       |
+---------------+---------------+--------------+-------------+
(2 rows)

openGauss=# 
openGauss=# 
openGauss=# --打开扩展表格式模式。
openGauss=# \x
Expanded display is on.

```


5.gsql中的事务:测试gsql中的默认事务自动提交功能
```openGauss=# --测试gsql中事务默认为自动提交功能
openGauss=# create  table customer_new as select * from customer_t;
ERROR:  relation "customer_new" already exists in schema "public"
DETAIL:  creating new table with existing name in the same schema
openGauss=# \q
omm@modb:~$ 
omm@modb:~$ --重新登录后看到之前创建的表customer_new:
-bash: $'--\351\207\215\346\226\260\347\231\273\345\275\225\345\220\216\347\234\213\345\210\260\344\271\213\345\211\215\345\210\233\345\273\272\347\232\204\350\241\250customer_new\357\274\232': command not found
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.

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}

```


6.--连接数据库时,可以使用如下命令获取帮助信息。

openGauss=# --连接数据库时,可以使用如下命令获取帮助信息。
```openGauss=# 
openGauss=# gsql --help
openGauss-# 
openGauss-# --\h获取和SQL语法有关的帮助信息
openGauss-# 
openGauss-# \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 GROUP             END
  ALTER FOREIGN TABLE               COPY                              CURSOR                            EXECUTE
  ALTER FOREIGN TABLE FOR HDFS      CREATE APP WORKLOAD GROUP         DEALLOCATE                        EXECUTE DIRECT
  ALTER FUNCTION                    CREATE APP WORKLOAD GROUP MAPPING DECLARE                           EXPLAIN
  ALTER GLOBAL CONFIGURATION        CREATE AUDIT POLICY               DELETE                            FETCH
  ALTER GROUP                       CREATE BARRIER                    DO                                GRANT
  ALTER INDEX                       CREATE CLIENT MASTER KEY          DROP APP WORKLOAD GROUP           INSERT
  ALTER LARGE OBJECT                CREATE COLUMN ENCRYPTION KEY      DROP APP WORKLOAD GROUP MAPPING   LOCK
  ALTER MASKING POLICY              CREATE DATA SOURCE                DROP AUDIT POLICY                 MERGE
  ALTER MATERIALIZED VIEW           CREATE DATABASE                   DROP CLIENT MASTER KEY            MOVE
  ALTER NODE                        CREATE DIRECTORY                  DROP COLUMN ENCRYPTION KEY        PREDICT BY
  ALTER NODE GROUP                  CREATE EXTENSION                  DROP DATA SOURCE                  PREPARE
  ALTER OPERATOR                    CREATE FOREIGN TABLE              DROP DATABASE                     PREPARE TRANSACTION
  ALTER PACKAGE                     CREATE FUNCTION                   DROP DIRECTORY                    PUBLISH SNAPSHOT
  ALTER PUBLICATION                 CREATE GROUP                      DROP EXTENSION                    PURGE
--More--
Most commands optionally preceded by integer argument k.  Defaults in brackets.
Star (*) indicates argument becomes new default.
-------------------------------------------------------------------------------
<space>                 Display next k lines of text [current screen size]
z                       Display next k lines of text [current screen size]*
<return>                Display next k lines of text [1]*
d or ctrl-D             Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt>   Exit from more
s                       Skip forward k lines of text [1]
f                       Skip forward k screenfuls of text [1]
b or ctrl-B             Skip backwards k screenfuls of text [1]
'                       Go to place where previous search started
=                       Display current line number
/<regular expression>   Search for kth occurrence of regular expression [1]
n                       Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd>       Execute <cmd> in a subshell
v                       Start up /usr/bin/vi at current line
ctrl-L                  Redraw screen
:n                      Go to kth next file [1]
:p                      Go to kth previous file [1]
:f                      Display current file name and line number
.                       Repeat previous command
-------------------------------------------------------------------------------
  ALTER RESOURCE LABEL              CREATE INDEX                      DROP FOREIGN TABLE                PURGE SNAPSHOT
  ALTER RESOURCE POOL               CREATE LANGUAGE                   DROP FUNCTION                     REASSIGN OWNED
  ALTER SCHEMA                      CREATE MODEL                      DROP INDEX                        RESET
  ALTER SEQUENCE                    CREATE NODE                       DROP MASKING POLICY               REVOKE
  ALTER SERVER                      CREATE NODE GROUP                 DROP MATERIALIZED VIEW            ROLLBACK
  ALTER ROLE                        CREATE MASKING POLICY             DROP GLOBAL CONFIGURATION         REFRESH MATERIALIZED VIEW
  ALTER ROW LEVEL SECURITY POLICY   CREATE MATERIALIZED VIEW          DROP GROUP                        REINDEX
  ALTER SESSION                     CREATE OPERATOR                   DROP MODEL                        ROLLBACK PREPARED
  ALTER SUBSCRIPTION                CREATE PACKAGE                    DROP NODE                         SAMPLE SNAPSHOT
  ALTER SYNONYM                     CREATE PACKAGE BODY               DROP NODE GROUP                   SAVEPOINT
  ALTER SYSTEM KILL SESSION         CREATE PROCEDURE                  DROP OPERATOR                     SELECT
  ALTER SYSTEM SET                  CREATE PUBLICATION                DROP OWNED                        SELECT INTO
  ALTER TABLE                       CREATE RESOURCE LABEL             DROP PACKAGE                      SET
  ALTER TABLE PARTITION             CREATE RESOURCE POOL              DROP PACKAGE BODY                 SET CONSTRAINTS
  ALTER TABLE SUBPARTITION          CREATE ROLE                       DROP PROCEDURE                    SET ROLE
  ALTER TABLESPACE                  CREATE ROW LEVEL SECURITY POLICY  DROP PUBLICATION                  SET SESSION AUTHORIZATION
  ALTER TEXT SEARCH CONFIGURATION   CREATE SCHEMA                     DROP RESOURCE LABEL               SET TRANSACTION
  ALTER TEXT SEARCH DICTIONARY      CREATE SEQUENCE                   DROP RESOURCE POOL                SHOW
  ALTER TRIGGER                     CREATE SERVER                     DROP ROLE                         START TRANSACTION
  ALTER TYPE                        CREATE SNAPSHOT AS                DROP ROW LEVEL SECURITY POLICY    TIMECAPSULE TABLE
  ALTER USER                        CREATE SNAPSHOT FROM              DROP SCHEMA                       TRUNCATE
  ALTER VIEW                        CREATE SUBSCRIPTION               DROP SEQUENCE                     UPDATE
  ALTER WORKLOAD GROUP              CREATE SYNONYM                    DROP SERVER                       VACUUM
  ANALYSE                           CREATE TABLE                      DROP SUBSCRIPTION                 VALUES
  ANALYZE                           CREATE TABLE AS                   DROP SYNONYM                      
  ANONYMOUS BLOCK                   CREATE TABLE PARTITION            DROP TABLE                        
  ARCHIVE SNAPSHOT                  CREATE TABLE SUBPARTITION         DROP TABLESPACE                   
openGauss-# \?
Invalid command \?. Try \? for help.```language

```


```



```language

```
```language

```
最后修改时间:2022-11-28 20:28:00
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论