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

openGauss每日一练第7天

原创 Sally 2021-12-17
725

openGauss每日一练第7天
1.创建表空间
–使用相对路径指定表空间的目录:数据库节点的数据目录/pg_location/相对路径

CREATE TABLESPACE ds_location1 RELATIVE LOCATION ‘tablespace/tablespace_1’;

create tablespace ds_location1 relative location 'tablespace/tablespace_1';
CREATE TABLESPACE


–指定owner

CREATE ROLE joe IDENTIFIED BY ‘abce@123’;
CREATE TABLESPACE ds_location2 OWNER joe RELATIVE LOCATION ‘tablespace/tablespace_2’;

create role joe identified by 'abce@123';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
create tablespace ds_location2 owner joe relative location 'tablespace/tablespace_2';
CREATE TABLESPACE

–查看表空间信息

\db

\db
              List of tablespaces
     Name     | Owner |        Location         
--------------+-------+-------------------------
 ds_location1 | omm   | tablespace/tablespace_1
 ds_location2 | joe   | tablespace/tablespace_2
 pg_default   | omm   | 
 pg_global    | omm   | 
(4 rows)

2.修改表空间属性
–重命名表空间

ALTER TABLESPACE ds_location1 RENAME TO ds_location3;

ALTER TABLESPACE

–修改表空间owner

CREATE ROLE jay IDENTIFIED BY ‘abcd@789’;
ALTER TABLESPACE ds_location2 OWNER TO jay;

create role jay identified by 'abcd@789';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter tablespace ds_location2 owner to jay;
ALTER TABLESPACE

–查看表空间信息

\db

\db
 ds_location3 | omm   | tablespace/tablespace_1
 pg_default   | omm   | 
 pg_global    | omm   | 
(4 rows)
List of tablespaces
     Name     | Owner |        Location         
--------------+-------+-------------------------
 ds_location2 | jay   | tablespace/tablespace_2

3.在表空间中建表
create table ds_t1(id int, name char(30)) tablespace ds_location2;

create table ds_t1(id int,name char(30)) tablespace ds_location2;
CREATE TABLE


–通过视图查看表所在的表空间

select * from pg_tables where tablename = ‘ds_t1’;

 public     | ds_t1     | omm        | ds_location2 | f          | f        | f           | omm  
        | 2021-12-12 12:56:12.139152+08 | 2021-12-12 12:56:12.139152+08
(1 row)

 schemaname | tablename | tableowner |  tablespace  | hasindexes | hasrules | hastriggers | table
creator |            created            |         last_ddl_time         
------------+-----------+------------+--------------+------------+----------+-------------+------
--------+-------------------------------+-------------------------------

4.删除表空间
–在删除一个表空间之前,表空间里面不能有任何数据库对象

drop table ds_t1;
DROP TABLESPACE IF EXISTS ds_location2;
DROP TABLESPACE IF EXISTS ds_location3;

drop tablespace if exists ds_locations2;
NOTICE:  Tablespace "ds_locations2" does not exist, skipping.
DROP TABLESPACE
drop tablespace if exists ds_location2;
ERROR:  tablespace "ds_location2" is not empty
drop tablespace if exists ds_location3;
DROP TABLESPACE
\db
              List of tablespaces
     Name     | Owner |        Location         
--------------+-------+-------------------------
 ds_location2 | jay   | tablespace/tablespace_2
 pg_default   | omm   | 
 pg_global    | omm   | 
(3 rows)

课程作业
1.创建表空间,表空间tspc1使用相对路径指定所在目录,表空间tspc2指定owner为Lucy

create tablespace tspc1 relative location 'tablespace/tspc1';
CREATE TABLESPACE
create user lucy password 'abcd@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
create tablespce tspc2 owner lucy relative location 'tablespace/tspc2';
craete tablespace tspc2 owner lucy relative location 'tablespace/tspc2';

create tablespace tspc2 owner lucy relative location 'tablespace/tspc2';
CREATE TABLESPACE
\db+
                           List of tablespaces
    Name    | Owner |     Location     | Access privileges | Description 
------------+-------+------------------+-------------------+-------------
 pg_default | omm   |                  |                   | 
 pg_global  | omm   |                  |                   | 
 tspc1      | omm   | tablespace/tspc1 |                   | 
 tspc2      | lucy  | tablespace/tspc2 |                   | 
(4 rows)

2.在表空间tspc1中建表,并使用视图pg_tables查看信息

create table test1(id int,name varchar(30)) tablespace tspc1;
CREATE TABLE
insert into test1 values(1,'xxxx');
INSERT 0 1

3.重命名tspc1,修改tspc2的用户为Lily,使用\db查看表空间信息

 alter tablespace tspc1 rename to tspc3;
ALTER TABLESPACE
create user lily identified by 'abcd@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# alter tablespace tspc2 owner to lily;
ALTER TABLESPACE
\db
          List of tablespaces
    Name    | Owner |     Location     
------------+-------+------------------
 pg_default | omm   | 
 pg_global  | omm   | 
 tspc2      | lily  | tablespace/tspc2
 tspc3      | omm   | tablespace/tspc1
(4 rows)


4.删除表空间

drop tablespace tspc3;
ERROR:  tablespace "tspc3" is not empty
drop tablespace tspc2;
DROP TABLESPACE
drop table test1;
DROP TABLE
drop tablespace tspc3;
DROP TABLESPACE


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

评论