今天是openGauss每日一练的第8天,我们来一起回顾以下第7天的内容,上一篇我们了解了在openGauss中一个数据库中可以创建多个模式,并做了相关的实验。今天我们来继续探讨openGauss数据库的知识,我们接下来通过实验来验证在openGauss中一个数据库可以存储在多个表空间中,接下来我们来动手做实验~~
实验内容
1.创建表空间newtbs1、 ds_location1,查看表空间
CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/new_ts1';
CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/ds_location_ts1';musicdb=> \db
List of tablespaces
Name | Owner | Location
--------------+-------+-------------------------
ds_location1 | omm | tablespace/ds_location_ts1
newtbs1 | omm | tablespace/new_ts1
pg_default | omm |
pg_global | omm |
(4 rows)
2.创建一个数据库newdb1,默认表空间为newtbs1
CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;musicdb=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+---------+-------+-------------------
newdb1 | 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
(5 rows)
3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
--执行下面的SQL语句,创建用户user5:
CREATE USER user5 IDENTIFIED BY 'kunpeng@1234';
--授予user5数据库系统的SYSADMIN权限:
ALTER USER user5 SYSADMIN;--建表newt1指定表空间ds_location1
\c newdb1 user5
create table newt1 (col1 char(10)) tablespace ds_location1;
4.查看表所在的表空间
--查看表newt1所在的表空间:
musicdb=> select * from pg_tables where tablename = 'newt1';musicdb=> schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | created | last_ddl_time
------------+--------------+------------+--------------+------------+----------+-------------+--------------+-------------------------------+-------------------------------
public | newt1 | user5 | ds_location1 | f | f | f | user5 | 2022-12-01 11:35:41.657001+08 | 2022-12-01 11:35:41.657001+08
(1 row)
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 in('newtbs1','ds_location1')
order by a.relpages desc;
结论
学习了在openGauss中表空间与数据库对象的关系。
在newdb1数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间newtbs1中,当我们在newdb1数据库中创建表newt1的时候,明确指定在表空间ds_location1中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




