MySQL常用查询Databases和Tables

数据库 MySQL
本篇文章分享一下工作中常见的MySQL脚本,具体内容如下,我们一起来看。

分享一下工作中常见的mysql脚本,此次分享的内容如下:

  • Databases
  • tables

一、Databases and schemas

列出了 MySQL 实例上的用户数据库(模式):

select schema_name as database_name
from information_schema.schemata
where schema_name not in('mysql','information_schema',
'performance_schema','sys')
order by schema_name

说明:database_name - 数据库(模式)名称。

二、Tables

1. 列出 MySQL 数据库中的表

下面的查询列出了当前或提供的数据库中的表。要列出所有用户数据库中的表

(1) 当前数据库

select table_schema as database_name,
table_name
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema = database()
order by database_name, table_name;

说明:

  • table_schema - 数据库(模式)名称
  • table_name - 表名

(2) 指定数据库

select table_schema as database_name,
table_name
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema = 'database_name' -- enter your database name here
order by database_name, table_name;

说明:

  • table_schema - 数据库(模式)名称
  • table_name - 表名

2. 列出 MySQL 中所有数据库的表

下面的查询列出了所有用户数据库中的所有表:

select table_schema as database_name,
table_name
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema not in ('information_schema','mysql',
'performance_schema','sys')
order by database_name, table_name;

说明:

  • table_schema - 数据库(模式)名称
  • table_name - 表名

3. 列出 MySQL 数据库中的 MyISAM 表

select table_schema as database_name,
table_name
from information_schema.tables tab
where engine = 'MyISAM'
and table_type = 'BASE TABLE'
and table_schema not in ('information_schema', 'sys',
'performance_schema','mysql')
-- and table_schema = 'your database name'
order by table_schema,
table_name;

说明:

  • database_name - 数据库(模式)名称
  • table_name - 表名

4. 列出 MySQL 数据库中的 InnoDB 表

select table_schema as database_name,
table_name
from information_schema.tables tab
where engine = 'InnoDB'
and table_type = 'BASE TABLE'
and table_schema not in ('information_schema', 'sys',
'performance_schema','mysql')
-- and table_schema = 'your database name'
order by table_schema,
table_name;

说明:

  • database_name - 数据库(模式)名称
  • table_name - 表名

5. 识别 MySQL 数据库中的表存储引擎(模式)

select table_schema as database_name,
table_name,
engine
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema not in ('information_schema','mysql',
'performance_schema','sys')
-- and table_schema = 'your database name'
order by table_schema,
table_name;

说明:

(1)table_schema - 数据库(模式)名称

(2)table_name - 表名

(3)engine- 表存储引擎。可能的值:

  • CSV
  • InnoDB
  • 记忆
  • MyISAM
  • 档案
  • 黑洞
  • MRG_MyISAM
  • 联合的

6. 在 MySQL 数据库中查找最近创建的表

select table_schema as database_name,
table_name,
create_time
from information_schema.tables
where create_time > adddate(current_date,INTERVAL -60 DAY)
and table_schema not in('information_schema', 'mysql',
'performance_schema','sys')
and table_type ='BASE TABLE'
-- and table_schema = 'your database name'
order by create_time desc,
table_schema;

MySQL 数据库中最近 60 天内创建的所有表,按表的创建日期(降序)和数据库名称排序

说明:

  • database_name - 表所有者,模式名称
  • table_name - 表名
  • create_time - 表的创建日期

7. 在 MySQL 数据库中查找最近修改的表

select table_schema as database_name,
table_name,
update_time
from information_schema.tables tab
where update_time > (current_timestamp() - interval 30 day)
and table_type = 'BASE TABLE'
and table_schema not in ('information_schema', 'sys',
'performance_schema','mysql')
-- and table_schema = 'your database name'
order by update_time desc;

所有数据库(模式)中最近 30 天内最后修改的所有表,按更新时间降序排列

说明:

  • database_name - 数据库(模式)名称
  • table_name - 表名
  • update_time - 表的最后更新时间(UPDATE、INSERT 或 DELETE 操作或 MVCC 的 COMMIT)
责任编辑:赵宁宁 来源: 今日头条
相关推荐

2021-03-02 12:34:47

MySQL解锁表锁定表

2022-05-30 06:30:20

查询MySQL数据库

2018-03-29 19:45:47

数据库MySQL查询优化

2019-08-14 15:18:55

MySQLSQL数据库

2018-12-24 18:12:41

SQL ServerMySQL数据库

2010-11-25 10:21:20

MySql查询时间段

2018-10-12 16:45:10

MySQL查询日志数据库

2018-06-07 08:54:01

MySQL性能优化索引

2019-03-25 19:13:37

MySQL常用工具数据库

2021-04-09 23:00:12

SQL数据库Pandas

2009-08-05 10:08:55

MySQL查询优化调度锁定

2017-09-01 21:00:05

MySQLSQL优化查询方法

2010-11-25 13:05:26

MySQL列类型

2022-05-17 08:44:33

数据库MySQL

2009-06-16 10:36:00

Google Fusi应用实例

2021-07-05 09:24:06

MySQL SQL 语句数据库

2011-03-03 16:10:04

Mysql数据库备份还原

2017-09-18 15:20:02

MySQL慢查询日志配置

2010-05-27 16:12:10

MySQL索引

2013-05-17 10:43:32

点赞
收藏

51CTO技术栈公众号