学习目标
学习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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
482次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
303次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
198次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
161次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
155次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
136次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
91次阅读
2025-04-18 10:49:53
Postgresql数据库单个Page最多存储多少行数据
maozicb
83次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
57次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
47次阅读
2025-04-16 10:33:59