学习目标
学习openGauss表的创建、搜索路径和访问方法等。openGauss的搜索路径指的是模式,可以通过schema_name.table_name访问表,当不指定schema_name时,默认访问的是搜索路径中的模式,具体可以通过show search_path命令查看,默认顺序是:$user、public模式,实际上就是public模式,创建模式可以通过
create schema schema_name; schema 是属于用户的,可以alter authorition to 改变用户。
课程作业
连接数据库 gsql -r
1.创建一个表(默认,不指定模式),查看该表在那个模式下
create table test_t(col1 char(20));
\dt+ test_t
| Schema | Name | Type | Owner | Size | Storage | Description |
|---|---|---|---|---|---|---|
| public | test_t | table | omm | 0 bytes | {orientation=row,compression=no} |
可以看到不指定模式Schema时,默认在public模式下
2.使用一个用户连接到enmdb数据库,测试该用户可以访问不同模式中的表
前提条件,创建用户
-- DROP USER if exists user1;
CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
ALTER USER user1 SYSADMIN;
-- 查看当前有哪些模式
\dn+
| Name | Owner |
|---|---|
| blockchain | omm |
| cstore | omm |
| db4ai | omm |
| dbe_perf | omm |
| dbe_pldebugger | omm |
| dbe_pldeveloper | omm |
| enmschm1 | user1 |
| enmschm2 | user1 |
| pkg_service | omm |
| public | omm |
| snapshot | omm |
| sqladvisor | omm |
\c enmdb user1;
show search_path;
-- 查看有模式'public', 'enmschm1', 'enmschm2'下有哪些表
select table_catalog,table_schema,table_name,table_type
from information_schema.tables
where table_schema in ('public', 'enmschm1', 'enmschm2');
| table_catalog | table_schema | table_name | table_type |
|---|---|---|---|
| enmdb | public | test_t | BASE TABLE |
| enmdb | enmschm2 | testtable2 | BASE TABLE |
| enmdb | enmschm2 | testtable1 | BASE TABLE |
| enmdb | enmschm2 | testtable | BASE TABLE |
| enmdb | enmschm1 | testtable | BASE TABLE |
| enmdb | public | testtable | BASE TABLE |
-- 测试该用户可以访问不同模式中的表
select * from public.testtable;
select * from enmschm1.testtable;
select * from enmschm2.testtable;
select * from enmschm2.testtable1;
select * from enmschm2.testtable2;
3.在会话级设置模式搜索路径为:模式enmschm1,使用SchemaName.TableName的表标识方法访问表(创建表、插入数据和查询表中数据)
show SEARCH_PATH;
SET SEARCH_PATH TO enmschm1;
show SEARCH_PATH;
create table enmschm1.tb_1 (col1 int);
create table enmschm2.tb_1 (col1 int);
create table enmschm1.tb_2 (col1 int);
insert into enmschm1.tb_1 values (1);
insert into enmschm2.tb_1 values (2);
insert into enmschm1.tb_2 values (2);
select * from enmschm1.tb_1;
select * from enmschm2.tb_1;
select * from enmschm1.tb_2;
最后修改时间:2022-12-06 18:04:28
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




