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

openGauss 每日一练第 12 天 | openGauss 逻辑结构:模式管理

原创 five14 2022-12-14
153

openGauss 的模式是对数据库做一个逻辑分割。所有的数据库对象都建立在模式下面。openGauss 的模式和用户是弱绑定的,所谓的弱绑定是指虽然创建用户的同时会自动创建一个同名模式,但用户也可以单独创建模式,并且为用户指定其他的模式。

在一个数据库中,可以有多个模式。模式可以把一组对象组织在一起。这样组织机构有多少个应用,我们可以将数据库对象组织成几个模式;组织机构有几个部门,也可以为该部门创建单独的模式。

默认情况下,用户将访问数据库的 public 模式。

学习目标

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

课程学习

1. 实验准备

su - omm gsql -r --执行如下的命令和SQL语句,创建表空间enmtbs和数据库enmdb: CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1'; CREATE DATABASE enmdb WITH TABLESPACE = enmtbs; –执行下面的gsql元命令\l,查看openGauss数据库集群上有哪些数据库: \l --执行下面的gsql元命令\db,查看openGauss数据库集群上有哪些表空间: \db

复制

image.png

2. 执行下面的 gsql 元命令,查看 openGauss 数据库上有哪些用户和模式

--执行下面的gsql元命令\du,查看openGauss数据库上有哪些用户: \du --执行下面的gsql元命令\dn,查看openGauss数据库上有哪些模式 \dn

复制

image.png

3. 执行下面的 SQL 语句,创建一个数据库用户 user1 ,其密码为 kunpeng@1234 ,并授予数据库用户 user1 SYSADMIN 权限

--创建数据库用户user1的同时,会在系统的omm数据库中创建一个与这个用户名同名的模式user1。 CREATE USER user1 IDENTIFIED BY 'kunpeng@1234'; ALTER USER user1 SYSADMIN; --再次执行下面的gsql元命令\du,查看openGauss数据库上有哪些用户: \du --再次执行下面的gsql元命令\dn,查看openGauss数据库上有哪些模式 \dn+ --或 SELECT catalog_name, schema_name, schema_owner FROM information_schema.schemata;

复制

image.png
image.png

4. PUBLIC 模式

openGauss 在创建一个新的数据库时,会自动创建一个 public 模式。当用户登录到该数据库时,如果没有特殊的指定,都是操作在 public 模式中的数据库对象。

默认情况下,用户新创建的表,位于 public 模式中。

--执行下面的SQL语句,创建一个测试表test: create table test(col1 char(10)); --执行下面的gsql元命令\dt,查看 test表的Schema: \dt test

复制

image.png

5. 为数据库创建模式

一个用户可以创建多个模式

--创建一个模式schm1,属主是用户user1,并再次查看当前连接的数据库下有哪些模式: create schema schm1 AUTHORIZATION user1; --继续在数据库enmdb中,创建模式schm2,模式schm3,属主是用户omm: create schema schm2; create schema schm3; \dn

复制

image.png

6. 查看和设置模式的搜索路径

--会话级设置模式搜索顺序 在gsql客户端会话中,执行命令SET SEARCH_PATH TO schm1可以修改模式搜索路径,但只在gsql客户端会话的持续过程中起作用,一旦退出gsql客户端会话,这个设置就丢失了。重新登录gsql会话将模式搜索路径恢复为默认值"$user",public。 SET SEARCH_PATH TO schm1; show SEARCH_PATH; \q gsql -r show SEARCH_PATH; --数据库级设置模式搜索顺序 修改数据库级别的搜索顺序后,数据库用户user1再次登录到数据库enmdb,其模式搜索路径已经变更为数据库默认的模式搜索路径schm1。 ALTER DATABASE enmdb SET SEARCH_PATH TO schm1; \q gsql -r \c enmdb user1 show SEARCH_PATH; --用户级设置模式搜索顺序 --设置数据库的用户user1的模式搜索顺序为模式schm2: ALTER USER user1 SET SEARCH_PATH TO schm2; \q gsql -d enmdb -U user1 -W kunpeng@1234 -r show SEARCH_PATH;

复制

image.png

7. 搜索顺序的优先级

--会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第2,数据库级模式搜索顺序的优先级最低。 #在会话级修改模式搜索顺序为模式schm3: SET SEARCH_PATH TO schm3; show SEARCH_PATH;

复制

image.png

8. 查看模式有哪些表

--查看当前连接的数据库中,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';

复制

image.png

课程作业

1. 创建一个名为 testsm 、 testsm1 的模式

--登录数据库 su - omm gsql -r --执行如下的命令创建testsm和testsm1模式: create schema testsm; create schema testsm1;

复制

2. 创建一个用户 john , 并将 testsm 的 owner 修改为 john ,且修改 owner 前后分别使用 \dn+ 查看模式信息

--创建用户并授权,修改testsm的owner为john create user john identified by 'kunpeng@1234'; alter user john sysadmin; \dn+ alter schema testsm owner to john; \dn+

复制

3. 重命名 testsm 为 testsm2

--可以看出testsm被改名为testsm2 \dn+ alter schema testsm rename to testsm2; \dn

复制

4. 在模式 testsm1 中建表 t1 、插入记录和查询记录

create table testsm1.t1(id int); insert into testsm1.t1 values(1); insert into testsm1.t1 values(2); select * from testsm1.t1 ;

复制

5. 在会话级设置模式搜索顺序

--在会话级设置模式搜索顺序,退出会话后,恢复初始设置 show SEARCH_PATH; set SEARCH_PATH TO testsm1; \q gsql -r show SEARCH_PATH;

复制

6. 在数据库级设置模式搜索顺序

--修改数据库级别的搜索顺序后,数据库用户john再次登录到数据库omm,其模式搜索路径已经变更为数据库默认的模式搜索路径testsm1 alter database omm SET SEARCH_PATH TO testsm1; show SEARCH_PATH; \q gsql -r show SEARCH_PATH;

复制

7. 在用户级设置模式搜索顺序

--在用户级设置模式搜索顺序,数据库用户john登录到数据库postgres,其模式搜索路径已经变更为数据库默认的模式搜索路径testsm1 gsql -d postgres -U john -W kunpeng@1234 -r show SEARCH_PATH; alter user john set SEARCH_PATH TO testsm1; gsql -d postgres -U john -W kunpeng@1234 -r show SEARCH_PATH;

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

评论