Skip to Content
《SQL 烹饪书》第二版
book

《SQL 烹饪书》第二版

by Anthony Molinaro, Robert de Graaf
May 2025
Intermediate to advanced
570 pages
7h 38m
Chinese
O'Reilly Media, Inc.
Content preview from 《SQL 烹饪书》第二版

第 5 章 元数据查询 元数据查询

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章介绍的秘诀可以让你找到指定模式的相关信息。例如,你可能想知道创建了哪些表或哪些外键没有索引。本书中的所有 RDBMS 都提供了用于获取此类数据的表和视图。本章中的方法将帮助你开始从这些表和视图中获取信息。

虽然从高层次上讲,在 RDBMS 中的表和视图中存储元数据的策略是常见的,但最终实现的标准化程度与本书涉及的大多数 SQL 语言特性不同。因此,与其他章节相比,在本章中,为每个 RDBMS 提供不同解决方案的情况要普遍得多。

以下是为本书涉及的每种 RDMS 编写的最常见模式查询的精选。可用的信息远远多于本章所介绍的方法。如果需要超出本章介绍的范围,请查阅 RDBMS 文档,了解目录或数据字典表/视图的完整列表。

提示

为便于演示,本章中的所有方法都假定有一个名为 SMEAGOL 的模式。

5.1 列出模式中的表

问题

您想查看在给定模式中创建的所有表的列表。

解决方案

下面的解决方案都假定你正在使用 SMEAGOL 模式。解决方案的基本方法对所有 RDBMS 都是一样的:查询一个系统表(或视图),其中包含数据库中每个表的记录。

DB2

查询 SYSCAT.TABLES:

1 select tabname
2   from syscat.tables
3  where tabschema = 'SMEAGOL'

Oracle

查询 SYS.ALL_TABLES:

select table_name
  from all_tables
 where owner = 'SMEAGOL'

PostgreSQL、MySQL 和 SQL Server

查询 INFORMATION_SCHEMA.TABLES:

1 select table_name
2   from information_schema.tables
3  where table_schema = 'SMEAGOL'

讨论

数据库以一种令人愉悦的循环方式,通过您为自己的应用程序创建的机制(表和视图)来揭示有关自身的信息。例如,Oracle 维护着大量的系统视图目录,如 ALL_TABLES,您可以通过查询这些目录来获取有关表、索引、授权和其他任何数据库对象的信息。

提示

Oracle 的目录视图就是视图。它们基于一组底层表,这些表以不方便用户使用的形式包含信息。视图为 Oracle 目录数据赋予了可用的面貌。

Oracle 的系统视图和 DB2 的系统表都是针对特定供应商的。而 PostgreSQL、MySQL 和 SQL Server 则支持一种叫做信息模式的东西,它是由 ISO SQL 标准定义的一组视图。这就是为什么同样的查询可以适用于所有这三种数据库的原因。

5.2 列出表格的列

问题

您要列出表格中的列及其数据类型,以及它们在表格中的位置。

解决方案

以下解决方案假定您要列出模式 SMEAGOL 中名为 EMP 的表中的列及其数据类型和数值位置。

DB2

查询 SYSCAT.COLUMNS:

1 select colname, typename, colno
2   from syscat.columns
3  where tabname   = 'EMP'
4    and tabschema = 'SMEAGOL'

Oracle

查询 ALL_TAB_COLUMNS:

1 select column_name, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

SQL Essentials For Dummies

SQL Essentials For Dummies

Richard Blum, Allen G. Taylor
Oracle SQL By Example

Oracle SQL By Example

Alice Rischert

Publisher Resources

ISBN: 9798341658813