473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Using last_id and transactions

348 100+
Hi all,

I am having a really hard time on this and I hope someone can help me out.

What I have are about 4 inserts that need need to happen at the same time using a transaction. I am having a major problem with my php code and the transaction playing "nice".

Table A
Table B
Table C
Table D

Table A is the parent and B, C and D are the children. I need to get the surrogate from table A by way of insert_id that will grab the last auto-inc. value from table A to insert FKs into tables B C and D. The problem is, when I use BEGIN to start the transaction I can't get the id value from table A because A has not yet been committed.

How can I get this to work? Someone told me to use a stored procedure but I am really clueless on that.

Thanks
Nov 10 '07 #1
4 2116
Atli
5,058 Expert 4TB
Hi.

You could grab the next ID before you insert anything into any of your tables and use that.
Expand|Select|Wrap|Line Numbers
  1. SELECT MAX(id) + 1 AS 'next_id' FROM tblA
  2.  
Nov 10 '07 #2
fjm
348 100+
Atli, thanks for the reply.

I have thought about doing just that but the app will be used in a multiuser environment and I am concerned about race conditions. If I am incorrect about this, please let me know.

Is this a factor?

Thanks
Nov 10 '07 #3
Atli
5,058 Expert 4TB
That may indeed be a factor. It would perhaps be best to look at some alternatives first.

You could try a stored procedure.
Something simple like:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER $$
  2. CREATE PROCEDURE AddSomething(IN someText VARCHAR(255))
  3. BEGIN
  4. DECLARE insert_id;
  5.  
  6. INSERT INTO tblA(textField) VALUES(someText);
  7.  
  8. SELECT last_insert_id() INTO insert_id;
  9.  
  10. INSERT INTO tblB(tblA_fk_reference) VALUES(insert_id);
  11.  
  12. END $$
  13. DELIMITER ;
  14.  
There are some helpful examples on how you can extend this in the MySQL Reference Manual. (Check out the User Comments)
Nov 10 '07 #4
fjm
348 100+
Atli, thanks you for the code example. It should be enough to get me started on the right track.

I will post back if I nedd further help. :)

That may indeed be a factor. It would perhaps be best to look at some alternatives first.

You could try a stored procedure.
Something simple like:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER $$
  2. CREATE PROCEDURE AddSomething(IN someText VARCHAR(255))
  3. BEGIN
  4. DECLARE insert_id;
  5.  
  6. INSERT INTO tblA(textField) VALUES(someText);
  7.  
  8. SELECT last_insert_id() INTO insert_id;
  9.  
  10. INSERT INTO tblB(tblA_fk_reference) VALUES(insert_id);
  11.  
  12. END $$
  13. DELIMITER ;
  14.  
There are some helpful examples on how you can extend this in the MySQL Reference Manual. (Check out the User Comments)
Nov 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Frank Millman | last post by:
Hi all From time to time there is a request for a simple way of storing and accessing data without using a full SQL database. I had this requirement recently, and I was pleasantly surprised by...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
3
by: Henrootje | last post by:
Hello Access-guru's, wizards and helpful people! I am trying to import several textfiles into several tables. Now I thought the following code up which works swell: Dim Importspecification,...
0
radcaesar
by: radcaesar | last post by:
Customer Table ID Name Address City Phone 1 Vijay Stores 6,Gandhi Road Pondy 0413-276564 2 Ram Stores 3, MG Road, Pondicherry 0413-29543756 3 Balu Papers 3, RG...
3
by: cachaco87 | last post by:
Hi, I'm new to this boards, and I need some help finishing a project for school. Im using nested if-statements inside a while loop to execute different actions. action C consist of printing the...
2
by: Sridhar | last post by:
Hi, I am trying to implement sql transactions. But I am not knowing how to do that. I created a data access layer which contains methods to select/insert/update tables in a database. I have also...
12
by: Rami | last post by:
I have some requirement for an automated payment system. The system has four machines setup as follows: 1- Two machines have a clustered database. 2- Two machines have a .net business logic...
0
by: David | last post by:
- Are there any peculiarities with using curs.executemany(...) vs. multiple How many times are you calling execute vs a single executemany? The python call overhead will add up for thousands of...
2
by: GaryDean | last post by:
When I use transactions with sql server I usually do this... using (TransactionScope scope = new TransactionScope) { using (SqlConnection1 = new SqlConnection . . . . . and this all works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.