MySQL 随机密码生成代码

  • A+
所属分类:数据库

晚上有朋友问起,简单的写了一个。

  1. DELIMITER $$
  2. CREATE
  3. FUNCTION `t_girl` . `func_rand_string` ( f_num tinyint unsigned , f_type tinyint unsigned )
  4. RETURNS varchar ( 32)
  5. BEGIN
  6. -- Translate the number to letter.
  7. -- No 1 stands for string only.
  8. -- No 2 stands for number only.
  9. -- No 3 stands for combination of the above.
  10. declare i int unsigned default 0;
  11. declare v_result varchar ( 255default '' ;
  12. while i < f_num do
  13. if f_type = 1 then
  14. set v_result = concat ( v_result, char ( 97+ ceil( rand ( ) * 25) ) ) ;
  15. elseif f_type= 2 then
  16. set v_result = concat ( v_result, char ( 48+ ceil( rand ( ) * 9) ) ) ;
  17. elseif f_type= 3 then
  18. set v_result = concat ( v_result, substring ( replace ( uuid ( ) , '-' , '' ) , i+ 11) ) ;
  19. end if;
  20. set i = i + 1;
  21. end while;
  22. return v_result;
  23. END $ $
  24. DELIMITER ;

调用方法示例:

  1. select func_rand_string(12,3);
图片引用自网络