Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all tables from information_schema, MySQL

Tags:

sql

mysql

I just dont understand one thing. When I type:

SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';

I get:

+----------------------------------------------+
| table_name                                   |
+----------------------------------------------+
| columns_priv                                 |
| db                                           |
| event                                        |
| func                                         |
| general_log                                  |
| help_category                                |
| help_keyword                                 |
| help_relation                                |
| help_topic                                   |
| host                                         |
| ndb_binlog_index                             |
| plugin                                       |
| proc                                         |
| procs_priv                                   |
| proxies_priv                                 |
| servers                                      |
| slow_log                                     |
| tables_priv                                  |
| time_zone                                    |
| time_zone_leap_second                        |
| time_zone_name                               |
| time_zone_transition                         |
| time_zone_transition_type                    |
| user                                         |
| cond_instances                               |
| events_waits_current                         |
| events_waits_history                         |
| events_waits_history_long                    |
| events_waits_summary_by_instance             |
| events_waits_summary_by_thread_by_event_name |
| events_waits_summary_global_by_event_name    |
| file_instances                               |
| file_summary_by_event_name                   |
| file_summary_by_instance                     |
| mutex_instances                              |
| performance_timers                           |
| rwlock_instances                             |
| setup_consumers                              |
| setup_instruments                            |
| setup_timers                                 |
| threads                                      |
+----------------------------------------------+
41 rows in set (0.23 sec)

but selecting seems not to work at all:

mysql> select * from db;
ERROR 1109 (42S02): Unknown table 'db' in information_schema
mysql>

How is that possible? I mean, SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'; showed that there IS a table named 'db' ...

Secondly, when I type: SELECT table_name FROM INFORMATION_SCHEMA.TABLES; it gives me :

+----------------------------------------------+
| table_name                                   |
+----------------------------------------------+
| CHARACTER_SETS                               |
| COLLATIONS                                   |
| COLLATION_CHARACTER_SET_APPLICABILITY        |
| COLUMNS                                      |
| COLUMN_PRIVILEGES                            |
| ENGINES                                      |
| EVENTS                                       |
| FILES                                        |
| GLOBAL_STATUS                                |
| GLOBAL_VARIABLES                             |
| KEY_COLUMN_USAGE                             |
| PARAMETERS                                   |
| PARTITIONS                                   |
| PLUGINS                                      |
| PROCESSLIST                                  |
| PROFILING                                    |
| REFERENTIAL_CONSTRAINTS                      |
| ROUTINES                                     |
| SCHEMATA                                     |
| SCHEMA_PRIVILEGES                            |
| SESSION_STATUS                               |
| SESSION_VARIABLES                            |
| STATISTICS                                   |
| TABLES                                       |
| TABLESPACES                                  |
| TABLE_CONSTRAINTS                            |
| TABLE_PRIVILEGES                             |
| TRIGGERS                                     |
| USER_PRIVILEGES                              |
| VIEWS                                        |
| INNODB_BUFFER_PAGE                           |
| INNODB_TRX                                   |
| INNODB_BUFFER_POOL_STATS                     |
| INNODB_LOCK_WAITS                            |
| INNODB_CMPMEM                                |
| INNODB_CMP                                   |
| INNODB_LOCKS                                 |
| INNODB_CMPMEM_RESET                          |
| INNODB_CMP_RESET                             |
| INNODB_BUFFER_PAGE_LRU                       |
| columns_priv                                 |
| db                                           |
| event                                        |
| func                                         |
| general_log                                  |
| help_category                                |
| help_keyword                                 |
| help_relation                                |
| help_topic                                   |
| host                                         |
| ndb_binlog_index                             |
| plugin                                       |
| proc                                         |
| procs_priv                                   |
| proxies_priv                                 |
| servers                                      |
| slow_log                                     |
| tables_priv                                  |
| time_zone                                    |
| time_zone_leap_second                        |
| time_zone_name                               |
| time_zone_transition                         |
| time_zone_transition_type                    |
| user                                         |
| cond_instances                               |
| events_waits_current                         |
| events_waits_history                         |
| events_waits_history_long                    |
| events_waits_summary_by_instance             |
| events_waits_summary_by_thread_by_event_name |
| events_waits_summary_global_by_event_name    |
| file_instances                               |
| file_summary_by_event_name                   |
| file_summary_by_instance                     |
| mutex_instances                              |
| performance_timers                           |
| rwlock_instances                             |
| setup_consumers                              |
| setup_instruments                            |
| setup_timers                                 |
| threads                                      |
+----------------------------------------------+
81 rows in set (0.00 sec)

and Im able to do: mysql> select * from events; and Im getting some results. Why is that? Why can I do select only on upper-case-names? And further, how can I select from information_schema tables given only in uppercase? Cheers

If it matters: Im logged as root to my db.

like image 369
yak Avatar asked Oct 18 '13 12:10

yak


2 Answers

You select only table_name, but what about table_schema:

MariaDB [(none)]> SELECT table_name, table_schema FROM INFORMATION_SCHEMA.TABLES WHERE 

    TABLE_TYPE = 'BASE TABLE';
    +----------------------------------------------+--------------------+
    | table_name                                   | table_schema       |
    +----------------------------------------------+--------------------+
    | columns_priv                                 | mysql              |
    | db                                           | mysql              |
    | event                                        | mysql              |
    | func                                         | mysql              |
    | general_log                                  | mysql              |
    | help_category                                | mysql              |
    | help_keyword                                 | mysql              |
    | help_relation                                | mysql              |
    | help_topic                                   | mysql              |
    | host                                         | mysql              |
    | ndb_binlog_index                             | mysql              |
    | plugin                                       | mysql              |
    | proc                                         | mysql              |
    | procs_priv                                   | mysql              |
    | proxies_priv                                 | mysql              |
    | servers                                      | mysql              |
    | slow_log                                     | mysql              |
    | tables_priv                                  | mysql              |
    | time_zone                                    | mysql              |
    | time_zone_leap_second                        | mysql              |
    | time_zone_name                               | mysql              |
    | time_zone_transition                         | mysql              |
    | time_zone_transition_type                    | mysql              |
    | user                                         | mysql              |
    | cond_instances                               | performance_schema |
    | events_waits_current                         | performance_schema |
    | events_waits_history                         | performance_schema |
    | events_waits_history_long                    | performance_schema |
    | events_waits_summary_by_instance             | performance_schema |
    | events_waits_summary_by_thread_by_event_name | performance_schema |
    | events_waits_summary_global_by_event_name    | performance_schema |
    | file_instances                               | performance_schema |
    | file_summary_by_event_name                   | performance_schema |
    | file_summary_by_instance                     | performance_schema |
    | mutex_instances                              | performance_schema |
    | performance_timers                           | performance_schema |
    | rwlock_instances                             | performance_schema |
    | setup_consumers                              | performance_schema |
    | setup_instruments                            | performance_schema |
    | setup_timers                                 | performance_schema |
    | threads                                      | performance_schema |
    +----------------------------------------------+--------------------+

And you get:

MariaDB [(none)]> select * from mysql.db;
Empty set (0.00 sec)
like image 63
Ed Ganiukov Avatar answered Oct 06 '22 00:10

Ed Ganiukov


'information_schema' has ONLY SERVICE INFORMATION. It has info about table 'db' is exist, but it table IS NOT IN 'information_schema' DATABASE -- somewhere, but not in 'information_schema'.

Info about database which has needed table saved in TABLE_SCHEMA field

like image 35
Max Avatar answered Oct 06 '22 00:10

Max