通过完整规范的列注释快速生成中文列名的SQL语句及数据字典。利用列注释可以减少表关联映射及联表查询,充分利用系统资源,简洁、高效。而其基础仅是一个列的注释规范。

一、用系统表元数据快速生成中文列名的SQL语句。

​#1.建表
drop   table if exists t_01;
create table           t_01
(id int auto_increment comment 'ID',part_id tinyint       comment '部门ID 0:总经办;1:办公室;2:设计部;3:生产部;4:仓储部;5:财务部;6:后勤部;7:采购部;8:销售部;9:保安部;',name varchar(10)      comment '姓名',sex tinyint           comment '性别 0:女;1:男;2:不详;',dt_birth date         comment '生日',post int              comment  '职位代码 0:总经理;1:副总经理;2:经理;3:副经理;4:处长;4:副处长;5:科长;6:副科长;',salary nemberic(12,2) comment '薪资',bonus  nemberic(12,2) comment '奖金',primary key(id)
) comment '员工信息表';
#2.清空表
truncate table t_01;
#3.插入测试数据
insert into t_01(id,part_id,name,sex,dt_birth,post,salary,bonus) values
(1,0,'name_01',0,'2000-01-01',0,20001.00,2001.00),
(2,1,'name_02',1,'2000-01-02',0,20002.00,2002.00),
(3,0,'name_03',2,'2000-01-03',0,20003.00,2003.00),
(4,1,'name_04',3,'2000-01-04',0,20004.00,2004.00),
(5,0,'name_05',4,'2000-01-05',0,20005.00,2005.00),
(6,0,'name_06',5,'2000-01-06',0,20006.00,2006.00),
(7,0,'name_07',5,'2000-01-07',0,20007.00,2007.00),
(8,0,'name_08',5,'2000-01-08',0,20008.00,2008.00),
(9,0,'name_09',5,'2000-01-09',0,20009.00,2009.00),
(10,0,'name_10',5,'2000-01-10',0,20010.00,2010.00),
(11,0,'name_11',5,'2000-01-11',0,20011.00,2011.00),
(12,0,'name_12',5,'2000-01-12',0,20012.00,2012.00);
#4.查询
select * from t_01;

此时列名全是英文的。如果想把列名变成见名知意的中文,可以用中文列名做别名:

select id          '员工ID',part_id     '部门',name        '姓名',sex         '性别', dt_birth    '生日',post        '职位', education   '学历',salary      '薪资',bonus       '奖金'
from t_01;

每行中的数字所代表的意思还是不知其中文意思,要弄明白每列数字所表达的中文意思,必须用if 或case :

select id '员工ID',(case part_id when 0 then '总经办' when 1 then '办公室' when 2 then '设计部' when 3 then '生产部' when 4 then '仓储部' when 5 then '财务部' when 6 then '后勤部' when 7 then '采购部' when 8 then '销售部' when 9 then '保安部'else null end ) '部门',`name` '姓名',if(sex=0 ,'女', if(sex=1 ,'男',if(sex=2 ,'不详',''))) '性别', dt_birth '生日',(case post when 0  then '总经理' when 1 then '副总经理' when 2 then '经理' when 3 then '副经理' when 4 then '处长' when 4 then '副处长' when 5 then '科长' when 6 then '副科长' else null end) '职位', (case education when 0 then '博士' when 1 then '硕士' when 2 then '本科' when 3 then '专科' when 4 then '高中' when 5 then '初中' when 6 then '小学' else null end)'学历',salary '薪资',bonus '奖金'
from t_01;

这样列名和表中的数字都变成了见名知意的中文了。但又把列名和表中数字给淹没了,不知表中原来的列名和数字。
将字段名和中文一起描述,将表中的数字和代表的中文意思一起描述:

select id 'id:员工ID',(case part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' else null end ) 'part_id:部门',`name` 'name:姓名',if(sex=0 ,'0:女', if(sex=1 ,'1:男',if(sex=2 ,'2:不详',''))) 'sex:性别', dt_birth 'dt_birth:生日',(case post when 0 then '0:总经理' when 1 then '1:副总经理'when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '5:副处长' when 5 then '6:科长' when 6 then '7:副科长' else null end) 'post:职位', (case education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' else null end)'education:学历',    salary 'salary:薪资',bonus 'bonus:奖金'
from t_01;

