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

openGauss每日一练第7天|opengauss中一个数据库中可以创建多个模式

原创 shezhang784938 2022-12-01
190

一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者SchemaName.TableName,来访问某个模式下的一个表。
默认情况下访问public模式下的表,可以不用添加模式名前缀。


  • 切换用户为omm
[root@enmoedu ~]# su - omm
Last login: Tue Nov 29 17:40:43 CST 2022 on pts/0
[omm@enmoedu ~]$


  • 查看状态
##查看状态
[omm@enmoedu ~]$ gs_om -t status --detail



  • 拉起数据库
[omm@enmoedu ~]$ gs_om -t start



  • 登录数据库
##登录数据库
[omm@enmoedu ~]$ gsql -d postgres -p 15400 -r



  • 创建模式、查看模式
--进入数据库omm,创建表空间、测试数据库
drop database  if exists  musicdb;
drop database  if exists  musicdb1;
drop database  if exists  musicdb2;
drop database  if exists  musicdb3;
drop tablespace if exists music_tbs;

create tablespace music_tbs relative location 'tablespace/test_ts1';
create database musicdb  with tablespace = music_tbs;

--执行下面的sql语句,创建用户user1:
create user user1 identified by 'enmoedu@1234';
 
--授予user1数据库系统的sysadmin权限:
alter user user1 sysadmin;


  • 使用用户user1连接到数据库musicdb,首先查看当前数据库下有哪些模式
--退出当前数据库
\q

--登录数据库
[omm@enmoedu ~]$ gsql -d musicdb  -U user1 -p 15400 -W enmoedu@1234 -r

--查看模式
\dn



  • 然后为数据库musicdb创建4个模式: schm1、 schm2、 schm3、 schm4
–-用户user1在数据库musicdb中,创建了4个模式:
create schema  schm1  authorization user1;
create schema  schm2  authorization user1;
create schema  schm3  authorization user1;
create schema  schm4  authorization user1;


  • 查看musicdb数据库下有哪些模式
–查看musicdb数据库下有哪些模式:
\dn



  • 除了可以用gsql的元命令\dn来查看数据库有哪些模式,还可以执行下面的sql语句,查看某个数据库下有哪些模式
select catalog_name, schema_name, schema_owner
  from information_schema.schemata;



  • 在数据库musicdb的不同的模式下创建同名的表
--在不同模式下,创建相同的表                 
create table schm1.ttt(col varchar(100));
create table schm2.ttt(col varchar(100));
create table schm3.ttt(col varchar(100));
create table schm4.ttt(col varchar(100));


  • 执行下面的sql语句,往4个模式中的表ttt分别插入一条数据
--在同一个数据库下,可以直接使用schemaname.tablename来指定一个表,可以省略数据库名。
insert into schm1.ttt values('hello! from schema schm1   11111');
insert into schm2.ttt values('hello! from schema schm2   22222');
insert into schm3.ttt values('hello! from schema schm3   33333');
insert into schm4.ttt values('hello! from schema schm4   44444');


  • 执行ql语句,查看musicdb数据库目前有哪些表
--创建视图:
create or replace view my_tables 
    as 
select table_catalog, table_schema, table_name, table_type 
  from information_schema.tables
 where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');


  • 查看视图
--查看视图:
select * from my_tables;



  • 查看用户在数据库中搜索模式的顺序
--查看默认的搜索模式的顺序
show search_path;



  • 访问musicdb数据库下其他模式的表,需要指定模式名前缀
--访问musicdb数据库下其他模式的表,需要指定模式名前缀:
select * from schm1.ttt;                    

select * from schm2.ttt;               

select * from schm3.ttt;         
    
select * from schm4.ttt;



  • 模式是在数据库层面,用户是在实例层面
--重新登录数据库
[omm@enmoedu ~]$ gsql -d musicdb  -U user1 -p 15400 -W enmoedu@1234 -r



  • 显示opengauss数据库集簇中,目前有哪些用户和角色
--显示opengauss数据库集簇中,目前有哪些用户和角色
\du



  • 查看模式
--查看模式
\dn



  • 退出当前数据库
--退出当前数据库
\q


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

评论