用MySQL 生成随机密码

本文介绍了一种在MySQL中创建随机字符串的方法,通过定义存储过程实现不同类型的随机字符串生成,包括纯字母、纯数字及字母数字混合等。该函数利用UUID、随机数生成等特性,适用于多种应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

CREATE
    FUNCTION `t_girl` . `func_rand_string` ( f_num tinyint unsigned , f_type tinyint unsigned )
    RETURNS varchar ( 32)
    BEGIN
      -- Translate the number to letter.
      -- No 1 stands for string only.
      -- No 2 stands for number only.
      -- No 3 stands for combination of the above.
      declare i int unsigned default 0;
      declare v_result varchar ( 255) default '' ;
      while i < f_num do
        if f_type = 1 then
          set v_result = concat ( v_result, char ( 97+ ceil( rand ( ) * 25) ) ) ;
        elseif f_type= 2 then
          set v_result = concat ( v_result, char ( 48+ ceil( rand ( ) * 9) ) ) ;
        elseif f_type= 3 then
          set v_result = concat ( v_result, substring ( replace ( uuid ( ) , '-' , '' ) , i+ 1, 1) ) ;
        end if;
        set i = i + 1;
      end while;
      return v_result;

    END $ $

DELIMITER ;


调用方法示例:
select func_rand_string(12,3);

转载于:https://www.cnblogs.com/secbook/archive/2009/09/12/2655285.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值