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

openGauss每日一练第13天 |openGauss逻辑结构:表管理1

原创 bingo_007 2022-12-06
964

学习目标

学习openGauss表的创建、搜索路径和访问方法等


课后作业

1.创建一个表(默认,不指定模式),查看该表在那个模式下

create table tmp_t1(name varchar(30));
insert into tmp_t1 values('Hello from tmp_t1!');
select * from tmp_t1;
\dt


--查看当前数据库enmdb下的public模式有哪些表

select table_catalog,table_schema,table_name,table_type
from information_schema.tables
where table_schema = 'public';



2.使用一个用户连接到enmdb数据库,测试该用户可以访问不同模式中的表

2.1 准备工作

--创建表空间enmtbs、数据库enmdb、用户robin,并授予用户robin SYSADMIN权限

su - omm
gsql -r
create tablespace enmtbs relative location 'tablespace/enmtbs1';
create database enmdb with tablespace = enmtbs;
create user robin identified by 'robin@123';
alter user robin sysadmin;
\q



--用户robin登录数据库enmdb,并创建tmpschm1/2

gsql -d enmdb -U robin -W robin@123 -r



--创建tmpschm1、tmpschm2模式

create schema tmpschm1;
create schema tmpschm2;


--查看当前的模式搜索顺序,并在会话级重新设置模式搜索路径为模式 tmpschm1

show SEARCH_PATH;
SET SEARCH_PATH TO tmpschm1;


--在数据库enmdb的模式tmpschm1中创建表

create table tmpschm1_tmp_t1(col1 char(100));
insert into tmpschm1_tmp_t1 values('Hello from tmpschm1_tmp_t1 in schema tmpschm1');

\dt

备注:由于当前的模式搜索顺序为模式tmpschm1,因此不需要指定模式名,就可以为tmpschm1.tmpschm1_tmp_t1插入新行。若需在其他模式表中DML,则需使用 SchemaName.TableName

SET SEARCH_PATH TO tmpschm2;
create table tmpschm2_tmp_t1(col1 char(100));
\dt



2.2 第二题客户作业答案

--用户robin登录数据库enmdb

gsql -d enmdb -U robin -W robin@123 -r

--查看当前会话的模式搜索顺序

show SEARCH_PATH;

--查看不同模式下的表

select * from tmpschm1.tmpschm1_tmp_t1;
select * from tmpschm2.tmpschm2_tmp_t1;




3.在会话级设置模式搜索路径为:模式enmschm1,使用SchemaName.TableName的表标识方法访问表(创建表、插入数据和查询表中数据)


--查看当前的模式搜索顺序

show SEARCH_PATH;

--在会话级重新设置模式搜索路径为模式enmschm1

SET SEARCH_PATH TO tmpschm1;

--执行下面的命令,再次查看当前的模式搜索顺序

show SEARCH_PATH;



--SchemaName.TableName的表标识方法 创建表

create table tmpschm1.tmpschm1_tmp_t2(col char(100));

--SchemaName.TableName的表标识方法 插入数据

insert into tmpschm1.tmpschm1_tmp_t2 values('Hello from tmpschm1_tmp_t2 in schema tmpschm1');
insert into tmpschm2.tmpschm1_tmp_t1 values('Hello from tmpschm1_tmp_t1 in schema tmpschm2');

--SchemaName.TableName的表标识方法 查看数据

select * from tmpschm1.tmpschm1_tmp_t1;
select * from tmpschm1.tmpschm1_tmp_t2;
select * from tmpschm2.tmpschm2_tmp_t1;



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

评论