简体   繁体   中英

MySQL Query To Return, Schema_Name, Table_NAme, Count, Size_of_Table, Primary_Key and Auto_Increment_Key

I have another tough MySQL query that I am trying to work out. I need a query that will return the following in a single command:

+----------+------------------------------+-----------+-------------+----------------+--------------------+
| Database | Table_Name                   | Count     | Size        | Primary-Key    | Auto_Increment_Key |
+----------+------------------------------+-----------+-------------+----------------+--------------------+
| mydb     | account_length               |      2408 | 0.04 Mb     | account_id     | account_id         |
| mydb     | account_log                  |      1225 | 0.09 Mb     | log_id         | NULL               |

I am still new to MySQL. I have tried various queries against the information_schema, but have not been able to get one that returns the data I need.

The query would simply get a table's row count, it's size in MB, it's primary-key column and it's auto_increment key column.

I appreciate any help.

Thanks

Something like this will help you:

SELECT t.table_schema, 
       t.table_name, 
       Round(( t.data_length + t.index_length ) / 1048576, 2) AS sizemb, 
       c1.column_name                                         AS primarycol, 
       c2.column_name                                         AS 
       autoincrementcol 
FROM   information_schema.tables t 
       LEFT JOIN information_schema.columns c1 
         ON t.table_schema = c1.table_schema 
            AND t.table_name = c1.table_name 
            AND c1.column_key = 'PRI' 
       LEFT JOIN information_schema.columns c2 
         ON t.table_schema = c2.table_schema 
            AND t.table_name = c2.table_name 
            AND c2.extra = 'auto_increment' 
WHERE  t.table_schema = '............' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM