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

openGauss每日一练第 12 天

原创 陶笑 2022-12-05
182

学习目标

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

课程作业

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年前
评论
暂无图片 0
作业审核合格,一起参与21天openGauss学习打卡活动! 活动详情:https://www.modb.pro/db/551619
2年前
暂无图片 点赞
评论
目录
  • 学习目标
  • 课程作业
    • 1.创建一个名为testsm、testsm1的模式
    • 2.创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息
    • 4.在模式testsm1中建表t1、插入记录和查询记录
    • 5.在会话级设置模式搜索顺序
    • 6.在数据库级设置模式搜索顺序
    • 7.在用户级设置模式搜索顺序