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

openGauss每日一练第8天|表空间与数据库对象的关系

原创 Lily 2022-12-01
1215

大道至简!!!!!!!!!!

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

数据准备

第一次进入等待15秒
数据库启动中…

课后作业
1.创建表空间newtbs1、 ds_location1,查看表空间

CREATE TABLESPACE music_tbs1 RELATIVE LOCATION 'tablespace/music_tbs1';
CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/ds_location1';

image.png

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

CREATE DATABASE newdb1  WITH TABLESPACE = music_tbs1;

image.png

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

–执行下面的SQL语句,创建用户user5:

 CREATE USER user5 IDENTIFIED BY 'kunpeng@1234';

image.png
–授予user1数据库系统的SYSADMIN权限:

ALTER USER user5 SYSADMIN;
![image.png](https://oss-emcsprod-public.modb.pro/image/editor/20221201-84ac2fe1-8532-4d32-8391-4f692c8f3ebf.png)

利用新建用户user5,连接数据库newtbs1,并在新建的非默认表空间中创建表newt1

omm=# \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=> \db
              List of tablespaces
     Name     | Owner |        Location         
--------------+-------+-------------------------
 ds_location1 | omm   | tablespace/ds_location1
 newtbs1      | omm   | tablespace/newtbs1
 pg_default   | omm   | 
 pg_global    | omm   | 
(4 rows)

newdb1=> create table newt1(id int) tablespace ds_location1;
CREATE TABLE

image.png

4.查看表所在的表空间,表属于表空间ds_location1


select * from pg_tables where tablename = 'newt1';

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

newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner  
where a.relkind in ('r', 'i')  
newdb1-> and a.reltablespace=tb.oid  
newdb1-> newdb1-> from pg_class a, pg_tablespace tb  
newdb1-> and tb.spcname='ds_location1'  
newdb1-> order by a.relpages desc;
(1 row)

newdb1=>  relname | relkind | relpages | pg_size_pretty | reltablespace | relowner 
---------+---------+----------+----------------+---------------+----------
 newt1   | r       |        0 | 0 bytes        |         16392 |    16394
newdb1=> 

test1:

 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;
 relname | relkind | relpages | pg_size_pretty | reltablespace | relowner 
---------+---------+----------+----------------+---------------+----------
(0 rows)

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

评论