The Cache: Technology Expert's Forum
Welcome, Guest. Please login or register. September 20, 2019, 11:17:56 AM

Login with username, password and session length

Pages: [1]
Author Topic: Primary Key Selection  (Read 2076 times)
Offline Offline

Posts: 105

View Profile
« on: December 10, 2009, 08:45:47 PM »

This may belong in the n00b section, but I'll post it here anyway.

When setting up my database tables I have always used an auto-increment numeric id field as the primary key.

But now I have a situation where I want to track every visitor that comes to my site. So you hit my page and I want to add a row to my "visitor" table so I cookie you with your ID and store that in my DB.

Now, for obvious reasons, I don't want this ID to be an auto-increment value. So what I am planning on doing is generating the ids with:

md5(uniqid(mt_rand(), true));

and using that (char(32)) as the primary key in my visitors table.

I'll be doing a fair amount of lookups using this value in this table as well as others.

Thoughts? Suggestions?

Will code for food.
Offline Offline

Posts: 5627

Back in my day we had 9 planets

View Profile
« Reply #1 on: December 10, 2009, 10:06:41 PM »

generally, I do autoinc int for the PK regardless. So ALL my tables always have that auto inc int PK. The reason why is to speed up subsequent lookups. Or at least that has been my reasoning, since I assume int is faster than char of course.

You could of course do the md5(uniqid(mt_rand(), true)); as UNIQUE index. It doesnt have to be PK. And, that way you can fall down to using the INT for followup lookups.

of course if there is generally going to only be 1 lookup then done with it, then never mind...

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Offline Offline

Posts: 14

View Profile
« Reply #2 on: January 12, 2010, 07:23:26 PM »

<spammer dick go boom>
« Last Edit: January 12, 2010, 08:31:28 PM by nutballs » Logged

No links in signatures please
Pages: [1]
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks

Valid XHTML 1.0! Valid CSS!