这样手工输入这些代码很繁琐,利用系统表information_schema.columns的列注释字段column_comment可以高效构造出所需的SQL语句,实现灵活的中文列和表中数字中文意思的查询。
关键:定义列注释的规范要求:中文列名 数字1:中文意思1;数字2:中文意思2;数字3:中文意思3;...数字n:中文意思n;

#创建构造SQL语句的存储过程
drop    procedure if exists `p_01`;
create  procedure           `p_01`(p_table_schema varchar(40),p_table_name varchar(40),p_type int,p_chinese int,p_alias varchar(4))
comment
'获取表的列名的中文名及列属性的中文含义 (英文列名+中文列名)长度<=64p_table_schema 数据库名p_table_name 表名p_type SQL语句样式类型 0:横式;1;纵式p_chinese 是否生成中文列名 0:仅英文列名;1:仅中文列名;2:英文列名+中文列名; 当仅中文列名时,若column_comment中无中文注释时,则用column_namep_alias 表的别名p_separator 列名与中文列名之间、数字与中文意思之间的分隔符(未用)
'
beginif p_type<0 or p_type>1 then select concat('SQL语句样式类型 0:横式;1;纵式。') message;
end if;if p_chinese<0 or p_chinese>2 then select concat('是否生成中文列名 0:仅英文列名;1:仅中文列名;2:英文列名+中文列名') message;end if;
if length(p_alias)<=0 or length(p_alias)>4 thenselect concat('表别名最大长度为4个字符') message;
end if;#删除所有的临时表drop temporary table if exists temp_01;drop temporary table if exists temp_02;drop temporary table if exists temp_03;drop temporary table if exists temp_04;drop temporary table if exists temp_07;drop temporary table if exists temp_06;drop temporary table if exists temp_05;create temporary table temp_01(table_schema varchar(100),table_name varchar(100),column_name varchar(100),title longtext);if p_chinese=0 then #仅英文列名insert into temp_01select table_schema,table_name,column_name,concat(p_alias,'.',column_name,' ','`',concat(p_alias,'.',column_name),'`') title from information_schema.columns where table_schema=p_table_schema and table_name=p_table_name  ;end if;if p_chinese=1 then #仅中文列名insert into temp_01select table_schema,table_name,column_name,concat(p_alias,'.',column_name,' ','`',concat(p_alias,'.',trim(SUBSTRING_INDEX(if(length(column_comment)<1,column_name,column_comment),' ',1))),'`') title from information_schema.columns where table_schema=p_table_schema and table_name=p_table_name  ;end if;if p_chinese=2 then #英文列名 +中文列名insert into temp_01select table_schema,table_name,column_name,concat(p_alias,'.',column_name,' ','`',concat(p_alias,'.',column_name,':',trim(SUBSTRING_INDEX(column_comment,' ',1))),'`') title from information_schema.columns where table_schema=p_table_schema and table_name=p_table_name  ;end if;-- select concat('select ',left(@col_name,char_length(@col_name)-0),' from `',p_table_schema,'`.`',p_table_name,'`;') select_sql;-- 1.获取空格后的字符串create temporary table temp_02select table_schema,table_name,column_name,substring_index(column_comment,' ',1) column_comment,mid(column_comment,locate(' ',column_comment)+1) col7 from information_schema.columns where table_schema=p_table_schema and table_name=p_table_name and column_comment like '%;%';-- 2.按分号分隔后再按冒号分隔后转为列(横转纵)写入表中-- (1) 按分号分隔set @s=';';create temporary table temp_03select table_schema,table_name,column_name,column_comment,trim(substring_index(substring_index(a.`col7`, @s, b.help_topic_id + 1 ), @s,- 1 )) `col7`             from temp_02 ajoin mysql.help_topic b on b.help_topic_id < ( length( a.col7 ) - length( replace ( a.col7, @s, '' ) ) + 1 ) ;delete from temp_03 where col7=''; -- 删除多余的空数据-- (2) 按冒号分隔create temporary table temp_04select table_schema,table_name,column_name,column_comment,left(col7,locate(':',col7)-1 ) sn,mid(col7,locate(':',col7)+1) zh from temp_03;-- 3:获取列属性的中文含义if p_chinese=0 then #仅英文列名create temporary table temp_05select table_schema,column_name,concat("(case ",p_alias,'.',column_name," ",group_concat(concat("when ",sn," then '",sn,":",zh,"'") separator " ")," end)"," `",p_alias,'.',column_name,"`") `whenthen` from temp_04 group by table_schema,column_name,column_comment ;end if;if p_chinese=1 then #仅中文列名create temporary table temp_05select table_schema,column_name,concat("(case ",p_alias,'.',column_name," ",group_concat(concat("when ",sn," then '",sn,":",zh,"'") separator " ")," end)"," `",p_alias,'.',if(length(column_comment)<1,column_name,column_comment),"`") `whenthen` from temp_04 group by table_schema,column_name,column_comment ;end if;    if p_chinese=2 then #英文 +中文列名create temporary table temp_05select table_schema,column_name,concat("(case ",p_alias,'.',column_name," ",group_concat(concat("when ",sn," then '",sn,":",zh,"'") separator " ")," end)"," `",p_alias,'.',column_name,":",column_comment,"`") `whenthen` from temp_04 group by table_schema,column_name,column_comment ;end if;update temp_01 a,temp_05 b set a.title = b.whenthen  where  a.column_name =b.column_name and a.table_schema=b.table_schema;-- select * from temp_01 ;-- 纵转横-- 最终生成中文列名及中文属性值的 sql(横向)if p_type=0 then select concat('select ',group_concat(title separator ','),' from ',table_schema,'.',table_name, ' ',p_alias,' where 1=1 limit 10;') `sql` from temp_01 group by table_name;end if;if p_type=1 then -- 最终生成中文列名及中文属性值的 sql(纵向)create temporary table temp_06(col longtext);insert into temp_06select concat('select\n',concat('\t',group_concat(concat('\t',title) separator ',')),' \nfrom\n\t ',table_schema,'.',table_name,' ',p_alias,' \nwhere\n\t 1=1 limit 10;') `col`  from temp_01 group by table_name;set @s=',';create temporary table temp_07select concat(' ',trim(substring_index(substring_index(col, @s, b.help_topic_id + 1 ), @s,-1 )),',') `cmd` from temp_06 ajoin mysql.help_topic b on b.help_topic_id < ( length( a.col ) - length( replace ( a.col, @s, '' ) ) + 1 );-- 将最后一行的;,替换为;update temp_07 set cmd=replace(cmd,';,',';');-- 查询构造的SQL语句select cmd from temp_07;end if;
end;

