MySQL Show Datadictionary of tables

前端 未结 6 1189
悲&欢浪女
悲&欢浪女 2021-01-13 09:40

I\'d like to show DataDictionary for entire tables in database.

SHOW COLUMNS
FROM `MyDataBase`.`MyTables` 
WHERE IN ( SELECT TABLE_NAME 
           FROM info         


        
相关标签:
6条回答
  • 2021-01-13 10:22

    Here is what I use to generate a data dictionary when I have to:

    SELECT t.table_schema AS db_name,
           t.table_name,
           (CASE WHEN t.table_type = 'BASE TABLE' THEN 'table'
                 WHEN t.table_type = 'VIEW' THEN 'view'
                 ELSE t.table_type
            END) AS table_type,
            c.column_name,
            c.column_type,
            c.column_default,
            c.column_key,
            c.is_nullable,
            c.extra,
            c.column_comment
    FROM information_schema.tables AS t
    INNER JOIN information_schema.columns AS c
    ON t.table_name = c.table_name
    AND t.table_schema = c.table_schema
    WHERE t.table_type IN ('base table', 'view')
    AND t.table_schema LIKE '%'
    ORDER BY t.table_schema,
             t.table_name,
             c.ordinal_position
    

    This will list all of the databases on the server. You may want to change the where clause to only look at the specific table schema you want.

    0 讨论(0)
  • 2021-01-13 10:22

    From MySQL 5.7 Manual

    Many sections indicate what SHOW statement is equivalent to a SELECT that retrieves information from INFORMATION_SCHEMA. For SHOW statements that display information for the default database if you omit a FROM db_name clause, you can often select information for the default database by adding an AND TABLE_SCHEMA = SCHEMA() condition to the WHERE clause of a query that retrieves information from an INFORMATION_SCHEMA table.

    0 讨论(0)
  • 2021-01-13 10:31

    The shortest syntax is this:

    SHOW COLUMNS 
    FROM `MyDataBase`.`MyTables`;
    

    Full SHOW Syntax for columns:

    SHOW [FULL] COLUMNS 
    FROM tbl_name [FROM db_name] 
    [like_or_where]
    
    0 讨论(0)
  • 2021-01-13 10:32
    describe table_name;
    

    Retrieving information from INFORMATION_SCHEMA, but need DBA privilege.

    0 讨论(0)
  • 2021-01-13 10:38

    is this what you want:

    SELECT `COLUMN_NAME` 
    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='yourdatabasename' 
        AND `TABLE_NAME`='yourtablename';
    
    0 讨论(0)
  • 2021-01-13 10:38

    Usually I prefer to take this with multiple DESC. I feel SHOW COLUMNS is bit slower than DESC table_name.

    So if want to get all the columns in some databases

    1. Loop thru SHOW TABLES FROM DB_NAME
    2. Loop thru all tables as DESC table_name

    In the same way, SHOW INDEXES is slower when compared to SHOW CREATE TABLE if you just want to see the indexes on a table

    0 讨论(0)
提交回复
热议问题