creating userID (int) - Use AutoIncrement?

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
Hi Guys,

Let's say you are building a user table.

Each user is going to get a userID, which is just a unique number for every account. What the number is is inconsequential as long as it's unique.

Would it be best to use the primaryKey/rowID for each userID?

For instance, let's look at a basic table.

userID (int, PK, auto)
userName (varchar255)
email (varchar255)

Is that ok? The UserID would simply autoincrement as records are inserted and the DBMS would decide what ID# was used next.

Or would it be best to determine this record ID # programatically so that I'm not reliant on the DBMS to keep userID's in order. In that case, I would use

rowID (int,auto)
userID (int, PK)
userName (varchar255)
email (varchar255)

The rowID would autoincrease as records were inserted, but I would programmatically decide on a userID prior to the insert. Of course I could enforce unique ID's with the DBMS by making sure the inserted userID is unique during the insert, but if I do things correctly that shouldn't be much of an issue.

Thoughts?

1. Rely on DBMS to create unique ID
2. Create uniqueID externally
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I don't have any problem with an auto-increment PK for the user ID. In some cases I might make it a GUID, but I think deriving it externally and passing it in is probably more error prone than relying on the DB.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> so that I'm not reliant on the DBMS to keep userID's in order.

Please explain this requirement in more detail. The auto-increment will guarantee uniqueness (never a duplicate ID), but with multiple near-simultaneous row inserts you might not know which process (or server in a cluster) would get its new row added first.

No ID numbers would be skipped at first, but if you delete rows then you'll add gaps in the existing numbers. (New inserts will still get new, unique IDs after that, deleted IDs would not be re-used.)

The only reason why I'd bother with GUIDs is security, if you don't want a bad person to be able to guess at valid ID numbers for some reason.
 
Last edited:

Hmongkeysauce

Senior member
Jun 8, 2005
360
0
76
Autoincrement/Identity or GUID. Relying on the application to generate this column may eliminate any ability to import data through SQL tools (for whatever reason you may have).
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
1. I'd like to avoid long userID's so a GUID would be pretty nasty. In fact, I'd like to keep the userID's simple. I don't have any real security reason to hide/encrypt them. I'd be perfectly happy with the 1st record being '1' and it auto incrementing by 1.

ex.

UserID UserName Email

1 Techboyjk techboyjk@gmail.com
2 DaveSimmons dave@gmail.com
3 nickbits nick@gmail.com


2. Performance wise, I've leaned towards having the DBMS do it since the application would have to calculate the next ID, lock it, and insert it. If I do it via the DBMS, by design it's not going to screw up the autoincrement, and it will more than likely be significantly more efficient at determining what's the next ID number.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Since you just need a simple number, auto-incement is the best and safest way to do it. The DB engine will guarantee unique IDs.

Anything you do in code yourself could have race conditions if there is ever more than one process running at a time.
 

LokutusofBorg

Golden Member
Mar 20, 2001
1,065
0
76
If you need auto-incrementing integers, have the database generate them.

If you need something else, your application should generate GUIDs. There is no reason to worry about race conditions or having to manually lock anything if you generate a GUID and insert it into the database. The chance of creating a duplicate GUID is astronomically low. That's the whole point of GUID.
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
I'd personally just use an auto-incrementing INT datatype. Requires much less storage per row than a GUID and is automatically handled by the sql engine.

Why reinvent the wheel?
 

dwell

pics?
Oct 9, 1999
5,189
2
0
If it's a web app and exposed anywhere in the UI: GUID. Predictable IDs are insecure.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
If it's a web app and exposed anywhere in the UI: GUID. Predictable IDs are insecure.

Only if the ID number lets you do anything by itself. If your site allows something like

display-secret-documents?user=12345

... without requiring more such as a valid session cookie tied to that specific user, then it's already broken beyond the ability of a GUID to help.
 

dwell

pics?
Oct 9, 1999
5,189
2
0
It's not security by obscurity. Obviously don't make your system insecure in the first place where even if the key was a sequence it would not matter. At the same time, giving someone who is tying to own your system the ability to predict the next element in a sequence is bad practice.

Point is, you may think your code is airtight because you wrote it, that does not mean you throw best security practices out the window just because you're feeling confident.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You're arguing not to use row IDs in tables, because then an attacker might break past all other security.