select * from t_01;

#执行存储过程将生成SQL语句。

set @db='db_01',@tab='t_01',@type=0,@chinese=0,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);
set @db='db_01',@tab='t_01',@type=0,@chinese=1,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);
set @db='db_01',@tab='t_01',@type=0,@chinese=2,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);
set @db='db_01',@tab='t_01',@type=1,@chinese=0,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);
set @db='db_01',@tab='t_01',@type=1,@chinese=1,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);
set @db='db_01',@tab='t_01',@type=1,@chinese=2,@alias='t1';call `db_01`.`p_01`(@db ,@tab,@type,@chinese,@alias);

#生成的SQL语句:

select t1.id `t1.id`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.part_id`,t1.name `t1.name`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.sex`,t1.dt_birth `t1.dt_birth`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.education`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.post`,t1.salary `t1.salary`,t1.bonus `t1.bonus` from db_01.t_01 t1 where 1=1 limit 10;select t1.id `t1.ID`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.部门ID`,t1.name `t1.姓名`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.性别`,t1.dt_birth `t1.生日`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.学历`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.职位代码`,t1.salary `t1.薪资`,t1.bonus `t1.奖金` from db_01.t_01 t1 where 1=1 limit 10;select t1.id `t1.id:ID`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.part_id:部门ID`,t1.name `t1.name:姓名`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.sex:性别`,t1.dt_birth `t1.dt_birth:生日`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.education:学历`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.post:职位代码`,t1.salary `t1.salary:薪资`,t1.bonus `t1.bonus:奖金` from db_01.t_01 t1 where 1=1 limit 10;selectt1.id `t1.id`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.part_id`,t1.name `t1.name`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.sex`,t1.dt_birth `t1.dt_birth`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.education`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.post`,t1.salary `t1.salary`,t1.bonus `t1.bonus`
fromdb_01.t_01 t1
where1=1 limit 10;selectt1.id `t1.ID`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.部门ID`,t1.name `t1.姓名`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.性别`,t1.dt_birth `t1.生日`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.学历`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.职位代码`,t1.salary `t1.薪资`,t1.bonus `t1.奖金`
fromdb_01.t_01 t1
where1=1 limit 10;selectt1.id `t1.id:ID`,(case t1.part_id when 0 then '0:总经办' when 1 then '1:办公室' when 2 then '2:设计部' when 3 then '3:生产部' when 4 then '4:仓储部' when 5 then '5:财务部' when 6 then '6:后勤部' when 7 then '7:采购部' when 8 then '8:销售部' when 9 then '9:保安部' end) `t1.part_id:部门ID`,t1.name `t1.name:姓名`,(case t1.sex when 0 then '0:女' when 1 then '1:男' when 2 then '2:不详' end) `t1.sex:性别`,t1.dt_birth `t1.dt_birth:生日`,(case t1.education when 0 then '0:博士' when 1 then '1:硕士' when 2 then '2:本科' when 3 then '3:专科' when 4 then '4:高中' when 5 then '5:初中' when 6 then '6:小学' end) `t1.education:学历`,(case t1.post when 0 then '0:总经理' when 1 then '1:副总经理' when 2 then '2:经理' when 3 then '3:副经理' when 4 then '4:处长' when 4 then '4:副处长' when 5 then '5:科长' when 6 then '6:副科长' end) `t1.post:职位代码`,t1.salary `t1.salary:薪资`,t1.bonus `t1.bonus:奖金`
fromdb_01.t_01 t1
where1=1 limit 10;

