imsome1
imsome1

Reputation: 1192

MySql Concurrency issue

I am using this query to retrieve auto incremented key value of "invoiceNo"

 "SELECT AUTO_INCREMENT FROM information_schema.TABLES 
 where TABLE_SCHEMA='DB_NAME' and TABLE_NAME='invoice';" 

and using that value as Foreign Key and inserting into invoice_sub table

Table
I use following query to insert into invoice and invoice_sub tables

  insert into invoice(netAmount)            
  insert into invoice_sub(PK, itemDescr, itemPrice)<br>
  PK= AUTO_INCREMENT value


What if two or more users inserting at same time

Upvotes: 1

Views: 121

Answers (1)

juergen d
juergen d

Reputation: 204894

Don't do that like this!

  1. Insert into invoice. The ID will be calculated automatically.
  2. Get that ID from the last insert
  3. use that ID to insert into invoice_sub

Upvotes: 3

Related Questions