概述
本文档记录openGauss 3.0.0数据库每日一练第12天课程作业,掌握openGauss逻辑结构,模式管理。
课程练习
创建一个名为testsm、testsm1的模式
omm@local:/opt/huawei/tmp [postgres]=#create database musicdb12;
CREATE DATABASE
omm@local:/opt/huawei/tmp [postgres]=#\c musicdb12
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb12" as user "omm".
omm@local:/opt/huawei/tmp [musicdb12]=#create schema testsm;
CREATE SCHEMA
omm@local:/opt/huawei/tmp [musicdb12]=#create schema testsm1;
CREATE SCHEMA
omm@local:/opt/huawei/tmp [musicdb12]=#\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
snapshot | omm
sqladvisor | omm
testsm | omm <===================
testsm1 | omm <===================
(12 rows)
创建一个用户john
创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息
omm@local:/opt/huawei/tmp [musicdb12]=#CREATE USER john with sysadmin IDENTIFIED BY 'zs@123456';
CREATE ROLE
omm@local:/opt/huawei/tmp [musicdb12]=#\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
testsm | omm
testsm1 | omm
(13 rows)
omm@local:/opt/huawei/tmp [musicdb12]=#\h alter schema
Command: ALTER SCHEMA
Description: change the definition of a schema
Syntax:
ALTER SCHEMA schema_name
RENAME TO new_name;
ALTER SCHEMA schema_name
OWNER TO new_owner;
ALTER SCHEMA schema_name {WITH | WITHOUT} BLOCKCHAIN;
omm@local:/opt/huawei/tmp [musicdb12]=#alter schema testsm OWNER TO john;
ALTER SCHEMA
omm@local:/opt/huawei/tmp [musicdb12]=#\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
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
(13 rows)
重命名testsm为testsm2
omm@local:/opt/huawei/tmp [musicdb12]=#alter schema testsm RENAME TO testsm2;
ALTER SCHEMA
omm@local:/opt/huawei/tmp [musicdb12]=#\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
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 <===================
(13 rows)
在模式testsm1中建表t1、插入记录和查询记录
omm@local:/opt/huawei/tmp [musicdb12]=#create table testsm1.day12(id int,name varchar(20));
CREATE TABLE
omm@local:/opt/huawei/tmp [musicdb12]=#insert into testsm1.day12 values (1,'day12 for opengauss');
INSERT 0 1
omm@local:/opt/huawei/tmp [musicdb12]=#select * from testsm1.day12;
id | name
----+---------------------
1 | day12 for opengauss
(1 row)
在会话级设置模式搜索顺序
omm@local:/opt/huawei/tmp [musicdb12]=#show search_path;
search_path
----------------
"$user",public
(1 row)
omm@local:/opt/huawei/tmp [musicdb12]=#set search_path to testsm1;
SET
omm@local:/opt/huawei/tmp [musicdb12]=#show search_path;
search_path
-------------
testsm1
(1 row)
omm@local:/opt/huawei/tmp [musicdb12]=#\d
List of relations
Schema | Name | Type | Owner | Storage
---------+-------+-------+-------+----------------------------------
testsm1 | day12 | table | omm | {orientation=row,compression=no}
(1 row)
在数据库级设置模式搜索顺序
omm@local:/opt/huawei/tmp [musicdb12]=#ALTER DATABASE musicdb12 SET SEARCH_PATH TO schm2;
ALTER DATABASE
omm@local:/opt/huawei/tmp [musicdb12]=#\q
[omm@ogauss1 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm@local:/opt/huawei/tmp [postgres]=#show search_path;
search_path
----------------
"$user",public
(1 row)
omm@local:/opt/huawei/tmp [postgres]=#\c musicdb12
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb12" as user "omm".
omm@local:/opt/huawei/tmp [musicdb12]=#show search_path;
search_path
-------------
schm2
(1 row)
在用户级设置模式搜索顺序
omm@local:/opt/huawei/tmp [musicdb12]=#\c - john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb12" as user "john".
john@local:/opt/huawei/tmp [musicdb12]=>show search_path;
search_path
-------------
schm2
(1 row)
john@local:/opt/huawei/tmp [musicdb12]=>ALTER USER john SET SEARCH_PATH TO schm1;
ALTER ROLE
john@local:/opt/huawei/tmp [musicdb12]=>show search_path;
search_path
-------------
schm2
(1 row)
john@local:/opt/huawei/tmp [musicdb12]=>\q
[omm@ogauss1 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:34 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm@local:/opt/huawei/tmp [postgres]=#\c musicdb12
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb12" as user "omm".
omm@local:/opt/huawei/tmp [musicdb12]=#show search_path;
search_path
-------------
schm2
(1 row)
omm@local:/opt/huawei/tmp [musicdb12]=#\c - john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb12" as user "john".
john@local:/opt/huawei/tmp [musicdb12]=>show search_path;
search_path
-------------
schm1 <========
(1 row)
用户级和数据库级切换search_path均需要退出重进生效。
搜索顺序的优先级
会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第2,数据库级模式搜索顺序的优先级最低。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