#执行生成的SQL语句,可得到预期的结果样式(结果一样,只是列头样式不一样)。
英文列名 

中文列名

英文+中文列名

二、用系统表元数据快速生成数据字典。

#生成数据字典
drop   procedure if exists p_02;
create procedure           p_02(p_table_schema varchar(40),p_table_name varchar(40),p_chinese tinyint)
comment
'生成数据字典p_table_schema 数据库名。为空或长度为0则为所有用户数据库;否则为指定的数据库。p_table_name 表名。为空或长库为0则为所有表。t_chinese 是否生成中文列名 0:仅英文列名;1:仅中文列名;2:英文列名+中文列名;9:显示存储过程用法帮助;
'
begindeclare v_ret int default 0;if p_chinese=9 then select '生成数据字典p_table_schema 数据库名。为空或长度为0则为所有用户数据库;否则为指定的数据库。p_table_name 表名。为空或长库为0则为所有表。t_chinese 是否生成中文列名 0:仅英文列名;1:仅中文列名;2:英文列名+中文列名;' messagefrom dual;end if;#所有数据库if p_table_schema is null or length(p_table_schema)=0 then#所有表if p_table_name is null or length(p_table_name)=0 then#0:仅英文列名if p_chinese =0 then select table_schema ,table_name ,column_name ,column_default ,is_nullable ,data_type ,column_type,column_key ,extra ,column_comment from information_schema.columnsorder by table_schema,table_name;end if;#1:仅中文列名if p_chinese=1 thenselect table_schema `数据库名`,table_name `表名`,column_name `列名`,column_default `缺省值`,is_nullable `是否允许为空`,data_type `数据类型`,column_type`列类型`,column_key`主键`,extra `特别`,column_comment `列注释` from information_schema.columnsorder by table_schema,table_name;end if;#英文列名+中文列名if p_chinese=2 thenselect table_schema `table_schema:数据库名`,table_name `table_name:表名`,column_name `column_name:列名`,column_default `column_default:缺省值`,is_nullable `is_nullable:是否允许为空`,data_type `data_type:数据类型`,column_type`column_type:列类型`,column_key`column_key:主键`,extra `extra:特别`,column_comment `column_comment:列注释` from information_schema.columnsorder by table_schema,table_name;end if; #指定表elseselect count(*) into v_ret from information_schema.tables where table_name=p_table_name;if v_ret <=0 thenselect concat('无此表名: ',p_table_name,' !') message;else#0:仅英文列名if p_chinese=0 thenselect table_schema ,table_name ,column_name ,column_default ,is_nullable ,data_type ,column_type,column_key ,extra ,column_comment  from information_schema.columns where table_name=p_table_nameorder by table_schema,table_name;end if;#1:仅中文列名if p_chinese=1 thenselect table_schema `数据库名`,table_name `表名`,column_name `列名`,column_default `缺省值`,is_nullable `是否允许为空`,data_type `数据类型`,column_type`列类型`,column_key`主键`,extra `特别`,column_comment `列注释`  from information_schema.columns where table_name=p_table_nameorder by table_schema,table_name;end if;#英文列名+中文列名if p_chinese=2 thenselect table_schema `table_schema:数据库名`,table_name `table_name:表名`,column_name `column_name:列名`,column_default `column_default:缺省值`,is_nullable `is_nullable:是否允许为空`,data_type `data_type:数据类型`,column_type`column_type:列类型`,column_key`column_key:主键`,extra `extra:特别`,column_comment `column_comment:列注释` from information_schema.columns where table_name=p_table_nameorder by table_schema,table_name;end if;end if;end if;#指定数据库elseselect count(*) into v_ret from information_schema.tables where table_schema=p_table_schema;if v_ret<=0 then select concat('无此数据库名: ',p_table_schema,' !') message;else#所有表if p_table_name is null or length(p_table_name)=0 then#0:仅英文列名if p_chinese=0 thenselect table_schema ,table_name ,column_name ,column_default ,is_nullable ,data_type ,column_type,column_key ,extra ,column_comment from information_schema.columns where lower(table_schema)=p_table_schemaorder by table_schema,table_name;end if;#1:仅中文列名if p_chinese=1 thenselect table_schema `数据库名`,table_name `表名`,column_name `列名`,column_default `缺省值`,is_nullable `是否允许为空`,data_type `数据类型`,column_type`列类型`,column_key`主键`,extra `特别`,column_comment `列注释`  from information_schema.columns where lower(table_schema)=p_table_schemaorder by table_schema,table_name;end if;#英文列名+中文列名if p_chinese=2 thenselect table_schema `table_schema:数据库名`,table_name `table_name:表名`,column_name `column_name:列名`,column_default `column_default:缺省值`,is_nullable `is_nullable:是否允许为空`,data_type `data_type:数据类型`,column_type`column_type:列类型`,column_key`column_key:主键`,extra `extra:特别`,column_comment `column_comment:列注释` from information_schema.columns where lower(table_schema)=p_table_schemaorder by table_schema,table_name;end if;#指定表elseselect count(*) into v_ret from information_schema.tables where table_schema=p_table_schema and table_name=p_table_name;if v_ret <=0 thenselect concat('无此表名: ',p_table_name,' !') message;else#0:仅英文列名if p_chinese=0 thenselect table_schema ,table_name ,column_name ,column_default ,is_nullable ,data_type ,column_type,column_key ,extra ,column_comment  from information_schema.columns where lower(table_schema)=p_table_schema and table_name=p_table_nameorder by table_schema,table_name;end if;#1:仅中文列名if p_chinese=1 thenselect table_schema `数据库名`,table_name `表名`,column_name `列名`,column_default `缺省值`,is_nullable `是否允许为空`,data_type `数据类型`,column_type`列类型`,column_key`主键`,extra `特别`,column_comment `列注释`from information_schema.columns where lower(table_schema)=p_table_schema and table_name=p_table_nameorder by table_schema,table_name;end if;#英文列名+中文列名if p_chinese=2 thenselect table_schema `table_schema:数据库名`,table_name `table_name:表名`,column_name `column_name:列名`,column_default `column_default:缺省值`,is_nullable `is_nullable:是否允许为空`,data_type `data_type:数据类型`,column_type`column_type:列类型`,column_key`column_key:主键`,extra `extra:特别`,column_comment `column_comment:列注释` from information_schema.columns where lower(table_schema)=p_table_schema and table_name=p_table_nameorder by table_schema,table_name;end if;end if;end if;end if;end if;
end;

