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

(openGauss每日一练第 6天):openGauss创建模式、修改模式属性和删除模式

原创 junzibuyuantian 恩墨学院 2021-12-06
192

1.创建模式

--1、使用gsql登录openGauss root@modb:~# su - omm omm@modb:~$ gsql -r gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# -2、创建名为ds的模式 omm=# CREATE SCHEMA ds; CREATE SCHEMA -3、查看ds模式信息, owner为omm omm=# \dn+ ds; List of schemas Name | Owner | Access privileges | Description ------+-------+-------------------+------------- ds | omm | | (1 row)

2.在ds中建表

--1、创建表 omm=# create table ds.t1(id int, name char(30)); CREATE TABLE --2、插入数据 omm=# insert into ds.t1 values(1 ,'xxxx'); INSERT 0 1 --3、查询验证 omm=# select * from ds.t1; id | name ----+-------------------------------- 1 | xxxx (1 row) --4、查看表信息 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

3.修改模式信息

-1、重命名模式为ds_new omm=# ALTER SCHEMA ds RENAME TO ds_new; ALTER SCHEMA --2、查询模式ds_new下表t1 omm=# select * from ds_new.t1; id | name ----+-------------------------------- 1 | xxxx (1 row) --3、创建用户jack omm=# CREATE USER jack PASSWORD 'abcd@123'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE --4、将ds_new的所有者修改为jack omm=# ALTER SCHEMA ds_new OWNER TO jack; ALTER SCHEMA --5、查询模式ds_new信息 omm=# \dn+ ds_new; List of schemas Name | Owner | Access privileges | Description --------+-------+-------------------+------------- ds_new | jack | | (1 row)

4.删除模式

--删除模式 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关键字

--1、再次尝试删除模式 omm=# DROP SCHEMA ds_new CASCADE; NOTICE: drop cascades to table ds_new.t1 DROP SCHEMA --2、删除用户 omm=# DROP USER jack; DROP ROLE

练习:

1.创建一个名为tpcds的模式

omm=# create schema tpcds; CREATE SCHEMA omm=# \dn+ List of schemas Name | Owner | Access privileges | Description -------------+-------+-------------------+---------------------------------- cstore | omm | | reserved schema for DELTA tables dbe_perf | omm | | dbe_perf schema pkg_service | omm | | pkg_service schema public | omm | omm=UC/omm +| standard public schema | | =U/omm | snapshot | omm | | snapshot schema tpcds | omm | | (6 rows)

2.创建一个用户tim, 并将tpcds的owner修改为tim,且修改owner前后分别使用\dn+查看模式信息

omm=# create user tim identified by 'tim_1234'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=# \du+ List of roles Role name | Attributes | Member of | Description -----------+------------------------------------------------------------------------------------------------------- -----------+-----------+------------- gaussdb | Sysadmin | {} | omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyad min, UseFT | {} | tim | | {} | omm=# alter schema tpcds owner to tim; ALTER SCHEMA omm-# \dn+ List of schemas Name | Owner | Access privileges | Description -------------+-------+-------------------+---------------------------------- cstore | omm | | reserved schema for DELTA tables dbe_perf | omm | | dbe_perf schema pkg_service | omm | | pkg_service schema public | omm | omm=UC/omm +| standard public schema | | =U/omm | snapshot | omm | | snapshot schema tim | tim | | tpcds | tim | | (7 rows)

3.重命名tpcds为tpcds1

omm=# alter schema tpcds rename to tpcds2; ALTER SCHEMA omm=# \dn+ List of schemas Name | Owner | Access privileges | Description -------------+-------+-------------------+---------------------------------- cstore | omm | | reserved schema for DELTA tables dbe_perf | omm | | dbe_perf schema pkg_service | omm | | pkg_service schema public | omm | omm=UC/omm +| standard public schema | | =U/omm | snapshot | omm | | snapshot schema tim | tim | | tpcds2 | tim | | (7 rows)

4.在模式tpcds1中建表customer、插入记录和查询记录

omm=# create table tpcds2.customer(id number(10), name varchar(50)); CREATE TABLE omm=# insert into tpcds2.customer values(1,'xiaoming'); INSERT 0 1 omm=# select * from tpcds2.customer; id | name ----+---------- 1 | xiaoming (1 row)

5.删除模式tpcds1

omm=# drop schema tpcds2 cascade; NOTICE: drop cascades to table tpcds2.customer DROP SCHEMA omm=# \dn+ List of schemas Name | Owner | Access privileges | Description -------------+-------+-------------------+---------------------------------- cstore | omm | | reserved schema for DELTA tables dbe_perf | omm | | dbe_perf schema pkg_service | omm | | pkg_service schema public | omm | omm=UC/omm +| standard public schema | | =U/omm | snapshot | omm | | snapshot schema tim | tim | | (6 rows)

6.删除用户

omm=# drop user tim; DROP ROLE omm=# \du+ List of roles Role name | Attributes | Member of | Description -----------+------------------------------------------------------------------------------------------------------- -----------+-----------+------------- gaussdb | Sysadmin | {} | omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyad min, UseFT | {} |
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论