生成数据字典,一个存储过程搞定

1、创建存储过程,下面的存储过程是以mysql数据库的语法写的,其中只查询出了tb_用户管理开头的表,可以根据自己的需求适当的修改:

DELIMITER $$

USE `dbname`$$

DROP PROCEDURE IF EXISTS `proc_getdatadicdemo`$$

CREATE DEFINER=`root`@`%` PROCEDURE `proc_getdatadicdemo`()
BEGIN
DECLARE v_tablename VARCHAR(50);
DECLARE v_columnname VARCHAR(50);
DECLARE v_disname VARCHAR(50);
DECLARE v_priname VARCHAR(50);
DECLARE v_datatype VARCHAR(50);
DECLARE v_datalen VARCHAR(50);
DECLARE v_desc VARCHAR(50);
DECLARE v_col VARCHAR(255);
DECLARE v_tname VARCHAR(50);
DECLARE v_count INT;

DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR 
	SELECT * FROM 
(
	SELECT 
	table_name 表名,
	column_name 字段名,
	column_name 显示名,
	IF(column_key='PRI','是','') 主键,data_type 数据类型,
	IF(character_maximum_length>0,character_maximum_length,'') 长度,
	IF(column_comment='',column_name,column_comment) 说明 
	 FROM information_schema.COLUMNS 
	WHERE TABLE_SCHEMA = 'dbname'
	AND table_name LIKE 'tb_name%'
	AND table_name NOT LIKE '%_mm_%'
	AND table_name NOT LIKE '%_dd_%'	
	AND table_name NOT LIKE '%19%'
	AND table_name NOT LIKE '%18%'
) t;	

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TEMPORARY TABLE IF EXISTS temp_dic;
CREATE TEMPORARY TABLE temp_dic 
(
col VARCHAR(255) NOT NULL
);
SET v_tname='';
SET v_count=1;
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_tablename,v_columnname,v_disname,v_priname,v_datatype,v_datalen,v_desc;
IF done THEN
LEAVE read_loop;
END IF;

IF(v_tablename!=v_tname) THEN
	IF(v_count>1) THEN
		INSERT INTO temp_dic VALUES('');
	END IF;	
	INSERT INTO temp_dic VALUES(CONCAT('

',v_tablename,'

')); INSERT INTO temp_dic VALUES(CONCAT(''));SET v_count=v_count+1;ENDIF;INSERTINTO temp_dic VALUES(CONCAT(''));SET v_tname=v_tablename;ENDLOOP;CLOSE cur;IF(v_count>1)THENINSERTINTO temp_dic VALUES('
字段名显示名主键数据类型长度说明
',v_columnname,'',v_disname,'',v_priname,'',v_datatype,'',v_datalen,'',v_desc,'
'
); END IF; SELECT * FROM temp_dic; END$$ DELIMITER ;

2、运行存储过程拿到结果:

call proc_getdatadic

3、将结果保存为一个html文件即可。

你可能感兴趣的:(数据库)