openGauss每日一练第12天 |openGauss逻辑结构:模式管理

openGauss的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。

在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。默认情况下,用户将访问数据库的public模式。

一、学习目标

模式管理包括为数据库创建模式、删除模式、查看和设置模式的搜索路径、查看模式中的信息。

二、课程作业

1.创建一个名为testsm、testsm1的模式

环境准备:
su - omm
gsql -r
--执行如下的命令和SQL语句,创建模式testsm,模式testsm1,属主是用户omm:
create schema testsm;
create schema testsm1;
执行下面的gsql元命令\dn,查看openGauss数据库上有哪些模式
\dn

实训环境截图:

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=# create schema testsm;
CREATE SCHEMA
omm=# create schema testsm1;
CREATE SCHEMA
omm=# \dn
     List of schemas
      Name       | Owner 
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 pkg_service     | omm
 public          | omm
 schm1           | user1
 schm2           | omm
 schm3           | omm
 snapshot        | omm
 sqladvisor      | omm
 testsm          | omm
 testsm1         | omm
 user1           | user1
(16 rows)

omm=# 

2.创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息

--创建一个数据库用户john,其密码为test@1234,并授予数据库用户john SYSADMIN权限:创建数据库用户john的同时,会在系统的omm数据库中创建一个与这个用户名同名的模式john。
CREATE USER john IDENTIFIED BY 'test@1234';
ALTER USER john  SYSADMIN;
\dn
--再次执行下面的gsql元命令\du,查看openGauss数据库上有哪些用户:
\du
--再次执行下面的gsql元命令\dn,查看openGauss数据库上有哪些模式
\dn+、\dn
或
SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
将testsm的owner修改为john:
ALTER SCHEMA testsm OWNER TO john;
\dn

omm=# CREATE USER john IDENTIFIED BY 'test@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER john  SYSADMIN;
ALTER ROLE
omm=# \dn
     List of schemas
      Name       | Owner 
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 john            | john
 pkg_service     | omm
 public          | omm
 schm1           | user1
 schm2           | omm
 schm3           | omm
 snapshot        | omm
 sqladvisor      | omm
 testsm          | omm
 testsm1         | omm
 user1           | user1
(17 rows)

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

omm=# SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;
 catalog_name |    schema_name     | schema_owner 
--------------+--------------------+--------------
 omm          | pg_toast           | omm
 omm          | cstore             | omm
 omm          | pkg_service        | omm
 omm          | dbe_perf           | omm
 omm          | snapshot           | omm
 omm          | blockchain         | omm
 omm          | pg_catalog         | omm
 omm          | public             | omm
 omm          | sqladvisor         | omm
 omm          | dbe_pldebugger     | omm
 omm          | dbe_pldeveloper    | omm
 omm          | information_schema | omm
 omm          | db4ai              | omm
 omm          | user1              | user1
 omm          | schm1              | user1
--More-- omm          | schm2              | omm
 omm          | schm3              | omm
 omm          | testsm             | omm
 omm          | testsm1            | omm
 omm          | john               | john
(20 rows)
omm=# ALTER SCHEMA testsm OWNER TO john;
ALTER SCHEMA
omm=# \dn
     List of schemas
      Name       | Owner 
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 john            | john
 pkg_service     | omm
 public          | omm
 schm1           | user1
 schm2           | omm
 schm3           | omm
 snapshot        | omm
 sqladvisor      | omm
 testsm          | john
 testsm1         | omm
 user1           | user1
(17 rows)

omm=# 

3.重命名testsm为testsm2

ALTER SCHEMA testsm RENAME TO testsm2;
通过\dn+查看重命名后的模式信息
\dn

omm=# ALTER SCHEMA testsm RENAME TO testsm2;
omm=# ALTER SCHEMA

