Okay.
So we've made it through the C, r and you portions of crud.
Now we finally made it to DX, which stands for Delete or Destroy, depending on who you ask.
So in this video we're going to see how to delete things in my SQL, and it's pretty straightforward.
It's very similar to selecting things, except rather than just selecting something, you're going to
delete it.
So let me show you what I mean.
Here's an example that will delete any cat with the name egg in our database.
So imagine first of all that we were selecting.
All we would have is select from cats where name is equal to egg.
So if we ran that it would give us the cat or cats with name egg and we can swap out select with delete
and that will delete it.
So that same rule of thumb from before where I said make sure before you update something that your
selection is correct, then do the update because it will mean there's no undo button.
Unfortunately, same holds true and more so with delete because your data is gone as soon as you delete
it.
So let's try this.
We'll delete eg.
Let's do first of all select star from cats.
Just make sure we have egg in here.
There he or she is.
I'm not quite sure what I was intending with that name.
EG So what we'll do first is just make sure we can select appropriately before we delete.
So select star from cats where name equals egg.
Remember that this is case and sensitive just for now.
I'll do lowercase just to prove that.
All right, so we get egg.
Perfect.
So now to remove egg delete from and then we can just copy this.
So before I hit enter, I do need to take a moment to address something pretty serious.
And that is that no cats are being harmed.
No cats are dying here.
Yes, they're being deleted from this table.
But in reality, deletion doesn't mean death.
It means that they're being adopted, perhaps to a loving family that will treat them with the care
that they deserve.
So there's no death here, and I hope I don't receive any furious comments about how I'm treating animals
in my course.
I love animals.
I learned from my first course I made a mistake.
I will never refer to deleting dogs or cats as killing them.
I'm very sorry for that.
So again, these cats are not dying.
So with that out of the way, let's hit enter.
Now let's go back and select star from Cats.
And as you can see, Egg is now gone.
Also something that does come up sometimes.
Notice that the cat IDs have not shifted.
They're still the same.
Some of my students in the past have had the expectation that the IDs would alter, but they always
will stay the same.
And that's because when something is inserted into a table that unique ID is generated for it.
Remember, it's a primary key, and eventually we'll learn about having multiple tables that interact
with one another and we'll be storing these IDs in other tables.
So if it changed, that could be problematic because we would have to change it in multiple places.
Anyways, all I'm trying to say is that we removed four, but we still have five, six and seven.
This didn't change to four.
This didn't change to five and so on.
All right.
So we deleted that rest.
And I mean, enjoy your new life with your happy family egg.
So one last thing here is that if you look at what I've written here, delete from cats, what do you
think that will do now?
I don't run it, but what do you think this would do?
And the answer is that it deletes everything.
All cats.
They're all released to their happy families.
So that's important to know that you can do that.
So it's different than dropping a table because dropping a table gets rid of the table entirely and
all data inside of it.
But deleting from cats just deletes the entries in the cat's table.
But we still have the shell.
We still have the cat's table.
There shouldn't be very many times when you need to use this command.
I'm not going to do it just yet because I want us to have our cats data for a little bit for the next
exercises.
But just keep that in mind because we didn't specify a where clause over here.
It's the same as selecting without a where clause.
It selects everything, so this will delete everything.
Next up, you're going to practice deleting.