openGauss 每日一练第12天
openGauss的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。openGauss的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。
在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。默认情况下,用户将访问数据库的public模式。
学习目标
模式管理包括为数据库创建模式、删除模式、查看和设置模式的搜索路径、查看模式中的信息。
环境准备
切换到数据库用户,进入数据库并创建表空间和数据库
su - omm
gsql -r
--创建 表空间和数据库music
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/music_tbs';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE music_tbs ;
CREATE DATABASE
课程作业
1.创建一个名为testsm、testsm1的模式
-- 切换到musicdb 下查看当前数据库有哪些模式
omm=# \c musicdb omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
omm=# \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
(10 rows)
-- 创建一个新的模式 testsm、testsm1
musicdb=# CREATE SCHEMA testsm;
CREATE SCHEMA
musicdb=# CREATE SCHEMA testsm1;
CREATE SCHEMA
-- 再次查看模式列表,可以看到多出了testsm 和testsm1
musicdb=# \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)
2.创建一个用户john, 并将testsm的owner修改为john,且修改owner前后分别使用\dn+查看模式信息
-- 创建用户 john
musicdb=# create user john identified by 'abcd@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
-- 修改testsm 的所有者为 john ,通过第一条可以看到未修改前的owner 是 omm ,而修改后 owner 更改为了 john
musicdb=# alter schema testsm owner to john ;
ALTER SCHEMA
musicdb=# \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
testsm | john | | | f
testsm1 | omm | | | f
(13 rows)
sqladvisor | omm | omm=UC/omm +| sqladvisor schema | f
| | =U/omm | |
3.重命名testsm为testsm2
-- 修改testsm 模式名称为 testsm2
musicdb=# alter schema testsm rename to testsm2;
ALTER SCHEMA
-- 通过dn 命令查看,testsm已成功修改为testsm2
musicdb=# \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
4.在模式testsm1中建表t1、插入记录和查询记录
-- 在testsm1 模式中创建 test_tb表
musicdb=# create table testsm1.t1(name_id int) tablespace music_tbs ;
CREATE TABLE
-- 查看创建好的表
musicdb=# \dt testsm1.t1
List of relations
Schema | Name | Type | Owner | Storage
---------+------+-------+-------+----------------------------------
testsm1 | t1 | table | omm | {orientation=row,compression=no}
(1 row)
-- 插入数据,查询数据
musicdb=# insert into testsm1.t1 values (0001);
musicdb=# INSERT 0 1
musicdb=# insert into testsm1.t1 values (000);2);
musicdb=# INSERT 0 1
musicdb=# select * from testsm1.t1;
name_id
---------
1
2
(2 rows)
5.在会话级设置模式搜索顺序
-- 设置搜索顺序为 testsm1
musicdb=# set search_path to testsm1;
SET
musicdb=# show search_path ;
search_path
-------------
testsm1
(1 row)
-- 退出后,再次查询搜索顺序
musicdb=# \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
----------------
"$user",public
(1 row)
-- 可以看到会话级别的搜索顺序更改以后,重新登录,又还原成初始值
6.在数据库级设置模式搜索顺序
-- 在数据库级别设置 模式搜索顺序
musicdb=# alter database musicdb set search_path TO testsm1;
ALTER DATABASE
-- 设置完成后,查看当前连接的模式搜索顺序,是默认搜索顺序
musicdb=# show search_path;
----------------
"$user",public
(1 row)
-- 退出后,重新进入数据库,再次查看模式搜索顺序,发现已更改成我们设置的testsm1
\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=# \c musicdb omm
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "omm".
musicdb=# show search_path
musicdb-# ;
search_path
-------------
testsm1
(1 row)
7.在用户级设置模式搜索顺序
-- 在omm 用户下,修改用户 john 的模式搜索顺序为 testsm2
musicdb=# alter user john set search_path to testsm2;
ALTER ROLE
-- 切换到 john 用户, 查看此时john 的搜索模式
musicdb=# \c musicdb john
Password for user john:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "john".
musicdb=> show search_path ;
search_path
-------------
testsm2
(1 row)
作业之外:
-
设置搜索模式的优先级:会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第二,数据库级模式搜索顺序的优先级最低。
-
查看模式有哪些表
--查看当前连接的数据库中,public模式下有哪些表: gsql -r select table_catalog,table_schema,table_name,table_type from information_schema.tables where table_schema = 'public'; --查看指定数据库中,public模式下有哪些表: select table_catalog,table_schema,table_name,table_type from information_schema.tables where table_catalog='omm' and table_schema = 'public';
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
目录