0

I am creating user detail table in MySQL where user details are stored when user submits registration form.

In my user table user id field is primary key and auto increment.

My application should be used by many users from many locations. Now there is one scenario when it is possible that two user clicks on submit button at same time from anywhere. My primary key is of integer type and has length of 15, but when two user clicks at same time then which user should get first next id. Or it is possible that none of that get registered and get error.

so what can i do in this case

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • MySQL should look after this for you invisibly. Thats why you use serious database management software. – RiggsFolly Nov 13 '14 at 01:08
  • you mean i will not get this kind of error or situation – Bhargav Thanki Nov 13 '14 at 01:10
  • your MySQL DBMS should handle that on its own. A primary key (by design) can't exist two times. MySQL needs it to identify a row. So one of them gets the first the other the second id. Even if you have 100 users registering at exactly the same nanosecond.. MySQL can manage that :) – GameDroids Nov 13 '14 at 01:10
  • @zerkms There are (many) things in SQL I don't know about, however I am constantly keeping an eye out for information I don't know about, including doing my own research on matters such as this. One of which that I just found now and reading up, a Q&A on Stack actually http://stackoverflow.com/q/10908561/ - One never ceases to learn ;) – Funk Forty Niner Nov 13 '14 at 01:47
  • @Fred-ii- okay. Btw, it's not mysql specific. In any RDBMS PK is unique – zerkms Nov 13 '14 at 01:48
  • @zerkms Thank you, I have noted it, *cheers* – Funk Forty Niner Nov 13 '14 at 01:50

1 Answers1

2

Your mySQL id field is good, so you should have no problem; however, when inserting a new record, then depending on what your insert SQL query looks like, make sure you leave the id undefined, or use something like:

insert into users values(NULL, "blah", "blah")

-where "NULL" is your first column / field. This will only work the way you expect if the field (column) in place "NULL" is set (when created, or altered) to AUTO INCREMENT. If these are set, the record id is incremented on each data entry, no matter if it happens at the same time; new records are always queued to be inserted one after the other.