1
00:00:00,697 --> 00:00:02,560
- [Narrator] Hi, and
welcome back to the course.

2
00:00:02,560 --> 00:00:05,985
In this video we're going
to look at deleting items.

3
00:00:05,985 --> 00:00:10,289
Once again, it's going to be
the same as the create item.

4
00:00:10,289 --> 00:00:12,777
Only instead of inserting into a database,

5
00:00:12,777 --> 00:00:16,915
we are going to be
deleting from a database.

6
00:00:16,915 --> 00:00:18,248
Let's go for it.

7
00:00:19,147 --> 00:00:23,079
Our current delete method takes
in the global variable items

8
00:00:23,079 --> 00:00:26,377
which, as you remember,
was our list of items,

9
00:00:26,377 --> 00:00:28,964
and then essentially filtered a name out.

10
00:00:28,964 --> 00:00:31,161
Filter the, an element out where

11
00:00:31,161 --> 00:00:34,109
the name didn't match the names.

12
00:00:34,109 --> 00:00:36,537
Or rather, filtered in all of the elements

13
00:00:36,537 --> 00:00:38,008
where the name didn't match the name.

14
00:00:38,008 --> 00:00:40,530
So essentially, ended
up with an item less.

15
00:00:40,530 --> 00:00:42,219
We're no longer gonna do that.

16
00:00:42,219 --> 00:00:44,802
Instead what we're gonna do is,

17
00:00:46,090 --> 00:00:47,759
you guessed it,

18
00:00:47,759 --> 00:00:49,121
we're gonna copy this in this case

19
00:00:49,121 --> 00:00:51,533
because I don't wanna bore you too much.

20
00:00:51,533 --> 00:00:53,412
We're gonna initialise a connection,

21
00:00:53,412 --> 00:00:55,838
create a cursor, and then create a query.

22
00:00:55,838 --> 00:00:59,701
The query is going to delete from a table.

23
00:00:59,701 --> 00:01:03,150
So, you guessed it, delete from.

24
00:01:03,150 --> 00:01:05,015
We're gonna delete from items,

25
00:01:05,015 --> 00:01:07,500
but we're not gonna
delete specific values,

26
00:01:07,500 --> 00:01:09,491
that's only used with columns,

27
00:01:09,491 --> 00:01:13,170
we're gonna delete from
items where the name

28
00:01:13,170 --> 00:01:15,587
is equal to a specific value.

29
00:01:16,855 --> 00:01:18,072
Okay?

30
00:01:18,072 --> 00:01:19,572
And the name equal

31
00:01:22,038 --> 00:01:22,871
the name

32
00:01:24,291 --> 00:01:26,458
that is passed in the URL.

33
00:01:27,817 --> 00:01:31,907
Now this part here of the where
clause is really important,

34
00:01:31,907 --> 00:01:33,432
really, really important.

35
00:01:33,432 --> 00:01:35,968
Because if you skip it, or
you forget to type it in,

36
00:01:35,968 --> 00:01:38,307
that deletes everything from the table.

37
00:01:38,307 --> 00:01:42,041
And you most likely don't
want to do that, okay?

38
00:01:42,041 --> 00:01:46,120
So make sure to have a where
clause that is very specific.

39
00:01:46,120 --> 00:01:50,257
The name, in our case,
is unique to each row.

40
00:01:50,257 --> 00:01:53,217
So if we delete where the
name is equal to something,

41
00:01:53,217 --> 00:01:57,286
we know we're only ever
gonna delete one row.

42
00:01:57,286 --> 00:01:59,712
You could do other things.

43
00:01:59,712 --> 00:02:02,211
For example imagine you
have a table of students.

44
00:02:02,211 --> 00:02:06,378
You could delete where the
age is greater than 15, say.

45
00:02:07,729 --> 00:02:10,464
And then that could delete
many, many students.

46
00:02:10,464 --> 00:02:13,082
So that's usually not the way
you want to delete things.

47
00:02:13,082 --> 00:02:15,947
It may be, but usually it isn't.

48
00:02:15,947 --> 00:02:18,850
Normally you want to
delete a specific element,

49
00:02:18,850 --> 00:02:21,930
and that's how you make
sure that you're doing that.

50
00:02:21,930 --> 00:02:26,097
Make sure that the where is
using that unique column.

51
00:02:28,527 --> 00:02:32,110
Okay, that's it, for this video really.

52
00:02:32,110 --> 00:02:35,713
Let's save that, and go over to Postman.

53
00:02:35,713 --> 00:02:39,751
And once again we are
going to create an item,

54
00:02:39,751 --> 00:02:42,334
which I think I have done here.

55
00:02:43,186 --> 00:02:44,852
We're going to create the item.

56
00:02:44,852 --> 00:02:46,718
Oh, it already exists,
because we've not changed

57
00:02:46,718 --> 00:02:48,660
the date or gone DB, so now what we can do

58
00:02:48,660 --> 00:02:52,827
is go over to the delete
endpoint, and put test in it.

59
00:02:53,827 --> 00:02:56,457
And now we get back item deleted.

60
00:02:56,457 --> 00:02:59,286
Now, there's no way for us to check,

61
00:02:59,286 --> 00:03:00,619
unless we go and

62
00:03:02,382 --> 00:03:04,299
get that specific item.

63
00:03:05,665 --> 00:03:06,498
What do you think's gonna happen

64
00:03:06,498 --> 00:03:10,132
when I do this, when I press send?

65
00:03:10,132 --> 00:03:11,632
Take a wild guess.

66
00:03:14,674 --> 00:03:17,447
Ah-ha! I'm sure you got
that, because I just opened

67
00:03:17,447 --> 00:03:20,199
this get endpoint new, so it doesn't have

68
00:03:20,199 --> 00:03:22,677
the most recent header,
so what we have to do

69
00:03:22,677 --> 00:03:26,844
is go to authenticate again
and make sure to copy that.

70
00:03:27,729 --> 00:03:29,416
In a later video I'm
gonna teach you a trick,

71
00:03:29,416 --> 00:03:31,597
so you don't have to do this every time.

72
00:03:31,597 --> 00:03:33,578
But, for now let's leave it as this.

73
00:03:33,578 --> 00:03:35,976
Let's go back to the get a specific item,

74
00:03:35,976 --> 00:03:40,526
and make sure in the header
to put your JWT space

75
00:03:40,526 --> 00:03:43,819
and your access token, and now we can see

76
00:03:43,819 --> 00:03:48,198
that item is not found
because we have deleted it.

77
00:03:48,198 --> 00:03:50,930
Okay, so that's everything
for this video then.

78
00:03:50,930 --> 00:03:53,673
How to delete item is
just the same as always,

79
00:03:53,673 --> 00:03:55,989
but deleting from the table

80
00:03:55,989 --> 00:03:58,777
instead of inserting or selecting.

81
00:03:58,777 --> 00:03:59,726
Hope you enjoyed it, and

82
00:03:59,726 --> 00:04:01,507
I'll see you in the very next video.

