Overview of MySQL information_schema Database With Practical Examples

by Ramesh Natarajan on November 13, 2008

[MySQL Information Schema]Starting from MySQL 5, when you execute show databases command, information_schema will be listed in the database list. information_schema is a virtual MySQL database that stores metadata information of all the MySQL databases.

Typically you will execute either DESCRIBE or SHOW commands to get the metadata information. However using the information_schema views, you can execute the standard select SQL command to get the metadata information. In this article, let us review few practical examples on how to use the information_schema database.

1. Get basic information about information_schema

show databases command will display the information_schema in the database list. information_schema is not a physical database.  When you perform select on the information_schema tables, it pulls the data from the real database and other system variables.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bugs               |
| mysql              |
| sugarcrm           |
+--------------------+
4 rows in set (0.00 sec)

Following are the tables (views) available in the information_schema database.

mysql> use information_schema;
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema          |
+---------------------------------------+
| CHARACTER_SETS                        |
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS                               |
| COLUMN_PRIVILEGES                     |
| KEY_COLUMN_USAGE                      |
| PROFILING                             |
| ROUTINES                              |
| SCHEMATA                              |
| SCHEMA_PRIVILEGES                     |
| STATISTICS                            |
| TABLES                                |
| TABLE_CONSTRAINTS                     |
| TABLE_PRIVILEGES                      |
| TRIGGERS                              |
| USER_PRIVILEGES                       |
| VIEWS                                 |
+---------------------------------------+
17 rows in set (0.00 sec)

2. Query to display tables with more than 1000 rows

Using the above mentioned information_schema tables you can build your own query to get metadata information. This example displays the MySQL tables that have more than 1000 rows.

mysql> select concat(table_schema,'.',table_name) as table_name,table_rows
    -> from information_schema.tables where table_rows > 1000
    -> order by table_rows desc;

+----------------------------------+------------+
| table_name                       | table_rows |
+----------------------------------+------------+
| bugs.series_data                 |      52778 |
| bugs.bugs_activity               |      26436 |
| bugs.longdescs                   |      21473 |
| bugs.email_setting               |       5370 |
| bugs.attachments                 |       4714 |
| bugs.attach_data                 |       4651 |
| bugs.cc                          |       4031 |
| bugs.bugs                        |       2190 |
| bugs.namedqueries_link_in_footer |       1228 |
+----------------------------------+------------+
9 rows in set (0.04 sec)

3. Query to list all tables without primary key

This example gives a list of all the tables without primary key.

SELECT CONCAT(t.table_name,".",t.table_schema) as table_name
FROM information_schema.TABLES t
LEFT JOIN information_schema.TABLE_CONSTRAINTS tc
ON t.table_schema = tc.table_schema
AND t.table_name = tc.table_name
AND tc.constraint_type = 'PRIMARY KEY'
WHERE tc.constraint_name IS NULL
AND t.table_type = 'BASE TABLE';

4. Implement history data for tables using information_schema

Putting the MySQL information_schema to Use article implements a history database using the information schema. The first half of this article describes the requirements for the history database, and a generic design to implement it. The second half describes the stepwise construction of code-generator that creates the SQL to construct and load the history database. The code-generator is driven by the information schema and some features of the information schema are discussed in detail.

5. Query to list top 5 largest tables in the database

This examples gives the top 5 largest space occupying tables in the database along with it’s size in MB.

mysql> SELECT concat(table_schema,'.',table_name) table_name,
    -> concat(round(data_length/(1024*1024),2),'M') data_length
    -> FROM information_schema.TABLES
    -> ORDER BY data_length DESC LIMIT 5;

+--------------------+-------------+
| table_name         | data_length |
+--------------------+-------------+
| bugs.attach_data   | 706.89M     |
| bugs.longdescs     | 3.45M       |
| bugs.bugs_activity | 1.45M       |
| bugs.series_data   | 0.75M       |
| bugs.attachments   | 0.51M       |
+--------------------+-------------+
5 rows in set (0.05 sec)

Additional References:

Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. Disable MySQL History – Clear ~/.mysql_history and MYSQL_HISTFILE
  2. Backup and Restore MySQL Database Using mysqldump
  3. Get Quick Info On MySQL DB, Table, Column and Index Using mysqlshow
  4. 15 Practical Usages of Mysqladmin Command For Administering MySQL Server
  5. 15 Practical PostgreSQL Database Administration Commands
  

Vim 101 Hacks Book

{ 1 trackback }

Show the largest MySQL tables on a box | tail -f findings.out
November 28, 2008 at 7:23 pm

{ 2 comments… read them below or add one }

1 Leslie Satenstein Montreal Canada April 18, 2009 at 12:49 pm

Hi Ramesh.

In working with some sophisticated ERPs, (Baan), their sql for retrievals appears like this

select xxxxxxx
from xxxxxxxx
where
xxxxxxx
selectdo
xxxxxxx
selectempty
xxxxxxxx
selecteos
xxxxxxxxx
endselect

The where clause can have other selects (recursive). such as from where exists (select … )
The where clause always did a left join, so the programmer decided if he wanted
where a = b,
or wanted
where b=a

selectdo
loop here until empty
selecteos
after the last match
selectempty
if there is no match
endselect

The from clause indicated if it was for update, etc. A break command and continue are supported in the seledtdo as well as the selecteos.

How difficult is it to develop this set of statements as a pre-parser expression for mysql, or oracle, or postgressql ?

2 JuanPablo December 10, 2009 at 7:16 pm

hi,

how I can use select table_name from information_schema.tables where table_schema=”miDB” in a query ?

and make same query over every table of mi db?

thanks.

Leave a Comment

Previous post:

Next post: