SQL 101 – DELETE

It looks like I’m getting close to the end of my 101 series.  After I show you how to remove data from tables, the last topic I plan to cover is creating tables.  It’s been quite a journey, collecting all this knowledge and putting it out there for you.  I had hoped to get some feedback by this point to know if I’m heading in the right direction on these articles.  For now, I’ll get these two articles out, then try and figure out what topics remain to be covered.

Now back to our regularly scheduled post.

DELETE

When you work in SQL, you’ll eventually come across data you want to get rid of.   I’m going to side-step the topic of version control, and backups for now.  When you get to that day, you’ll need to learn how to use the DELETE command.

DELETE FROM tableName WHERE column = 'value'

Basically you call DELETE FROM then enter the table you want to delete from.  The WHERE clause here is as important as it was for the UPDATE statement.  If you don’t include the WHERE clause, you’ll end up deleting every row in your table, and you don’t want to do that, do you?

This brings me back to a set up steps nearly identical to those I shared in the UPDATE arcticle.

  1. SELECT the delete.  Before running an delete,  run a SELECT statement with your where clause to make sure you’re only getting the records you expect.
  2. BEGIN a transaction.
  3. Run the DELETE statement
  4. SELECT the changes, verify you only deleted the records you think you did.
  5. COMMIT the transaction.

These six steps will make it far less likely to DELETE something you don’t mean to.  There is nothing to prevent you from deleting or updating something you don’t mean to.  SQL isn’t like that.  There is no prevention, only insurance.  Transactions are one form of insurance, having a bulletproof backup and recovery system is the other.

The DELETE command is very simple.  You can get fancy with it, and I’ll show you that later.  For now, get comfortable with this command.  Practice with it on a test server (or your local machine), and soon you’ll be ready for more!

If you have any questions, please send them in!  I can only teach you what you want to know if you tell me what that is.  I look forward to your comments!

Previous: TRANSACTION

Tags: , ,

4 Responses to “SQL 101 – DELETE”

  1. Allen 20130731 at 09:50 #

    This has been a good overview of the SQL basics. I am looking forward to reading the rest of these training posts. I am new to SQL and the IT field with very little time in the seat so far. So far your site has been pretty helpful. I will be telling others that are interested in learning SQL about this site. I feel it will help them in understanding the basics and also with the more advanced rules. Thanks for your time invested in helping to expand the knowledge of us that are just starting out on the path.

    • Shannon Lowder 20130731 at 22:18 #

      Please let me know if there are any topics you can’t find here. I’ll add them for you!

Trackbacks/Pingbacks

  1. SQL 101 | Shannon Lowder - 20110202

    [...] DELETE — removing data from your database [...]

  2. SQL201-The Syllabus | Shannon Lowder - 20110809

    [...] It’s that time again!  Time to start up a new class and cover some more advanced topics in SQL Development.  Before beginning these lessons, You need to be comfortable with all the material in my SQL101 series.  Specifically you’re going to need to know SELECT, INSERT, UPDATE, and DELETE. [...]

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.