学习目标
模式管理包括为数据库创建模式、删除模式、查看和设置模式的搜索路径、查看模式中的信息。
课程作业
1.创建一个名为testsm、testsm1的模式
--默认不指定schema,是创建在public schema下,如果有public权限的话
openGauss=# \c newdb1
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "omm".
newdb1=# create table newdb_t1(id number);
CREATE TABLE
newdb1=# \dt+ newdb_t1
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+----------+-------+-------+------------+----------------------------------+-------------
public | newdb_t1 | table | omm | 8192 bytes | {orientation=row,compression=no} |
(1 row)
--创建2个schema 属于用户user1
newdb1=# create schema testsm AUTHORIZATION user1;
CREATE SCHEMA
newdb1=# create schema testsm1 AUTHORIZATION user1;
CREATE SCHEMA
newdb1=# \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
test | test
testsm | user1
testsm1 | user1
(14 rows)
newdb1=#
复制
2.创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息
--创建用户john
newdb1=# create user john identified by 'john54321!';
CREATE ROLE
newdb1=# alter user john sysadmin;
ALTER ROLE
--修改shcma testsm的owner
newdb1=# \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 | |
test | test | | | f
testsm | user1 | | | f
testsm1 | user1 | | | f
(15 rows)
newdb1=# alter schema testsm1 owner to john;
ALTER SCHEMA
newdb1=# \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 | |
test | test | | | f
testsm | user1 | | | f
testsm1 | john | | | f
(15 rows)
## 3.重命名testsm为testsm2
```sql
newdb1=# \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 | |
test | test | | | f
testsm | user1 | | | f
testsm1 | john | | | f
(15 rows)
newdb1=# alter schema testsm1 rename to testsm2;
ALTER SCHEMA
newdb1=# \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 | |
test | test | | | f
testsm | user1 | | | f
testsm2 | john | | | f
(15 rows)
newdb1=#
复制
4.在模式testsm1中建表t1、插入记录和查询记录
newdb1=# create table testsm.t1(id number);
CREATE TABLE
newdb1=# insert into testsm.t1 values(1);
INSERT 0 1
newdb1-# \dt+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+----------+-------+-------+------------+----------------------------------+-------------
public | newdb_t1 | table | omm | 8192 bytes | {orientation=row,compression=no} |
(1 row)
newdb1=# set SEARCH_PATH TO testsm;
SET
newdb1=# \dt+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+------+-------+-------+-------+----------------------------------+-------------
testsm | t1 | table | omm | 16 kB | {orientation=row,compression=no} |
(1 row)
newdb1=# select * FROM T1;
id
----
1
(1 row)
复制
5.在会话级设置模式搜索顺序
newdb2=# \c newdb1
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "omm".
newdb1=# show search_path ;
search_path
----------------
"$user",public
(1 row)
newdb1=# set SEARCH_PATH TO testsm,public;
SET
newdb1=# \dt+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+----------+-------+-------+------------+----------------------------------+-------------
public | newdb_t1 | table | omm | 8192 bytes | {orientation=row,compression=no} |
testsm | t1 | table | omm | 16 kB | {orientation=row,compression=no} |
(2 rows)
newdb1-# \q
[omm@og3 ~]$ gsql -p 26000 -d postgres -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.
openGauss=# \c newdb1
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "omm".
newdb1=# show search_path ;
search_path
----------------
"$user",public
(1 row)
复制
6.在数据库级设置模式搜索顺序
newdb1=# show search_path ;
search_path
----------------
"$user",public
(1 row)
newdb1=# alter database newdb1 set search_path to testsm,public;
ALTER DATABASE
newdb1=# show search_path ;
search_path
----------------
"$user",public
(1 row)
newdb1-# \q
[omm@og3 ~]$ gsql -p 26000 -d postgres -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.
openGauss=# \c newdb1
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "omm".
newdb1=# show search_path ;
search_path
----------------
testsm, public
(1 row)
复制
7.在用户级设置模式搜索顺序
newdb1=# alter user john set search_path to public,testsm;
ALTER ROLE
newdb1=# show search_path ;
search_path
----------------
testsm, public
(1 row)
newdb1=# \c newdb1 john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "john".
newdb1=> show search_path ;
search_path
----------------
public, testsm
(1 row)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
559次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
308次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
174次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
162次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
151次阅读
2025-04-30 16:39:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
96次阅读
2025-04-23 16:02:19
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
96次阅读
2025-04-18 10:49:53
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
67次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
50次阅读
2025-04-16 10:33:59