The syntax for using transactions in MySQL is a little different than with some other systems. But it does conform to the standard. For this lesson, we're going to be using the scratch database. And I'll show you how to maintain the integrity of your database by ensuring the related statements are completed successfully before they're committed to the database. And for this. I'm going to copy and paste some SQL from the example file, because this is just a lot of typing. And we don't need to sit here and type all of this.
So, I'm going to copy everything from these two CREATE statements all the way to this SELECT on line 21. Pressing Cmd+C on my Mac. You can use Ctrl+C and Ctrl+V on your PC. And so here we have a CREATE TABLE, it creates a table with three columns, a serial number, a description, and an on hand integer. And another table for sales. So this one's for inventory and this one's for sales.
You notice that it's connected with the inventory id for the item that's been sold, and it has quantity and price. So we'll insert three rows into inventory, rock, paper, and scissors, each with a quantity of 25 on hand. And then we'll just do a SELECT and then we'll do. Transaction after that. So we're going to press Go and here is our 6 queries performed, 3 rows, 3 rows returned and here is our 3 rows rock, paper and scissors with 25 on hand for each of them.
So now we're going to perform a transaction and I'm just going to select all of this and delete it and come back out here to the exercise files. And this is the transaction. I'm just going to copy and paste it along with these two select statements after it. And we'll paste this into the SID here, into the SQL box. So we have the START TRANSACTION statement, and that begins the transaction. Everything here must complete successfully before it's committed to the database.
So, then, we have the COMMIT statement here. And these two statements inside the transaction are the INSERT INTO the widget Sales and an UPDATE in the widget Inventory that subtracts 5 from the on hand. And so, I'm going to go ahead and press Go. And now you'll notice, in the widget sales table, we have sold 5 of inventory ID number 1, which is rock. And you notice our on hand has been updated to a smaller quantity of 20.
If I press this again, you see we get another sales line in the sales table and the on hand for rock is down another 5. So this is exactly what we expect. Now here's the interesting thing. Come out here to our exercise files and grab everything from here to here. So this is another transaction with a ROLLBACK instead of a command and two select statements. So I copy that and paste that in. In, here in SID and you notice we have this INSERT INTO widget inventory a new item toy and there's 25 of them.
But then I have a ROLLBACK statement instead of a commit statement so you would expect this not to happen and for our inventory and sales. Tables not to change at all, so I'm going to press Go and you can see our inventory and sales tables have not changed at all. There were 5 queries performed, 1 row affected but it was then rolled back. So, it was never committed to storage. So, this is what you can do programatically in a program or a script you can execute some SQL and if you find that you don't have everything that you need to complete the transaction you can simply rollback and whatever statements you have executed will not be committed to the database.
So this is how you can maintain the integrity of your database. By making sure that related statements, related queries, are all performed as a group or rolled back if they cannot be performed as a group. So let's go ahead and restore the database to our original state, and I'll go ahead and I'll just copy these two. DROP TABLE statements, and paste them in, and execute them. That drops our tables, and restores the database to it's original state. Transactions are a valuable database tool.
Using in them in MySQL is very simple. This can be a valuable technique for maintaining the integrity of related tables in your database.
- Writing queries
- Creating and updating databases and tables
- Using MySQL built-in functions
- Sorting and filtering data
- Updating tables with triggers
- Working with subselects and views
- Creating and using a stored function