MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 6th, 2008, 05:35 AM
mr_shadow's Avatar
mr_shadow mr_shadow is offline
java & php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 159 mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 10 h 9 m 26 sec
Reputation Power: 3
Last insert id ?!?

Hi,
consider a situation which we have a table in mysql (ex. innodb storage engine, I mention it 'cause I thought locking may be useful ) with the following fields :

id int not_null auto_increment primary key
field_x varchar(20) not null

we define id as primary key and field_x as unique key

AND also for some reasons, we want to have values in our table with the help of insert command with the following style :

id field_x
1 cod1
2 cod2

in fact, we want to get the current auto increment value in each row, add it to some string and place it to the current row with the same insert command,

the only way that I've think about it is that we perform a pre select query , which returns the latest auto increment value , add it by 1 and then place it in the main insert command
but as you know, this may cause serious problems when multiple persons execute your program at the same time and the returned value of the latest inserted record plus one is not trustable for insertion,

any helps would be appreciated,
__________________
I have given a name to my pain and call it "dog": it is just as faithful, just as obtrusive and shameless, just as entertaining, and just as clever as any other dog !

Reply With Quote
  #2  
Old May 6th, 2008, 05:55 AM
JustPHP's Avatar
JustPHP JustPHP is offline
Software Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Location: East London
Posts: 132 JustPHP User rank is Sergeant (500 - 2000 Reputation Level)JustPHP User rank is Sergeant (500 - 2000 Reputation Level)JustPHP User rank is Sergeant (500 - 2000 Reputation Level)JustPHP User rank is Sergeant (500 - 2000 Reputation Level)JustPHP User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 55 m 59 sec
Reputation Power: 10
Send a message via MSN to JustPHP Send a message via Yahoo to JustPHP Send a message via Skype to JustPHP
how about using update get your id value and append concat it to your str for the value of field_x
__________________
So much to learn.. so little time!

Reply With Quote
  #3  
Old May 6th, 2008, 05:55 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 667 SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 22 h 21 m 9 sec
Reputation Power: 196
Quote:
Originally Posted by mr_shadow
the only way that I've think about it is that we perform a pre select query , which returns the latest auto increment value , add it by 1 and then place it in the main insert command but as you know, this may cause serious problems when multiple persons execute your program at the same time and the returned value of the latest inserted record plus one is not trustable for insertion, any helps would be appreciated,

I think you have said it yourself - you cannot rely on next auto incr value being 1 higher than the current highest and you run into the mult-update issue you refer to. I think you will have to do an INSERT, and then use last_insert_id() as part of a follow on UPDATE command, unless a guru (like Mr 937) knows different?
__________________
"I feel so miserable without you; it's almost like having you here" - Stephen Bishop

Reply With Quote
  #4  
Old May 6th, 2008, 05:56 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
how about not appending the auto_increment to the field

that's an even better solution

__________________
r937.com | rudy.ca

Reply With Quote
  #5  
Old May 6th, 2008, 05:57 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 667 SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonJM User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 22 h 21 m 9 sec
Reputation Power: 196
He speaks!!!

On that point, if your field is ALWAYS going to be 'literal' plus auto incr value, why not SELECT CONCAT(field_x,id) as afield ?

Reply With Quote
  #6  
Old May 6th, 2008, 06:29 AM
mr_shadow's Avatar
mr_shadow mr_shadow is offline
java & php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 159 mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 10 h 9 m 26 sec
Reputation Power: 3
Quote:
how about using update get your id value and append concat it to your str for the value of field_x


as you see, I said field_x is not null and it's also defined as unique.so the question is this : which value should we insert into that field at the insert time ? how can we make sure that the value that we entered is unique that in the next level we get the real previous auto increment value and perform the update ?

Quote:
I think you have said it yourself


I don't think so buddy

Quote:
- you cannot rely on next auto incr value being 1 higher than the current highest and you run into the mult-update issue you refer to. I think you will have to do an INSERT, and then use last_insert_id() as part of a follow on UPDATE command


first question : as mentioned above, how can we meke sure that the first value for field_x with insert command is unique ? what should we insert in this time ?
second question: user a insert a row , user b insert a row after user a, but for some reasons, the second query of the user b runs faster than second query of user a !!! (something like syncronization problems ; ) ) so ... ?!!!

