OK so we've made it through the C R and you portions of crud.
Now we finally made it to D which stands for delete or destroyed depending on who you ask.
So in this video we're going to see how to do things in my ask you will well and it's pretty straightforward.
It's very similar to selecting things except rather than just selecting something you're going to delete.
So let me show you what I mean.
Here's an example that will delete any cat with the name eg 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 you update something that your selection
is correct.
Then do the update because it will I 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 will delete.
Eg.
Let's do.
First of all it's like start 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 eg.
Remember that this is case insensitive.
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 Furius 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 he can see eg is now gone.
Also something that does come up sometimes.
Notice that the cat ID's 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 remembers
the primary key and eventually will learn about having multiple tables that interact with one another
and will be storing these ideas in other tables.
So if it changed that could be problematic because we'd have to change it in multiple places anyways.
I'm trying to say is that we removed 4 but we still have 5 6 and 7.
This didn't change to 4 and change to 5 and so on.
All right so we deleted that rest in.
I mean enjoy your new life with your happy family eg.
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 cats 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 cat's 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's like everything so this will delete everything.
OK next up you're going to practice deleting.