I'd argue the effort needed to replace the row IDs with GUIDs is better spent on testing and hardening the rest of your code.

Both are of course opinions.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Let the database do autoincrement IDs. Make sure you scrub and sanitize data on your front end to ensure security. Keeping a numeric allows indexing to be faster.

Also, as a final consideration, if you are planning on having more than 2147483647 records in that table, consider bigint. I've seen plenty of bad bugs because of a bad datatype.
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
It's not security by obscurity. Obviously don't make your system insecure in the first place where even if the key was a sequence it would not matter. At the same time, giving someone who is tying to own your system the ability to predict the next element in a sequence is bad practice.

Point is, you may think your code is airtight because you wrote it, that does not mean you throw best security practices out the window just because you're feeling confident.

That means everything out there that utilizes numerical data types are bad practices.

http://forums.anandtech.com/showthread.php?t=2244269
http://forums.anandtech.com/showthread.php?t=2244268
http://forums.anandtech.com/showthread.php?t=2244267

Quick, someone alert a mod about a vulnerability in the site!
 
Last edited:

jvroig

Platinum Member
Nov 4, 2009
2,394
1
81
Look no further than our own forums for user IDs that are simply auto-incremented.
 

BoberFett

Lifer
Oct 9, 1999
37,563
9
81
That means everything out there that utilizes numerical data types are bad practices.

http://forums.anandtech.com/showthread.php?t=2244269
http://forums.anandtech.com/showthread.php?t=2244268
http://forums.anandtech.com/showthread.php?t=2244267

Quick, someone alert a mod about a vulnerability in the site!

For data you want to obscure, he's correct. AutoIncrement makes things predictable. If you don't want somebody scraping your site, then use GUIDs. If you want to make it easy to scrape then go ahead and use an autoincrement and put it right in the URL. There are reasons to do both.
 

douglasb

Diamond Member
Apr 11, 2005
3,163
0
76
One reason that you might NOT want to use "userID" as the Primary Key is that primary keys can never be changed/edited. You would have to delete the entire row and recreate it with the new PK/userID. If you don't ever plan on changing userID's, this is not an issue. Just something to think about.
 

velvetpants

Member
Aug 29, 2009
72
0
0
One reason that you might NOT want to use "userID" as the Primary Key is that primary keys can never be changed/edited. You would have to delete the entire row and recreate it with the new PK/userID. If you don't ever plan on changing userID's, this is not an issue. Just something to think about.

What dbms are you talking about?
Should have no issues updating the primary key as long as the foreign have 'on update cascade'.
 

dwell

pics?
Oct 9, 1999
5,189
2
0
Quick, someone alert a mod about a vulnerability in the site!

It does not matter much on a forum, it does matter if you don't want someone to mine your site and create metrics on your userbase and sequence of signups, etc. As mentioned, it's case dependent.

Also if you're sharding you'll avoid hotspots by using UUIDs.
 

Train

Lifer
Jun 22, 2000
13,863
68
91
www.bing.com
For data you want to obscure, he's correct. AutoIncrement makes things predictable. If you don't want somebody scraping your site, then use GUIDs. If you want to make it easy to scrape then go ahead and use an autoincrement and put it right in the URL. There are reasons to do both.

If it's data someone shouldn't see, then you should not show it, unless they are authenticated. Plain and simple. Making the ID unpredictable doesn't change this basic need.

And if someone CAN gain information (like if they are denied information based on auth, or the use in question does not exist) then that is information leakage, and should also be prevented. On my secure sites, if you request data by ID, and the app cannot show it for any reason (not found, not authorized, etc) the app returns a generic error, not what the error is.
 

BoberFett

Lifer
Oct 9, 1999
37,563
9
81
If it's data someone shouldn't see, then you should not show it, unless they are authenticated. Plain and simple. Making the ID unpredictable doesn't change this basic need.

And if someone CAN gain information (like if they are denied information based on auth, or the use in question does not exist) then that is information leakage, and should also be prevented. On my secure sites, if you request data by ID, and the app cannot show it for any reason (not found, not authorized, etc) the app returns a generic error, not what the error is.

Again, there are different solutions for different reasons. You may want randomized IDs but still want it in the URL so it can be bookmarked for instance. And yes, authentication and security is part of that. There is no one right answer, use the pieces that are correct for any particular problem.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |