openGauss每日一练第8天 | openGauss中一个数据库可以存储在多个表空间中
创始人
2024-03-06 16:44:06
0

一、学习目标

学习表空间与数据库对象的关系。
在musicdb数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间music_tbs中,当我们在musicdb数据库中创建表warehouse_t1的时候,明确指定在表空间ds_location1中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.

二、课程学习

1.连接数据库,准备测试环境

--进入数据库omm,创建表空间、测试数据库
su - omm
gsql -r
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';
CREATE DATABASE musicdb  WITH TABLESPACE = music_tbs;--执行下面的SQL语句,创建用户user1:CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';--授予user1数据库系统的SYSADMIN权限:ALTER USER user1 SYSADMIN;
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=# 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  musicdb2;
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
omm=# 
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 ROLE

2.创建表空间、查看表空间

--执行下面的命令,查看当前表空间:
\db
--创建一个新的名为ds_location1的表空间:CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/tablespace_1';
--执行下面的命令,查看实例当前有哪些表空间:
\db
omm=# \dbList of tablespacesName    | Owner |      Location       
------------+-------+---------------------music_tbs  | omm   | tablespace/test_ts1pg_default | omm   | pg_global  | omm   | 
(3 rows)omm=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/tablespace_1';
CREATE TABLESPACE
omm=# \dbList of tablespacesName     | Owner |        Location         
--------------+-------+-------------------------ds_location1 | omm   | tablespace/tablespace_1music_tbs    | omm   | tablespace/test_ts1pg_default   | omm   | pg_global    | omm   | 
(4 rows)omm=# 

3.使用user1用户,访问musicdb数据库 ,在表空间ds_location1上创建表warehouse_t1,查看musicdb数据库目前有哪些表:

\c musicdb user1create table warehouse_t1 (col1 char(10)) tablespace ds_location1;
select table_catalog, table_schema, table_name, table_typefrom information_schema.tableswhere table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
musicdb=> \c musicdb user1
Password for user user1: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "user1".
musicdb=> create table warehouse_t1 (col1 char(10)) tablespace ds_location1;
CREATE TABLE
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');table_catalog |  table_schema   |  table_name  | table_type 
---------------+-----------------+--------------+------------musicdb       | db4ai           | snapshot     | BASE TABLEmusicdb       | dbe_pldeveloper | gs_errors    | BASE TABLEmusicdb       | dbe_pldeveloper | gs_source    | BASE TABLEmusicdb       | public          | warehouse_t1 | BASE TABLE
(4 rows)musicdb=> 

4.查询表在那个表空间
系统表在默认表空间,非系统表在指定的表空间中(否则在默认表空间)

--建表warehouse_t1指定表空间ds_location1,查看表warehouse_t1所在的表空间:
select * from pg_tables where tablename = 'warehouse_t1';--创建表warehouse_t12未指定表空间,则在默认表空间(不显示默认表空间名)
create table warehouse_t12 (col1 char(10));
select * from pg_tables where tablename = 'warehouse_t12';
musicdb=> select * from pg_tables where tablename = 'warehouse_t1';schemaname |  tablename   | tableowner |  tablespace  | hasindexes | hasrules | hastriggers | tablecreator |            created            |         last_ddl_time      ------------+--------------+------------+--------------+------------+----------+-------------+--------------+-------------------------------+----------------------------
---public     | warehouse_t1 | user1      | ds_location1 | f          | f        | f           | user1        | 2022-12-01 11:24:42.613711+08 | 2022-12-01 11:24:42.613711+
08
(1 row)musicdb=> create table warehouse_t12 (col1 char(10));
CREATE TABLE
musicdb=> select * from pg_tables where tablename = 'warehouse_t12';schemaname |   tablename   | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator |            created            |         last_ddl_time       ------------+---------------+------------+------------+------------+----------+-------------+--------------+-------------------------------+-----------------------------
--public     | warehouse_t12 | user1      |            | f          | f        | f           | user1        | 2022-12-01 11:27:36.858823+08 | 2022-12-01 11:27:36.858823+0
8
(1 row)musicdb=> 

6.查看openGuass数据库的默认表空间

select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid
musicdb=> select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;datname  | dattablespace |  spcname   
-----------+---------------+------------template1 |          1663 | pg_defaultomm       |          1663 | pg_defaultmusicdb   |         16389 | music_tbstemplate0 |          1663 | pg_defaultpostgres  |          1663 | pg_default
(5 rows)musicdb=> 

7.查询数据库的默认表空间上的对象

select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
from pg_class a  
where a.relkind in ('r', 'i')  
and reltablespace='0'  
order by a.relpages desc;
musicdb=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
musicdb-> from pg_class a  
musicdb-> where a.relkind in ('r', 'i')  
and reltablespace='0'  
musicdb-> musicdb-> order by a.relpages desc;relname                     | relkind | relpages | pg_size_pretty | reltablespace | relowner 
------------------------------------------------+---------+----------+----------------+---------------+----------pg_attribute                                   | r       |      184 | 1472 kB        |             0 |       10pg_proc                                        | r       |      140 | 1120 kB        |             0 |       10pg_depend                                      | r       |       59 | 472 kB         |             0 |       10pg_class                                       | r       |       52 | 416 kB         |             0 |       10pg_attribute_relid_attnam_index                | i       |       44 | 352 kB         |             0 |       10pg_proc_proname_all_args_nsp_index             | i       |       41 | 328 kB         |             0 |       10pg_proc_proname_args_nsp_new_index             | i       |       39 | 312 kB         |             0 |       10pg_proc_proname_args_nsp_index                 | i       |       39 | 312 kB         |             0 |       10pg_rewrite                                     | r       |       37 | 296 kB         |             0 |       10pg_description                                 | r       |       31 | 248 kB         |             0 |       10pg_attribute_relid_attnum_index                | i       |       30 | 240 kB         |             0 |       10pg_depend_depender_index                       | i       |       34 | 272 kB         |             0 |       10pg_depend_reference_index                      | i       |       34 | 272 kB         |             0 |       10pg_type                                        | r       |       20 | 160 kB         |             0 |       10pg_statistic                                   | r       |       19 | 152 kB         |             0 |       10pg_operator                                    | r       |       15 | 120 kB         |             0 |       10pg_class_relname_nsp_index                     | i       |       14 | 112 kB         |             0 |       10pg_proc_oid_index                              | i       |       13 | 104 kB         |             0 |       10pg_class_tblspc_relfilenode_index              | i       |       10 | 80 kB          |             0 |       10pg_amop                                        | r       |        9 | 72 kB          |             0 |       10
--More-- pg_description_o_c_o_index                     | i       |       16 | 128 kB         |             0 |       10pg_class_oid_index                             | i       |        7 | 56 kB          |             0 |       10sql_features                                   | r       |        7 | 56 kB          |             0 |       10pg_type_typname_nsp_index                      | i       |        7 | 56 kB          |             0 |       10pg_index                                       | r       |        7 | 56 kB          |             0 |       10pg_amop_fam_strat_index                        | i       |        6 | 48 kB          |             0 |       10pg_operator_oprname_l_r_n_index                | i       |        6 | 48 kB          |             0 |       10pg_amop_opr_fam_index                          | i       |        6 | 48 kB          |             0 |       10pg_amop_oid_index                              | i       |        5 | 40 kB          |             0 |       10pg_operator_oid_index                          | i       |        5 | 40 kB          |             0 |       10pg_type_oid_index                              | i       |        5 | 40 kB          |             0 |       10pg_opclass_am_name_nsp_index                   | i       |        4 | 32 kB          |             0 
...gs_column_keys_args                            | r       |        0 | 0 bytes        |             0 |       10gs_client_global_keys_args                     | r       |        0 | 0 bytes        |             0 |       10gs_client_global_keys                          | r       |        0 | 0 bytes        |             0 |       10pg_publication_rel                             | r       |        0 | 0 bytes        |             0 |       10gs_matview                                     | r       |        0 | 0 bytes        |             0 |       10gs_encrypted_proc                              | r       |        0 | 0 bytes        |             0 |       10gs_job_argument                                | r       |        0 | 0 bytes        |             0 |       10warehouse_t12                                  | r       |        0 | 0 bytes        |             0 |    16391gs_asp                                         | r       |        0 | 0 bytes        |             0 |       10gs_matview_dependency                          | r       |        0 | 0 bytes        |             0 |       10pg_object                                      | r       |        0 | 8192 bytes     |             0 |       10gs_auditing_policy_privileges                  | r       |        0 | 0 bytes        |             0 |       10pg_synonym                                     | r       |        0 | 0 bytes        |             0 |       10
(316 rows)

8.查询表空间ds_location1上的对像

\c musicdb user1
select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
from pg_class a, pg_tablespace tb  
where a.relkind in ('r', 'i')  
and a.reltablespace=tb.oid  
and tb.spcname='ds_location1'  
order by a.relpages desc;
musicdb=> \c musicdb user1
Password for user user1: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb" as user "user1".
musicdb=> musicdb-> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
musicdb-> from pg_class a, pg_tablespace tb  
where a.relkind in ('r', 'i')  
musicdb-> and a.reltablespace=tb.oid  
musicdb-> and tb.spcname='ds_location1'  
musicdb-> order by a.relpages desc;relname    | relkind | relpages | pg_size_pretty | reltablespace | relowner 
--------------+---------+----------+----------------+---------------+----------warehouse_t1 | r       |        0 | 0 bytes        |         16396 |    16391
(1 row)musicdb=> 

