New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Maintaining database integrity with transactions

From: MySQL Essential Training

Video: Maintaining database integrity with transactions

The syntax for using transactions in MySQL is And I'll show you how to maintain the integrity of your database by And now you'll notice, in the widget sales table, we So this is exactly what we expect.

Maintaining database integrity with transactions

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.

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 4689 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.