Mysql 查看获取表注释 或 字段注释

mac2024-06-02  44

目前常用的,获取 表、表中字段的注释的方式,主要是使用  information_schema 数据库,然后查看每个表的元数据;

例:

use information_schema; select * from TABLES where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名'  

 

 

mysql表与字段注释查看修改

1、 创建表的时候写注释

create table t_user( ID INT(19) primary key auto_increment comment '主键', NAME VARCHAR(300) comment '字段的注释', CREATE_TIME date comment '创建时间' )comment = '表的注释';

 

2、修改表的注释

alter table 表名 comment '修改后的表的注释';

 

3、修改字段的注释

alter table 表名 modify column 字段名 字段类型 comment '修改后的字段注释';

--注意:字段名和字段类型照写就行,如果之前有规定长度 这里也要指定一下

如:alter table t_cal_res_channel  MODIFY  column resume_channel varchar(30) COMMENT  '渠道'

 

4、 查看表注释的方法 --  在生成的SQL语句中看   查看表生成的DDL   注意:表名不加单引号

show create table 表名;

–  在元数据的表里面看

use information_schema; select * from TABLES where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名'

5、查看字段注释的方法 – show

show full columns from 表名;

– 在元数据的表里面看

select * from COLUMNS where TABLE_SCHEMA='数据库名' and TABLE_NAME='表名'

 

 

二.引申知识

1.使用alter table方法

ALTER TABLE table_name  MODIFY COLUMN column_name TINYINT(tinyint表示column类型) COMMENT '-1:默认值,1:人员id,2:公司id';   这种方法会重建表,如果数据量大,一个表100w数据,执行1min,如果分表数30个那就总共需要执行30min

 

2.使用元数据表

USE information_schema; UPDATE COLUMNS t  SET t.column_comment  = '-1:默认值,1:人员id,2:公司id'  WHERE t.TABLE_SCHEMA='database_name' AND t.table_name='table_name'  AND t.COLUMN_NAME='column_name ';   查询语句,更新前可以查询一下

SELECT t.column_comment FROM COLUMNS t WHERE t.TABLE_SCHEMA='database_name' AND t.TABLE_NAME='table_name' AND t.COLUMN_NAME='column_name ';   本想这种修改元数据的方法可以修改表的字段注释,但是没有权限执行,错误如下:

错误代码: 1044 Access denied for user 'usr'@'%' to database 'information_schema' 官方解释:

  INFORMATION_SCHEMA是信息数据库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息。在INFORMATION_SCHEMA中,有数个只读表。它们实际上是视图,而不是基本表,因此,你将无法看到与之相关的任何文件。

  每位MySQL用户均有权访问这些表,但仅限于表中的特定行,在这类行中含有用户具有恰当访问权限的对象。

  事实上,尽管不需要生成名为INFORMATION_SCHEMA的文件,我们仍提供了名为INFORMATION_SCHEMA的新数据库。可以使用USE语句将INFORMATION_SCHEMA选择为默认数据库,但访问该数据库中所含表的唯一方式是使用SELECT语句。不能在其中插入内容,不能更新它们,也不能删除其中的内容。

网摘:

  Mysql没有Oracle那么方便,修改注释的时候需要指定{type}(这是Mysql设计的不合理性!);系统数据字典表COLUMNS中有保存所有表的字段信息,但是系统字典表是只读的,无法修改注释;

  所以,Mysql修改注释必须按照上面的格式对不同类型做判断,并且带有长度的字段必须指明length与decimals,否则更新注释的时候会修改length,decimals为默认值的;

  修改Mysql注释是件麻烦的事情,但是也只能这样了。

 

3.难道就没有别的方法了?

  当对于一个大表进行ALTER TABLE的时候,性能问题就产生了。MySQL大部分改动的步骤如下:根据新的表结构创建一个空表,从旧表中把数据取出来插入到新表中,在删除旧表。这是个非常漫长的过程。许多人ALTER TABLE之后,都有等待1小时或者1天的痛苦经历。

  你可以这样,新建一个表,改变那个列的comment,然后原始实例关闭,替换.frm文件,再重启。

 

参考文章:

https://www.cnblogs.com/xsj1989/p/6795382.html

https://www.cnblogs.com/PatrickLiu/p/6235868.html  

 

 

 

 

 

 

 

 

 

