学习目标
学习表空间与数据库对象的关系。在数据库中创建的所有的表,没有指定表空间的名字,因此都创建在数据库默认的表空间中,当我们在数据库中创建表的时候,明确指定在表空间中创建时,这个表会存储在这个指定的表空间。即一个数据库中的对象,可以位于不同的表空间.
学习内容
本章节学些了,数据库和表空间的关系。在openGauss数据库中,可以指定表创建到某个表空间下,如果不指定会默认到创建数据库指定的表空间。
1.创建表空间,创建数据库并指定一个默认的表空间1。如果不不指定会默认到pg_default
2.创建用户并权限sysadmin权限
3.再创建一个表空间2。
4.使用创建的用户登录到创建的数据库中,创建一张表指定到表空间2。并确实表空间2是否存在创建的表。
5.在指定的数据库中创建表,查询是否表默认到表空间1。
6.分别查询表空间1和表空间2的数据对象。
课后作业
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 DCREATE TABLESPACE
openGauss=# \db
List of tablespaces
Name | Owner | Location
--------------+-----------+-------------------------
ds_location1 | opengauss | tablespace/ds_location1
newtbs1 | opengauss | tablespace/newtbs1
pg_default | opengauss |
pg_global | opengauss |
(4 rows)2.创建一个数据库newdb1,默认表空间为 newtbs1。
openGauss=# CREATE DATABASE newdb1 WITH TABLESPACE = newtbs1;
CREATE DATABASE
openGauss=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+-----------+-----------+---------+-------+-------------------------+--------+------------+--------------------------------------------
musicdb1 | opengauss | SQL_ASCII | C | C | | 13 MB | pg_default |
musicdb2 | opengauss | SQL_ASCII | C | C | | 13 MB | pg_default |
newdb1 | opengauss | SQL_ASCII | C | C | | 13 MB | newtbs1 |
postgres | opengauss | SQL_ASCII | C | C | | 387 MB | pg_default | default administrative connection database
template0 | opengauss | SQL_ASCII | C | C | =c/opengauss +| 12 MB | pg_default | default template for new databases
| | | | | opengauss=CTc/opengauss | | |
template1 | opengauss | SQL_ASCII | C | C | =c/opengauss +| 12 MB | pg_default | unmodifiable empty database
| | | | | opengauss=CTc/opengauss | | |
(6 rows)3.创建用户user5,并授予SYSADMIN权限,访问数据库newdb1,在表空间ds_location1上,创建一个表newt1(表结构自定义)
openGauss=# CREATE USER user5 IDENTIFIED BY 'test@1234';
CREATE ROLE
openGauss=# ALTER USER user5 SYSADMIN;
ALTER ROLE
openGauss=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------------------------------------------------------------+-----------
jack | | {}
opengauss | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
user1 | Sysadmin | {}
user5 | Sysadmin 访问数据库并建表
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(product_id integer,product_name char(20),category char(30)) tablespace ds_location1;
CREATE TABLE
newdb1=> create table newt12(product_id integer,product_name char(20),category char(30));
CREATE TABLE
newdb1=> \dt+
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+--------+-------+-------+---------+----------------------------------+-------------
public | newt1 | table | user5 | 0 bytes | {orientation=row,compression=no} |
public | newt12 | table | user5 | 0 bytes | {orientation=row,compression=no} |
(2 rows)4.查看表所在的表空间
newdb1=> select * from pg_tables where tablename like 'newt%';
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:07:46.098793+08 | 2022-12-01 11:07:46.098793+08
public | newt12 | user5 | | f | f | f | user5 | 2022-12-01 11:09:11.516481+08 | 2022-12-01 11:09:11.516481+08
(2 rows)
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 | newt12 | BASE TABLE
newdb1 | public | newt1 | BASE TABLE
(5 rows)5.查看表空间newtbs1、 ds_location1上的对象3
openGauss=# \c newdb1 user1
Password for user user1:
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "newdb1" as user "user1".
newdb1=>
newdb1=>
newdb1=>
newdb1=> select relname, relkind, relpages,pg_size_pretty(pg_relation_size(a.oid)),reltablespace,relowner
newdb1-> from pg_class a
newdb1-> where a.relkind in ('r', 'i')
newdb1-> and reltablespace='0'
newdb1-> and relname not like 'gs%'
newdb1-> and relname not like 'pg%'
newdb1-> and relname not like 's%'
newdb1-> order by a.relpages desc;
relname | relkind | relpages | pg_size_pretty | reltablespace | relowner
-----------------+---------+----------+----------------+---------------+----------
newt12 | r | 0 | 0 bytes | 0 | 18922
plan_table_data | r | 0 | 0 bytes | 0 | 10
(2 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 | 18920 | 18922
(1 row)
最后修改时间:2022-12-01 15:52:50
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




