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

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

原创 落笔花生 2022-11-30
348

学习目标

学习openGauss数据库、用户和模式的关系和访问方式,理解模式是在数据库层面,用户是在实例层面

课程学习

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

1.创建模式、查看模式

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.
--进入数据库omm,创建表空间、测试数据库
omm=# drop database if exists musicdb;
NOTICE:  database "musicdb" does not exist, skipping
DROP DATABASE
omm=# drop database if exists musicdb1;
NOTICE:  database "musicdb1" does not exist, skipping
DROP DATABASE
omm=# drop database if exists musicdbb2;
NOTICE:  database "musicdb2" does not exist, skipping
DROP DATABASE
omm=# drop database if exists musicdb3;
NOTICE:  database "musicdb3" does not exist, skipping
DROP DATABASE
omm=# drop tablespace if exists music_tbs;
NOTICE:  Tablespace "music_tbs" does not exist, skipping.
DROP TABLESPACE
omm=# create tablespace music_tbs relative location ''tablespace/test_ts1';
CREATE TABLESPACE
omm=# create database musicdb with tablespace = music_tbs ;
CREATE DATABASE--执行下面的SQL语句,创建用户user1:
omm=# create user user1 identified by 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
omm=# CREATE ROLE
--授予user1数据库系统的SYSADMIN权限:
omm=# alter user user1 sysadmin ; ALTER ROLE
--使用用户user1连接到数据库musicdb,首先查看当前数据库下有哪些模式;
omm=# \q omm@modb:~$ gsql -d musicdb -U user1 -p5432 -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个模式: sche1、 sche2、 sche3、 sche4

–用户user1在数据库musicdb中,创建了4个模式:
musicdb=> create schema sche1 authorization user1;
CREATE SCHEMA
musicdb=> create schema sche2 authorization user1;
CREATE SCHEMA
musicdb=> create schema sche3 authorization user1;
CREATE SCHEMA
musicdb=> create schema sche4 authorization user1;
CREATE SCHEMA–查看musicdb数据库下有哪些模式:
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
 sche1           | user1
 sche3           | user1
 sche4           | user1
 sche2           | user1
 snapshot        | omm
 sqladvisor      | omm
(14 rows)
–除了可以用gsql的元命令\dn来查看数据库有哪些模式,还可以执行下面的SQL语句,查看某个数据库下有哪些模式:
musicdb=> select catalog_name,schema_name,schema_owner 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 | sche1 | user1 musicdb | sche2 | user1 musicdb | sche3 | user1 musicdb | sche4 | user1 (17 rows)

3.在数据库musicdb的不同的模式下创建同名的表:

--在不同模式下,创建相同的表
musicdb=> create table sche1.ttt(col varchar(100)); CREATE TABLE
musicdb=> create table sche2.ttt(col varchar(100)); CREATE TABLE
musicdb=> create table sche3.ttt(col varchar(100)); CREATE TABLE musicdb=> create table sche4.ttt(col varchar(100)); CREATE TABLE
--执行下面的SQL语句,往4个模式中的表ttt分别插入一条数据: --在同一个数据库下,可以直接使用SchemaName.TableName来指定一个表,可以省略数据库名。
musicdb=> insert into sche1.ttt values ('Hello! from schema sche1 111'); INSERT 0 1musicdb=> insert into sche2.ttt values ('Hello ! from schema sche2 222'); INSERT 0 1 musicdb=> insert into sche3.ttt values ('Hello! from schema sche3 333'); INSERT 0 1 musicdb=> insert into sche4.ttt values ('Hello! from schema sch4 444'); INSERT 0 1
--执行下面的SQL语句,查看musicdb数据库目前有哪些表 --创建视图: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'); 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 | sche2 | ttt | BASE TABLE musicdb | sche4 | ttt | BASE TABLE musicdb | sche3 | ttt | BASE TABLE musicdb | sche1 | ttt | BASE TABLE (8 rows)

4.查看用户在数据库中搜索模式的顺序:

--查看默认的搜索模式的顺序
musicdb=> show search_path ;
(1 row)

musicdb=>   search_path   
----------------
 "$user",public
--访问musicdb数据库下其他模式的表,需要指定模式名前缀:
musicdb=> select * from sche1.ttt; col ------------------------------ Hello! from schema sche1 111 (1 row)
musicdb=> select * from sche2.ttt; col ------------------------------- Hello ! from schema sche2 222 (1 row)
musicdb=> select * from sche3.ttt; col ------------------------------ Hello! from schema sche3 333 (1 row) musicdb=> select * from sche4.ttt; col ----------------------------- Hello! from schema sch4 444 (1 row)

5.模式是在数据库层面,用户是在实例层面

--登录musicdb数据库,查看用户和模式
musicdb=> gsql -d musicdb  -U user1 -p 5432 -W kunpeng@1234 -r
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
     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
 sche1           | user1
 sche3           | user1
 sche4           | user1
 sche2           | user1
 snapshot        | omm
 sqladvisor      | omm
(14 rows)

musicdb-> \q--登录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.

omm=# \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                                                                                                         | {}

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

课程作业(使用上面的环境继续试验)

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
 sche1           | user1
 sche3           | user1
 sche4           | user1
 shce2           | user1
 snapshot        | omm
 sqladvisor      | omm
(14 rows)

2.然后为数据库musicdb创建4个模式,名称自定义

musicdb=> create schema xx1 authorization user1;
CREATE SCHEMA
musicdb=> create  schema  xx2 authorization  user1;
CREATE SCHEMA
musicdb=> create schema  xx3 authorization  user1;
CREATE SCHEMA
musicdb=> create schema xx4 authorization user1;
CREATE SCHEMA
musicdb=> \dn List of schemas Name | Owner -----------------+------- blockchain | omm cstore | omm db4ai | omm xx3 | user1 xx4 | user1 (18 rows) dbe_perf | omm dbe_pldebugger | omm dbe_pldeveloper | omm pkg_service | omm public | omm sche1 | user1 sche3 | user1 sche4 | user1 shce2 | user1 snapshot | omm sqladvisor | omm xx1 | user1 xx2 | user1

3.在数据库musicdb的不同的模式下创建同名的表

musicdb=> create table xx1.tb(col varchar(100));
CREATE TABLE
musicdb=> create table xx2.tb(col varchar(100)); CREATE TABLE
musicdb=> create table xx3.tb(col varchar(100)); CREATE TABLE
musicdb=> create table xx4.tb(col varchar(100)); CREATE TABLE
musicdb=> insert into xx1.tb values ('Hello 111'); INSERT 0 1 musicdb=> insert into xx2.tb values ('Hello 222'); INSERT 0 1 musicdb=> insert into xx3.tb values ('Hello 333'); INSERT 0 1 musicdb=> insert into xx4.tb values ('Hello 444'); INSERT 0 1
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'); 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 | xx4 | tb | BASE TABLE musicdb | xx3 | tb | BASE TABLE musicdb | xx2 | tb | BASE TABLE musicdb | xx1 | tb | BASE TABLE musicdb | public | my_tables | VIEW musicdb | shce2 | ttt | BASE TABLE musicdb | sche4 | ttt | BASE TABLE musicdb | sche3 | ttt | BASE TABLE musicdb | sche1 | ttt | BASE TABLE (12 rows)

4.访问musicdb数据库下不同模式的同名表

musicdb=> show search_path ;
musicdb=>   search_path   
----------------
 "$user",public
(1 row)
musicdb=> select  * from xx1.tb ;
    col    
-----------
 Hello 111
(1 row)

musicdb=> select  * from xx2.tb ;
    col    
-----------
 Hello 222
(1 row)

musicdb=> select  * from xx3.tb ;
    col    
-----------
 Hello 333
(1 row)

musicdb=> select  * from xx4.tb ;
    col    
-----------
 Hello 444
(1 row)

5.实验理解:模式是在数据库层面,用户是在实例层面

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

评论