#执行存储过程将生成相应数据库对象的数据字典(含系统数据库及系统表)。

set @db='',@tab='',@chinese=0;call p_02(@db,@tab,@chinese);
set @db='',@tab='',@chinese=1;call p_02(@db,@tab,@chinese);
set @db='',@tab='',@chinese=2;call p_02(@db,@tab,@chinese);set @db='',@tab='t_01',@chinese=0;call p_02(@db,@tab,@chinese);
set @db='',@tab='t_01',@chinese=1;call p_02(@db,@tab,@chinese);
set @db='',@tab='t_01',@chinese=2;call p_02(@db,@tab,@chinese);set @db='db_01',@tab='',@chinese=0;call p_02(@db,@tab,@chinese);
set @db='db_01',@tab='',@chinese=1;call p_02(@db,@tab,@chinese);
set @db='db_01',@tab='',@chinese=2;call p_02(@db,@tab,@chinese);set @db='db_01',@tab='t_01',@chinese=0;call p_02(@db,@tab,@chinese);
set @db='db_01',@tab='t_01',@chinese=1;call p_02(@db,@tab,@chinese);
set @db='db_01',@tab='t_01',@chinese=2;call p_02(@db,@tab,@chinese);

三、用函数封装case 或 if 语句。

select id '员工ID',(case part_id when 0 then '总经办' when 1 then '办公室' when 2 then '设计部' when 3 then '生产部' when 4 then '仓储部' when 5 then '财务部' when 6 then '后勤部' when 7 then '采购部' when 8 then '销售部' when 9 then '保安部'else null end ) '部门',`name` '姓名',if(sex=0 ,'女', if(sex=1 ,'男',if(sex=2 ,'不详',''))) '性别', dt_birth '生日',(case post when 0  then '总经理' when 1 then '副总经理' when 2 then '经理' when 3 then '副经理' when 4 then '处长' when 4 then '副处长' when 5 then '科长' when 6 then '副科长' else null end) '职位', (case education when 0 then '博士' when 1 then '硕士' when 2 then '本科' when 3 then '专科' when 4 then '高中' when 5 then '初中' when 6 then '小学' else null end)'学历',salary '薪资',bonus '奖金'
from t_01;

