Guide to Deleting Records from a SQL Database
This lesson walks through how to delete records from a MySQL database. Additionally, you'll learn how to add BEGIN and ROLLBACK commands to allow you to revert back to a previous state.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, we're going to discuss how to remove records from a database. First thing I'm going to do is find a record that we want to delete! SELECT * FROM users WHERE users_ID =199 I'm going to run this and this brings up a record with the users_id that is 199 with their username and their email address.

large

Now if I want to remove this user I can use the “delete command”. Now if you remember back to our edit command guide. I reminded you that many times if you're running a new script it's a good idea to wrap it in a “begin” and “rollback” block. That allows us to reverse anything that we weren't really intending to do. I’m going to say DELETE FROM users WHERE users_id = 199. Then I'm going to add a rollback command here.

large

So we already saw that we were able to query for this user. Now if I run this command it's going to run it and it looks like that worked.
Now if I run this query again it runs.

large

You can see there are no values because there is no user id of 199. Now if I want to push this back. I can high roll back and run it.

large

Then I can come back here to run the query again.

large

You can see that this is back to working. This is a pretty basic one. This one of the easier things to do in a SQL database. It is easy to delete a record from a database or to delete it using the delete command. So all you have to say is DELETE FROM pass in a WHERE condition and you will be good to go.

Code

USE devcamp_sql_course_schema;

SELECT *
FROM users
WHERE users_id = 199;

BEGIN;
DELETE FROM users
WHERE users_id = 199;

ROLLBACK;