第6课程schema学习
学习目标
学习openGauss创建模式、修改模式属性和删除模式
模式是一组数据库对象的集合,主要用于控制对数据库对象的访问
1.创建一个名为tpcds的模式
CREATE SCHEMA tpcds;
\dn+ tpcds
omm=# CREATE SCHEMA tpcds;
omm=# \dn+ tpcds
CREATE SCHEMA
List of schemas
Name | Owner | Access privileges | Description
-------+-------+-------------------+-------------
tpcds | omm | |
(1 row)
2.更改模式owner
创建一个用户tim, 并将tpcds的owner修改为tim,且修改owner前后分别使用\dn+查看模式信息
CREATE USER tim PASSWORD ‘abcd@123’;
\dn+ tpcds;
ALTER SCHEMA tpcds OWNER TO tim;
\dn+ tpcds;
omm=# CREATE USER tim PASSWORD 'abcd@123';
NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# \dn+ tpcds;
List of schemas
Name | Owner | Access privileges | Description
-------+-------+-------------------+-------------
tpcds | omm | |
(1 row)
omm=# ALTER SCHEMA tpcds OWNER TO tim;
ALTER SCHEMA
omm=# \dn+ tpcds;
List of schemas
Name | Owner | Access privileges | Description
-------+-------+-------------------+-------------
tpcds | tim | |
(1 row)
3.重命名tpcds为tpcds1
ALTER SCHEMA tpcds RENAME TO tpcds1;
omm=# \dn+ tpcds1
List of schemas
Name | Owner | Access privileges | Description
--------+-------+-------------------+-------------
tpcds1 | tim | |
(1 row)
4.模式中建表
在模式tpcds1中建表customer、插入记录和查询记录
create table tpcds1.customer(id int, name char(30));
insert into tpcds1.customer values(1 ,‘test-schema’);
select * from tpcds1.customer;
\d+ tpcds1.customer;
omm=# create table tpcds1.customer(id int, name char(30));
CREATE TABLE
omm=# insert into tpcds1.customer values(1 ,'test-schema');
INSERT 0 1
omm=# select * from tpcds1.customer;
id | name
----+--------------------------------
1 | test-schema
(1 row)
omm=# \d+ tpcds1.customer;
Table "tpcds1.customer"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------------+-----------+----------+--------------+-------------
id | integer | | plain | |
name | character(30) | | extended | |
Has OIDs: no
Options: orientation=row, compression=no
5.删除模式tpcds1
DROP SCHEMA tpcds1;
omm=# DROP SCHEMA tpcds1;
ERROR: cannot drop schema tpcds1 because other objects depend on it
DETAIL: table tpcds1.customer depends on schema tpcds1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
omm=#
当schema非空时,如果要删除一个schema及其包含的所有对象,需要使用CASCADE关键字
omm=# DROP SCHEMA tpcds1 CASCADE;
NOTICE: drop cascades to table tpcds1.customer
DROP SCHEMA
omm=# DROP USER tim;
DROP ROLE
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




