MySQL常用查询Databases和Tables

发布时间:2025-05-11 13:21:23 作者:益华网络 来源:147小编 浏览量(3) 点赞(2)
摘要:分享一下工作中常见的mysql脚本,此次分享的内容如下:Databasestables 一、Databases and schemas 列出了 MySQL 实例上的用户数据库(模式): select schema_name as database_name from information_schema.schemata where schema_name not in(mysql,information_s

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

Databasestables

一、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- 表存储引擎。可能的值:

CSVInnoDB记忆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)

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!