tags:

views:

29

answers:

2

Hi,

My situation is i need to update 4 tables upon a request.

I execute four statements like a delete two inserts and one update.

What if in any given time after executing two queries the database fails or for any other reason the code exits (probably of any sort of db error/exception).

If that happens then many reference would get wrong and oops :-(. Data will be totally wrong and will have incomplete references.

Is there an option like executing many sql statements in a one shot like multiple insert into statement?

Or if there is a commit mechanism(even if there one i have to learn it to use in mysql) ?

so, at any point i dont want an invalid entry to take place.

for example...

// start save point

// execute all four statements one by one

// at this line commit

I am using codeigniter framework.

I would like to have any kind of reply to this. (suggestions/comments/answers).

Thank you.

+3  A: 

You are looking for transactions. Do a START TRANSACTION at the start of the sequence and COMMIT at the end. If something goes wrong do ROLLBACK. Either all the changes will get made or none.

(If you are using a programming language with a data access layer, that may provide methods to call instead of explicitly executing COMMIT et al as queries.)

You will need to be using InnoDB tables to support transactions and other data integrity features that MyISAM can't provide.

bobince
I am using php, apache and mysql. Engine is MyISAM. mostly i was suggested to use MyISAM and i have to read the difference between MyISAM and InnoDB. can i have some references? what if i needed it in MyISAM? Is it by default not allowed in MyISAM? can i have some references for the above... i am already with google for this. thank you.
Jayapal Chandran
I am using Codeigniter framework which i did not mention in my quesiton.
Jayapal Chandran
MyISAM doesn't support transactions, foreign keys or the other integrity constraints associated with ACID databases. I personally wouldn't advise using it unless you have to (typically for `FULLTEXT` indexing, which InnoDB doesn't implement). [Transactions in CI](http://codeigniter.com/user_guide/database/transactions.html)
bobince
Oh. i see. then why people go for MyISAM? i don't mean that they shouldn't but what its advantages? just a few words would be nice. also i will check by searching.
Jayapal Chandran
1. It's the default for historical reasons (never underestimate the power of laziness); 2. it was regarded as typically faster than InnoDB (always contentious this one, and highly dependent on what kind of workload you've got. There may still be some places MyISAM is faster, but anything with a bunch of updates tends to gum quick quickly in MyISAM due to its unhelpful table-level locking); 3. sometimes you do need fulltext indexing, and don't want the alternatives like SphinxDB.
bobince
If i need to use commit/rollback then i have to use InnoDB. Is it good to use InnoDB for this purpose alone?
Jayapal Chandran
Transactions are just part of the data integrity features of InnoDB which are required parts of the ANSI SQL standard. MyISAM skimps on integrity which as far as I'm concerned is unequivocally a Bad Thing.
bobince
+1  A: 

if you use PDo, there is an transaction start, after that you can execute you four statements and then commit or roll back.

oezi
i missed to mention that i am using code igniter.
Jayapal Chandran
Engine is MyISAM. and i heard that transactions are possible only with InnoDB. could you please give me more suggestions regarding this and in need to refer the documentation to see how it fits in my project.
Jayapal Chandran