mysql 查询数据库和数据表

推荐阅读: 使用SQL查询所有数据库名和表名

查询语句

  • 查询所有数据库
    show databases;
    
  • 查询指定数据库中所有表名
    select table_name from information_schema.tables 
    	where table_schema='database_name'
    
  • 查询指定表中的所有字段名
    select column_name from information_schema.columns 
    	where table_schema='database_name' and table_name='table_name';
    
  • 查询指定表中的所有字段名和字段类型
    select column_name,data_type from information_schema.columns 
    	where table_schema='database_name' and table_name='table_name';
    
  • 检查 mysql 数据库是否存在
    SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA 
    	where SCHEMA_NAME='database_name';CREATE DATABASE IF NOT EXISTS data_name;
    
  • 查询指定数据库下,是否存在某张表
    SELECT DISTINCT t.table_name, n.SCHEMA_NAME FROM information_schema.TABLES t, information_schema.SCHEMATA n 
    	WHERE t.table_name = 'tableName' AND n.SCHEMA_NAME = 'databaseName';
    

information_schema.tables字段说明

  • table_catalog: 数据表登记目录
  • table_schema: 数据表所属的数据库名
  • table_name: 表名称
  • table_type: 表类型[system view|base table]
  • engine: 使用的数据库引擎[MyISAM|CSV|InnoDB]
  • version: 版本,默认值10
  • row_format: 行格式[Compact|Dynamic|Fixed]
  • table_rows: 表里所存多少行数据
  • avg_row_length: 平均行长度
  • data_length: 数据长度
  • max_data_length: 最大数据长度
  • index_length: 索引长度
  • data_free: 空间碎片
  • auto_increment: 做自增主键的自动增量当前值
  • create_time: 表的创建时间
  • update_time: 表的更新时间
  • check_time: 表的检查时间
  • table_collation: 表的字符校验编码集
  • checksum: 校验和
  • create_options: 创建选项
  • table_comment: 表的注释、备注
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值