omm=# \dn
     List of schemas
      Name       | Owner 
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 john            | john
 pkg_service     | omm
 public          | omm
 schm1           | user1
 schm2           | omm
 schm3           | omm
 snapshot        | omm
 sqladvisor      | omm
 testsm1         | omm
 testsm2         | john
 user1           | user1
(17 rows)

omm=# 

4.在模式testsm1中建表t1、插入记录和查询记录

SET SEARCH_PATH TO testsm1;
create table t1(id int primary key,col1 char(10));
insert into t1 values(1,‘abc’);
insert into t1 values(2,‘abc’);
select * from t1;
查看t1表的Schema
\dt t1

omm=# SET SEARCH_PATH TO testsm1;
omm=# SET
create table t1(id int primary key,col1 char(10));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
CREATE TABLE
omm=# insert into t1 values(1,'abc');
INSERT 0 1
omm=# insert into t1 values(2,'abc');
omm=# INSERT 0 1
select * from t1;
 id |    col1    
----+------------
  1 | abc       
  2 | abc       
(2 rows)

omm=# \dt t1
                         List of relations
 Schema  | Name | Type  | Owner |             Storage              
---------+------+-------+-------+----------------------------------
 testsm1 | t1   | table | omm   | {
    
    orientation=row,compression=no}
(1 row)

omm=# 

5.在会话级设置模式搜索顺序

--会话级设置模式搜索顺序
在gsql客户端会话中,执行命令SET SEARCH_PATH TO 模式名可以修改模式搜索路径,但只在gsql客户端会话的持续过程中起作用,一旦退出gsql客户端会话,这个设置就丢失了。重新登录gsql会话将模式搜索路径恢复为默认值"$user",publicSET SEARCH_PATH TO 模式名;
SET SEARCH_PATH TO testsm1;
show SEARCH_PATH;
\q

gsql -r
show SEARCH_PATH;
omm=# SET SEARCH_PATH TO testsm1;
SET
omm=# show SEARCH_PATH;

omm=#  search_path 
-------------
 testsm1
(1 row)

omm=# \q
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=# show SEARCH_PATH;
  search_path   
----------------
 "$user",public
(1 row)

omm=# 

6.在数据库级设置模式搜索顺序

--数据库级设置模式搜索顺序
ALTER DATABASE 数据名 SET SEARCH_PATH TO 模式名;
修改数据库级别的搜索顺序后,数据库用户john再次登录到数据库enmdb,其模式搜索路径已经变更为数据库默认的模式搜索路径testsm1。
1.先创建数据库(准备阶段已做过)
CREATE TABLESPACE enmtbs RELATIVE LOCATION ‘tablespace/enmtbs1’;
CREATE DATABASE enmdb WITH TABLESPACE = enmtbs;

ALTER DATABASE enmdb SET SEARCH_PATH TO testsm1;
\q
gsql -r
\c enmdb john
show SEARCH_PATH;

omm=# ALTER DATABASE enmdb SET SEARCH_PATH TO testsm1;
ALTER DATABASE
omm=# \q
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=# \c enmdb john
Password for user john: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "enmdb" as user "john".
enmdb=> show SEARCH_PATH;
 search_path 
-------------
 testsm1
(1 row)

enmdb=> 

7.在用户级设置模式搜索顺序

--用户级设置模式搜索顺序
--设置数据库的用户john的模式搜索顺序为模式testsm1:
ALTER USER john SET SEARCH_PATH TO testsm1;
\q
gsql -d enmdb   -U john   -W test@1234 -r
show SEARCH_PATH;

omm=# ALTER USER john SET SEARCH_PATH TO testsm1;
ALTER ROLE
omm=# \q
omm@modb:~$ gsql -d enmdb   -U john   -W test@1234 -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.

enmdb=> show SEARCH_PATH;
enmdb=>  search_path 
-------------
 testsm1
(1 row)


enmdb=> 

会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第2,数据库级模式搜索顺序的优先级最低。

猜你喜欢

转载自blog.csdn.net/qq_40220309/article/details/128320588
今日推荐