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

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

原创 whtnkf 2022-12-06
341
  • openGauss的模式是对数据库做一个逻辑分割。
  • 所有的数据库对象都建立在模式下面。
  • openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。
  • 在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。
  • 默认情况下,用户将访问数据库的public模式。

学习目标

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

课程作业

0.环境准备

openGauss=# create tablespace schm_tbs relative location 'tablespace/schm_tbs';
CREATE TABLESPACE
openGauss=# create database schmdb with tablespace=schm_tbs;
CREATE DATABASE
openGauss=# \c schmdb
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "schmdb" as user "omm".

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

schmdb=# create schema testsm;
CREATE SCHEMA
schmdb=# create schema testsm1;
CREATE SCHEMA
schmdb=# \dn
     List of schemas
      Name       | Owner
-----------------+-------
 blockchain      | omm
 cstore          | omm
 db4ai           | omm
 dbe_perf        | omm
 dbe_pldebugger  | omm
 dbe_pldeveloper | omm
 dbe_sql_util    | omm
 pkg_service     | omm
 public          | omm
 snapshot        | omm
 sqladvisor      | omm
 testsm          | omm
 testsm1         | omm
(13 rows)

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

1) 查看\dn+

schmdb=# \dn+
                                         List of schemas
      Name       | Owner | Access privileges |           Description            | WithBlockChain
-----------------+-------+-------------------+----------------------------------+----------------
 blockchain      | omm   |                   | blockchain schema                | f
 cstore          | omm   |                   | reserved schema for DELTA tables | f
 db4ai           | omm   | omm=UC/omm       +| db4ai schema                     | f
                 |       | =U/omm            |                                  |
 dbe_perf        | omm   |                   | dbe_perf schema                  | f
 dbe_pldebugger  | omm   | omm=UC/omm       +| dbe_pldebugger schema            | f
                 |       | =U/omm            |                                  |
 dbe_pldeveloper | omm   | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  |
 dbe_sql_util    | omm   | omm=UC/omm       +| sql util schema                  | f
                 |       | =U/omm            |                                  |
 pkg_service     | omm   |                   | pkg_service schema               | f
 public          | omm   | omm=UC/omm       +| standard public schema           | f
                 |       | =U/omm            |                                  |
 snapshot        | omm   |                   | snapshot schema                  | f
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  |
 testsm          | omm   |                   |                                  | f
 testsm1         | omm   |                   |                                  | f
(13 rows)

2)创建用户john

schmdb=# create user john identified by 'kunpeng@1234';
CREATE ROLE

3)将testsm的owner修改为john

schmdb=# alter schema testsm owner to john;
ALTER SCHEMA

4)再次查看\dn+

schmdb=# \dn+
                                         List of schemas
      Name       | Owner | Access privileges |           Description            | WithBlockChain
-----------------+-------+-------------------+----------------------------------+----------------
 blockchain      | omm   |                   | blockchain schema                | f
 cstore          | omm   |                   | reserved schema for DELTA tables | f
 db4ai           | omm   | omm=UC/omm       +| db4ai schema                     | f
                 |       | =U/omm            |                                  |
 dbe_perf        | omm   |                   | dbe_perf schema                  | f
 dbe_pldebugger  | omm   | omm=UC/omm       +| dbe_pldebugger schema            | f
                 |       | =U/omm            |                                  |
 dbe_pldeveloper | omm   | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  |
 dbe_sql_util    | omm   | omm=UC/omm       +| sql util schema                  | f
                 |       | =U/omm            |                                  |
 john            | john  |                   |                                  | f
 pkg_service     | omm   |                   | pkg_service schema               | f
 public          | omm   | omm=UC/omm       +| standard public schema           | f
                 |       | =U/omm            |                                  |
 snapshot        | omm   |                   | snapshot schema                  | f
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  |
 testsm          | john  |                   |                                  | f
 testsm1         | omm   |                   |                                  | f
(14 rows)

3.重命名testsm为testsm2

1)重命名testsm为testsm2

schmdb=# alter schema testsm rename to testsm2;
ALTER SCHEMA

2)再次查看\dn+

schmdb=# \dn+
                                         List of schemas
      Name       | Owner | Access privileges |           Description            | WithBlockChain
