Jump to content

stored procedure with or without transactions ???


Recommended Posts

hello :)

 

please...

I'd like to know what's the difference between these two procedure statements :

 

one with a transaction :

begin 
start transaction; 
insert into emp(name) values(p_name); 
insert into emp_details(id,address,phone) values(last_insert_id(),p_address,p_phone);
commit; 
end

 

and the other is without the transaction :

begin 
insert into emp(name) values(p_name); 
insert into emp_details(id,address,phone) values(last_insert_id(),p_address,p_phone);
end

 

which one is better ???? and why ???

 

plz explain it simply coz i'm a beginner

 

 

you eventually could pretty much answer that question yourself just doing some reading, even if you are a beginner.

 

a couple pointers:

http://dev.mysql.com/doc/refman/5.5/en/ansi-diff-transactions.html

 

http://dev.mysql.com/doc/refman/5.0/en/commit.html

It is possible that the query could be run at the same time. This means that your last_insert_id() might not be for a different query. For example, if you run "insert into emp" it might create an ID of 7. If, before you run the second insert, the first query is run again you would get an ID of 8. So the second query would have an ID of 8 and not 7.

 

Using transactions you can prevent this problem. When you use transactions you sort of create temporary data. The data is only saved permanently once you commit. So the ID would always be accurate using transactions.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.