获取MySQL 5.5表元信息

来源:这里教程网 时间:2026-02-27 12:22:29 作者:

最近,在写一个代码工具,从数据读取表生成实体Bean、SQLMap,需要获取到数据库的元信息,为了适应不同的数据库,需要针对每一种

select t.column_name,
             t.data_type,
             cast(substr(t.column_type, instr(t.column_type, '(') + 1, instr(t.column_type,')') - instr(t.column_type, '(') - 1) as char(20)) data_length,
             cast(t.column_type as char(20)) column_type,
             t.column_comment,
             if (t.is_nullable='yes',1,0) is_nullable,
             if (t.column_key = 'pri', 1, 0) is_key
from information_schema.columns t
where t.table_schema = schema() and
            t.table_name = '表名'
order by t.ordinal_position;

相关推荐