码迷,mamicode.com
首页 > 数据库 > 详细

MySQL INFORMATION_SCHEMA 使用

时间:2016-03-31 12:52:21      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:

--  查看指定数据库的表信息

SET @table_schema=employees;
SELECT 
    table_name,
    table_type,
    engine,
    table_rows,
    avg_row_length,
    data_length,
    index_length,
    table_collation,
    create_time
FROM
    information_schema.tables
WHERE
    table_schema = @table_schema
ORDER BY table_name;

 

-- 查看会话连接信息

SELECT 
    THREAD_ID,
    name,
    type,
    PROCESSLIST_ID,
    PROCESSLIST_USER AS user,
    PROCESSLIST_HOST AS host,
    PROCESSLIST_DB AS db,
    PROCESSLIST_COMMAND AS cmd,
    PROCESSLIST_TIME AS time,
    PROCESSLIST_STATE AS state,
    PROCESSLIST_INFO AS info,
    CONNECTION_TYPE AS type,
    THREAD_OS_ID AS os_id
FROM
    performance_schema.threads
WHERE
    type = FOREGROUND
ORDER BY THREAD_ID;

 

-- CHARACTER_SETS 查看数据库支持的字符集

SELECT * FROM INFORMATION_SCHEMA.CHARACTER_SETS
WHERE CHARACTER_SET_NAME LIKE utf%;

SHOW CHARACTER SET LIKE utf%;

 

-- COLLATIONS  字符序

-- 用于指定数据集如何排序,以及字符串的比对规则
SELECT * FROM INFORMATION_SCHEMA.COLLATIONS
WHERE COLLATION_NAME LIKE utf%;

SHOW COLLATION LIKE utf%;

-- 查看表结构定义信息

SELECT 
    table_name,
    COLUMN_NAME,
    ordinal_position,
    DATA_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT,
    column_type,
    column_key,
    character_set_name,
    collation_name
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    table_name = employees
        AND table_schema = employees;

show columns from employees from employees;


desc employeees.employees;

 

-- 查看支持的引擎

SELECT *  FROM INFORMATION_SCHEMA.ENGINES;
show ENGINES;

 

-- 查看数据库的数据文件信息

SELECT 
    FILE_ID,
    FILE_NAME,
    FILE_TYPE,
    TABLESPACE_NAME,
    FREE_EXTENTS,
    TOTAL_EXTENTS,
    ((TOTAL_EXTENTS - FREE_EXTENTS) * EXTENT_SIZE) / 1024 / 1024 AS MB_used,
    EXTENT_SIZE,
    INITIAL_SIZE,
    MAXIMUM_SIZE,
    AUTOEXTEND_SIZE,
    DATA_FREE,
    STATUS,
    ENGINE
FROM
    INFORMATION_SCHEMA.FILES;

 

-- 查看指定表的约束

SELECT 
    constraint_schema,
    table_name,
    constraint_name,
    column_name,
    ordinal_position,
    CONCAT(table_name,
            .,
            column_name,
             -> ,
            referenced_table_name,
            .,
            referenced_column_name) AS list_of_fks
FROM
    information_schema.KEY_COLUMN_USAGE
WHERE
    REFERENCED_TABLE_SCHEMA = employees
        AND REFERENCED_TABLE_NAME IS NOT NULL
ORDER BY TABLE_NAME , COLUMN_NAME;

 

-- 查看指定分区表信息

SELECT 
    TABLE_SCHEMA,
    table_name,
    partition_name,
    subpartition_name sub_par,
    partition_ordinal_position par_position,
    partition_method method,
    partition_expression expression,
    partition_description description,
    table_rows
FROM
    information_schema.PARTITIONS
WHERE
    table_schema = test
        AND table_name = t;

 

-- 查看支持的插件

SELECT
  PLUGIN_NAME, PLUGIN_STATUS, PLUGIN_TYPE,
  PLUGIN_LIBRARY, PLUGIN_LICENSE
FROM INFORMATION_SCHEMA.PLUGINS;

SHOW PLUGINS;

 

-- 查看数据库连接信息

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;

SHOW FULL PROCESSLIST;

 

-- 查看数据库中的存储过程、函数等

SELECT 
    ROUTINE_SCHEMA,
    routine_name,
    ROUTINE_TYPE,
    data_type,
    routine_body,
    routine_definition,
    routine_comment
FROM
    INFORMATION_SCHEMA.ROUTINES
WHERE
    ROUTINE_TYPE = PROCEDURE
AND ROUTINE_SCHEMA="employees";

 

-- 查看存在的数据库及字符集信息

SELECT 
    SCHEMA_NAME,
    DEFAULT_CHARACTER_SET_NAME,
    DEFAULT_COLLATION_NAME
FROM
    INFORMATION_SCHEMA.SCHEMATA;

SHOW DATABASES;

 

-- 查看索引信息

SELECT 
    table_schema,
    table_name,
    index_name,
    COLUMN_NAME,
    COLLATION,
    CARDINALITY,
    index_type
FROM
    INFORMATION_SCHEMA.STATISTICS
WHERE
    table_name = employees
        AND table_schema = employees;

SHOW INDEX FROM employees FROM employees;

 

-- 查看数据库大小

SELECT 
    table_schema database,
    CONCAT(ROUND(SUM(data_length + index_length) / (1024 * 1024),
                    2),
            M) size
FROM
    information_schema.TABLES
WHERE
    ENGINE = (MyISAM || InnoDB)
GROUP BY table_schema;

 

-- 查看表大小  

SELECT 
    CONCAT(table_schema, ., table_name) table_name,
    CONCAT(ROUND(data_length / (1024 * 1024), 2),
            M) data_length,
    CONCAT(ROUND(index_length / (1024 * 1024), 2),
            M) index_length,
    CONCAT(ROUND(ROUND(data_length + index_length) / (1024 * 1024),
                    2),
            M) total_size
FROM
    information_schema.TABLES
ORDER BY data_length DESC;

 

MySQL INFORMATION_SCHEMA 使用

标签:

原文地址:http://www.cnblogs.com/zhenxing/p/5340592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!