学习目标
学习查看表的相关信息,查看表结构最基础的命令, \d[+] table_name,结果格式如下
| Column | Type | Modifiers |
|---|---|---|
| id | bigint | not null |
| name | character varying(50) | not null |
| age | integer | default 20 |
课后作业
1.创建表和约束
drop table if exists book;
create table book (
id int primary key,
name char(20) not null,
language char(20) default 'CN'
);
2.使用\d tableNmae命令查看表的定义、模式和所有者
\d book
Table "public.book" 可以看到表book的结构定义信息
| Column | Type | Modifiers |
|---|---|---|
| id | integer | not null |
| name | character(20) | not null |
| language | character(20) | default ‘CN’::bpchar |
Indexes:
“book_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
如果想查看表所属模式、和所有者信息可以使用 \dt+ book
List of relations
| Schema | Name | Type | Owner | Size | Storage | Description |
|---|---|---|---|---|---|---|
| public | book | table | omm | 0 bytes | {orientation=row,compression=no} |
3.查看某个模式下有哪些表
注释:information_schema.tables实际上是一个系统视图,可以通过命令\dv information_schema.tables来证实
--查看public模式下的所有表名:
SELECT table_name FROM information_schema.tables WHERE table_schema='public';
是否可以通过pg_tables 达到同样的效果,pg_tables也是一个视图,属于pg_catalog Schema
select * from pg_tables where schemaname='public';
4.查看一个表下有哪些约束
--查看约束名称、约束类型
select conname, connamespace, contype, conkey
from pg_constraint
where conrelid in ( select oid
from pg_class
where relname='book');
--或者使用gsql的元命令\d tableName很方便地查看一个表上有哪些约束
\d book
5.查看一个表属于数据库的哪个模式
-- \x 开启或关闭Expanded display,默认不开启,所以第一次输入会开启
-- 适用于查看列多、行少的情况
\x
SELECT * FROM information_schema.tables WHERE table_name='book';
-- 或者
select schemaname from pg_tables where tablename='book';
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




