Skip to content

Guide

MySQL data dictionary query and template

MySQL / data dictionary / database documentation / information_schema

Generate a MySQL data dictionary from information_schema.COLUMNS with column comments, export it to CSV, and keep it accurate after every ALTER TABLE.

MySQL keeps everything a data dictionary needs in information_schema, including a free-text comment on every table and column. Most MySQL schemas also carry years of ENUM columns whose allowed values are the most useful thing in the whole dictionary, because nobody remembers what state 7 meant. Both come out of one query.

The query

SELECT
    c.TABLE_NAME                   AS table_name,
    c.ORDINAL_POSITION             AS position,
    c.COLUMN_NAME                  AS column_name,
    c.COLUMN_TYPE                  AS data_type,
    c.IS_NULLABLE                  AS is_nullable,
    c.COLUMN_DEFAULT               AS default_value,
    c.COLUMN_KEY                   AS key_type,
    k.REFERENCED_TABLE_NAME        AS references_table,
    k.REFERENCED_COLUMN_NAME       AS references_column,
    c.COLUMN_COMMENT               AS description
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.KEY_COLUMN_USAGE k
       ON k.TABLE_SCHEMA = c.TABLE_SCHEMA
      AND k.TABLE_NAME   = c.TABLE_NAME
      AND k.COLUMN_NAME  = c.COLUMN_NAME
      AND k.REFERENCED_TABLE_NAME IS NOT NULL
WHERE c.TABLE_SCHEMA = 'sales'
ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION;

COLUMN_TYPE is the useful one: it keeps lengths, unsigned and the full enum('pending','paid','shipped') list, where DATA_TYPE only says enum. COLUMN_KEY marks primary keys as PRI, unique keys as UNI and indexed columns as MUL. Table comments are in information_schema.TABLES.TABLE_COMMENT.

Exporting it

The mysql client writes tab-separated output with a header, which Excel opens as it is:

mysql --defaults-extra-file=/etc/reports/sales.cnf --batch \
  -e "SELECT ... FROM information_schema.COLUMNS ..." > data_dictionary.tsv

MySQL Workbench can also export a result grid to CSV, HTML or JSON from the query panel, which is the quickest path for a one-off.

Writing the descriptions

MySQL has no separate COMMENT ON statement. A column comment is part of the column definition, so changing it means repeating the whole definition:

ALTER TABLE orders
  MODIFY COLUMN total DECIMAL(12,2) NOT NULL DEFAULT 0
  COMMENT 'Order total in EUR including VAT, after discounts.';

Getting the definition slightly wrong, a missing NOT NULL or a different default, changes the column as well as the comment. That risk is why many MySQL schemas have no comments at all, and why the descriptions end up in a spreadsheet instead.

Why the spreadsheet drifts

The export is a picture of the schema on the day it was taken. The next ALTER TABLE adds a column the spreadsheet does not list, a new ENUM value appears that the spreadsheet does not explain, and the descriptions typed into the file slowly detach from the columns they describe.

A MySQL data dictionary that stays current

Taavik's documentation regenerates the dictionary from information_schema after every scan. An agent inside the network reads the catalog with a read-only login, and the document lists, for every table, each column with its full type including ENUM values, nullability, default, COLUMN_COMMENT and the date of its last change, plus the indexes. Notes written in the workspace attach to a database, schema, table or column and survive regeneration, so explaining a column no longer requires an ALTER TABLE.

On the Pro plan the agent can also learn the actual values of short varchar status columns that were never declared as ENUM, which is often where the real states of a MySQL application hide, and hands them to AI assistants through the MCP connector. Each schema change creates a new revision with a diff, the document can be shared by link or copied as HTML, and the schema map exports to Mermaid, DBML or SQL. There is no Excel or PDF export.

Guides for the other engines: SQL Server data dictionary and PostgreSQL data dictionary.