学习目标
学习openGauss创建模式、修改模式属性和删除模式
课程学习实操
模式是一组数据库对象的集合,主要用于控制对数据库对象的访问
连接openGauss
#第一次进入等待15秒 #数据库启动中... su - omm gsql -r
复制
1.创建模式
–创建名为ds的模式
CREATE SCHEMA ds;
omm=# CREATE SCHEMA ds; CREATE SCHEMA
复制
–查看ds模式信息, owner为omm
\dn+ ds;
omm=# \dn+ ds; List of schemas Name | Owner | Access privileges | Description ------+-------+-------------------+------------- ds | omm | | (1 row)
复制
2.在ds中建表
create table ds.t1(id int, name char(30)); insert into ds.t1 values(1 ,'xxxx'); select * from ds.t1;
omm=# create table ds.t1(id int, name char(30)); omm=# insert into ds.t1 values(1 ,'xxxx'); CREATE TABLE INSERT 0 1 omm=# select * from ds.t1; id | name ----+-------------------------------- 1 | xxxx (1 row) omm=#
复制
–查看表信息
\d+ ds.t1;
omm=# \d+ ds.t1; Table "ds.t1" Column | Type | Modifiers | Storage | Stats target | Description --------+---------------+-----------+----------+--------------+------------- id | integer | | plain | | name | character(30) | | extended | | Has OIDs: no Options: orientation=row, compression=no omm=#
复制
3.修改模式信息
–重命名模式为ds_new
ALTER SCHEMA ds RENAME TO ds_new; select * from ds_new.t1;
omm=# ALTER SCHEMA ds RENAME TO ds_new; ALTER SCHEMA omm=# select * from ds_new.t1; id | name ----+-------------------------------- 1 | xxxx (1 row)
复制
–创建用户jack
CREATE USER jack PASSWORD 'abcd@123';
omm=# CREATE USER jack PASSWORD 'abcd@123'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=#
复制
–将ds_new的所有者修改为jack
ALTER SCHEMA ds_new OWNER TO jack;
omm=# ALTER SCHEMA ds_new OWNER TO jack; ALTER SCHEMA omm=#
复制
–查看ds_new模式信息
\dn+ ds_new;
omm=# \dn+ ds_new; List of schemas Name | Owner | Access privileges | Description --------+-------+-------------------+------------- ds_new | jack | | (1 row)
复制
4.删除模式
DROP SCHEMA ds_new;
omm=# DROP SCHEMA ds_new; ERROR: cannot drop schema ds_new because other objects depend on it DETAIL: table ds_new.t1 depends on schema ds_new HINT: Use DROP ... CASCADE to drop the dependent objects too.
复制
–当schema非空时,如果要删除一个schema及其包含的所有对象,需要使用CASCADE关键字
DROP SCHEMA ds_new CASCADE; DROP USER jack;
omm=# DROP SCHEMA ds_new CASCADE; NOTICE: drop cascades to table ds_new.t1 DROP SCHEMA omm=# DROP USER jack; DROP ROLE omm=#
复制
课后作业
1.创建一个名为tpcds的模式
omm=# CREATE SCHEMA tpcds; CREATE SCHEMA omm=#
复制
2.创建一个用户tim, 并将tpcds的owner修改为tim,且修改owner前后分别使用\dn+查看模式信息
omm=# create user tim password '@163.com'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=#
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=#
omm=# \dn+ tpcds List of schemas Name | Owner | Access privileges | Description -------+-------+-------------------+------------- tpcds | tim | | (1 row)
复制
3.重命名tpcds为tpcds1
omm=# alter schema tpcds rename to tpcds1; ALTER SCHEMA
复制
4.在模式tpcds1中建表customer、插入记录和查询记录
omm=# create table tpcds1.customer (id integer, name varchar(30), address varchar(60)); CREATE TABLE omm=# insert into tpcds1.customer values (1,'Sean','Futian'); INSERT 0 1
omm=# insert into tpcds1.customer values (2,'Leton','Luohu'); INSERT 0 1
omm=# select * from tpcds1.customer; id | name | address ----+-------+--------- 1 | Sean | Futian 2 | Leton | Luohu (2 rows)
复制
5.删除模式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=# drop schema tpcds1 cascade; NOTICE: drop cascades to table tpcds1.customer DROP SCHEMA omm=#
复制
学习总结
通过本节课的学习,我学会了创建模式、修改模式属性和删除模式。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
472次阅读
2025-04-03 15:21:16
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
191次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
154次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
124次阅读
2025-04-01 12:27:03
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
100次阅读
2025-04-01 10:30:07
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
74次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
71次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
58次阅读
2025-03-18 10:45:01
openGauss 学习之路:集群部署实战探索
openGauss
48次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
42次阅读
2025-03-18 19:06:13