这段简单的SQL语句里有多个的case和if 子句,复杂的SQL语句可能会更多这样的case 和if 子句。
将其中的case 用函数封装,可以统一固化数字所表达的中文语义、令SQL语句简洁,逻辑清晰,且可被所有成员共享调用。

#根据part_id返回部门名称
drop   function if exists f_01;
create function           f_01(p_part_id int) returns varchar(100)
comment '根据part_id返回部门名称'
begindeclare v_ret varchar(100) default '';select (   case p_part_id when 0 then '总经办'when 1 then '办公室'   when 2 then '设计部' when 3 then '生产部' when 4 then '仓储部' when 5 then '财务部' when 6 then '后勤部' when 7 then '采购部' when 8 then '销售部'when 9 then '保安部' else null end) into v_ret;return(v_ret);
end;

#根据post返回职位
drop   function if exists f_02;
create function           f_02(p_post int) returns varchar(100)
comment '根据post返回职位'
begindeclare v_ret varchar(100) default '';select (case p_post when 0 then'总经理' when 1 then'副总经理' when 2 then'经理' when 3 then'副经理' when 4 then'处长' when 5 then'副处长' when 6 then'科长' when 7 then'副科长' else null end )  into v_ret;
return(v_ret);
end;

#根据education返回学历
drop   function if exists f_03;
create function           f_03(p_education int) returns varchar(100)
comment '根据education返回学历'
begindeclare v_ret varchar(100) default '';select (case p_education when 0 then'博士' when 1 then'硕士' when 2 then'本科' when 3 then'专科' when 4 then '高中' when 5 then '初中' when 6 then '小学' else null end) into v_ret;return(v_ret);
end;
#根据sex返回性别
drop   function if exists f_04;
create function           f_04(p_sex int) returns varchar(100)
comment '根据sex返回部门性别'
begindeclare v_ret varchar(100) default '';select if(p_sex=0,'女',if(p_sex=1,'男',if(p_sex=2,'不详',''))) into v_ret;return(v_ret);
end;

再用函数来执行之前的SQL语句,清爽简洁。

select id                 '员工ID',f_01(part_id)      '部门',`name`             '姓名',f_04(sex)          '性别', dt_birth           '生日',f_02(post)         '职位', f_03(education)    '学历',salary             '薪资',bonus              '奖金'
from t_01;

