openGauss每日一练第15天
今日目标:查看表的相关信息
查看表定义
\d[S+] <表名> //describe table, view, sequence, or index,表有哪些约束
查看表的模式和所有者
\dt[S+] [PATTERN] list tables
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系统表存储数据库对象信息及其之间的关系
PG_CONSTRAINT
PG_CONSTRAINT系统表存储表上的检查约束、主键和唯一约束。
名称 | 类型 | 描述 |
---|---|---|
oid | oid | 行标识符(隐含属性,必须明确选择)。 |
conname | name | 约束名称(不一定是唯一的)。 |
connamespace | oid | 包含这个约束的名称空间的OID。 |
contype | “char” | c = 检查约束 p = 主键约束 u = 唯一约束 t = 触发器约束 x = 互斥约束 f = 外键约束 s = 聚簇约束 i = 无效约束 |
condeferrable | boolean | 这个约束是否可以推迟。 |
condeferred | boolean | 缺省时这个约束是否可以推迟。 |
convalidated | boolean | 约束是否有效。目前,只有外键和CHECK约束可将其设置为FALSE。 |
conrelid | oid | 这个约束所在的表;如果不是表约束则为0。 |
contypid | oid | 这个约束所在的域;如果不是一个域约束则为0。 |
conindid | oid | 与约束关联的索引ID。 |
confrelid | oid | 如果是外键,则为参考的表;否则为0。 |
confupdtype | “char” | 外键更新动作代码。a = 没动作r = 限制c = 级联n =设置为nulld =设置为缺省 |
confdeltype | “char” | 外键删除动作代码。a = 没动作r = 限制c = 级联n =设置为nulld =设置为缺省 |
confmatchtype | “char” | 外键匹配类型。f = 全部p = 部分u = 未指定(在f的基础上允许匹配NULL值) |
conislocal | boolean | 是否是为关系创建的本地约束。 |
coninhcount | integer | 约束直接继承父表的数目。继承父表数非零时,不能删除或重命名该约束。 |
connoinherit | boolean | 是否可以被继承。 |
consoft | boolean | 是否为信息约束(Informational Constraint)。 |
conopt | boolean | 是否使用信息约束优化执行计划。 |
conkey | smallint[] | 如果是表约束,则是约束控制的字段列表。 |
confkey | smallint[] | 如果是一个外键,是参考的字段的列表。 |
conpfeqop | oid[] | 如果是一个外键,是做PK=FK比较的相等操作符ID的列表。 |
conppeqop | oid[] | 如果是一个外键,是做PK=PK比较的相等操作符ID的列表。 |
conffeqop | oid[] | 如果是一个外键,是做FK=FK比较的相等操作符ID的列表。由于当前不支持外键,所以值为空。 |
conexclop | oid[] | 如果是一个排他约束,是列的排他操作符ID列表。 |
conbin | pg_node_tree | 如果是检查约束,那就是其表达式的内部形式。 |
consrc | text | 如果是检查约束,则是表达式的人类可读形式。 |
conincluding | smallint[] | 不用做约束,但是会包含在INDEX中的属性列。 |
课后作业
1 创建表和约束
omm=# create table test(
omm(# id bigint,
omm(# name varchar(100) not null,
omm(#age int default 20,
omm(# primary key (id)
omm(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
omm=# \dt
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | test | table | omm | {orientation=row,compression=no}
(1 row)
omm=#
复制
2 使用\d tableNmae命令查看表的定义、模式和所有者
\d <表名> //查看表定义
\d+ <表名> //
\dt <表名> //查看表的模式及所有者
\dt+ <表名> //查看表的模式及所有者
omm=# \d test
Table "public.test"
Column | Type | Modifiers
--------+------------------------+------------
id | bigint | not null
name | character varying(100) | not null
age | integer | default 20
Indexes:
"test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# \d+ test
Table "public.test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+------------+----------+--------------+-------------
id | bigint | not null | plain | |
name | character varying(100) | not null | extended | |
age | integer | default 20 | plain | |
Indexes:
"test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no
omm=# \dt test
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | test | table | omm | {orientation=row,compression=no}
(1 row)
omm=# \dt+ test
List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+------+-------+-------+---------+----------------------------------+-------------
public | test | table | omm | 0 bytes | {orientation=row,compression=no} |
(1 row)
复制
3 查看某个模式下有哪些表
select table_name from information_schema.tables where table_schema =<模式名>;
--查询public模式下有哪些表
omm=# select table_name from information_schema.tables where table_schema = 'public';
table_name
------------
test
(1 row)
-- 查询db4ai模式下有哪些表
omm=# select table_name ,table_schema from information_schema.tables where table_schema = 'db4ai';
table_name | table_schema
------------+--------------
snapshot | db4ai
(1 row)
-- 查询cstore模式下有哪些表
omm=# select table_name ,table_schema from information_schema.tables where table_schema = 'cstore';
table_name | table_schema
------------+--------------
(0 rows)
omm=#
复制
4. 查看一个表下有哪些约束
select conname,connamespace,contype,conkey from pg_constraint where conrelid in (select oid from pg_class where relname=‘表名’);
pg_class部分字段
relname: 表、索引、视图等对象的名称。
or 使用一下命令
\d+ <表名>
omm=# select conname,connamespace,contype,conkey from pg_constraint where conrelid in (select oid from pg_class where relname='test');
(1 row)
omm=# conname | connamespace | contype | conkey
-----------+--------------+---------+--------
test_pkey | 2200 | p | {1}
omm=#
复制
5 查看一个表属于数据库的哪个模式
select * from information_schema.tables where table_name =<表名>;
omm=# select * from information_schema.tables where table_name ='test';
-[ RECORD 1 ]----------------+-----------
table_catalog | omm
table_schema | public
table_name | test
table_type | BASE TABLE
self_referencing_column_name |
reference_generation |
user_defined_type_catalog |
user_defined_type_schema |
user_defined_type_name |
is_insertable_into | YES
is_typed | NO
commit_action |
omm=#
复制