2007. 03. 22, csütörtök keltezéssel 11.42-kor tedd ezt írta: > At 4:28 PM +0100 3/22/07, Németh Zoltán wrote: > >2007. 03. 22, csütörtök keltezéssel 11.24-kor tedd ezt írta: > >> At 11:18 AM -0600 3/21/07, ccspencer@xxxxxxxxxxxx wrote: > >> >Hello, > >> >I want to add a random unique ID to a Mysql table. Collisions > >> >are unlikely but possible so to handle those cases I'd like to > >> >regenerate the random ID until there is no collision and only > >> >then add my row. Any suggestions for a newbie as to the right > >> >way to go about doing this? > >> >Best, > >> >Craig > >> > >> Craig: > >> > >> Simple. > >> > >> Generate a "random unique ID" via whatever method you want. However, > >> before using, search your dB to see if it's already in use. If it is, > >> then generate another one and repeat. > > > >it's probably more efficient to try to insert with the generated ID at > >once, and repeat only if it fails. > >if the ID generation method is good enough, repeat will be needed only > >extremely rarely > > > >greets > >Zoltán Németh > > > I like to write code that avoids failure if > possible rather than writing something that I > know can fail and then dealing with resultant > errors. Just differences in perspectives. that's a matter of taste, which can not be argued ;) > > As for efficiency, that's probably not even worth mentioning in this case. why not? you would use 2 sql queries while I would use one most of the time and 2 in case of already reserved ID - that is almost twice as much processor time that may count much if the routine is used frequently greets Zoltán Németh > > Cheers, > > tedd > -- > ------- > http://sperling.com http://ancientstones.com http://earthstones.com > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php