Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id AF2292E0182 for ; Tue, 18 Mar 2008 16:00:00 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 28200-05 for ; Tue, 18 Mar 2008 15:59:52 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtp186.iad.emailsrvr.com (smtp186.iad.emailsrvr.com [207.97.245.186]) by postgresql.org (Postfix) with ESMTP id 71C572E0191 for ; Tue, 18 Mar 2008 15:59:52 -0300 (ADT) Received: from relay8.relay.iad.mlsrvr.com (localhost [127.0.0.1]) by relay8.relay.iad.mlsrvr.com (SMTP Server) with ESMTP id AC13F1B4084; Tue, 18 Mar 2008 14:59:51 -0400 (EDT) Received: by relay8.relay.iad.mlsrvr.com (Authenticated sender: erik-AT-myemma.com) with ESMTP id 7CAE41B47C7; Tue, 18 Mar 2008 14:59:51 -0400 (EDT) Cc: "Vivek Khera" , Message-Id: From: Erik Jones To: "Campbell, Lance" In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v919.2) Subject: Re: Create on insert a unique random number Date: Tue, 18 Mar 2008 13:59:50 -0500 References: <268179.51357.qm@web54301.mail.re2.yahoo.com> <20080318165138.GA6578@a-kretschmer.de> <6649C6F5-BEDE-4A66-A9A3-33CE95633A83@khera.org> X-Mailer: Apple Mail (2.919.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/110 X-Sequence-Number: 30438 On Mar 18, 2008, at 1:40 PM, Campbell, Lance wrote: > Thanks for all of your input. It appears that the best way to do this > is to create a default random number in the primary id field in the > table definition and then return that value after insert. If an > exception occurs because of duplicates I will simple perform the same > insert statement again. I doubt there would be many duplicate hits =20 > if I > use a really large number. > > Why use a random number as a primary key? Security via obscurity. > > I build web applications for a living. In most of my applications =20 > it is > preferable to use a random primary key. Why? > > Example: > > I built a web application called the Form Builder. It allows > individuals to create web forms. After a user is done building their > web form the tool provides a URL for the user to access the form. > Obviously the URL has the random ID of the form in it. Most of the > forms created with this tool can be accessed and filled out by the > general public. > > So why not use a sequential number? So if I used a sequential number > then a student or outside individual could easily change the number in > the URL to see what other forms there are. It is not that they don't > have access to the forms but they should not be messing with them if > they really don't have a reason to. So by using a very large random > number the users filling out a form cannot easily guess what another > form ID is. > > Does that make sense? I have at least a dozen tools that I prefer to > use this approach on. > > This is not a security approach. It is more about not giving obvious > access to people that want to mess around. I'd say it makes total sense and we do much the same thing with =20 unsubscribe links in the emails we push here. However, we keep the =20 primary key based on sequences and for sent messages generate a random =20= id using md5(now()::text). In a sense, then, we have "public" and =20 "private" keys. Erik Jones DBA | Emma=AE erik@myemma.com 800.595.4401 or 615.292.5888 615.292.0777 (fax) Emma helps organizations everywhere communicate & market in style. Visit us online at http://www.myemma.com