views:

369

answers:

3

I need to store posts from users.

Their usernames are unique and I'm sure they can't make posts at the same millisecond.

Then the URL of their post would contain /Username/12345678890/title-of-the-post

I can't use the Auto_Inc number as I don't know it in the DB. want them to know the number or be able to guess.

Is this sufficient enough to be unique enough for storage in the DB


If this is the case would the best way to store the unique id in the DB (MySQL)

The PK wouldn't be relative for the URL.

E.g.

AUTO_INCREMENT PK   Username   createDate (BIGINT)
-----------------   --------   -------------------
23456               Daxon       12345678922

Should I be indexing a column?

-- Edit --

I forgot to mention that I don't what the user to know how many posts there are or be able to guess a number for them.

So I thought that currentTime in milliseconds would be unguessable.

Mabye now this value should be encrypted but still URL-SAFE

+1  A: 
  1. Unless different people can login using the same username from different PCs, username + time in milliseconds should be enough. If they can post from different PCs, you should make sure that the server says what the current time is (user PCs can quickly get out of sync).

  2. Create an index on the columns username and createDate. A single index should be enough.

Aaron Digulla
+2  A: 

I can't use the Auto_Inc number as I don't know it in the DB

Then the solution to your problem is to learn it, or ask a question to clear up what you don't understand (MySql even offers the serial alias to make these easy). Creating work arounds because you don't know how to use a feature of your language just sets up problems for you to have to solve later. Solve them now while they're easy and there are a lot of people who can help (more people know how to setup incrementing keys then how to fix bizarre concurrency issues or make late "change name" features that screw up old data)

David
A: 

Then the URL of their post would contain /Username/12345678890/title-of-the-post

I would suggest you to fetch the primary key and create a url like below

/Username/AUTO_INCREMENT_ID/title-of-the-post

I can't use the Auto_Inc number as I don't know it in the DB

You can get the AUTO_INCREMENT_ID the same way as you'd do to get the value for username and title of post. Just select it in the SELECT statement. e.g.

SELECT auto_increment_id, username, anything_other FROM table1
Wbdvlpr
Thanks but I wanted to minimize the amount of DB hits,(im learning, i'll remember to type all my requirements before I post)So I would save the users Post, Then have to use another select to find out what ID it has,Then serve up the page with that Id,I needed something unique as it goes to save so I can return the page then and there
Daxon