-----------------+-------+-------------------+----------------------------------+----------------
 blockchain      | omm   |                   | blockchain schema                | f
 cstore          | omm   |                   | reserved schema for DELTA tables | f
 db4ai           | omm   | omm=UC/omm       +| db4ai schema                     | f
                 |       | =U/omm            |                                  |
 dbe_perf        | omm   |                   | dbe_perf schema                  | f
 dbe_pldebugger  | omm   | omm=UC/omm       +| dbe_pldebugger schema            | f
                 |       | =U/omm            |                                  |
 dbe_pldeveloper | omm   | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  |
 dbe_sql_util    | omm   | omm=UC/omm       +| sql util schema                  | f
                 |       | =U/omm            |                                  |
 john            | john  |                   |                                  | f
 pkg_service     | omm   |                   | pkg_service schema               | f
 public          | omm   | omm=UC/omm       +| standard public schema           | f
                 |       | =U/omm            |                                  |
 snapshot        | omm   |                   | snapshot schema                  | f
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  |
 testsm1         | omm   |                   |                                  | f
 testsm2         | john  |                   |                                  | f
(14 rows)

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

1)在数据库schmdb模式testsm1中创建表,插入一条记录

schmdb=# create table testsm1.t1(col1 char(20));
CREATE TABLE
schmdb=# insert into testsm1.t1 values('hello testsm1');
INSERT 0 1
schmdb=# select * from testsm1.t1;
         col1
----------------------
 hello testsm1
(1 row)

2)使用元命令\dt查询

schmdb=# \dt
No relations found.

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

1)在会话级设置模式搜索为testsm1

schmdb=# set search_path to testsm1;
SET

2)查询search_path

schmdb=# show search_path;
 search_path
-------------
 testsm1
(1 row)

3)使用元命令\dt查询表信息,得到返回列表

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

4)再次查询t1表,不加schema前缀,得到返回结果

schmdb=# select * from t1;
         col1
----------------------
 hello testsm1
(1 row)

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

1)设置数据库schmdb的模式搜索为testsm2;

schmdb=# alter database schmdb set search_path to testsm2;
ALTER DATABASE

2)当前会话查询search_path,仍为testsm1

schmdb=# show search_path;
 search_path
-------------
 testsm1
(1 row)

3)退出重新登录schmdb,再次查看search_path已经显示为testsm2

schmdb=# \q
[omm@hecs-30579 ~]$ gsql -d schmdb -p 26000 -r
gsql ((openGauss 3.1.0 build 4e931f9a) compiled at 2022-09-29 14:19:24 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

schmdb=# show search_path;
 search_path
-------------
 testsm2
(1 row)

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

1)使用用户john登录schmdb,查看search_path,显示testsm2

schmdb=# \c schmdb john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "schmdb" as user "john".
schmdb=> show search_path;
 search_path
-------------
 testsm2
(1 row)

2)查看\dn+信息,显示john拥有两个schema:john和testsm2

chmdb=> \dn+
                                         List of schemas
      Name       | Owner | Access privileges |           Description            | WithBlockChain
-----------------+-------+-------------------+----------------------------------+----------------
 blockchain      |       |                   | blockchain schema                | f
 cstore          |       |                   | reserved schema for DELTA tables | f
 db4ai           |       | omm=UC/omm       +| db4ai schema                     | f
                 |       | =U/omm            |                                  |
 dbe_perf        |       |                   | dbe_perf schema                  | f
 dbe_pldebugger  |       | omm=UC/omm       +| dbe_pldebugger schema            | f
                 |       | =U/omm            |                                  |
 dbe_pldeveloper |       | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  |
 dbe_sql_util    |       | omm=UC/omm       +| sql util schema                  | f
                 |       | =U/omm            |                                  |
 john            | john  |                   |                                  | f
 pkg_service     |       |                   | pkg_service schema               | f
 public          |       | omm=UC/omm       +| standard public schema           | f
                 |       | =U/omm            |                                  |
 snapshot        |       |                   | snapshot schema                  | f
 sqladvisor      |       | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  |
 testsm1         |       |                   |                                  | f
 testsm2         | john  |                   |                                  | f
(14 rows)

3)修改用户john的search_path为testsm1,当前会话查询search_path仍然显示为testsm2

schmdb=> alter user john set search_path to testsm1;
ALTER ROLE
schmdb=>
schmdb=> show search_path;
 search_path
-------------
 testsm2
(1 row)

4)用户john连接到postgres库,再次查看search_path,显示为testsm1

schmdb=> \c postgres john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "john".
openGauss=> show search_path;
 search_path
-------------
 testsm1
(1 row)
最后修改时间:2022-12-06 15:44:57
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论