1
00:00:00,090 --> 00:00:04,150
Next up, we're going to focus our time on learning to create data.

2
00:00:04,170 --> 00:00:08,220
How do we add or insert data into the tables that we've already made?

3
00:00:08,580 --> 00:00:14,520
This is another one of the most important and most commonly used commands when you're working with databases.

4
00:00:14,850 --> 00:00:19,200
Obviously, it's really important to have a way to get your data in the database.

5
00:00:19,470 --> 00:00:22,970
So we'll be using what we learn here throughout the rest of the course.

6
00:00:22,980 --> 00:00:27,840
And you can say that for everything else that we've learned so far, but in particular, this command

7
00:00:28,260 --> 00:00:30,360
insert or insert into.

8
00:00:31,590 --> 00:00:39,390
So the syntax here is insert into and then the name of the table cats in our case.

9
00:00:40,080 --> 00:00:41,700
And then it gets a little weird after that.

10
00:00:41,700 --> 00:00:45,570
So let's just focus on that first insert into and then the table name.

11
00:00:46,140 --> 00:00:52,680
Then afterwards in parentheses, we're specifying the columns that we are going to insert.

12
00:00:52,680 --> 00:00:57,120
So I'm saying I'm going to give you a name comma, I'm going to give you an age.

13
00:00:57,120 --> 00:01:03,960
And then down here I have values, which is another keyword specifying that I told you I'm giving you

14
00:01:03,960 --> 00:01:04,769
a name and an age.

15
00:01:04,769 --> 00:01:05,610
Here they are.

16
00:01:06,000 --> 00:01:09,480
The name is Jetson and the age is seven.

17
00:01:09,840 --> 00:01:11,400
So these do need to match.

18
00:01:11,400 --> 00:01:13,260
Name is first Jetson.

19
00:01:13,260 --> 00:01:14,790
Age a second seven.

20
00:01:15,000 --> 00:01:20,250
Before we go, try it out in cloud nine, I just want to show you that I wrote it this way, mainly

21
00:01:20,250 --> 00:01:22,020
because it looks best on a slide.

22
00:01:22,620 --> 00:01:24,120
You can do it on one line.

23
00:01:24,120 --> 00:01:32,130
So insert into cat's name and age the values Jetson and seven or you can do it like this and this is

24
00:01:32,130 --> 00:01:34,950
actually a lot of people's preferred way of writing it.

25
00:01:34,950 --> 00:01:40,530
But this is a huge pain to enter into the shell this way where you have to space things manually.

26
00:01:40,530 --> 00:01:46,950
But when we start working with individual SQL files where we'll save our own code basically into these

27
00:01:46,950 --> 00:01:50,520
separate files in a text editor and we can space them out how we want.

28
00:01:50,670 --> 00:01:55,560
This is definitely a preferable way, but we're going to work with this.

29
00:01:55,560 --> 00:01:58,290
I just like this when I'm presenting and when I'm teaching it.

30
00:01:58,290 --> 00:02:03,510
It's a mix of readability, but also it's not so laborious like this.

31
00:02:04,860 --> 00:02:11,460
And just to hit this point home, the order that we write these column names matters.

32
00:02:12,120 --> 00:02:19,470
So if I say insert into cats and I say Age is coming first and then name, I have to provide age first

33
00:02:19,470 --> 00:02:20,640
and then name.

34
00:02:21,090 --> 00:02:26,760
So it doesn't matter which order you use, but whatever you establish up here you have to follow through

35
00:02:26,760 --> 00:02:27,240
with.

36
00:02:27,330 --> 00:02:28,860
Otherwise it will try.

37
00:02:28,860 --> 00:02:31,800
If you had this reversed, it would try and aside.

38
00:02:32,070 --> 00:02:35,760
It would try and assign Victoria to age and 12 to name.

39
00:02:35,880 --> 00:02:40,470
And that's going to be a problem because we have these strict data types where things are supposed to

40
00:02:40,470 --> 00:02:42,600
be numbers and strings.

41
00:02:42,930 --> 00:02:44,910
And so if we mix them up, it's problematic.

42
00:02:45,270 --> 00:02:46,950
Not to mention that it makes no sense.

43
00:02:47,070 --> 00:02:49,080
So let's give it a shot in cloud nine.

44
00:02:50,610 --> 00:02:51,990
I'll make some space here.

45
00:02:52,470 --> 00:02:59,280
And the first thing we need to do is actually recreate a table, because right now I don't have a cat's

46
00:02:59,280 --> 00:02:59,730
table.

47
00:02:59,730 --> 00:03:02,340
I deleted it because I showed you how delete works.

48
00:03:02,490 --> 00:03:14,790
So let's recreate that create table cat and it's going to have name so far char 50 comma age is going

49
00:03:14,790 --> 00:03:17,010
to be an int just like that.

50
00:03:18,040 --> 00:03:18,820
Perfect.

51
00:03:19,300 --> 00:03:22,570
So now we'll use the insert into command we just learned.

52
00:03:22,570 --> 00:03:34,030
So we will insert a new cat insert into cat and I'll start with name and then H and then I'll go to

53
00:03:34,030 --> 00:03:34,720
a new line.

54
00:03:34,720 --> 00:03:41,920
I don't have to, but I like to values and the values will put in, let's say for name.

55
00:03:41,920 --> 00:03:45,640
Let's work with my cat, blue comma.

56
00:03:46,150 --> 00:03:48,390
And her age is somewhere between one and two.

57
00:03:48,400 --> 00:03:51,280
We'll just leave it as one semicolon.

58
00:03:51,280 --> 00:03:52,180
We'll hit enter.

59
00:03:53,020 --> 00:03:54,310
We get our query.

60
00:03:54,310 --> 00:03:55,010
Okay.

61
00:03:55,420 --> 00:03:57,460
This time it says one row affected.

62
00:03:57,460 --> 00:04:02,680
You may have noticed that everything else we've done so far has said zero rows affected and I think

63
00:04:02,680 --> 00:04:04,000
that's pretty self explanatory.

64
00:04:04,000 --> 00:04:09,730
We haven't had any rows of data yet, so all of these commands have returned, zero rows affected.

65
00:04:09,730 --> 00:04:12,430
Now we actually have created something.

66
00:04:12,430 --> 00:04:16,329
We've added data to a table and it says one row affected.

67
00:04:16,810 --> 00:04:20,079
And before we move on, let's insert one more cat here.

68
00:04:20,649 --> 00:04:27,610
And this time we'll switch up the order, insert into cats and I'll do it on one line, AJJ, comma,

69
00:04:27,610 --> 00:04:30,340
name and then our values.

70
00:04:30,730 --> 00:04:38,950
Let's say the age for this cat will be 11 and the name will be Draco.

71
00:04:40,300 --> 00:04:40,990
All right.

72
00:04:42,670 --> 00:04:45,070
And we also get one row affected.

73
00:04:45,070 --> 00:04:49,330
So the next logical question might be, how do I see the data?

74
00:04:49,330 --> 00:04:51,250
How do I know that it's in there correctly?

75
00:04:51,250 --> 00:04:55,150
Basically, the same thing that you might have asked when we saw how to create a table.

76
00:04:55,150 --> 00:04:56,500
How do we check our work?

77
00:04:56,500 --> 00:04:58,780
Well, we've created data in a table.

78
00:04:58,780 --> 00:04:59,920
How do we check our work?

79
00:05:00,280 --> 00:05:01,960
And we'll see that in the next video.

80
00:05:01,960 --> 00:05:03,010
Sorry for the cliffhanger.

