Is it possible to insert a row into multiple tables at once? If you do several tables related by an ID; what is the best way to ensure integrity is maintained in case an INSERT fails?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s exactly what transactions are for. If any of the commands fail, the whole thing since
START TRANSACTIONis rolled back:This being MySQL, you can’t use transactions with MyISAM tables (you’ll need the tables to use some engine that supports this, probably InnoDB).
This will never be inserted into the table (normally you’d have some branching, e.g. an IF):
Caveat: SQL commands which change the database structure (e.g.
CREATE,ALTER,DROP) cannot be rolled back!