Second Brain: Crafted, Curated, Connected, Compounded on 10月02日
数据库元数据查询指南
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文档提供了关于如何使用 INFORMATION_SCHEMA 查询数据库元数据的全面指南。INFORMATION_SCHEMA 是一个标准的 SQL 接口,允许用户访问数据库的结构信息,包括表、列、主键、外键和索引等。文档详细列举了查询各类数据库对象的 SQL 语句示例,并介绍了 INFORMATION_SCHEMA 在不同数据库系统(如 MySQL, PostgreSQL, SQL Server 等)中的支持情况,以及一些数据库特有的查询方式。此外,还提供了查看数据库和表大小的方法,旨在帮助用户更有效地探索和管理数据库。

🗂️ **INFORMATION_SCHEMA 概述**:INFORMATION_SCHEMA 是一个标准的 SQL 接口,提供了访问数据库元数据(如数据库名、表名、列数据类型、访问权限等)的统一方式,类似于数据字典或系统目录,方便用户查询数据库的结构信息。

🔍 **核心查询功能**:文档提供了针对数据库探索的常用 SQL 查询语句,包括列出所有表及其模式和类型、获取特定表的列详细信息(如名称、数据类型、长度、是否可空、默认值)、查找主键和外键关系,以及查询索引信息。这些查询能够帮助用户全面了解数据库的构成。

📊 **数据库兼容性与扩展**:INFORMATION_SCHEMA 在主流数据库系统中得到广泛支持,包括 MySQL/MariaDB、Microsoft SQL Server、PostgreSQL、Oracle、IBM Db2 等。对于 SQLite、H2 Database、Snowflake、Amazon Redshift 和 Google BigQuery 等,也提供了部分支持或有相应的替代实现方式,满足不同环境下的元数据查询需求。

📈 **性能与大小监控**:除了结构信息,文档还介绍了如何查询数据库和表的大小。以 PostgreSQL 为例,提供了查看整个数据库大小和单个表总大小的 SQL 语句,这对于资源规划、性能优化和容量管理至关重要。

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.

These are the Open Table Format Catalogs of today’s world, where you query what tables are in a relational database.

Similar to the Hive Metastore before, an index for what tables you have in your Data Lake.

# Queries for Database Exploration

Here are some of the most commonly used queries.

# Tables Information

12345
-- List all tables in the databaseSELECT table_schema, table_name, table_typeFROM INFORMATION_SCHEMA.TABLESWHERE table_schema NOT IN ('information_schema', 'pg_catalog', 'sys')ORDER BY table_schema, table_name;

# Columns Information

123456
-- Get column details for a specific tableSELECT column_name, data_type, character_maximum_length,        is_nullable, column_defaultFROM INFORMATION_SCHEMA.COLUMNSWHERE table_schema = 'your_schema' AND table_name = 'your_table'ORDER BY ordinal_position;

# Primary Keys

 1 2 3 4 5 6 7 8 910
-- Find primary keysSELECT     tc.table_schema,     tc.table_name,     kc.column_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tcJOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kc     ON tc.constraint_name = kc.constraint_nameWHERE tc.constraint_type = 'PRIMARY KEY'ORDER BY tc.table_schema, tc.table_name;

# Foreign Keys

Foreign Key exploration:

 1 2 3 4 5 6 7 8 91011121314
-- Find foreign key relationshipsSELECT    fk.table_schema,    fk.table_name,    fk.column_name,    fk.constraint_name,    rc.referenced_table_schema,    rc.referenced_table_name,    rc.referenced_column_nameFROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE fkJOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc    ON fk.constraint_name = rc.constraint_nameWHERE rc.referenced_table_name IS NOT NULLORDER BY fk.table_schema, fk.table_name;

# Indexes

 1 2 3 4 5 6 7 8 9101112
-- List indexes (implementation varies by database system)-- For PostgreSQL:SELECT    t.relname AS table_name,    i.relname AS index_name,    a.attname AS column_nameFROM pg_class tJOIN pg_index ix ON t.oid = ix.indrelidJOIN pg_class i ON i.oid = ix.indexrelidJOIN pg_attribute a ON a.attnum = ANY(ix.indkey) AND a.attrelid = t.oidWHERE t.relkind = 'r'ORDER BY t.relname, i.relname;

# Database Size and Growth

123456
-- Database size (PostgreSQL example)SELECT    pg_database.datname,    pg_size_pretty(pg_database_size(pg_database.datname)) AS sizeFROM pg_databaseORDER BY pg_database_size(pg_database.datname) DESC;

# Table Size

1234567
-- Table sizes (PostgreSQL example)SELECT    table_name,    pg_size_pretty(pg_total_relation_size(table_name)) AS total_sizeFROM information_schema.tablesWHERE table_schema = 'your_schema'ORDER BY pg_total_relation_size(table_name) DESC;

# Supported Databases

Full Support:

    MySQL/MariaDB: Extensive implementation of INFORMATION_SCHEMAMicrosoft SQL Server: Comprehensive support with Microsoft extensionsPostgreSQL: Supports INFORMATION_SCHEMA plus additional system catalogsOracle Database: Has INFORMATION_SCHEMA views (but traditionally relies more on its own data dictionary views like ALL_TABLES, DBA_TABLES)IBM Db2: Supports INFORMATION_SCHEMA

Partial/Alternative Implementations:

    SQLite: Does not have INFORMATION_SCHEMA but provides similar functionality through SQLite system tables and PRAGMA statementsDuckDB: Has INFORMATION_SCHEMAH2 Database: Supports INFORMATION_SCHEMASnowflake: Supports INFORMATION_SCHEMA with cloud data warehouse adaptationsAmazon Redshift: Provides INFORMATION_SCHEMA viewsGoogle BigQuery: Has INFORMATION_SCHEMA with cloud-specific adaptations

Origin: Open Table Format Catalogs
References:
Created 2025-04-30

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

INFORMATION_SCHEMA 数据库元数据 SQL查询 数据库管理 系统目录 数据字典 Database Metadata SQL Queries Database Management System Catalog Data Dictionary
相关文章