The final piece of our CRUD puzzle is RD for deletion, or actually some people think it's for destroy.
It doesn't matter.
They mean the same thing.
We need to learn how to delete rows from a table in SQL, so it's pretty straightforward to use delete
from and then some table name.
And then usually we're going to narrow it down because if we don't, if we did something like this,
we're going to delete every row from a table so we don't delete the table.
That would be done with drop table but delete from and then a whole table name with nothing else will
completely empty out the table.
It's an empty shell, a husk that contains no rows.
So let's start by deleting our cat named eg if we wanted to hear eg is he or she is a Persian cat four
years old.
Just for the sake of time.
I won't verify my selection.
I'll just do a straight up delete from.
I see a lot of students forget the from so I'll see like delete cat where something if I did name equals
eg it doesn't work.
We get a SQL syntax error.
It has to be delete from just like select from delete from and then cats where name equals eg and I'll
do a select star from cats.
We don't have egg anymore.
Goodbye.
EG.
I hope you were adopted and that's why you're gone.
Not because you passed away in our care.
And then as I said, if you do a delete from and then a table name, nothing else to narrow it down,
it works in the same way as if you didn't update with no where clause.
You update every row in the table.
But in this case we're deleting every row.
So why don't I work with employees?
Select star from employees.
We've got three rows.
If I do delete from employees just like that, well, we have no rows left, but the table still exists.
Right.
If I did describe employees, we don't get an error.
The table is here.
It's just totally empty.
So sometimes maybe you want to do that.
You want to clear out a table.
But more often than not, you're trying to delete some targeted portion of a table, some set of rows,
if not just a single row.
So just like with updates, I recommend selecting what you're trying to delete before you actually do
the delete.
Make sure that your where clause is selecting the things you expect.
Next up, delete exercise.