Removing Data with SQL Delete, Truncate, and Drop

Tyler Clark
InstructorTyler Clark
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

Deleting data within our table is easily done by using ether the delete, truncate, or just dropping the table altogether from our database. Let’s remove a user from our table using the delete command, remove everyone with truncate, then drop the entire table with the drop command.

Instructor: [00:00] Our users table currently has two rows of data, one for Danny Clark and a Debbie Jones. When we need to actually remove rows from our tables, instead of just updating column data, we'll use the DELETE command. If we don't put a WHERE clause with our statement here, it's going to loop over each row over our table and delete everything.

[00:21] Make sure, before running the DELETE command on your table, you have a conditional clause that targets only the rows you want to delete. After defining which table we're removing rows from, we're saying, delete all the rows where the last name column has a value of the text Clark.

[00:37] We're also able to add other combinations of conditions here within the WHERE clause. For example, we could say, where the last name is Clark and the first name is Danny, or even use the OR statement here, as well.

[00:49] The key point here is, you want to make sure you're targeting the correct rows of data when deleting so that you don't actually lose the wrong rows. If you wanted to delete everything in our table, I mentioned you could just use the DELETE command without a condition.

[01:02] However, using TRUNCATE is a more pro forma way to do so. Unlike the DELETE command, it doesn't scan over the entire table. We also have the ability to truncate more than one table with just one statement by listing the other tables with commas.

[01:18] Finally, if we're trying to remove the table completely from our database, we use the DROP command. Once we run this command, all of the data is deleted and we cannot query anything from this table.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today