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

openGauss每日一练第 13天 |学习笔记

原创 newdata 2022-12-06
485

openGauss每日一练第13天

今日目标:openGauss的逻辑结构:openGauss表的创建、搜索路径和访问方法等

gsql常用命令

使用SchemaName.TableName的表标识方法访问表来指定在哪个模式下创建、插入、查询表。

查看库

\l

查看表空间

\db

查看用户

\du

查看模式

\dn+

查看表

\dt //查看当前库所有的表 list tables

\dt <表名> //查看指定表的信息

查看模式的搜索路径

show search_path;

设置模式的搜索路径

会话级模式搜索顺序的优先级最高,用户级模式搜索顺序的优先级第2,数据库级模式搜索顺序的优先级最低。

  • 会话级别(已登录数据库)

    set search_path to <模式名>

  • 数据库级别设置模式搜索顺序

    ALTER DATABASE <数据库> SET SEARCH_PATH TO <模式名>;

  • 用户级别 设置模式搜索顺序

    ALTER USER <用户名> SET SEARCH_PATH TO <模式名>;

information_schema

信息模式本身是一个名为information_schema的模式。这个模式自动存在于所有数据库中。信息模式由一组视图构成,它们包含定义在当前数据库中对象的信息。这个模式的拥有者是初始数据库用户,并且该用户自然地拥有这个模式上的所有特权,包括删除它的能力。

information_schema.schemata

The view schemata contains all schemas in the current database that are owned by a currently enabled role.

Name Data Type Description
catalog_name sql_identifier Name of the database that the schema is contained in (always the current database)
schema_name sql_identifier Name of the schema
schema_owner sql_identifier Name of the owner of the schema
default_character_set_catalog sql_identifier Applies to a feature not available in PostgreSQL
default_character_set_schema sql_identifier Applies to a feature not available in PostgreSQL
default_character_set_name sql_identifier Applies to a feature not available in PostgreSQL
sql_path character_data Applies to a feature not available in PostgreSQL

information_schema.tables

The view tables contains all tables and views defined in the current database. Only those tables and views are shown that the current user has access to (by way of being the owner or having some privilege).

Name Data Type Description
table_catalog sql_identifier Name of the database that contains the table (always the current database)
table_schema sql_identifier Name of the schema that contains the table
table_name sql_identifier Name of the table
table_type character_data Type of the table: BASE TABLE for a persistent base table (the normal table type), VIEW for a view, FOREIGN TABLE for a foreign table, or LOCAL TEMPORARY for a temporary table
self_referencing_column_name sql_identifier Applies to a feature not available in PostgreSQL
reference_generation character_data Applies to a feature not available in PostgreSQL
user_defined_type_catalog sql_identifier If the table is a typed table, the name of the database that contains the underlying data type (always the current database), else null.
user_defined_type_schema sql_identifier If the table is a typed table, the name of the schema that contains the underlying data type, else null.
user_defined_type_name sql_identifier If the table is a typed table, the name of the underlying data type, else null.
is_insertable_into yes_or_no YES if the table is insertable into, NO if not (Base tables are always insertable into, views not necessarily.)
is_typed yes_or_no YES if the table is a typed table, NO if not
commit_action character_data Not yet implemented

系统表

PG_DATABASE

系统表存储关于可用数据库的信息。

名称 类型 描述
oid oid 行标识符(隐含属性,必须明确选择)。
datname name 数据库名称。
datdba oid 数据库所有人,通常为其创建者。
encoding integer 数据库的字符编码方式。
datcollate name 数据库使用的排序顺序。
datctype name 数据库使用的字符分类。
datistemplate boolean 是否允许作为模板数据库。
datallowconn boolean 如果为假,则没有用户可以连接到这个数据库。这个字段用于保护template0数据库不被更改。
datconnlimit integer 该数据库上允许的最大并发连接数,-1表示无限制。
datlastsysoid oid 数据库里最后一个系统OID 。
datfrozenxid xid32 用于跟踪该数据库是否需要为了防止事务ID重叠而进行清理。当前版本该字段已经废弃使用,为保持前向兼容,保留此字段,新增datfrozenxid64用于记录此信息。
dattablespace oid 数据库的缺省表空间。
datcompatibility name 数据库兼容模式,当前支持四种兼容模式:A、B、C、PG,分别表示兼容O、MY、TD和POSTGRES。
datacl aclitem[] 访问权限。
datfrozenxid64 xid 用于跟踪该数据库是否需要为了防止事务ID重叠而进行清理。
datminmxid xid 该数据库中中所有在这个之前的多事务ID已经被一个事务ID替换。这用于跟踪该数据库是否需要为了防止事务ID重叠或者允许收缩pg_clog而进行清理。它是此数据库中所有表的pg_class.relminmxid中的最小值。

PG_TABLESPACE

PG_TABLESPACE系统表存储表空间信息。

名称 类型 描述
oid oid 行标识符(隐含属性,必须明确选择)。
spcname name 表空间名称。
spcowner oid 表空间的所有者,通常是创建它的人。
spcacl aclitem[] 访问权限。具体请参见[GRANT](mk:@MSITStore:F:\openGauss学习\openGauss-document-zh-3.0.0\openGauss 3.0.0 开发者指南(企业版)01.chm::/zh-cn_topic_0289900312.html)和[REVOKE](mk:@MSITStore:F:\openGauss学习\openGauss-document-zh-3.0.0\openGauss 3.0.0 开发者指南(企业版)01.chm::/zh-cn_topic_0289900263.html)。
spcoptions text[] 表空间的选项。
spcmaxsize text 可使用的最大磁盘空间大小,单位Byte。
relative boolean 标识表空间指定的存储路径是否为相对路径。

pg_class

PG_CLASS系统表存储数据库对象信息及其之间的关系

2.课后作业

2.1 创建一个表(默认,不指定模式),查看该表在那个模式下

\dt 查看表

或者使用如下方式

select table_catalog,table_schema,table_name from information_schema.tables;

omm=# create table test1(col varchar(200)); omm=# CREATE TABLE omm=# \dt List of relations Schema | Name | Type | Owner | Storage --------+-------+-------+-------+---------------------------------- public | test1 | table | omm | {orientation=row,compression=no} (1 row) omm=# select table_catalog,table_schema,table_name from information_schema.tables where table_name = 'test1'; table_catalog | table_schema | table_name ---------------+--------------+------------ omm | public | test1 (1 row) omm=#

2.2 使用一个用户连接到enmdb数据库,测试该用户可以访问不同模式中的表

omm@modb:~$ gsql -d enmdb -U user1 -W gauss@1234 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. enmdb=> \dt List of relations Schema | Name | Type | Owner | Storage --------+-----------+-------+-------+---------------------------------- public | testtable | table | user1 | {orientation=row,compression=no} (1 row) enmdb=> \dn List of schemas Name | Owner -----------------+------- blockchain | omm cstore | omm db4ai | omm dbe_perf | omm dbe_pldebugger | omm dbe_pldeveloper | omm enmschem1 | user1 enmschem2 | user1 pkg_service | omm public | omm snapshot | omm sqladvisor | omm (12 rows) enmdb=> select * from testtable ; col ------------------- hello table study (1 row) enmdb=> select * from public.testtable ; col ------------------- hello table study (1 row) enmdb=> select * from enmschem1.testtable ; enmdb=> col -------------------------------- hello from enmschema.testtable (1 row) enmdb=> select * from enmschem2.testtable ; col1 -------------------------------- hello from enmschem2.testtable (1 row) enmdb=>

2.3 在会话级设置模式搜索路径为:模式enmschm1,使用SchemaName.TableName的表标识方法访问表(创建表、插入数据和查询表中数据)

– 会话级设置模式搜索路径

set search_path to <模式名>

omm@modb:~$ gsql -d enmdb -U user1 -W gauss@1234 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. enmdb=> show search_path ; search_path ---------------- "$user",public (1 row) enmdb=> \dt List of relations Schema | Name | Type | Owner | Storage --------+-----------+-------+-------+---------------------------------- public | testtable | table | user1 | {orientation=row,compression=no} (1 row) enmdb=> \dn List of schemas Name | Owner -----------------+------- blockchain | omm cstore | omm db4ai | omm dbe_perf | omm dbe_pldebugger | omm dbe_pldeveloper | omm enmschem1 | user1 enmschem2 | user1 pkg_service | omm public | omm snapshot | omm sqladvisor | omm (12 rows) enmdb=> set search_path to enmschem1; SET enmdb=> show search_path ; search_path ------------- enmschem1 (1 row) enmdb=> \dt List of relations Schema | Name | Type | Owner | Storage -----------+-----------+-------+-------+---------------------------------- enmschem1 | testtable | table | user1 | {orientation=row,compression=no} (1 row) enmdb=> select * from testtable ; enmdb=> col -------------------------------- hello from enmschema.testtable (1 row) enmdb=> enmdb=> create table enmschem1.test2(col varchar(200)); CREATE TABLE enmdb=> insert into enmschem1.test2 values('hello from enmschem1.test2'); INSERT 0 1 enmdb=> select * from enmschem1.test2; col ---------------------------- hello from enmschem1.test2 (1 row) enmdb=> create table enmschem2.test3(col varchar(200)); CREATE TABLE enmdb=> insert into enmschem2.test3 values ('hello from enmschema2.test3'); INSERT 0 1 enmdb=> select * from enmschem2.test3 ; col ----------------------------- hello from enmschema2.test3 (1 row)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论