应用:

python代码获取mysql字段名和注释

# coding=utf-8

import pymysql

  

def get_mysql_zi_duan():

conn = pymysql.connect(host='192.168.', port=3306, user='hs', passwd='xi', db='db_x', charset='utf8')

cursor01 = conn.cursor()

cursor01.execute(

"select column_name, column_comment from information_schema.columns where table_schema ='db_xingyun' and table_name = 'api_ind_guan_yi_s_d'")

all_info = cursor01.fetchall() # 数据库字段名和注释

# print(all_info)

zi_duan_ming = []

zhushi = []

for data in all_info:

zi_duan_ming.append(data[0])

zhushi.append(data[1])

# print(data[0])

print(str(zi_duan_ming).replace('[','').replace(']','').replace("'",''))

print(zhushi)

 

cursor01.close()

conn.close()

  

if __name__ == '__main__':

# 获取一个表的 所有字段名

get_mysql_zi_duan()

 

 

 

java代码获取mysql数据库表注释及字段注释

2018-01-04 16:00:42 技术无私 阅读数 2688更多

分类专栏: mysql

public static Connection getMySQLConnection() throws Exception {  

        Class.forName("com.mysql.jdbc.Driver");  

        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename", "root", "root");  

        return conn;  

}  

     

    /** 

    * 获取当前数据库下的所有表名称 

    * @return 

    * @throws Exception 

    */  

    public static List getAllTableName() throws Exception {  

        List tables = new ArrayList();  

        Connection conn = getMySQLConnection();  

        Statement stmt = (Statement) conn.createStatement();  

        ResultSet rs = stmt.executeQuery("SHOW TABLES ");  

        while (rs.next()) {  

            String tableName = rs.getString(1);  

            tables.add(tableName);  

        }  

        rs.close();  

        stmt.close();  

        conn.close();  

        return tables;  

    }  

     

 

    /** 

    * 获得某表的建表语句 

    * @param tableName 

    * @return 

    * @throws Exception 

    */  

    public static Map getCommentByTableName(List tableName) throws Exception {  

        Map map = new HashMap();  

        Connection conn = getMySQLConnection();  

        Statement stmt = (Statement) conn.createStatement();  

        for (int i = 0; i < tableName.size(); i++) {  

            String table = (String) tableName.get(i);  

            ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);  

            if (rs != null && rs.next()) {  

                String createDDL = rs.getString(2);  

                String comment = parse(createDDL);  

                map.put(table, comment);  

            }  

            rs.close();  

        }  

        stmt.close();  

        conn.close();  

        return map;  

    }  

    /** 

    * 获得某表中所有字段的注释 

    * @param tableName 

    * @return 

    * @throws Exception 

    */  

    public static void getColumnCommentByTableName(List tableName) throws Exception {  

        Map map = new HashMap();  

        Connection conn = getMySQLConnection();  

        Statement stmt = (Statement) conn.createStatement();  

        for (int i = 0; i < tableName.size(); i++) {  

            String table = (String) tableName.get(i);  

            ResultSet rs = stmt.executeQuery("show full columns from " + table);  

            System.out.println("【"+table+"】");  

            while (rs.next()) {      

                System.out.println(rs.getString("Field") + "\t:\t"+  rs.getString("Comment") );  

            }

            rs.close();  

        }  

        stmt.close();  

        conn.close();  

    }  

 

     

 

    /** 

    * 返回注释信息 

    * @param all 

    * @return 

    */  

     

    public static String parse(String all) {  

        String comment = null;  

        int index = all.indexOf("COMMENT='");  

        if (index < 0) {  

            return "";  

        }  

        comment = all.substring(index + 9);  

        comment = comment.substring(0, comment.length() - 1);  

        return comment;  

    }  

 

    public static void main(String[] args) throws Exception {  

        List tables = getAllTableName();  

        Map tablesComment = getCommentByTableName(tables);  

        Set names = tablesComment.keySet();  

        Iterator iter = names.iterator();  

        while (iter.hasNext()) {  

            String name = (String) iter.next();  

            System.out.println("Table Name: " + name + ", Comment: " + tablesComment.get(name));  

        }  

         

        getColumnCommentByTableName(tables);  

    }

 

 

 

 

 

 

 

 

 

 

最新回复(0)