学习心得
openGauss的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。
在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。默认情况下,用户将访问数据库的public模式。
课程作业
1.创建一个名为testsm、testsm1的模式
omm=# create schema testsm;
omm=# CREATE SCHEMA
omm=# create schema testsm1;
omm=# CREATE SCHEMA
2.创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息
omm=# CREATE USER john IDENTIFIED BY 'john@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER john SYSADMIN;
ALTER ROLE
omm=# \dn+ testsm
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
--------+-------+-------------------+-------------+----------------
testsm | omm | | | f
(1 row)
omm=# alter schema testsm owner to john;
ALTER SCHEMA
omm=# \dn+ testsm
List of schemas
Name | Owner | Access privileges | Description | WithBlockChain
--------+-------+-------------------+-------------+----------------
testsm | john | | | f
(1 row)
omm=# 
3.重命名testsm为testsm2
omm=# alter schema testsm rename to testsm2;
ALTER SCHEMA
4.在模式testsm1中建表t1、插入记录和查询记录
omm=# create table testsm1.t1 (txt char(20));
omm=# CREATE TABLE
insert into testsm1.t1 values ('2022年12月5日11点06分');
INSERT 0 1
omm=# select * from testsm1.t1;
txt
----------------------------
2022年12月5日11点06分
(1 row)
omm=# 
5.在会话级设置模式搜索顺序
--会话级设置模式搜索顺序
在gsql客户端会话中,执行命令SET SEARCH_PATH TO testsm1可以修改模式搜索路径,但只在gsql客户端会话的持续过程中起作用,一旦退出gsql客户端会话,这个设置就丢失了。重新登录gsql会话将模式搜索路径恢复为默认值"$user",public。
SET SEARCH_PATH TO testsm1;show SEARCH_PATH;
6.在数据库级设置模式搜索顺序
--数据库级设置模式搜索顺序
修改数据库级别的搜索顺序后,数据库用户user1再次登录到数据库enmdb,其模式搜索路径已经变更为数据库默认的模式搜索路径testsm1。ALTER DATABASE enmdb SET SEARCH_PATH TO testsm1;

7.在用户级设置模式搜索顺序
--用户级设置模式搜索顺序
--设置数据库的用户user1的模式搜索顺序为模式testsm1:
ALTER USER user1 SET SEARCH_PATH TO testsm1;

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