三、课后作业

1. 创建表空间newtbs1、ds_location1,查看表空间

create tablespace newtbs1 relative location ‘tablespace/tablespace_2’;
create tablespace ds_location1 relative location ‘tablespace/tablespace_1’;

musicdb=> \dbList of tablespacesName    | Owner |      Location       
------------+-------+---------------------music_tbs  | omm   | tablespace/test_ts1pg_default | omm   | pg_global  | omm   | 
(3 rows)musicdb=> 
musicdb=> 
musicdb=> create tablespace newtbs1 relative location 'tablespace/tablespace_2';
CREATE TABLESPACE
musicdb=> create tablespace ds_location1 relative location 'tablespace/tablespace_1';
CREATE TABLESPACE
musicdb=> \dbList of tablespacesName     | Owner |        Location         
--------------+-------+-------------------------ds_location1 | user1 | tablespace/tablespace_1music_tbs    | omm   | tablespace/test_ts1newtbs1      | user1 | tablespace/tablespace_2pg_default   | omm   | pg_global    | omm   | 
(5 rows)musicdb=> 

2. 创建一个数据库newdb1,默认表空间为newtbs1

CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;
\l

musicdb=> CREATE DATABASE newdb1  WITH TABLESPACE = newtbs1;
CREATE DATABASE
musicdb=> \lList of databasesName    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------musicdb   | omm   | UTF8     | C       | C     | newdb1    | user1 | UTF8     | C       | C     | omm       | omm   | UTF8     | C       | C     | postgres  | omm   | UTF8     | C       | C     | template0 | omm   | UTF8     | C       | C     | =c/omm           +|       |          |         |       | omm=CTc/ommtemplate1 | omm   | UTF8     | C       | C     | =c/omm           +|       |          |         |       | omm=CTc/omm
(6 rows)

3. 创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)

--执行下面的SQL语句,创建用户user5:CREATE USER user5 IDENTIFIED BY 'kunpeng@1234';
--授予user1数据库系统的SYSADMIN权限:
ALTER USER user5 SYSADMIN;
--使用user5用户,访问newdb1数据库
\c newdb1 user5
create table newt1 (id int primary key,col1 char(10)) tablespace ds_location1;
musicdb=> CREATE USER user5 IDENTIFIED BY 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
musicdb=> CREATE ROLEmusicdb=> ALTER USER user5 SYSADMIN;
ALTER ROLE
musicdb=> \duomm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}user1     | Sysadmin                                                                                                         | {}user5     | Sysadmin                                                                                                         | {}List of rolesRole name |                                                    Attributes                                                    | Member of 
-----------+------------------------------------------------------------------------------------------------------------------+-----------gaussdb   | Sysadmin                                                                                                         | {}
musicdb=> 
musicdb=> \c newdb1 user5
Password for user user5: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "user5".
newdb1=> create table newt1 (id int primary key,col1 char(10)) tablespace ds_location1;
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "newt1_pkey" for table "newt1"
CREATE TABLE
newdb1=> 

4. 查看表所在的表空间

–建表newt1指定表空间ds_location1,查看表newt1所在的表空间:
select * from pg_tables where tablename = ‘newt1’;

newdb1=> select * from pg_tables where tablename = 'newt1';schemaname | tablename | tableowner |  tablespace  | hasindexes | hasrules | hastriggers | tablecreator |            created            |         last_ddl_time         
------------+-----------+------------+--------------+------------+----------+-------------+--------------+-------------------------------+-------------------------------public     | newt1     | user5      | ds_location1 | t          | f        | f           | user5        | 2022-12-01 11:47:28.821703+08 | 2022-12-01 11:47:28.821703+08
(1 row)newdb1=> 

5. 查看表空间newtbs1、 ds_location1上的对象

