天天看点

获取数据库元数据

  1、show语句,如show tables等等。

  2、information_schema数据库里的数据表。

  3、命令行程序,如mysqlshow或者是mysqldump。

  一、用show语句获取

  获取服务器所管理的数据库。

show databases;

查看给定数据库的创建语句。

show create database db_name;

  列出默认数据库里的所有数据表。

show tables;

  列出给定数据库里的所有数据表。

show tables from db_name;

  查看给定数据表的创建语句。

show create table tbl_name;

  查看指定数据表的数据列和索引信息。

show columns from tbl_name;  这个语句和desc 的输出是一样的。

show index from tbl_name;

  有几种show语句还可以带有like 'pattern'子句,用来把show语句的输出限定在给定范围,并且允许使用通配符号。

  二、从information_schema数据库获取元数据

  可以将这个数据库看成一个虚拟的数据库,这个数据库里的数据表是一些由不同的数据库元素数据构成的视图,这个数据库里有以下数据表项:

mysql> show tables from information_schema;

+---------------------------------------+

| tables_in_information_schema          |

| character_sets                        |

| collations                            |

| collation_character_set_applicability |

| columns                               |

| column_privileges                     |

| engines                               |

| events                                |

| files                                 |

| global_status                         |

| global_variables                      |

| key_column_usage                      |

| partitions                            |

| plugins                               |

| processlist                           |

| profiling                             |

| referential_constraints               |

| routines                              |

| schemata                              |

| schema_privileges                     |

| session_status                        |

| session_variables                     |

| statistics                            |

| tables                                |

| table_constraints                     |

| table_privileges                      |

| triggers                              |

| user_privileges                       |

| views                                 |

28 rows in set (0.01 sec)

分别对这些列简单的说明。

  1、schemata、tables、views、routimes、triggers、events、partitions、columns,表示的信息分别是数据库,数据表,视图,存储例程,触发器,数据库里的事件,数据表分区和数据列的信息,以数据表为例,就是tables表,有以下列:

mysql> show columns from information_schema.columns;

+--------------------------+---------------------+------+-----+---------+-------+

| field                    | type                | null | key | default | extra |

| table_catalog            | varchar(512)        | yes  |     | null    |       | 

| table_schema             | varchar(64)         | no   |     |         |       | 

| table_name               | varchar(64)         | no   |     |         |       | 

| column_name              | varchar(64)         | no   |     |         |       | 

| ordinal_position         | bigint(21) unsigned | no   |     | 0       |       | 

| column_default           | longtext            | yes  |     | null    |       | 

| is_nullable              | varchar(3)          | no   |     |         |       | 

| data_type                | varchar(64)         | no   |     |         |       | 

| character_maximum_length | bigint(21) unsigned | yes  |     | null    |       | 

| character_octet_length   | bigint(21) unsigned | yes  |     | null    |       | 

| numeric_precision        | bigint(21) unsigned | yes  |     | null    |       | 

| numeric_scale            | bigint(21) unsigned | yes  |     | null    |       | 

| character_set_name       | varchar(32)         | yes  |     | null    |       | 

| collation_name           | varchar(32)         | yes  |     | null    |       | 

| column_type              | longtext            | no   |     | null    |       | 

| column_key               | varchar(3)          | no   |     |         |       | 

| extra                    | varchar(27)         | no   |     |         |       | 

| privileges               | varchar(80)         | no   |     |         |       | 

| column_comment           | varchar(255)        | no   |     |         |       | 

+--------------------------+---------------------+------+-----+---------+-------|

  这是我查看一些数据表的记录

mysql> select * from information_schema.tables where table_schema != 'information_schema' and table_schema!='mysql'\g;

*************************** 1. row ***************************

  table_catalog: null

   table_schema: db_info

     table_name: i_node

     table_type: base table

         engine: myisam

        version: 10

     row_format: dynamic

     table_rows: 11

 avg_row_length: 21

    data_length: 240

max_data_length: 281474976710655

   index_length: 2048

      data_free: 0

 auto_increment: 16

    create_time: 2012-09-07 03:07:37

    update_time: 2012-09-23 07:57:37

     check_time: null

table_collation: latin1_swedish_ci

       checksum: null

 create_options: 

  table_comment: 

*************************** 2. row ***************************

     table_name: test

     row_format: fixed

     table_rows: 3

 avg_row_length: 7

    data_length: 21

max_data_length: 1970324836974591

   index_length: 1024

 auto_increment: null

    create_time: 2012-09-22 02:25:01

    update_time: 2012-09-22 02:37:18

*************************** 3. row ***************************

   table_schema: test

 avg_row_length: 20

    data_length: 60

    create_time: 2012-09-07 20:44:41

    update_time: 2012-09-07 20:44:41

3 rows in set (0.00 sec)

 2、files。关于ndb硬盘数据文件的信息。

  3、table_constrains、key_column_usage:关于数据表和数据列上的约束条件的信息,一般唯一化索引和外键都属于这些约束条件。

  4、statistics。关于数据表索引特性的信息。

  5、referential_constrains。关于外键的信息。

  6、character_sets、collations、collation_character_set_applicability。关于所支持的字符集,每种字符集的排序方式、每种排序方式与它的字符集的映射关系信息。

  7、engines、plugins。关于存储引擎和服务器插件的信息。

  8、user_privileges、schema_privileges、table_privileges、column_privileges。全局、数据库、数据表和数据列的权限信息。这些信息分别来自mysql数据库里的user,db,tables_priv,column_priv数据表。

  9、processlist。在服务器内执行的线程的信息。

  10、global_variables、session_variables、global_status、session_status。全局和会话级系统变量和状态变量的值。

  三、从命令行获取元数据

  先介绍一个命令的使用:mysqlshow。

mysqlshow[选项] [db_name [tbl_name [col_name]]]

  1、如果没有给出数据库,显示所有匹配的数据库。

  2、如果没有给出表,显示数据库中所有匹配的表。

  3、如果没有给出列,显示表中所有匹配的列和列类型。

  说明几个常用的选项信息:

  1、--keys。查看某给定数据表里的索引信息。

  2、--status。查看某给定数据库里的数据表的描述性信息。如:

[root@localhost ~]# mysqlshow --status db_info

database: db_info

+--------+--------+---------+------------+------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-------------------+----------+----------------+---------+

| name   | engine | version | row_format | rows | avg_row_length | data_length | max_data_length  | index_length | data_free | auto_increment | create_time         | update_time         | check_time | collation         | checksum | create_options | comment |

| i_node | myisam | 10      | dynamic    | 11   | 21             | 240         | 281474976710655  | 2048         | 0         | 16             | 2012-09-07 03:07:37 | 2012-09-23 07:57:37 |            | latin1_swedish_ci |          |                |         |

| test   | myisam | 10      | fixed      | 3    | 7              | 21          | 1970324836974591 | 1024         | 0         |                | 2012-09-22 02:25:01 | 2012-09-22 02:37:18 |            | latin1_swedish_ci |          |                |         |

+--------+--------+---------+------------+------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-------------------+----------+----------------+---------

  在使用该工具的时候,如果没有默认的数据库服务例程,不要忘了加上--host --user --password甚至是 --socket等信息。

====================================分割线================================

最新内容请见作者的github页:http://qaseven.github.io/