本文介绍一下有关Oracle随机字符串的生成方法及具体应用一例。
Oracle生成随机字符串的方法是通过dbms_random.string实现的。
1.dbms_random.string用法
Oracle官方文档参考链接:http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_random.htm#i996825
用法是DBMS_RANDOM.STRING(选项, 返回字符串长度)
选项有如下几种可供选择:
1)'u', 'U' - returning string in uppercase alpha characters
2)'l', 'L' - returning string in lowercase alpha characters
3)'a', 'A' - returning string in mixed case alpha characters
4)'x', 'X' - returning string in uppercase alpha-numeric characters
5)'p', 'P' - returning string in any printable characters.
6)Otherwise the returning string is in uppercase alpha characters.
2.以随机生成8位密码这个简单需求为例演示一下各个选项的用法
1)生成由大写字母组成的8位密码
sys@ora10g> select dbms_random.string('u',8) "u_8_password" from dual;
u_8_password
------------------------------------------------------------------
HXGBMNCF
sys@ora10g> select dbms_random.string('U',8) "U_8_password" from dual;
U_8_password
------------------------------------------------------------------
JEZLDJXK
2)生成由小写字母组成的8位密码
sys@ora10g> select dbms_random.string('l',8) "l_8_password" from dual;
l_8_password
------------------------------------------------------------------
ilxpvvmy
sys@ora10g> select dbms_random.string('L',8) "L_8_password" from dual;
L_8_password
------------------------------------------------------------------
pzvjvpji
3)生成由大小写字母混合出现的8位密码
sys@ora10g> select dbms_random.string('a',8) "a_8_password" from dual;
a_8_password
------------------------------------------------------------------
YfeaneZx
sys@ora10g> select dbms_random.string('A',8) "A_8_password" from dual;
A_8_password
------------------------------------------------------------------
IghqWAVu
4)生成由大写字母和数字组成的8位密码
sys@ora10g> select dbms_random.string('x',8) "x_8_password" from dual;
x_8_password
------------------------------------------------------------------
2VKQ4FSH
sys@ora10g> select dbms_random.string('X',8) "X_8_password" from dual;
X_8_password
------------------------------------------------------------------
MMYTPC40
5)生成由任何可打印字符组成的8位密码
sys@ora10g> select dbms_random.string('p',8) "p_8_password" from dual;
p_8_password
------------------------------------------------------------------
G7`Rbe#V
sys@ora10g> select dbms_random.string('P',8) "P_8_password" from dual;
P_8_password
------------------------------------------------------------------
wPqHt*0.
6)当选项为其他字母的时候返回的内容仍将是大写字母
选项以“8”为例,返回的8位随机字符串内容是由大写字母组成的。
sys@ora10g> select dbms_random.string('8',8) "8_8_password" from dual;
8_8_password
------------------------------------------------------------------
PJGYPPLG
3.小结
本文以具体实例形式演示了dbms_random.string的用法。有时候也可以混合使用。
sys@ora10g> select dbms_random.string('u',8)||dbms_random.string('l',8) "u_l_16_password" from dual;
u_l_16_password
------------------------------------------------------------------
EMVXOZLGixziwvny
Good luck.
secooler
11.04.25
-- The End --