\c newdb1 user5
select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
from pg_class a, pg_tablespace tb  
where a.relkind in ('r', 'i')  
and a.reltablespace=tb.oid  
and tb.spcname='newtbs1'  
order by a.relpages desc;select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
from pg_class a, pg_tablespace tb  
where a.relkind in ('r', 'i')  
and a.reltablespace=tb.oid  
and tb.spcname='ds_location1'  
order by a.relpages desc;
newdb1=> \c newdb1 user5
Password for user user5: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "user5".
newdb1=> 
newdb1=> 
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
newdb1-> from pg_class a, pg_tablespace tb  
newdb1-> where a.relkind in ('r', 'i')  
newdb1-> and a.reltablespace=tb.oid  
newdb1-> and tb.spcname='newtbs1'  
newdb1-> order by a.relpages desc;relname | relkind | relpages | pg_size_pretty | reltablespace | relowner 
---------+---------+----------+----------------+---------------+----------
(0 rows)newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
newdb1-> from pg_class a, pg_tablespace tb  
newdb1-> where a.relkind in ('r', 'i')  
newdb1-> and a.reltablespace=tb.oid  
newdb1-> newdb1-> and tb.spcname='ds_location1'  
order by a.relpages desc;relname | relkind | relpages | pg_size_pretty | reltablespace | relowner 
---------+---------+----------+----------------+---------------+----------newt1   | r       |        0 | 0 bytes        |         16408 |    16410
(1 row)newdb1=> 

相关内容

热门资讯

从展厅到蓝天,创新保险护航低空... 在四川,无人机打通包裹进村“最后一公里”;在长三角,“五一”假期首条跨省低空载客航线带客观光忙不停…...
江苏65所技工院校今年拟增设1... 交汇点讯 江苏省人社厅5月14日发布消息,根据《江苏省技工院校教学管理规范》要求,经专家审核、综合评...
王宁率云南省友好代表团访问马来...   云南网讯  5月11日至14日,省委书记、省人大常委会主任王宁率云南省友好代表团访问马来西亚,深...
10吨“《浪潮》”激荡西岸大剧... 转自:上观新闻在左联成立95周年之际,聚焦革命烈士与文学志士的舞台剧《浪潮》今起在西岸大剧院上演至1...
硕鼠硕鼠,无食我黍 ■毕传国 转自:嘉兴日报硕鼠硕鼠,无食我黍 ■毕传国
储罐相继爆炸,3人死亡! 转自:中国应急管理2024年9月11日9时14分许河北省张家口市怀来县大黄庄镇张家口龙潭酿酒有限责任...
易生活控股拟获中国创新投资拆让... .ct_hqimg {margin: 10px 0;} .hqimg_wrapper {text-a...
郑钦文首胜萨巴伦卡!刚刚发文回... 当地时间5月14日,在WTA1000罗马站1/4决赛中,中国选手郑钦文以6-4、6-3战胜头号种子萨...
父亲的平凡一生   □盘和林  2025年4月26日上午,老父亲入土为安,按照他生前遗愿,葬于我奶奶坟墓旁边。享年7...
冀菜:冀味新潮 转自:河北日报大河之北·河北非遗文化解读 美食篇冀菜:冀味新潮 5月3日,游客在保定宴饮食博物...
不满16岁,岂能入职当主播?   本报讯(记者刘立新 通讯员李贺 常静静)“公司之前拖欠我的工钱拿到啦,我打算去职业技术学校学门手...
是秀场更是生意场 文博会“交易...   羊城晚报记者 李艺戈 林园  “今年我们还要来。”第二十一届中国(深圳)国际文化产业博览交易会(...
卡夫亨氏计划斥资30亿美元升级... (转自:观点网)观点网讯:5月15日,卡夫亨氏宣布将斥资30亿美元升级美国工厂,预计创造约3500个...
“5·19中国旅游日”倒计时东... 转自:中国旅游报 本报讯(记者 魏彪)5月14日,在2025年“5·19中国旅游日”倒计时城市山东省...
我省抽查化肥等产品440批发现... 本报讯 记者朱柏玲报道 日前,辽宁省市场监督管理局对2025年农用薄膜等产品质量监督抽查情况进行通报...
菲律宾中期选举初步结果出炉杜特... 新华社马尼拉5月14日电 菲律宾媒体14日公布的中期选举初步统计结果显示,菲总统马科斯所在阵营参议院...
恒瑞医药拟全球发售2.245亿...   恒瑞医药(01276)于2025年5月15日-5月20日招股,拟全球发售2.245亿股H股,其中...
@来深建设者,深圳福彩“常回家...   记者从深圳市福彩中心获悉,深圳“爱心福彩·常回家看看”第八届公益活动于5月13日开放报名通道,有...
多地更新数字人民币发展计划,涉... 2025年以来,已有北京、广州、上海等多地发布数字人民币试点工作发展计划,不断创新扩大应用场景,推动...
总能找到改变的“钥匙”    2025年3月,江苏省苏州市吴江区检察院举办检察开放日活动,李冬梅为参加活动的女孩讲授自护知识...