How to Export Only View Schemas from MySQL Database using mysqldump


34 views

When working with MySQL database migrations or documentation, you'll often need to export view definitions separately from table schemas. The standard mysqldump --no-data approach only dumps table structures, leaving views behind - a common pain point for database administrators.

To specifically dump view schemas, we need to modify our mysqldump command with these key parameters:

mysqldump --no-data --routines --skip-triggers \
--no-create-info --no-create-db --no-tablespaces \
your_database_name > views_schema.sql

The magic combination works because:

  • --routines: Includes stored procedures and functions (views are treated similarly)
  • --skip-triggers: Avoids unnecessary trigger definitions
  • --no-create-info: Skips table creation statements
  • --no-create-db: Omits database creation syntax

For more control, query the INFORMATION_SCHEMA directly:

SELECT TABLE_NAME, VIEW_DEFINITION 
FROM INFORMATION_SCHEMA.VIEWS 
WHERE TABLE_SCHEMA = 'your_database_name';

Here's how you might automate this for multiple databases:

#!/bin/bash
DB_NAME="production_db"
VIEW_FILE="views_${DB_NAME}_$(date +%Y%m%d).sql"

mysqldump -u admin -p --no-data --routines \
--skip-triggers --no-create-info --no-create-db \
${DB_NAME} | grep -E "CREATE (ALGORITHM=.*)?VIEW" > ${VIEW_FILE}
  • Missing dependencies (views referencing other views)
  • Character set inconsistencies between dump and restore environments
  • Version-specific syntax differences in view definitions

For complete view documentation, combine with table relationships:

SELECT 
    v.TABLE_NAME, 
    v.VIEW_DEFINITION,
    v.CHECK_OPTION,
    v.IS_UPDATABLE,
    GROUP_CONCAT(t.TABLE_NAME) AS dependent_tables
FROM 
    INFORMATION_SCHEMA.VIEWS v
JOIN 
    INFORMATION_SCHEMA.VIEW_TABLE_USAGE t
ON 
    v.TABLE_NAME = t.VIEW_NAME
WHERE 
    v.TABLE_SCHEMA = 'your_db'
GROUP BY 
    v.TABLE_NAME;

When working with MySQL databases, many developers discover that the standard mysqldump --no-data command only exports table schemas while completely skipping view definitions. This behavior occurs because:


# This won't dump view schemas:
mysqldump -u username -p --no-data database_name > schema.sql

To specifically export view definitions, you need to combine several mysqldump parameters:


# This correctly dumps view schemas:
mysqldump -u username -p --no-data --routines --triggers \
--no-create-info --no-create-db --no-tablespaces \
database_name > views_schema.sql

For more control over the output, you can query the INFORMATION_SCHEMA directly:


SELECT TABLE_NAME, VIEW_DEFINITION 
FROM INFORMATION_SCHEMA.VIEWS 
WHERE TABLE_SCHEMA = 'your_database_name';

Here's how to export specific views matching a pattern:


# Export only views starting with 'report_'
mysqldump -u root -p --no-data --routines \
--no-create-info --no-create-db \
database_name $(mysql -u root -p -Nse \
"SELECT table_name FROM information_schema.views \
WHERE table_schema = 'database_name' \
AND table_name LIKE 'report_%'") > report_views.sql

You might encounter these issues:

  • Error 1449: Fix by adding --skip-add-drop-table
  • Collation issues: Use --default-character-set=utf8mb4
  • Performance problems: Add --single-transaction for large databases