MySQL 系统表列注释的应用相关推荐

  1. MySQL系统表空间和独立表空间

    文章目录 第1章 基础环境和官方架构图 1.1 我的实践环境 1.2 官方的架构图 第2章 系/独表空间的基本认识一 2.1 系统表空间的认识 2.2 独立表空间的认识 第1章 基础环境和官方架构图 ...

  2. MySQL修改表名注释

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

  3. mysql 快速升级_快速升级MySQL系统表

    一些MySQL发布对MySQL中的系统表的结构进行了更改,添加了新权限或特性.当你更新到新版本MySQL,你应同时更新系统表,以确保它们的结构最新.首先备份数据库,然后按照下面的程序操作. 在Unix ...

  4. MySQL 系统表空间ibdata:我也不想当一个死肥宅

    InnoDB的系统表空间,由一个或多个ibdata文件构成,存放InnoDB表的元数据,数据字典等等. 说到数据字典,在MySQL8.0里忽然又想起另一个文件mysql.ibd,这个也是存放数据字典的 ...

  5. MySQL 系统表损坏导致xtrabackup备份失败Cannot open filepath

    在进行innobackupex备份时,出现如下错误: 190910 12:04:48 Connecting to MySQL server host: localhost, user: dba, pa ...

  6. mysql 建表及注释

    1.建表+注释 CREATE TABLE student( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '学号', name VARCHAR(200) COMM ...

  7. mysql 系统表 存储过程_数据库系统(六)---MySQL语句及存储过程

    1.存储过程是一组为了完成某项特定功能的 SQL 语句集,其实质上就是一段存储在数据库中的代码,它可以由声明式的 SQL 语句(如 CREATE.UPDATE 和SELECT 等语句)和过程式 SQL ...

  8. sql数据库系统表和mysql系统表

    sql数据库系统表,常用的(sysobjects,sysindexes,sysindexkeys,SYSCOLUMNS,SYSTYPES 及更多解释说明): https://docs.microsof ...

  9. mysql 修改表列名称_MYSQL数据库- 修改数据表名称、列名称

    目录 一.修改列定义(modify col_name) 二.修改列类型(modify col_name) 三.修改列名称(change col_name) 三.修改表名称(rename table a ...

  10. mysql 表 字符集_如何查看MySQL数据库/表/列的字符集?

    我就是这么做的- 对于模式:SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = ...

最新文章

  1. python opencv 图像网络传输
  2. Dataset之Handwritten Digits:Handwritten Digits(手写数字图片识别)数据集简介、安装、使用方法之详细攻略
  3. 阿里云安全运营中心:DDoS攻击趁虚而入,通过代理攻击已成常态
  4. sap模块介绍_小迈说|SAP究竟有多少模块?
  5. 数字表达_神奇的数字英语表达一定会让你大吃一惊
  6. 验证Vsphere 5 支持大于2TB磁盘
  7. 虚拟机linux和主机网络连接,linux虚拟机中和主机三种网络连接方式的区别
  8. YUV格式学习:YUV444转换RGB24
  9. 斯特林数-斯特林反演
  10. HTML5矢量实现文件上传进度条
  11. Windows下Nginx负载均衡配置和优化方案
  12. 【springmvc】springmvc基本知识
  13. 通过HttpURLConnection模拟post表单提交
  14. ARM Cortex-M0+Kinetis L系列学习笔记_MKL25Z4.h
  15. 计算机论文指数,我国计算机领域学术论文引用中的马太效应——以《计算机学报》和《计算机研究与发展》为例...
  16. wordpress登录美化css,wordpress的XIU主题显示美化css配置
  17. Titanic第二章:第一节数据清洗及特征处理
  18. 2010年5月24日--2015年5月24日
  19. 大数据相加_大数据相加
  20. Chrome的无痕浏览实现初探

热门文章

  1. 设计模式学习之组合模式
  2. 桥本分数式(蓝桥杯)
  3. linux设置 wps 窗口颜色设置,WPS文字办公—修改默认字体颜色的方法
  4. Duplicate Cleaner Pro v5.0.13 电脑重复文件查找清理工具
  5. 解决Windows7下virtualbox安装ubuntu出现的0x00000000指令引用0x00000000内存,该内存不能为written问题
  6. python桌面快捷方式不见了怎么办_桌面快捷方式不见了怎么办?桌面快捷方式不见了解决方法...
  7. mybatis中获取当前时间_MySQL NOW和SYSDATE函数:获取当前时间日期
  8. 什么是主数据?有什么作用?
  9. apollo 自动驾驶中的GNSS/融合定位技术
  10. 上海大学2020计算机考研专业,2019上海大学计算机专业考研参考科目