1
00:00:00,290 --> 00:00:01,480
All right we'll come back.

2
00:00:01,530 --> 00:00:04,630
It's time for us to start working with databases interact locations.

3
00:00:04,770 --> 00:00:09,270
And before we actually write code I just want to take a few minutes here so we'll be pretty quick to

4
00:00:09,270 --> 00:00:14,850
talk about what databases actually are and then to introduce two broad categories of databases which

5
00:00:14,850 --> 00:00:18,690
are sequel databases or Escudo versus no sequel or no.

6
00:00:18,690 --> 00:00:20,120
Q All databases.

7
00:00:20,720 --> 00:00:21,060
OK.

8
00:00:21,060 --> 00:00:26,160
So let's begin here and to start I want to illustrate the problem that we have right now.

9
00:00:26,250 --> 00:00:32,340
I have the POST request demo running this friends up where I can list all my friends and we can add

10
00:00:32,340 --> 00:00:33,410
some new friends.

11
00:00:33,600 --> 00:00:39,450
I just made a new one today named Garfield and another friend named her mine.

12
00:00:40,550 --> 00:00:41,120
OK.

13
00:00:41,310 --> 00:00:48,780
And then I can go back here and if I shut off the server and started again and I refresh my page.

14
00:00:49,340 --> 00:00:51,520
Gosh you just lost two friends.

15
00:00:51,570 --> 00:00:57,240
So the problem that we're facing is that our data doesn't persist if our server stops for any reason

16
00:00:57,690 --> 00:00:59,430
whether it's because we turned it off.

17
00:00:59,430 --> 00:01:00,390
We wanted to restart it.

18
00:01:00,390 --> 00:01:06,360
We made some changes to the code or if it's because the power goes out or some freak event happens if

19
00:01:06,360 --> 00:01:12,990
the server stops we would lose all data which is obviously a really really bad thing for a web application

20
00:01:13,010 --> 00:01:13,320
.

21
00:01:13,600 --> 00:01:18,450
If Facebook lost your data every time you logged out or every time the server stopped that would be

22
00:01:18,450 --> 00:01:21,700
really really bad for Facebook although it might be pretty good for me.

23
00:01:21,840 --> 00:01:22,980
I'd be productive.

24
00:01:23,040 --> 00:01:27,840
I stopped comparing myself to my friends at Stop trying to prove to everyone that I live a fun life

25
00:01:28,050 --> 00:01:29,530
and I just get stuff done.

26
00:01:29,940 --> 00:01:33,940
So all of this is to say that databases will help us with this problem.

27
00:01:34,230 --> 00:01:38,950
So let's talk about what databases are in the simplest possible definition.

28
00:01:38,950 --> 00:01:47,160
The database is a collection of information data but a database isn't just a collection of information

29
00:01:47,160 --> 00:01:47,480
.

30
00:01:47,490 --> 00:01:53,910
So if we had a file just a text file and sublime that had a bunch of information and saved it that wouldn't

31
00:01:53,910 --> 00:01:55,100
be a database.

32
00:01:55,380 --> 00:01:57,100
Yes it might store data for us.

33
00:01:57,120 --> 00:02:02,100
It might be a collection of information but the other important aspect of databases is that they have

34
00:02:02,100 --> 00:02:08,130
an interface for interacting with that data whether it's adding new information like adding a new user

35
00:02:08,130 --> 00:02:14,550
to the database or if it's removing all users or editing an existing user or whatever it is there needs

36
00:02:14,550 --> 00:02:16,830
to be a way for us to interact with the data.

37
00:02:16,890 --> 00:02:22,740
So a database is a collection of information slushed data and it has an interface.

38
00:02:23,160 --> 00:02:27,350
When I say interface I mean that we can write code to interact with it.

39
00:02:27,360 --> 00:02:33,470
So if you've ever worked with a sequel database before you might have seen things like this select star

40
00:02:34,090 --> 00:02:36,340
I should capitalize this.

41
00:02:37,820 --> 00:02:41,270
It's like star from users.

42
00:02:41,280 --> 00:02:44,630
Or you might see something like insert blah blah blah into.

43
00:02:44,730 --> 00:02:51,930
And this is a language called sequel and this is the interface to some databases the language that we'll

44
00:02:51,930 --> 00:02:53,730
be using looks like this.

45
00:02:53,910 --> 00:03:08,820
D-B dot dogs dot find or D-B dot dogs dot delete and will delete the dogs where we have age just 14

46
00:03:08,830 --> 00:03:08,950
.

47
00:03:09,060 --> 00:03:13,430
Unfortunately all my dogs have died at that age and I want to talk about it and I'll delete all dogs

48
00:03:13,430 --> 00:03:14,790
where age is 14.

49
00:03:15,210 --> 00:03:20,070
We'll get into the specifics of the language in the next video but I just want you to be aware.

50
00:03:20,160 --> 00:03:24,690
This is what I mean when I say that there's an interface to the collection of data slash information

51
00:03:24,700 --> 00:03:25,270
.

52
00:03:25,830 --> 00:03:26,400
OK.