Quote:
how about not appending the auto_increment to the field


sorry dude, all situations in this problem is mandatory ! unfortunately , we don't have what if and what about in the real life

Quote:
On that point, if your field is ALWAYS going to be 'literal' plus auto incr value, why not SELECT CONCAT(field_x,id) as afield ?


sorry, I didn't undrestand what you mean sir ? !!

thanks all for your attention ... any Ideas ?

Reply With Quote
  #7  
Old May 6th, 2008, 06:35 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
Quote:
Originally Posted by mr_shadow
sorry dude, all situations in this problem is mandatory !
okay, sure

but you said the field was unique

so why do you want to append a number onto the end of it? are you trying to make it "more unique" ??

Reply With Quote
  #8  
Old May 6th, 2008, 06:39 AM
mr_shadow's Avatar
mr_shadow mr_shadow is offline
java & php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 159 mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 10 h 9 m 26 sec
Reputation Power: 3
no sir ! it was just an example for a bigger problem in the real life, It's too hard to explain the situation in the real project, but we can model it exactly like the problem I said !

Reply With Quote
  #9  
Old May 6th, 2008, 06:47 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
okay, then my answer is too hard to explain in the real situation



but you can model it exactly like the problem you posted -- namely, do not append the auto_increment onto an already unique field value

Reply With Quote
  #10  
Old May 6th, 2008, 06:57 AM
mr_shadow's Avatar
mr_shadow mr_shadow is offline
java & php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 159 mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 10 h 9 m 26 sec
Reputation Power: 3
Quote:
Originally Posted by r937
okay, then my answer is too hard to explain in the real situation



but you can model it exactly like the problem you posted -- namely, do not append the auto_increment onto an already unique field value


OK !!!!
so, what about the real problem :
consider a web-based user management system , ...
users can register and in this case automatically get a code (for some reasons, it cannot be the exact auto_increment id and it cannot be null, and probably it should have something like COD at the first )
the system administrator also can create users in his control panel, but he/she must explicitly enter the user's code (it canot contain term COD in it)

then my friend, you may ask when we have a real unique auto increment column, then why we need this code ? the answer is : I am a programmer in this project and not a db designer and I'm forced to do what I have told , and also this project is developing for a company which each employee has a code and also they want to integrate registered users and users that admin creates in one table , and separate them with their role (defined in another table) and also their code ,,,

I would appreciate if you model your help in the real problem !!!

Reply With Quote
  #11  
Old May 6th, 2008, 07:14 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 7 h 21 m 33 sec
Reputation Power: 891
"the system administrator also can create users in his control panel, but he/she must explicitly enter the user's code"

how does he/she know what code to assign?

the solution is to acknowledge what you said --

1. each employee has a code
2. they want to integrate registered users
3. they want to integrate users that admin creates
4. and separate them with their role

so as long as you assign a unique value, and use a proper role, you're fine

easy, see?


Reply With Quote
  #12  
Old May 6th, 2008, 07:22 AM
mr_shadow's Avatar
mr_shadow mr_shadow is offline
java & php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 159 mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level)mr_shadow User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 10 h 9 m 26 sec
Reputation Power: 3
Quote:
how does he/she know what code to assign?

by company rules, sir ... I don't know ! but they want this feature !
---
yeah , each user has a role , such as system administrator, registered user, saler manager , etc ... but it's different from their code , for example saler manager may have code : 13243434 based on the company rules ! but system managers has the role no 1, salers with role no 2, (and also one person may have multiple roles but just one company code , in fact, forget role issue dear !)

Quote:
easy, see?

easy , but unfortunately impossible !! thanks anyway for your attention sir,
any ideas ?

Last edited by mr_shadow : May 6th, 2008 at 07:24 AM.

Reply With Quote
  #13  
Old May 6th, 2008, 07:29 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,331 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)