赞
踩
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 |
| SCHEMATA |
| 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;
例:查表t_ticket中字段含有"nonOrder"字符串(可以是部分字符串)的数据行
select id,ticket_number ,content from t_ticket where LOCATE("nonOrder",content)>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名';
15.查MySQL数据库最大连接数
show variables like '%max_connections%';
16.查MySQL数据库当前连接数
show global status like 'Max_used_connections';
17.查看mysql自增主键id的自增起始量(偏移量):
show create table 表名;
即下次插入一条数据的自增id=122 。
此处的AUTO_INCREMENT就是偏移量或者自增指针,指向下一个将要但还没有插入的数据行。
18. 设置自增偏移量
alter table 表名 auto_increment = 60;
19.查看并设置自增步长
不能设置单表的自增步长,即mysql所有表的自增步长一定是一样的。
mysql当中的 auto_increment_increment 和 auto_increment_offset 都是设置之后,有global-全局影响。
查看步长:
mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
auto_increment_increment这是自增长的步长。
auto_increment_offset这是自增长开始的值。
设置步长:
SET @@auto_increment_increment=3; -- 将自增长步长设置为3
SET @@auto_increment_offset=10; -- 将自增长开始值设置为10
也可在/etc/my.cnf文件中设置:
auto_increment_increment=1;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。