Locked lesson.
About this lesson
We can use DELETE FROM to remove data from the SQL database
Exercise files
Download this lesson’s related exercise files.
DELETE Statement.docx59 KB DELETE Statement - Solution.docx
59.4 KB
Quick reference
DELETE Statement
DELETE FROM allows us to remove data from our table.
When to use
Use it whenever you want to remove something from your table using SQL.
Instructions
DELETE FROM [dbo].[Current_Customers]
WHERE [First Name] LIKE 'Tracy'
This will delete any customers with the first name 'Tracy' from the current customers database.
To delete all the data from your table, use Delete From without a Where Clause:
DELETE FROM [dbo].[Current_Customers]
Hints & tips
- Remove items from your table with DELETE FROM
- Remember to use WHERE
- If you don't use WHERE, it will delete all the data in your table
- 00:05 So we've learned how to insert stuff into our database,
- 00:08 we've learned how to update stuff.
- 00:09 Now, it's time to learn how to delete stuff from our table.
- 00:12 And to do that, we're gonna use the delete from statement.
- 00:15 And it's just as easy as the last two we've learned, we'll just type in DELETE,
- 00:19 FROM, and we have to designate our table.
- 00:23 So it's [dbo].[Current_Customers],
- 00:28 and then we give it a WHERE.
- 00:31 And like I said in the past, in the last video, normally,
- 00:35 we would have a customer ID for these, which we could designate,
- 00:38 delete customer ID number 7, and it would delete Tracy Smitherton.
- 00:42 We haven't done that in this specific table, so we need to be more specific.
- 00:46 So let's say, WHERE [First Name] is LIKE "Tracy".
- 00:53 So pretty simple, if we execute this, boom, Tracy Smitherton,
- 00:57 poor Tracy Smitherton is gone.
- 00:59 She has left us, so DELETE FROM in that.
- 01:02 Let's go ahead and I just pasted this insert, boom, she's back again so
- 01:06 that is DELETE FROM, very, very simple.
- 01:08 In fact, let's pull this back up, oops, "Tracy".
- 01:13 And one more thing I wanna talk about when it comes to DELETE FROM,
- 01:16 we've just deleted one record, right, so that's pretty simple.
- 01:20 What if we wanna delete all the records?
- 01:22 Well, we could just come over here and
- 01:24 right-click, And let's see, delete this whole thing.
- 01:29 We may not want to delete the entire table,
- 01:32 we might just wanna delete all the data that's in it.
- 01:35 And you can use DELETE FROM to do that, now I don't recommend you do this because,
- 01:38 in our case, we don't wanna get rid of all of our data.
- 01:41 But it's pretty easy, you just DELETE FROM, and
- 01:44 then give it the table name, shouldn't have deleted that, probably.
- 01:50 And that'll do it.
- 01:51 So you can also do DELETE FROM or you could do DELETE and
- 01:55 then give it a *, delete everything from this table.
- 02:00 It's sort of ubiquitous, you don't have to do that, but you can.
- 02:03 This will do the same thing.
- 02:04 If you DELETE FROM, it'll just recognize that you wanna
- 02:07 delete everything without you having to actually specify it.
- 02:10 So, like I said, don't do that unless you wanna delete all of the data,
- 02:14 all of the records in your table.
- 02:16 And like I said, you might wanna do that because you might wanna keep the actual
- 02:19 table itself.
- 02:20 You might wanna keep all the column headings, first name, last name,
- 02:24 email address, etc.
- 02:25 But you just wanna get rid of all the data that's in the column, and start over for
- 02:30 any other, any number of reasons the DELETE FROM will allow you to do that.
- 02:34 But for the most part,
- 02:35 you're probably gonna use this to just delete certain records in your table.
- 02:39 And just like with the update statement,
- 02:40 if you don't put that WHERE clause second, it'll delete everything.
- 02:46 Remember, with the update statement, if we don't put the WHERE clause and
- 02:49 it will update everything in the column, and we don't want that.
- 02:52 So you're always gonna wanna put your WHERE statements in there.
- 02:55 So that's DELETE.
- 02:56 In the next video, we'll look at the altar table statement.
Lesson notes are only available for subscribers.