Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Querying Relational Databases Database Keys Foreign Keys

Noah Fields
Noah Fields
13,985 Points

What happens when you do a Transaction with an error in it?

Let's say that I created a Transaction on a database with primary and foreign keys, and the Transaction inserted many rows - five, ten, or hundreds - of which some, but not all, had errors. For instance, some of the additions had primary key values that were already taken, or a foreign key referencing a primary key that didn't exist. When uploading all the changes specified in the Transaction, would it add the values that were correct and send errors for the incorrect ones? Would none of the changes take place until the errors were corrected? Or would something else occur?

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya Noah! Query wont happen at all. And If a TRANSACTION is successful, all of the data modifications made during the TRANSACTION are committed and become a permanent part of the database. If a TRANSACTION encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

In your script file, you've to put "BEGIN", short for Begin Transaction, to let SQL server know that the following query needs to be executed in TRANSACTION mode. And followed by "COMMIT", to commit and finalize all your changes if everything happens successfully.

I hope it helped. (:

Teacher Russell
Teacher Russell
16,873 Points

I just wanted to add that this topic is covered in the previous course, for anyone else who comes along and reads this. https://teamtreehouse.com/library/modifying-data-with-sql