第11天 |openGauss逻辑结构:数据库管理

接着昨天继续学习openGauss,今天是第11天了。今天学习内容是openGauss逻辑结构:表空间管理。

老规矩,先登陆墨天轮为我准备的实训实验室

复制代码
root@modb:~# su - omm
omm@modb:~$ gsql -r
作业要求
1.创建表空间enmtbs和数据库musicdb
复制代码
omm=# CREATE TABLESPACE enmtbs RELATIVE LOCATION 'tablespace/enmtbs1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb WITH TABLESPACE = enmtbs;
CREATE DATABASE
2.查看数据库集簇中有哪些数据库
复制代码
omm=# SELECT datname FROM pg_database;
  datname  
-----------
 template1
 omm
 musicdb
 template0
 postgres
(5 rows)

omm-# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb   | omm   | UTF8     | C       | C     | 
 omm       | omm   | UTF8     | C       | C     | 
 postgres  | omm   | UTF8     | C       | C     | 
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(5 rows)
3.查看数据库默认的表空间信息

--首先执行下面的语句,查看musicdb数据库默认表空间的对象OID:

复制代码
omm=# select datname,dattablespace from pg_database where datname='musicdb';
 datname | dattablespace 
---------+---------------
 musicdb |         16389
(1 row)

--根据表空间对象的OID,来查看表空间的名字:

复制代码
omm=# select oid,spcname from pg_tablespace where oid=16389;
  oid  | spcname 
-------+---------
 16389 | enmtbs
(1 row)

--可以将上面的两条语句合并成一条语句,来查询数据库musicdb默认表空间的名字:

复制代码
omm=# select spcname 
omm-# from pg_tablespace 
omm-# where oid=( select dattablespace
omm(#            from pg_database
omm(#            where datname='musicdb' )
omm-# ;
 spcname 
---------
 enmtbs
(1 row)
4.查看数据库下有哪些模式
复制代码
omm-#\dn+
                                         List of schemas
      Name       | Owner | Access privileges |           Description            | WithBlockChain 
-----------------+-------+-------------------+----------------------------------+----------------
 blockchain      | omm   |                   | blockchain schema                | f
 cstore          | omm   |                   | reserved schema for DELTA tables | f
 db4ai           | omm   | omm=UC/omm       +| db4ai schema                     | f
                 |       | =U/omm            |                                  | 
 dbe_perf        | omm   |                   | dbe_perf schema                  | f
 dbe_pldebugger  | omm   | omm=UC/omm       +| dbe_pldebugger schema            | f
                 |       | =U/omm            |                                  | 
 dbe_pldeveloper | omm   | omm=UC/omm       +| dbe_pldeveloper schema           | f
                 |       | =U/omm            |                                  | 
 pkg_service     | omm   |                   | pkg_service schema               | f
 public          | omm   | omm=UC/omm       +| standard public schema           | f
 snapshot        | omm   |                   | snapshot schema                  | f
 sqladvisor      | omm   | omm=UC/omm       +| sqladvisor schema                | f
                 |       | =U/omm            |                                  | 
                 |       | =U/omm            |                                  | 
(10 rows)
5.查询当前连接的数据库下有哪些表:
复制代码
omm=# with my_tables(table_catalog, table_schema, table_name, table_type) as
omm-#       (   select table_catalog, table_schema, table_name, table_type
omm(#           from information_schema.tables
omm(#          where table_schema not in ('pg_catalog', 'information_schema','dbe_perf')
omm(#        )
omm-# 
omm-#  select * from my_tables;
 table_catalog |  table_schema   | table_name | table_type 
---------------+-----------------+------------+------------
 omm           | db4ai           | snapshot   | BASE TABLE
 omm           | dbe_pldeveloper | gs_errors  | BASE TABLE
 omm           | dbe_pldeveloper | gs_source  | BASE TABLE
(3 rows)
6.更改数据库默认的表空间
复制代码
omm=# CREATE TABLESPACE app_ts  RELATIVE LOCATION 'tablespace/app_ts1';
CREATE TABLESPACE
omm=# ALTER DATABASE musicdb  SET TABLESPACE app_ts;
ALTER DATABASE

--查看表空间是否更改完成

复制代码
omm=# select spcname 
omm-# from pg_tablespace 
omm-# where oid=( select dattablespace
omm(#            from pg_database
omm(#            where datname='musicdb' );
 spcname 
---------
 app_ts
(1 row)
7.重新命名数据库
复制代码
omm=# ALTER DATABASE musicdb RENAME TO  musicdb1;
ALTER DATABASE
omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb1  | omm   | UTF8     | C       | C     | 
 omm       | omm   | UTF8     | C       | C     | 
 postgres  | omm   | UTF8     | C       | C     | 
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(5 rows)
8.修改数据库的默认用户
复制代码
omm=# CREATE USER user1  IDENTIFIED BY 'user1@ustb2020';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user1  SYSADMIN;
ALTER ROLE
omm=# ALTER DATABASE musicdb OWNER to user1;
ERROR:  database "musicdb" does not exist
omm=# \l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb1  | omm   | UTF8     | C       | C     | 
 omm       | omm   | UTF8     | C       | C     | 
 postgres  | omm   | UTF8     | C       | C     | 
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(5 rows)
9.删除数据库
复制代码
omm=# DROP DATABASE musicdb1;
DROP DATABASE
总结

今天练习了关于数据库管理的几条命令,受益匪浅.

相关推荐
Elastic 中国社区官方博客19 分钟前
Elasticsearch:使用 Agent Builder 的 A2A 实现 - 开发者的圣诞颂歌
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
2301_8166602122 分钟前
PHP怎么处理Eloquent Attribute Inference属性推断_Laravel从数据自动推导类型【操作】
jvm·数据库·python
qq_372154231 小时前
Go 中自定义类型与基础类型的显式转换规则详解
jvm·数据库·python
_下雨天.2 小时前
NoSQL之Redis配置与优化
数据库·redis·nosql
LiAo_1996_Y2 小时前
CSS如何实现文字渐变效果_通过background-clip实现艺术字
jvm·数据库·python
2401_887724502 小时前
CSS如何让表单在手机端友好展示_利用Flexbox实现堆叠排版
jvm·数据库·python
数据库小组2 小时前
MySQL 删库后怎么恢复?binlog2sql 之外,NineData 还能做什么
数据库·sql·mysql·安全·数据·ninedata·删库
zhangchaoxies2 小时前
Layui轮播图(carousel)怎么设置自动播放间隔
jvm·数据库·python
切糕师学AI3 小时前
HBase:一文搞懂分布式宽列数据库(原理 + 架构 + 实战)
数据库·分布式·nosql·hbase·分布式宽列数据库·wide column db
competes3 小时前
慈善基金投资底层逻辑应用 顶层代码低代码配置平台开发结构方式数据存储模块
java·开发语言·数据库·windows·sql