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

openGauss每日一练第12天 | 模式管理

原创 田灬禾 2022-12-06
299

打卡第十二天,模式管理

SCHEMA又称作模式。通过管理SCHEMA,允许多个用户使用同一数据库而不相互干扰,可以将数据库对象组织成易于管理的逻辑组,同时便于将第三方应用添加到相应的SCHEMA下而不引起冲突。

每个数据库包含一个或多个SCHEMA。数据库中的每个SCHEMA包含表和其他类型的对象。数据库创建初始,默认具有一个名为PUBLIC的SCHEMA,且所有用户都拥有此SCHEMA的USAGE权限,只有系统管理员和初始化用户可以在public Schema下创建普通函数、聚合函数、存储过程和同义词对象,只有初始化用户可以在public Schema下创建操作符,其他用户即使赋予create权限后也不可以创建上述五种对象。可以通过SCHEMA分组数据库对象。SCHEMA类似于操作系统目录,但SCHEMA不能嵌套。

相同的数据库对象名称可以应用在同一数据库的不同SCHEMA中,而没有冲突。例如,a_schema和b_schema都可以包含名为mytable的表。具有所需权限的用户可以访问数据库的多个SCHEMA中的对象。

通过CREATE USER创建用户的同时,系统会在执行该命令的数据库中,为该用户创建一个同名的SCHEMA。

https://docs.opengauss.org/zh/docs/3.1.0/docs/BriefTutorial/SCHEMA.html

openGauss的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。

openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。


课程作业

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

omm=# create schema testsm;
CREATE SCHEMA
omm=# create schema testsm1;
CREATE SCHEMA
omm=# 


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

omm=# create user john identified by 'huawei@123';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# \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            |                                  | 
 public          | omm   | omm=UC/omm       +| standard public schema           | f
 john            | john  |                   |                                  | f
 pkg_service     | omm   |                   | pkg_service schema               | f
                 |       | =U/omm            |                                  | 
 snapshot        | omm   |                   | snapshot schema                  | f
 dbe_pldeveloper | omm   | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  | 
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  | 
 testsm          | omm   |                   |                                  | f
 testsm1         | omm   |                   |                                  | f
(13 rows)

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

omm=# \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            |                                  | 
 john            | john  |                   |                                  | f
                 |       | =U/omm            |                                  | 
 snapshot        | omm   |                   | snapshot schema                  | f
 pkg_service     | omm   |                   | pkg_service schema               | f
 public          | omm   | omm=UC/omm       +| standard public schema           | f
 testsm          | john  |                   |                                  | f
 testsm1         | omm   |                   |                                  | f
(13 rows) sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  | 


omm=# 


3.重命名testsm为testsm2

omm=# alter schema testsm rename to testsm2;
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
 snapshot        | omm
 sqladvisor      | omm
 testsm1         | omm
 testsm2         | john
(13 rows)


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

omm=# create table testsm1.t1(a int);
CREATE TABLE

omm=# insert into testsm1.t1 values (1);
INSERT 0 1
omm=# select * from testsm1.t1 ;
 a 
---
 1
(1 row)


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

omm=# select table_catalog,table_schema,table_name,table_type
 from information_schema.tables  
 where table_schema = 'testsm1';
 table_catalog | table_schema | table_name | table_type 
---------------+--------------+------------+------------
 omm           | testsm1      | t1         | BASE TABLE
(1 row)

omm=# set search_path to testsm1,public;
SET
omm=# select * from t1;
 a 
---
 1
(1 row)

omm=# show search_path;
   search_path   
-----------------
 testsm1, public
(1 row)



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

omm=# alter database omm 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=# show search_path ;
 search_path 
-------------
 testsm1
(1 row)


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

omm=# alter user omm set search_path to testsm2;
ALTER ROLE
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 
-------------
 testsm2
(1 row)

omm=# 




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

评论