Skip to content

MAgento database structure Query #9

Description

@Genaker

To generate a JSON output containing only the table names and their structures without the schema, you can use the following SQL query:

SELECT JSON_OBJECTAGG(
    table_name, 
    JSON_OBJECT(
        'structure', CREATE_TABLE
    )
) AS tables_structure
FROM (
    SELECT 
        table_name,
        CONCAT(
            'CREATE TABLE ', table_name, ' (',
            GROUP_CONCAT(
                CONCAT(column_name, ' ', column_type, 
                       IF(is_nullable = 'NO', ' NOT NULL', ''),
                       IF(column_default IS NOT NULL, CONCAT(' DEFAULT ', column_default), ''),
                       IF(extra != '', CONCAT(' ', extra), '')
                ) SEPARATOR ', '
            ),
            ')'
        ) AS CREATE_TABLE
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
    GROUP BY table_name
) AS table_structures;

This query will produce a JSON object where each key is a table name, and the value is another JSON object containing the table structure. You can run this query in your MySQL database to get a JSON representation of all table structures without the schema.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions