MySQL是一種常用的關系型數據庫管理系統,可以使用其生成隨機密碼。
在MySQL中,使用以下SQL語句可生成固定長度的隨機密碼:
SELECT CONCAT(SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1)) as password;
上述SQL語句用到了MySQL的內置函數RAND()和SUBSTRING()。其中RAND()函數返回0到1之間的隨機數,SUBSTRING()函數用于截取字符串。
在上述SQL語句中,隨機數函數RAND()控制CSUBSTRING()函數隨機截取字符串中的字符,最終生成的密碼由6個隨機字符組成,包括大小寫字母和數字。
若要生成不同位數的隨機密碼,只需修改密碼位數的個數,例如,要生成10位隨機密碼,可將上述SQL語句修改為:
SELECT CONCAT(SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1), SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', RAND()*62+1, 1)) as password;
生成的10位隨機密碼包括大小寫字母和數字。
上一篇mysql中的隨機數是
下一篇用css實現一幅圖畫