在工作中共享常见的mysql脚本。本次分享的内容如下:Databasestables1.Databasesandschemas列出MySQL实例上的用户数据库(schemas):selectschema_nameasdatabase_namefrominformation_schema。schemawhereschema_namenotin('mysql','information_schema','performance_schema','sys')orderbyschema_name描述:database_name-数据库(模式)名称。二、表一。列出MySQL数据库中的表下面的查询列出了当前或提供的数据库中的表。列出所有用户数据库中的表(1)当前数据库selecttable_schemaasdatabase_name,table_namefrominformation_schema.tableswheretable_type='BASETABLE'andtable_schema=database()orderbydatabase_name,table_name;description:table_schema-database(schema)nametable_name-tablename(2)指定数据库selecttable_schemaasdatabase_name,table_namefrominformation_schema.tableswheretable_type='BASETABLE'andtable_schema='database_name'--enteryourdatabasenamehereorderbydatabase_name,表名;说明:table_schema-数据库(模式)名table_name-表名2.列出MySQL中所有数据库中的表以下查询列出所有用户数据库中的所有表:selecttable_schemaasdatabase_name,table_namefrominformation_schema.tableswheretable_type='BASETABLE'andtable_schemanotin('information_schema','mysql','performance_schema','sys')orderbydatabase_name,table_name;说明:table_schema-数据库(模式)名table_name-表名3.列出MySQL数据库中的MyISAM表selecttable_schemaasdatabase_name,table_namefrominformation_schema.tablestabwhereengine='MyISAM'andtable_type='BASETABLE'andtable_schemanotin('information_schema','sys','performance_schema','mysql')--andtable_schema='yourdatabasename'按表模式排序,表名;说明:database_name-数据库(schema)名称table_name-表名4.列出MySQL数据库中的InnoDB表selecttable_schemaasdatabase_name,table_namefrominformation_schema.tablestabwhereengine='InnoDB'andtable_type='BASETABLE'andtable_schemanotin('information_schema','sys','performance_schema','mysql')--andtable_schema='yourdatabasename'orderbytable_schema,table_name;说明:database_name-数据库(模式)名table_name-表名5.识别MySQL数据库中的表存储引擎(模式)selecttable_schemaasdatabase_name,table_name,enginefrominformation_schema.tableswheretable_type='BASETABLE'andtable_schemanotin('信息模式','mysql','performance_schema','sys')--andtable_schema='yourdatabasename'orderbytable_schema,table_name;说明:(1)table_schema——数据库(schema)名(2)table_name——表名(3)engine——表存储引擎可能的值:CSVInnoDB内存MyISAM存档黑洞MRG_MyISAM联合6.在MySQL数据库中查找最近创建的表selecttable_schemaasdatabase_name,table_name,create_timefrominformation_schema.tableswherecreate_time>adddate(current_date,INTERVAL-60DAY)andtable_schemanotin('information_schema','mysql','performance_schema','sys')andtable_type='BASETABLE'--andtable_schema='yourdatabasename'orderbycreate_timedesc,table_schema;最近60天内在MySQL数据库中创建的所有表,按表创建日期(降序)和数据库名称排序说明:database_name-表所有者,模式名称table_name-表名称create_time-表创建日期7.查找最近修改的MySQL数据库中的表selecttable_schemaasdatabase_name,table_name,update_timefrominformation_schema.tablestabwhereupdate_time>(current_timestamp()-interval30day)andtable_type='BASETABLE'andtable_schemanotin('information_schema','sys','performance_schema','mysql')--和table_schema='你的数据库名称'orderbyupdate_timedesc;所有数据库(模式)中最近30天最后修改的所有表,按更新时间降序排列说明:database_name-数据库(模式)名称table_name-表名update_time-表的最后更新时间(UPDATE,INSERTorDELETE操作,或MVCC的COMMIT)
