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

openGauss每日一练第8天 | openGauss中一个数据库可以存储在多个表空间中

原创 张乐 2022-12-01
182

8.1 openGauss每日一练第8天 | openGauss中一个数据库可以存储在多个表空间中

学习目标

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

课程学习

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

#第一次进入等待15秒
#数据库启动中...
su - omm
gsql -r
--进入数据库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';
CREATE DATABASE musicdb  WITH TABLESPACE = music_tbs;

--执行下面的SQL语句,创建用户user1:

 CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
 
--授予user1数据库系统的SYSADMIN权限:

ALTER USER user1 SYSADMIN;

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

--执行下面的命令,查看当前表空间:
\db
 
--创建一个新的名为ds_location1的表空间:
 
 CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/tablespace_1';
 
--执行下面的命令,查看实例当前有哪些表空间:
\db

3.使用user1用户,访问musicdb数据库 ,在表空间ds_location1上创建表warehouse_t1:

\c musicdb user1

 create table warehouse_t1 (col1 char(10)) tablespace ds_location1;

4.查看musicdb数据库目前有哪些表:

select table_catalog, table_schema, table_name, table_type
  from information_schema.tables
   where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
  1. 查询表在那个表空间
    系统表在默认表空间,非系统表在指定的表空间中(否则在默认表空间)
--建表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';

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

select datname,dattablespace,spcname from pg_database d, pg_tablespace t where d.dattablespace=t.oid;

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;

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;

课后作业

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

openGauss=# CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/newtbs1';
CREATE TABLESPACE
openGauss=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/ds_location1';
CREATE TABLESPACE
openGauss=# \db
              List of tablespaces
     Name     | Owner |        Location
--------------+-------+-------------------------
 ds_location1 | omm   | tablespace/ds_location1
 music_tbs    | omm   | tablespace/test_ts1
 newtbs1      | omm   | tablespace/newtbs1
 pg_default   | omm   |
 pg_global    | omm   |
(5 rows)

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

openGauss=# CREATE DATABASE newdb1 TABLESPACE newtbs1;
CREATE DATABASE
openGauss=# \l
                          List of databases
   Name    | Owner | Encoding  | Collate | Ctype | Access privileges
-----------+-------+-----------+---------+-------+-------------------
 musicdb   | omm   | SQL_ASCII | C       | C     |
 newdb1    | omm   | SQL_ASCII | C       | C     |
 postgres  | omm   | SQL_ASCII | C       | C     |
 template0 | omm   | SQL_ASCII | C       | C     | =c/omm           +
           |       |           |         |       | omm=CTc/omm
 template1 | omm   | SQL_ASCII | C       | C     | =c/omm           +
           |       |           |         |       | omm=CTc/omm
(5 rows)

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

openGauss=# create user user5 identified by "user5@123";
CREATE ROLE
openGauss=# alter user user5 sysadmin;
ALTER ROLE
openGauss=# \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) tablespace ds_location1;
CREATE TABLE

4.查看表所在的表空间

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 | f          | f        | f           | user5        | 2022-12-01 22:39:32.226942+08 | 2022-12-01 22:39:
32.226942+08
(1 row)

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

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-> and tb.spcname='ds_location1'
newdb1-> order by a.relpages desc;
 relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
 newt1   | r       |        0 | 0 bytes        |         16423 |    16425
(1 row)

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

评论