MySQL常用查詢Databases和Tables
分享一下工作中常見的mysql腳本,此次分享的內(nèi)容如下:
- Databases
- tables
一、Databases and schemas
列出了 MySQL 實例上的用戶數(shù)據(jù)庫(模式):
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 - 數(shù)據(jù)庫(模式)名稱。
二、Tables
1. 列出 MySQL 數(shù)據(jù)庫中的表
下面的查詢列出了當(dāng)前或提供的數(shù)據(jù)庫中的表。要列出所有用戶數(shù)據(jù)庫中的表
(1) 當(dāng)前數(shù)據(jù)庫
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 - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
(2) 指定數(shù)據(jù)庫
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 - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
2. 列出 MySQL 中所有數(shù)據(jù)庫的表
下面的查詢列出了所有用戶數(shù)據(jù)庫中的所有表:
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 - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
3. 列出 MySQL 數(shù)據(jù)庫中的 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 - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
4. 列出 MySQL 數(shù)據(jù)庫中的 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 - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
5. 識別 MySQL 數(shù)據(jù)庫中的表存儲引擎(模式)
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 - 數(shù)據(jù)庫(模式)名稱
(2)table_name - 表名
(3)engine- 表存儲引擎??赡艿闹担?/p>
- CSV
- InnoDB
- 記憶
- MyISAM
- 檔案
- 黑洞
- MRG_MyISAM
- 聯(lián)合的
6. 在 MySQL 數(shù)據(jù)庫中查找最近創(chuàng)建的表
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 數(shù)據(jù)庫中最近 60 天內(nèi)創(chuàng)建的所有表,按表的創(chuàng)建日期(降序)和數(shù)據(jù)庫名稱排序
說明:
- database_name - 表所有者,模式名稱
- table_name - 表名
- create_time - 表的創(chuàng)建日期
7. 在 MySQL 數(shù)據(jù)庫中查找最近修改的表
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;
所有數(shù)據(jù)庫(模式)中最近 30 天內(nèi)最后修改的所有表,按更新時間降序排列
說明:
- database_name - 數(shù)據(jù)庫(模式)名稱
- table_name - 表名
- update_time - 表的最后更新時間(UPDATE、INSERT 或 DELETE 操作或 MVCC 的 COMMIT)