分类: Mysql

  • 做mysql数据库字典,查出表的表名,字段名,数据类型,注释,默认值

    单独查询某个表的注释、字段

    select
    TABLE_NAME AS ‘表名’,
    COLUMN_NAME AS ‘字段名’,
    COLUMN_TYPE AS ‘数据类型’,
    column_default as ‘默认值’,
    COLUMN_COMMENT AS ‘注释’
    from information_schema.columns
    where TABLE_SCHEMA=’数据库名’ and TABLE_NAME=’某个表名’

    查询整库

    SELECT
    a.TABLE_NAME AS 表名,
    a.TABLE_COMMENT AS 表注释,
    b.COLUMN_NAME AS 字段名,
    b.COLUMN_TYPE AS 数据类型,
    b.COLUMN_COMMENT AS 字段注释
    FROM
    INFORMATION_SCHEMA.TABLES AS a,
    INFORMATION_SCHEMA.COLUMNS AS b
    WHERE
    b.TABLE_NAME = a.TABLE_NAME
    AND a.TABLE_SCHEMA = ‘数据库名字’;

    可根据所需查询属性
    1、table_catalog          :不管是table | view 这个列的值总是def

    2、table_schema          :表 | 视图所在的数据库名

    3、table_name           :表名 | 视图名

    4、column_name          :列名

    5、column_default         :列的默认值

    6、is_nullable            :是否可以取空值

    7、data_type            :列的数据类型

    8、character_maximum_length   :列的最大长度(这列只有在数据类型为char | varchar 时才有意义)

    9、column_type           :列类型这个类型比data_type列所指定的更加详细,如data_type 是int 而column_type 就有可以能是int(11)

    10、column_key           :列上的索引类型 主键–>PRI | 唯一索引 –>UNI 一般索引 –>MUL
      
    11、column_comment :备注

  • 常用mysql语句(一)

    mysql查询数据库大小: select concat(round(sum(DATA_LENGTH/1024/1024),2),’MB’) as data  from information_schema.TABLES

  • 字符串函数 find_in_set(); mysql 多个字符串 找出其中一个,Mysql查找如何判断字段是否包含某个字符串

    SELECT  *  FROM wm_app_stats    where  email like '%skyzaizhe@126.com%'
    这种会查询出过多数据比如sskyzaizhe@126.com,前后多的字符依然查出来,
    SELECT  *  FROM wm_app_stats    where  email in ('skyzaizhe@126.com')
    只查询email只有'skyzaizhe@126.com'这个值,这种格式就会查询不出来,skyzaizhe@126.com,q25@1.com,
    只能用find_in_set函数
    SELECT  *  FROM wm_app_stats    where  find_in_set('skyzaizhe@126.com', email);     
    skyzaizhe@126.com
    skyzaizhe@126.com,q25@1.com
    这两种格式数据都可以查询出来
  • Mysql错误:Ignoring query to other database解决方法-缺少-uroot中的-u

    D:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 66
    Server version: 5.6.15 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show databases;
    Ignoring query to other database
    mysql> show tables;
    Ignoring query to other database
    
    

    折腾了半天才发现原来是在连接mysql时没有”-u”参数导致的
    D:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -uroot

    重新连接mysql 加上-u参数 一切正常

  • linux mysql查看数据库大小

    use information_schema;

    select concat(round(sum(data_length/1024/1024),2),’MB’) as data from tables where TABLE_SCHEMA=’数据库名’;

    得到的结果是以字节为单位,除1024为K,除1048576为M。

    netstat -na |grep mysql 看看有多少连接

    netstat -na |grep mysql |wc -l 查看mysql进程有多少个。