学习openGauss数据库、用户和模式的关系和访问方式,理解模式是在数据库层面,用户是在实例层面
一个用户连接到数据库后,可以在这个数据库中创建多个模式。要访问这些模式,可以使用DatabaseName.SchemaName.TableName或者SchemaName.TableName,来访问某个模式下的一个表。
1.创建模式、查看模式
--默认情况下访问public模式下的表,可以不用添加模式名前缀。
--进入数据库omm,创建表空间、测试数据库
drop DATABASE IF EXISTS musicdb;
drop DATABASE IF EXISTS musicdb1;
drop DATABASE IF EXISTS musicdb2;
drop DATABASE IF EXISTS musicdb3;
drop tablespace IF EXISTS music_tbs;
-- CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
omm=> \db
List of tablespaces
Name | Owner | Location
------------+-------+---------------------
music_tbs | omm | tablespace/test_ts1
pg_default | omm |
pg_global | omm |
-- CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
omm=# create database musicdb with tablespace = music_tbs;
CREATE DATABASE
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
musicdb | omm | UTF8 | C | C |
omm | omm | UTF8 | C | C |
postgres | omm | UTF8 | C | C |
template0 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | C | C | =c/omm +
| | | | | omm=CTc/omm
omm=# (5 rows)
--
--执行下面的SQL语句,创建用户user1:
CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
--授予user1数据库系统的SYSADMIN权限:
ALTER USER user1 SYSADMIN;
--使用用户user1连接到数据库musicdb,首先查看当前数据库下有哪些模式;
-- \q
-- gsql -d musicdb -U user1 -p 5432 -W kunpeng@1234 -r
-- \dn
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个模式: schm1、 schm2、 schm3、 schm4.
–用户user1在数据库musicdb中,创建了4个模式:
create schema schm1 AUTHORIZATION user1;
create schema schm2 AUTHORIZATION user1;
create schema schm3 AUTHORIZATION user1;
create schema schm4 AUTHORIZATION user1;
–查看musicdb数据库下有哪些模式:
\dn
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 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
(14 rows)
–除了可以用gsql的元命令\dn来查看数据库有哪些模式,还可以执行下面的SQL语句,查看某个数据库下有哪些模式:
SELECT catalog_name, schema_name, schema_owner
FROM information_schema.schemata;
musicdb=> Smusicdb-> ELECT 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 | schm3 | user1
musicdb | schm4 | user1
musicdb | schm1 | user1
musicdb | schm2 | user1
3.在数据库musicdb的不同的模式下创建同名的表:
--在不同模式下,创建相同的表
create table schm1.ttt(col varchar(100));
create table schm2.ttt(col varchar(100));
create table schm3.ttt(col varchar(100));
create table schm4.ttt(col varchar(100));
--执行下面的SQL语句,往4个模式中的表ttt分别插入一条数据:
--在同一个数据库下,可以直接使用SchemaName.TableName来指定一个表,可以省略数据库名。
insert into schm1.ttt values('Hello! from schema schm1 11111');
insert into schm2.ttt values('Hello! from schema schm2 22222');
insert into schm3.ttt values('Hello! from schema schm3 33333');
insert into schm4.ttt values('Hello! from schema schm4 44444');
--执行下面的SQL语句,查看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');
--查看视图:
select * from my_tables;
---
musicdb=> 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');
musicdb=> CREATE VIEW
musicdb=>
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 | public | ggg | BASE TABLE
musicdb | public | schmttt | BASE TABLE
musicdb | schm4 | ttt | BASE TABLE
musicdb | schm3 | ttt | BASE TABLE
musicdb | schm2 | ttt | BASE TABLE
musicdb | schm1 | ttt | BASE TABLE
(10 rows)
musicdb=> \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+---------+-------+-------+----------------------------------
public | ggg | table | user1 | {orientation=row,compression=no}
public | schmttt | table | user1 | {orientation=row,compression=no}
(2 rows)
4.查看用户在数据库中搜索模式的顺序:
--查看默认的搜索模式的顺序
show SEARCH_PATH;
--访问musicdb数据库下其他模式的表,需要指定模式名前缀:
select * from schm1.ttt;
select * from schm2.ttt;
select * from schm3.ttt;
select * from schm4.ttt;
---
musicdb=> show search_path;
search_path
----------------
"$user",public
(1 row)
musicdb=> select * from schm1.ttt;
col
-----
(0 rows)
5.模式是在数据库层面,用户是在实例层面:
--登录musicdb数据库,查看用户和模式
gsql -d musicdb -U user1 -p 5432 -W kunpeng@1234 -r
\du
\dn
\q
--登录omm数据库,查看用户和模式
gsql -r
\du
\dn
\
5.作业
1.查看当前数据库下有哪些模式
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 | user1
schm2 | user1
schm3 | user1
schm4 | user1
snapshot | omm
sqladvisor | omm
2.然后为数据库musicdb创建4个模式,名称自定义
create schema c1 AUTHORIZATION user1;
create schema c2 AUTHORIZATION user1;
create schema c3 AUTHORIZATION user1;
create schema c4 AUTHORIZATION user1;
musicdb=# create schema c1 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=# create schema c2 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=# create schema c3 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=# create schema c4 AUTHORIZATION user1;
CREATE SCHEMA
musicdb=# \dn
List of schemas
Name | Owner
-----------------+-------
blockchain | omm
c1 | user1
c2 | user1
c3 | user1
c4 | user1
cstore | omm
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
(18 rows)
3.在数据库musicdb的不同的模式下创建同名的表
create table c1.user(name varchar(100));
create table c2.user(name varchar(100));
create table c3.user(name varchar(100));
create table c4.user(name varchar(100));
musicdb=# create table c1.user(name varchar(100));
CREATE TABLE
musicdb=# create table c2.user(name varchar(100));
CREATE TABLE
musicdb=# create table c3.user(name varchar(100));
CREATE TABLE
musicdb=# create table c4.user(name varchar(100));
CREATE TABLE
musicdb=# select * from my_tables;
musicdb | db4ai | snapshot | BASE TABLE
musicdb | dbe_pldeveloper | gs_errors | BASE TABLE
musicdb | dbe_pldeveloper | gs_source | BASE TABLE
musicdb | c4 | user | BASE TABLE
musicdb | c3 | user | BASE TABLE
musicdb | c2 | user | BASE TABLE
musicdb | c1 | user | BASE TABLE
musicdb | public | my_tables | VIEW
table_catalog | table_schema | table_name | table_type
---------------+-----------------+------------+------------
musicdb | schm3 | ttt | BASE TABLE
musicdb | schm2 | ttt | BASE TABLE
musicdb | public | ggg | BASE TABLE
musicdb | public | schmttt | BASE TABLE
musicdb | schm4 | ttt | BASE TABLE
musicdb | schm1 | ttt | BASE TABLE
(14 rows)
4.访问musicdb数据库下不同模式的同名表
select * from c1.user;
select * from c2.user;
select * from c3.user;
select * from c4.user;
musicdb=# select * from c1.user;
name
------
(0 rows)
musicdb=# select * from c2.user;
name
------
(0 rows)
musicdb=# select * from c3.user;
name
------
(0 rows)
musicdb=# select * from c4.user;
name
------
(0 rows)
5.实验理解:模式是在数据库层面,用户是在实例层面
最后修改时间:2022-11-30 21:15:07
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