53
00:03:26,400 --> 00:03:31,950
So the last thing I want to do is talk about the two broad categories or two of the broad categories

54
00:03:31,950 --> 00:03:38,240
of databases and those are sequel which is also called relational databases versus no.

55
00:03:38,250 --> 00:03:43,830
As you well know sequel which are called non-relational databases when I'm talking to my students about

56
00:03:43,830 --> 00:03:49,830
databases a lot of them have had some experience with some sort of database whether they were in marketing

57
00:03:49,830 --> 00:03:52,130
or retail or something online.

58
00:03:52,200 --> 00:03:57,810
Pretty much every company out there has some database usually lots of databases with lots of information

59
00:03:57,810 --> 00:04:05,010
in it whether it's a database for all of the Skewes in the gap or a database for payroll of all the

60
00:04:05,010 --> 00:04:09,870
employees and what they've been paid and when or if it's a bunch of data on users like Facebook has

61
00:04:10,140 --> 00:04:14,700
where it's storing all of your user information on your photos your comments your posts your likes your

62
00:04:14,700 --> 00:04:17,420
tags all that stuff is stored in databases.

63
00:04:17,430 --> 00:04:22,470
When I talk to students about databases the students who are familiar and have experience with databases

64
00:04:22,830 --> 00:04:30,120
almost always only have experience with sequel or relational databases and that's because sequel databases

65
00:04:30,180 --> 00:04:34,950
have been around the longest and they're what most people think of when they think of a database.

66
00:04:35,280 --> 00:04:38,970
So now let's talk about what sequel data bases what relational means.

67
00:04:39,270 --> 00:04:43,740
So sequel databases are tabular databases and they're flat.

68
00:04:43,830 --> 00:04:45,120
Let me show you what I mean.

69
00:04:45,120 --> 00:04:52,290
So I have some crude diagrams that I typed up and this is showing what the tables for a database involving

70
00:04:52,290 --> 00:04:54,530
users and comments could look like.

71
00:04:54,540 --> 00:04:57,760
Let's just start by focusing on the users.

72
00:04:57,840 --> 00:05:04,110
So what we have to do in a relational database or Seacole database is we define what a user looks like

73
00:05:04,120 --> 00:05:04,320
.

74
00:05:04,500 --> 00:05:12,840
So one user has an ID as a name and age and a city and then every single user that we add must follow

75
00:05:12,840 --> 00:05:13,520
that pattern.

76
00:05:14,420 --> 00:05:20,280
So we're defining these tables and then we're adding instances of the user or of users to this table

77
00:05:20,840 --> 00:05:23,780
and then let's suppose I also have comments.

78
00:05:23,840 --> 00:05:26,220
So I want a user to be able to comment.

79
00:05:26,330 --> 00:05:33,830
So I have a comments table and all that a comment has is an ID and some text of the comment like EHLO

80
00:05:33,830 --> 00:05:41,030
or come visit Montana or I love puppies if I want there to be a relationship between users and comments

81
00:05:41,600 --> 00:05:45,130
where a user can have a comment those associated with it.

82
00:05:45,320 --> 00:05:51,370
So Ira I hear who's 24 from Missoula can be associated with some of these comments here.

83
00:05:51,410 --> 00:05:55,160
Like come visit Montana and seriously Montana is great.

84
00:05:55,160 --> 00:06:00,590
The only way to do that to express this relationship is through another table which is what we have

85
00:06:00,590 --> 00:06:01,120
here.

86
00:06:01,330 --> 00:06:03,020
And these are called join tables.

87
00:06:03,170 --> 00:06:08,450
And what this table does is it joins a user's ID with a comment Id.

88
00:06:08,450 --> 00:06:14,700
So in this case we see that the user with ID one owns the comment with ID 3.

89
00:06:14,930 --> 00:06:24,560
So that means that Tim said that I love puppies and the user with the idea of two commented twice and

90
00:06:24,560 --> 00:06:27,270
those comments or idea of to an idea for.

91
00:06:27,560 --> 00:06:33,080
So we can see that IRA commented come visit Montana and also seriously.

92
00:06:33,080 --> 00:06:34,360
Montana is great.

93
00:06:34,370 --> 00:06:36,470
And yes 110 is great.

94
00:06:36,500 --> 00:06:39,740
What I really want to show with all of this is that everything is tabular.

95
00:06:39,800 --> 00:06:44,020
So we have to define a table ahead of time and its not very flexible at all.

96
00:06:44,120 --> 00:06:48,830
Lets suppose I wanted to add another attribute to IRA that was favorite color and I wanted to set it

97
00:06:48,830 --> 00:06:50,590
to be purple.

98
00:06:50,690 --> 00:06:54,240
I would then have to go an add favorite color for everyone.

99
00:06:54,240 --> 00:07:01,040
Let's just call it a fav color and it would have to be empty so I'd have to have Noel or undefined or

100
00:07:01,040 --> 00:07:05,900
nyl or just false or something here for every single person.

101
00:07:06,020 --> 00:07:07,450
So it's not flexible.

