navicat mysql查資料庫中表名、表數量,欄位名、欄位數量(持續更新中)
1.查資料庫中表數量
(紅色標記的是常用到的重要的表結構資訊表)
mysql> use information_schema;
Database changed
mysql> show tables;
+---------------------------------------+
| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| COLLATIONS |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS |
| COLUMN_PRIVILEGES |
| ENGINES |
| EVENTS |
| FILES |
| GLOBAL_STATUS |
| GLOBAL_VARIABLES |
| KEY_COLUMN_USAGE |
| OPTIMIZER_TRACE |
| PARAMETERS |
| PARTITIONS |
| PLUGINS |
| PROCESSLIST |
| PROFILING |
| REFERENTIAL_CONSTRAINTS |
| ROUTINES |
|
| SCHEMA_PRIVILEGES |
| SESSION_STATUS |
| SESSION_VARIABLES |
| STATISTICS |
| TABLES |
| TABLESPACES |
| TABLE_CONSTRAINTS |
| TABLE_PRIVILEGES |
| TRIGGERS |
| USER_PRIVILEGES |
| VIEWS |
| INNODB_LOCKS |
| INNODB_TRX |
| INNODB_SYS_DATAFILES |
| INNODB_LOCK_WAITS |
| INNODB_SYS_TABLESTATS |
| INNODB_CMP |
| INNODB_METRICS |
| INNODB_CMP_RESET |
| INNODB_CMP_PER_INDEX |
| INNODB_CMPMEM_RESET |
| INNODB_FT_DELETED |
| INNODB_BUFFER_PAGE_LRU |
| INNODB_SYS_FOREIGN |
| INNODB_SYS_COLUMNS |
| INNODB_SYS_INDEXES |
| INNODB_FT_DEFAULT_STOPWORD |
| INNODB_SYS_FIELDS |
| INNODB_CMP_PER_INDEX_RESET |
| INNODB_BUFFER_PAGE |
| INNODB_CMPMEM |
| INNODB_FT_INDEX_TABLE |
| INNODB_FT_BEING_DELETED |
| INNODB_SYS_TABLESPACES |
| INNODB_FT_INDEX_CACHE |
| INNODB_SYS_FOREIGN_COLS |
| INNODB_SYS_TABLES |
| INNODB_BUFFER_POOL_STATS |
| INNODB_FT_CONFIG |
+---------------------------------------+
59 rows in set
(紅色標記的表COLUMNS、SCHEMATA 、TABLES 中COLUMNS 和TABLES 表的列太多不展示,看看SCHEMATA 表 )
mysql> select * from schemata;
+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def | information_schema | utf8 | utf8_general_ci | NULL |
| def | ceshi | utf8 | utf8_general_ci | NULL |
| def | cms | utf8 | utf8_general_ci | NULL |
| def | mysql | utf8 | utf8_general_ci | NULL |
| def | performance_schema | utf8 | utf8_general_ci | NULL |
+--------------+--------------------+----------------------------+------------------------+----------+
7 rows in set
(上表中我本地MySQL裡的自己建的表出現了)。
查資料庫中表數量:
mysql> select table_schema,count(*) from tables group by table_schema;
+--------------------+----------+
| table_schema | count(*) |
+--------------------+----------+
| ceshi | 6 |
| cms | 227 |
| information_schema | 59 |
| mysql | 29 |
| performance_schema | 52 |
+--------------------+----------+
7 rows in set
2.查資料庫中所有表的資訊
在navicat中新建查詢,通過TABLES表來查,執行
select * from information_schema.tables where TABLE_SCHEMA='資料庫名';
可以檢視每張表對應的結構資訊。
進一步,
3.查資料庫中所有表名
在navicat中,
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='資料庫名';
4.查詢(指定)資料庫中哪些表有某個指定的欄位
如:查哪些表有play_id欄位
SELECT * FROM information_schema.columns WHERE column_name='player_id' -- AND TABLE_SCHEMA='資料庫名';;
5.查詢欄位含有指定字串的表:
如:查欄位有含有"country"字串(部分字串)的表
SELECT * FROM information_schema.columns WHERE LOCATE("country",column_name)>0;
6.查庫裡的所有的空表
SELECT table_schema,table_name,table_rows FROM information_schema.TABLES WHERE TABLE_SCHEMA='資料庫名' AND table_rows = 0 ORDER BY table_name ;
7.檢視指定資料庫中最近被更新的表
select TABLE_NAME,UPDATE_TIME from information_schema.TABLES where TABLE_SCHEMA='資料庫名' ORDER BY UPDATE_TIME desc -- limit 1;
8.檢視指定表最近被更新的時間
select TABLE_NAME,UPDATE_TIME from information_schema.TABLES where TABLE_SCHEMA='資料庫名' and information_schema.TABLES.TABLE_NAME = '表名';
9.檢視指定資料庫中最近新建的表:
select TABLE_NAME,CREATE_TIME from information_schema.TABLES where TABLE_SCHEMA='資料庫名' ORDER BY CREATE_TIME desc
10.檢視MySQL資料庫大小
SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH) FROM information_schema.TABLES where TABLE_SCHEMA='資料庫名';
得到的結果是以位元組為單位,除1024為K,除1048576(=1024*1024)為M。
11.檢視資料庫表基本資訊。
select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA = '資料庫名' and information_schema.TABLES.TABLE_NAME = '表名';
12.查某欄位重複的資料條數
select 欄位,count(*) as count from 表名 group by 欄位 having count>1;
13.查有資料庫中所有表字段為text型別(指定型別)的表及欄位:
SELECT TABLE_NAME,column_name FROM information_schema.columns WHERE table_schema='newmedia' AND data_type="text";
即查資料庫newmedia中所有表字段是text型別的表及其欄位
14.查詢表字段名、註釋、欄位型別
select column_name,column_comment,data_type from information_schema.columns where table_name='你的表名'
查詢dababase下所有表名及表註釋:
SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='database名';