学习目标
学习表空间与数据库对象的关系。
在musicdb数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间music_tbs中,当我们在musicdb数据库中创建表warehouse_t1的时候,明确指定在表空间ds_location1中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
课程学习
测试同一个数据库,创建表存储在非默认表空间中。
课后作业
1.创建表空间newtbs1、 ds_location1,查看表空间
--创建表空间newtbs1、 ds_location1
/*
DROP tablespace IF EXISTS newtbs1;
CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/test_newtbs1';
DROP tablespace IF EXISTS ds_location1;
CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/test_ds_location1';
*/
omm=# DROP tablespace IF EXISTS newtbs1;
NOTICE: Tablespace "newtbs1" does not exist, skipping.
DROP TABLESPACE
omm=# CREATE TABLESPACE newtbs1 RELATIVE LOCATION 'tablespace/test_newtbs1';
CREATE TABLESPACE
omm=# DROP tablespace IF EXISTS ds_location1;
NOTICE: Tablespace "ds_location1" does not exist, skipping.
DROP TABLESPACE
omm=# CREATE TABLESPACE ds_location1 RELATIVE LOCATION 'tablespace/test_ds_location1';
CREATE TABLESPACE
--查看表空间
omm=# \db
List of tablespaces
Name | Owner | Location
---------------+-------+------------------------------
ds_location1 | omm | tablespace/test_ds_location1
music_tbs | omm | tablespace/test_ts1
music_tbs1 | omm | tablespace/test_ts12
musicdb10_tbs | omm | tablespace/test_ts10
newtbs1 | omm | tablespace/test_newtbs1
pg_default | omm |
pg_global | omm |
(7 rows)
2.创建一个数据库newdb1,默认表空间为newtbs1
--创建数据库
/*
DROP DATABASE IF EXISTS newdb1;
CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;
*/
omm=# DROP DATABASE IF EXISTS newdb1;
NOTICE: database "newdb1" does not exist, skipping
DROP DATABASE
omm=# CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;
CREATE DATABASE
3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
--创建用户user5,并授予SYSADMIN权限
/*
CREATE USER user5 IDENTIFIED BY 'meiriyilian@1234';
ALTER USER user1 SYSADMIN;
*/
omm=# CREATE USER user5 IDENTIFIED BY 'meiriyilian@1234';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user5 SYSADMIN;
ALTER ROLE
--访问数据库newdb1,在表空间ds_location1上,创建一个表newt1
/*
\c newdb1 user5
create table newt1(id integer, comm char(20)) tablespace ds_location1;
*/
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=> create table newt1(id integer, comm char(20)) tablespace ds_location1;
CREATE TABLE
4.查看表所在的表空间
--查看newdb1数据库目前有哪些表:
newdb1=> select table_catalog, table_schema, table_name, table_type
newdb1-> from information_schema.tables
newdb1-> where table_schema not in ('pg_catalog', 'information_schema','dbe_perf');
table_catalog | table_schema | table_name | table_type
---------------+-----------------+------------+------------
newdb1 | db4ai | snapshot | BASE TABLE
newdb1 | dbe_pldeveloper | gs_errors | BASE TABLE
newdb1 | dbe_pldeveloper | gs_source | BASE TABLE
newdb1 | public | newt1 | BASE TABLE
(4 rows)
--查询pg_tables: select * from pg_tables where tablename = 'newt1';
newdb1=> select * from pg_tables where tablename = 'newt1';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | tablecreator | cr
eated | last_ddl_time
------------+-----------+------------+--------------+------------+----------+-------------+--------------+--------------
-----------------+-------------------------------
public | newt1 | user5 | ds_location1 | f | f | f | user5 | 2022-12-12 22
:04:13.084053+08 | 2022-12-12 22:04:13.084053+08
(1 row)
--确认表newt1所在的表空间为指定的ds_location1。
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='ds_location1'
order by a.relpages desc;
*/
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-> newdb1-> and a.reltablespace=tb.oid
newdb1-> and tb.spcname='ds_location1'
order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
---------+---------+----------+----------------+---------------+----------
newt1 | r | 0 | 0 bytes | 16482 | 16483
(1 row)
/*
create table newt2(id integer, comm char(20));
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;
*/
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)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