102
00:07:07,530 --> 00:07:13,010
What I have to do is define exact patterns of what a user looks like and then I need to follow that

103
00:07:13,010 --> 00:07:15,300
pattern very closely.

104
00:07:15,410 --> 00:07:22,760
So the other type of database non-relational databases or no as well databases and you probably catch

105
00:07:22,760 --> 00:07:23,900
them going here.

106
00:07:23,900 --> 00:07:26,240
We don't have to define patterns ahead of time.

107
00:07:26,240 --> 00:07:27,660
They're much more flexible.

108
00:07:27,890 --> 00:07:32,400
So here's an example of representing irah using a non-relational database.

109
00:07:32,480 --> 00:07:33,670
There are no tables.

110
00:07:33,710 --> 00:07:37,880
So we don't have to define this tabular structure and things can be nested.

111
00:07:37,880 --> 00:07:40,380
So it's not a flat database.

112
00:07:40,670 --> 00:07:43,780
So you can see here it looks just like javascript.

113
00:07:43,790 --> 00:07:45,360
In fact it's really really similar.

114
00:07:45,530 --> 00:07:51,590
It's something called beats on which stands for binary javascript object notation but it's basically

115
00:07:51,680 --> 00:07:57,110
javascript objects that we're familiar with and they have a bunch of key value pairs so name is Ira

116
00:07:57,110 --> 00:07:57,450
.

117
00:07:57,490 --> 00:08:00,530
Age is 24 city is Missoula.

118
00:08:00,530 --> 00:08:02,510
And then this is the really important part.

119
00:08:02,510 --> 00:08:06,040
Comments can just be nested right inside of the data.

120
00:08:06,110 --> 00:08:07,760
I don't have to deal with IDS.

121
00:08:07,850 --> 00:08:10,390
I don't have to define this table ahead of time.

122
00:08:10,490 --> 00:08:16,820
I can just nest comments in here and just start adding objects and if I recommit something else I can

123
00:08:16,820 --> 00:08:19,910
just add in another comment push into that array.

124
00:08:19,910 --> 00:08:27,360
And so this comment could say why does no one care about on Tanno.

125
00:08:27,790 --> 00:08:29,120
And that's a really good question.

126
00:08:29,150 --> 00:08:30,470
I don't know either.

127
00:08:30,590 --> 00:08:36,290
So we end up now with this very very flexible structure where if we wanted to I could just define favorite

128
00:08:36,290 --> 00:08:37,270
color down here.

129
00:08:37,400 --> 00:08:47,870
Fav color is purple and then I could have a whole other user who is named Tammy.

130
00:08:47,870 --> 00:08:52,900
And Tammy can be 24 from a zoo as well and she won't have a fave color at all.

131
00:08:52,910 --> 00:08:57,290
And instead she has favorite food which is revi.

132
00:08:57,320 --> 00:08:57,550
All right.

133
00:08:57,560 --> 00:08:58,740
So that's enough of that.

134
00:08:58,850 --> 00:09:05,060
But I really wanted to focus on with all of this is the difference between relational and non-relational

135
00:09:05,120 --> 00:09:10,730
so not the nitty gritty details of the syntax or how it actually works just the broad difference that

136
00:09:10,790 --> 00:09:14,920
sequel databases or relational databases have been around the longest.

137
00:09:15,010 --> 00:09:19,580
Are what most people think of when they think of databases where we have to define a table.

138
00:09:19,640 --> 00:09:25,520
Everything is tabular and if we want to relate data we have to have multiple tables to relate that data

139
00:09:25,610 --> 00:09:31,940
and we we often use IDs to do that with a non-relational database which is what we're going to be focusing

140
00:09:31,940 --> 00:09:34,040
on for most of this course.

141
00:09:34,130 --> 00:09:36,660
We don't have to define any sort of tables.

142
00:09:36,680 --> 00:09:41,930
We don't have tables at all actually and instead we have a much more flexible structure.

143
00:09:41,930 --> 00:09:45,390
Now this isn't to say that non-relational databases are better.

144
00:09:45,440 --> 00:09:50,990
In fact in a lot of cases they're not almost in all cases but there are specific situations where they

145
00:09:50,990 --> 00:09:55,490
make sense and I'll talk about why we're using a non-relational database in the next video.

146
00:09:55,490 --> 00:10:00,610
But I don't need to get the impression that because this is more flexible it's inherently better.

147
00:10:00,620 --> 00:10:05,600
That's not the case but it is more flexible which if that's what you're looking for then you want to

148
00:10:05,600 --> 00:10:07,590
use an non-relational database.

149
00:10:07,670 --> 00:10:09,580
All right so let's wrap this up.

150
00:10:09,650 --> 00:10:11,980
A database is a collection of information.

151
00:10:12,020 --> 00:10:17,330
It has an interface some sort of language some sort of technology or tools to interact with the data

152
00:10:17,480 --> 00:10:22,910
add new things read things out of the database delete things edit things and then there are two broad

153
00:10:22,910 --> 00:10:26,460
categories of databases sequel and no sequel.

154
00:10:26,840 --> 00:10:27,460
OK.
