1 00:00:00,090 --> 00:00:07,440 The final piece of our CRUD puzzle is RD for deletion, or actually some people think it's for destroy. 2 00:00:07,470 --> 00:00:08,100 It doesn't matter. 3 00:00:08,100 --> 00:00:09,180 They mean the same thing. 4 00:00:09,390 --> 00:00:16,170 We need to learn how to delete rows from a table in SQL, so it's pretty straightforward to use delete 5 00:00:16,170 --> 00:00:18,750 from and then some table name. 6 00:00:18,750 --> 00:00:24,120 And then usually we're going to narrow it down because if we don't, if we did something like this, 7 00:00:24,120 --> 00:00:28,890 we're going to delete every row from a table so we don't delete the table. 8 00:00:29,130 --> 00:00:36,060 That would be done with drop table but delete from and then a whole table name with nothing else will 9 00:00:36,060 --> 00:00:37,680 completely empty out the table. 10 00:00:37,680 --> 00:00:41,520 It's an empty shell, a husk that contains no rows. 11 00:00:41,880 --> 00:00:50,100 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 12 00:00:50,100 --> 00:00:50,850 years old. 13 00:00:51,930 --> 00:00:52,890 Just for the sake of time. 14 00:00:52,890 --> 00:00:54,600 I won't verify my selection. 15 00:00:54,600 --> 00:00:56,670 I'll just do a straight up delete from. 16 00:00:56,670 --> 00:01:05,670 I see a lot of students forget the from so I'll see like delete cat where something if I did name equals 17 00:01:05,940 --> 00:01:08,580 eg it doesn't work. 18 00:01:08,580 --> 00:01:10,380 We get a SQL syntax error. 19 00:01:10,380 --> 00:01:23,310 It has to be delete from just like select from delete from and then cats where name equals eg and I'll 20 00:01:23,310 --> 00:01:25,860 do a select star from cats. 21 00:01:26,520 --> 00:01:28,200 We don't have egg anymore. 22 00:01:28,230 --> 00:01:28,740 Goodbye. 23 00:01:28,740 --> 00:01:29,190 EG. 24 00:01:29,190 --> 00:01:31,800 I hope you were adopted and that's why you're gone. 25 00:01:31,980 --> 00:01:34,200 Not because you passed away in our care. 26 00:01:34,770 --> 00:01:40,260 And then as I said, if you do a delete from and then a table name, nothing else to narrow it down, 27 00:01:40,260 --> 00:01:44,280 it works in the same way as if you didn't update with no where clause. 28 00:01:44,280 --> 00:01:45,960 You update every row in the table. 29 00:01:45,960 --> 00:01:48,420 But in this case we're deleting every row. 30 00:01:48,420 --> 00:01:50,370 So why don't I work with employees? 31 00:01:50,640 --> 00:01:53,190 Select star from employees. 32 00:01:55,490 --> 00:01:56,820 We've got three rows. 33 00:01:56,840 --> 00:02:06,480 If I do delete from employees just like that, well, we have no rows left, but the table still exists. 34 00:02:06,500 --> 00:02:06,950 Right. 35 00:02:06,950 --> 00:02:10,190 If I did describe employees, we don't get an error. 36 00:02:10,220 --> 00:02:11,330 The table is here. 37 00:02:11,330 --> 00:02:12,680 It's just totally empty. 38 00:02:13,160 --> 00:02:15,470 So sometimes maybe you want to do that. 39 00:02:15,470 --> 00:02:16,880 You want to clear out a table. 40 00:02:16,880 --> 00:02:22,160 But more often than not, you're trying to delete some targeted portion of a table, some set of rows, 41 00:02:22,160 --> 00:02:24,020 if not just a single row. 42 00:02:24,590 --> 00:02:30,560 So just like with updates, I recommend selecting what you're trying to delete before you actually do 43 00:02:30,560 --> 00:02:31,250 the delete. 44 00:02:31,250 --> 00:02:34,580 Make sure that your where clause is selecting the things you expect. 45 00:02:35,480 --> 00:02:37,850 Next up, delete exercise.