From the course: SQL Essential Training

Deleting data - SQL Tutorial

From the course: SQL Essential Training

Deleting data

- [Instructor] The final DML statement we're going to take a look at is the delete statement. Now the delete statement is used to remove existing records from a table, and just like the update, it is usually used with the where clause. Without the where clause, using a delete statement will delete every single record from your table. Now this is extremely important and again very important to include the where clause when using a delete statement. So let's demonstrate the use of a delete statement and remove our record that we have just inserted. So once again, we've gotten word from the management at WSDA Music that once again there has been a little bit of a mix up. They have now decided to remove altogether the newly added then modified artist Damien Marley from the database. Let's see how we would construct a DML statement to remove this record. Now we do need a few new keywords, starting with delete. So we specify the keyword delete. Next we need from, so with these two keywords then we can now specify the table we would like to delete data from. And in this case, we're targeting the artist table. So we specify the artist table. Now at this point, again, word of caution. If we were to run this statement, we would delete every single record from the entire artist table. And we do not want to do this, so we must include a where clause at this point. And I'm going to specify where, and we want to say where that artist ID is equal to 276. And if we weren't sure about this, we would go over to that browse data tab which is always worth the time to take a check to make sure you're deleting the right record and make sure you have the right id which is 276. I head over to execute. I put 276 there. And now I'm ready to run this statement, and I check for the message I want to see, which is that this query executed successfully without error. I verify the action of this delete by going over to the browse data. I can tell already that my total account is 275. It was just 276. If I go look for a record 276, it is no longer there. So our delete statement has worked, and we could report back to WSDA Music Management that we have now removed that newly added record.

Contents