学习openGauss数据库、用户和模式的关系和访问方式,理解模式是在数据库层面,用户是在实例层面
一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者SchemaName.TableName,来访问某个模式下的一个表。
默认情况下访问public模式下的表,可以不用添加模式名前缀。
1.查看当前数据库下有哪些模式
omm@modb:~$ gsql -d musicdb -U user1 -p 5432 -W kunpeng@1234 -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.
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
(10 rows)
2.然后为数据库musicdb创建4个模式,名称可以自定义
musicdb=> \h create schema
Command: CREATE SCHEMA
Description: define a new schema
Syntax:
CREATE SCHEMA schema_name
[ AUTHORIZATION user_name ] [WITH BLOCKCHAIN] [ schema_element [ ... ] ];
musicdb=> create schema schm1 authorization omm;
CREATE SCHEMA
musicdb=> create schema schm2 authorization omm;
CREATE SCHEMA
musicdb=> create schema schm3 authorization omm;
CREATE SCHEMA
musicdb=> create schema schm4 authorization omm;
CREATE SCHEMA
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
schm1 | omm
schm2 | omm
schm3 | omm
schm4 | omm
snapshot | omm
sqladvisor | omm
(14 rows)
musicdb=> SELECT catalog_name, schema_name, schema_owner
musicdb-> FROM information_schema.schemata;
catalog_name | schema_name | schema_owner
--------------+--------------------+--------------
musicdb | pg_toast | omm
musicdb | cstore | omm
musicdb | pkg_service | omm
musicdb | dbe_perf | omm
musicdb | snapshot | omm
musicdb | blockchain | omm
musicdb | pg_catalog | omm
musicdb | public | omm
musicdb | sqladvisor | omm
musicdb | dbe_pldebugger | omm
musicdb | dbe_pldeveloper | omm
musicdb | information_schema | omm
musicdb | db4ai | omm
musicdb | schm1 | omm
musicdb | schm2 | omm
musicdb | schm3 | omm
musicdb | schm4 | omm
(17 rows)
3.在数据库musicdb的不同的模式下创建同名的表
musicdb=>
musicdb=> create table schm1.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm2.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm3.ttt(col varchar(100));
CREATE TABLE
musicdb=> create table schm4.ttt(col varchar(100));
musicdb=> CREATE TABLE
musicdb=> insert into schm1.ttt values('Hello! from schema schm1 11111');
INSERT 0 1
musicdb=> insert into schm2.ttt values('Hello! from schema schm2 22222');
INSERT 0 1
musicdb=> insert into schm3.ttt values('Hello! from schema schm3 33333');
INSERT 0 1
musicdb=> insert into schm4.ttt values('Hello! from schema schm4 44444');
INSERT 0 1
musicdb=> select table_catalog, table_schema, table_name, table_type
musicdb-> from information_schema.tables
musicdb-> where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
musicdb | schm3 | ttt | BASE TABLE
musicdb | schm2 | ttt | BASE TABLE
musicdb | schm1 | ttt | BASE TABLE
(7 rows)
musicdb=> table_catalog | table_schema | table_name | table_type
---------------+-----------------+------------+------------
musicdb | db4ai | snapshot | BASE TABLE
musicdb | dbe_pldeveloper | gs_errors | BASE TABLE
musicdb | dbe_pldeveloper | gs_source | BASE TABLE
musicdb | schm4 | ttt | BASE TABLE
4.查看用户在数据库中搜索模式的顺序
musicdb=> show SEARCH_PATH;
musicdb=> search_path
----------------
"$user",public
(1 row)
musicdb=> select * from schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
(1 row)
musicdb=> select * from schm2.ttt;
col
----------------------------------
Hello! from schema schm2 22222
(1 row)
musicdb=> select * from schm3.ttt;
col
----------------------------------
Hello! from schema schm3 33333
(1 row)
musicdb=> select * from schm4.ttt;
col
----------------------------------
Hello! from schema schm4 44444
(1 row)
5.实验理解:模式是在数据库层面,用户是在实例层面
omm@modb:~$ gsql -d musicdb -U user1 -p 5432 -W kunpeng@1234 -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.
musicdb=> \du
List of roles
Role name | Attributes
| Member of
-----------+---------------------------------------------------------------------------------------------------
---------------+-----------
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Poli
cyadmin, UseFT | {}
user1 | Sysadmin
| {}
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
schm1 | omm
schm2 | omm
schm3 | omm
schm4 | omm
snapshot | omm
sqladvisor | omm
(14 rows)
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=# \du
List of roles
Role name | Attributes
| Member of
-----------+---------------------------------------------------------------------------------------------------
---------------+-----------
gaussdb | Sysadmin
| {}
omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Poli
cyadmin, UseFT | {}
user1 | Sysadmin
| {}
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
user1 | user1
(11 rows)
omm=# \q
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




