MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 19th, 2007, 08:26 AM
prakashs.pdi prakashs.pdi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Location: chennai
Posts: 3 prakashs.pdi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 21 sec
Reputation Power: 0
Send a message via AIM to prakashs.pdi
Random String in mysql

Is there any build in function availabe in MySQL to generate
random strings ?

Reply With Quote
  #2  
Old February 19th, 2007, 10:08 AM
Humanetainit's Avatar
Humanetainit Humanetainit is offline
Beyond help
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: The Netherlands
Posts: 223 Humanetainit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 32 m 30 sec
Reputation Power: 2
Code:
UPDATE `table` SET col = MD5(RAND()) WHERE condition=TRUE;
should do the trick.
__________________
One World... Humanetainit | Program secure

Reply With Quote
  #3  
Old February 20th, 2007, 01:07 AM
prakashs.pdi prakashs.pdi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Location: chennai
Posts: 3 prakashs.pdi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 21 sec
Reputation Power: 0
Send a message via AIM to prakashs.pdi
Quote:
Originally Posted by Humanetainit
Code:
UPDATE `table` SET col = MD5(RAND()) WHERE condition=TRUE;
should do the trick.


Hi,

This works.. Thanks... but the result comes with the combination of both numeric and alphabets. I need the random string should contain only alpha both upper and lower case.

Reply With Quote
  #4  
Old February 20th, 2007, 03:43 AM
Humanetainit's Avatar
Humanetainit Humanetainit is offline
Beyond help
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: The Netherlands
Posts: 223 Humanetainit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 32 m 30 sec
Reputation Power: 2
Are you sure it has to be native MySQL? PHP can do this just as fast at runtime:
Code:
$q = "UPDATE `table` " . 
"SET col = " . substr(str_shuffle("abcefghijklmnopqrstuvwxyz" . 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 16) . 
"WHERE condition=TRUE";
Just change 16 to the length you need.

Reply With Quote
  #5  
Old September 24th, 2007, 04:05 PM
Arpee Ong Arpee Ong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 7 Arpee Ong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by Humanetainit
Are you sure it has to be native MySQL? PHP can do this just as fast at runtime:
Code:
$q = "UPDATE `table` " . 
"SET col = " . substr(str_shuffle("abcefghijklmnopqrstuvwxyz" . 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 16) . 
"WHERE condition=TRUE";
Just change 16 to the length you need.


i think this can be done purely in mysql as well.. like
Code:
UPDATE `table` SET col = substring(MD5(RAND()), -8) WHERE condition=TRUE;

which will generate an 8 character random string for each row/column where the condition holds true..

Reply With Quote
  #6  
Old September 24th, 2007, 04:09 PM
Humanetainit's Avatar
Humanetainit Humanetainit is offline
Beyond help
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: The Netherlands
Posts: 223 Humanetainit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 32 m 30 sec
Reputation Power: 2
Please read the entire thread...

Reply With Quote
  #7  
Old October 2nd, 2007, 04:47 PM
Arpee Ong Arpee Ong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 7 Arpee Ong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 25 sec
Reputation Power: 0
oops you got me there sorry, i was drunk when i posted that... anyways heres a bare one.. you can do this entirely in MySQL without PHP

Code:
UPDATE `table` SET col =
(SELECT concat(
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' ),
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' )
)
) WHERE condition=TRUE;


I know this is long.. but if you want to go an extra and can or would like to implement this within a transaction, then you can shorten the code above dramatically by two ways.

1.) Enclose the above query within a Transaction then Store the alphabets in a variable like
Code:
SELECT @alphavar:= a','b','c','d','e','f','g','h','i','j','k','l','m'  ,'n','o','p','q','r','s','t','u','v','w','x','y','  z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z';

and call above to all parts of the query calling the characters

and/or

2.) Wrap it around a while statement.

Hope that helps.. i would just like to emphasize no matter how dirty.. generating random strings within MySQL is possible.. and i know there is a cleaner way than this that is yet to be invented

Reply With Quote
  #8  
Old October 2nd, 2007, 05:54 PM
Arpee Ong Arpee Ong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 7 Arpee Ong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 47 m 25 sec
Reputation Power: 0
or..

you can set up a function to generate a random one character alpha like this:

Code:
DROP function if exists generate_alpha;
CREATE FUNCTION generate_alpha () RETURNS CHAR(1)
RETURN 
ELT(FLOOR(1 + (RAND() * (50-1))), 'a','b','c','d','e','f','g','h','i','j','k','l','m  ','n','o','p','q','r','s','t','u','v','w','x','y',  'z',
'A','B','C','D','E','F','G','H','I','J','K','L','M  ','N','O','P','Q','R','S','T','U','V','W','X','Y',  'Z' );
)


then you can update your table like this in a bit shorter query like

Code:
UPDATE `table` SET col =
(SELECT concat(
generate_alpha (),
generate_alpha (),
generate_alpha (),
generate_alpha (),
generate_alpha (),
generate_alpha (),
generate_alpha (),
generate_alpha ()
)
) WHERE condition=TRUE;


then maybe optimize this further by then again doing this query within a Transaction and enclose the 1st 7 generate_alpha (), within a WHILE statement..

this should get shorter as someone thinks of it.. but for now this should work as it should be within MySQL bounds without help of any other languages..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Random String in mysql


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT