一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者SchemaName.TableName,来访问某个模式下的一个表。
默认情况下访问public模式下的表,可以不用添加模式名前缀。连接数据库
#第一次进入等待15秒
#数据库启动中...
su - omm
gsql -r
root@modb:~# su - omm
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.
1.查看当前数据库下有哪些模式
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1'; CREATE TABLESPACE omm=# CREATE DATABASE musicdb WITH TABLESPACE = music_tbs; CREATE DATABASE omm=# CREATE USER user1 IDENTIFIED BY 'kunpeng@1234'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=# ALTER USER user1 SYSADMIN; ALTER ROLEomm=# \q 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=> create schema schm1 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm2 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm3 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=> create schema schm4 AUTHORIZATION user1;
musicdb=> CREATE SCHEMA3.在数据库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));
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');
musicdb=> INSERT 0 1
musicdb=> insert into schm4.ttt values('Hello! from schema schm4 44444');
INSERT 0 1musicdb=> create or replace view my_tables as
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');
CREATE VIEW
musicdb=> select * from my_tables;
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 | public | my_tables | VIEW
musicdb | schm4 | ttt | BASE TABLE
musicdb | schm3 | ttt | BASE TABLE
musicdb | schm2 | ttt | BASE TABLE
musicdb | schm1 | ttt | BASE TABLE
(8 rows)4.访问musicdb数据库下不同模式的同名表
musicdb=> show SEARCH_PATH;
search_path
----------------
"$user",public
(1 row)
musicdb=> select * from schm1.ttt;
col
----------------------------------
Hello! from schema schm1 11111
Hello! from schema schm1 11111
(2 rows)
musicdb=> select * from schm2.ttt;
musicdb=> col
----------------------------------
Hello! from schema schm2 22222
(1 row)
musicdb=> select * from schm3.ttt;
musicdb=> col
----------------------------------
Hello! from schema schm3 33333
(1 row)
musicdb=> select * from schm4.ttt;
musicdb=> 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, Policyadmin, UseFT | {}
user1 | Sysadmin
| {}
musicdb=> \dn
db4ai | omm
dbe_perf | omm
dbe_pldebugger | omm
dbe_pldeveloper | omm
pkg_service | omm
public | omm
schm1 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
(14 rows)
List of schemas
Name | Owner
-----------------+-------
blockchain | omm
cstore | ommmusicdb=> 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 -----------+----------------------------------------------------------------- -------------------------------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} user1 | Sysadmin | {} List of roles Role name | Attributes | Member of 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)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




