MySQL INFORMATION_SCHEMA 使用

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

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;
时间: 2024-11-11 03:34:44

MySQL INFORMATION_SCHEMA 使用的相关文章

mysql information_schema介绍

mysql information_schema介绍 一.information_schema是什么 information_schema是MySQL自带的一个信息数据库,其保存着关于MySQL服务器所维护的所有其他数据库的信息,如数据库名,数据库的表,表栏的数据类型与访问权限等. 也就是说当你建立一个新的数据库,或者在已有的数据库中增删改表的话,都会记录在information_schema库中. 你可以通过information_schema库来查看各个数据库的信息:有时候是很有用的哦,例如

MySQL information_schema表查询导致内存暴涨

case:下面的一条sql语句,导致mysql实例内存暴涨: select * from tables where table_name not in(select table_name from partitions group by table_name having count(*)>1 ); mysql 5.5, 1w+的innodb表. 下面看下调查的结果: 1.  sql的执行情况以及内存分配:   step1: 构造information_schema.tables临时表 1.1 

十分钟了结MySQL information_schema

information_schema数据库是MySQL系统自带的数据库,它提供了数据库元数据的访问方式.感觉information_schema就像是MySQL实例的一个百科全书,记录了数据库当中大部分我们需要了结的信息,比如字符集,权限相关,数据库实体对象信息,外检约束,分区,压缩表,表信息,索引信息,参数,优化,锁和事物等等.通过information_schema我们可以窥透整个MySQL实例的运行情况,可以了结MySQL实例的基本信息,甚至优化调优,维护数据库等,可以说是真正的一部百科全

mysql information_schema 数据库简介:

1.CHARACTER_SETS 表 CREATE TEMPORARY TABLE `CHARACTER_SETS` ( `CHARACTER_SET_NAME` varchar(32) NOT NULL DEFAULT '', 字符集的名字,例如set names utf8: `DEFAULT_COLLATE_NAME` varchar(32) NOT NULL DEFAULT '', 字符集的全称 `DESCRIPTION` varchar(60) NOT NULL DEFAULT '',

mysql information_schema

SELECT TABLE_NAME,COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH,COLUMN_COMMENT FROM COLUMNS WHERE TABLE_SCHEMA='zhongchao' AND COLUMN_NAME LIKE 'Is%'SELECT * FROM TABLES WHERE TABLE_SCHEMA='zhongchao'

[MySQL] INFORMATION_SCHEMA 数据库包含所有表的字段

sql注入后可以通过该数据库获取所有表的字段信息 1. COLLATIONS表 提供有关每个字符集的排序规则的信息. COLLATIONS表包含以下列: COLLATION_NAME 排序规则名称. CHARACTER_SET_NAME 与排序规则关联的字符集的名称. ID 排序规则ID. IS_DEFAULT 排序规则是否为其字符集的默认值. IS_COMPILED 字符集是否已编译到服务器中. SORTLEN 这与对字符集中表示的字符串进行排序所需的内存量有关. 2. SHOW COLLAT

Centos7+Mariadb+Keepalived实现Mariadb(MYSQL)的高可用(HA)

我们前面几篇文章中有介绍了Keepalived的功能--HA,关于MySQL-HA,目前有多种解决方案,比如heartbeat.drbd.mmm.共享存储,但是它们各有优缺点.heartbeat.drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对于不会脚本语言的人来说,这无疑是一种脑裂问题:对于mmm,生产环境中很少有人用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支:对于共享存储,个人觉得MySQL数据还是放在本

mysql mmm安装管理详解

机器简介: monitor 192.168.146.131 监控服务器 master-001 192.168.146.129 读写主机001 master-002 192.168.146.130 读写主机002 slave-001 192.168.146.131 只读主机001 虚拟IP分配 master-001 192.168.146.135 writer master-002 192.168.146.136 reader slave-001  192.168.146.137 reader 三

centos lvs+keepalived+mysql实现mysql数据库热备主主复制-亲测ok

实验环境: linux:centos6.6 虚拟ip(vip):192.168.135.199 mysql master:192.168.20.193 mysql slave:192.168.20.195 (这里只是为了后文方便区分,一个叫master,一个叫slave,其实在主主复制中,都是master也都是slave,没有主从之分) mysql的版本: [[email protected] ~]# mysql -V mysql  Ver 14.14 Distrib 5.1.73, for r