实操作业
学习表空间与数据库对象的关系。在musicdb数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间music_tbs中,当我们在musicdb数据库中创建表warehouse_t1的时候,明确指定在表空间ds_location1中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
1.创建表空间newtbs1、 ds_location1,查看表空间
--若表空间或数据库中有对象,不可删除。
drop tablespace if exists newtbs1;
drop tablespace if exists ds_location1;
CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/newtbs1';
CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/ds_location1';
--查看表空间
omm=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Description
--------------+-------+-------------------------+-------------------+-------------
ds_location1 | omm | tablespace/ds_location1 | |
newtbs1 | omm | tablespace/newtbs1 | |
pg_default | omm | | |
pg_global | omm | | |
(4 rows)
2.创建一个数据库newdb1,默认表空间为newtbs1
create database newdb1 tablespace newtbs1 ;
3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
create user user5 sysadmin password 'test@123';
\c newdb1 user5
create table newt1(id int2) tablespace ds_location1 ;
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-09 15:11:33.152688+08 | 2022-12-09 15:11:33.152688+08
(1 row)
--或
newdb1=> \d+ newt1
Table "public.newt1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+----------+-----------+---------+--------------+-------------
id | smallint | | plain | |
Has OIDs: no
Tablespace: "ds_location1"
Options: orientation=row, compression=no
5.查看表空间newtbs1、 ds_location1上的对象
select relkind,reltablespace from pg_class a where a.reltablespace=16394 ;
select a.oid,a.* from pg_tables a where a.tablename='t1';
select b.oid,b.* from pg_tablespace b;
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='newtbs2'
order by a.relpages desc;
6、总结
在数据库中创建的所有的表,当没有指定表空间的名字时,因此都创建在当前数据库默认的表空间中,可通过元 命令 \l+查看当前数据库的默认表空间,当我们在某数据库中创建表的时候,明确指定在表空间中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




