All right we'll come back.
It's time for us to start working with databases interact locations.
And before we actually write code I just want to take a few minutes here so we'll be pretty quick to
talk about what databases actually are and then to introduce two broad categories of databases which
are sequel databases or Escudo versus no sequel or no.
Q All databases.
OK.
So let's begin here and to start I want to illustrate the problem that we have right now.
I have the POST request demo running this friends up where I can list all my friends and we can add
some new friends.
I just made a new one today named Garfield and another friend named her mine.
OK.
And then I can go back here and if I shut off the server and started again and I refresh my page.
Gosh you just lost two friends.
So the problem that we're facing is that our data doesn't persist if our server stops for any reason
whether it's because we turned it off.
We wanted to restart it.
We made some changes to the code or if it's because the power goes out or some freak event happens if
the server stops we would lose all data which is obviously a really really bad thing for a web application
.
If Facebook lost your data every time you logged out or every time the server stopped that would be
really really bad for Facebook although it might be pretty good for me.
I'd be productive.
I stopped comparing myself to my friends at Stop trying to prove to everyone that I live a fun life
and I just get stuff done.
So all of this is to say that databases will help us with this problem.
So let's talk about what databases are in the simplest possible definition.
The database is a collection of information data but a database isn't just a collection of information
.
So if we had a file just a text file and sublime that had a bunch of information and saved it that wouldn't
be a database.
Yes it might store data for us.
It might be a collection of information but the other important aspect of databases is that they have
an interface for interacting with that data whether it's adding new information like adding a new user
to the database or if it's removing all users or editing an existing user or whatever it is there needs
to be a way for us to interact with the data.
So a database is a collection of information slushed data and it has an interface.
When I say interface I mean that we can write code to interact with it.
So if you've ever worked with a sequel database before you might have seen things like this select star
I should capitalize this.
It's like star from users.
Or you might see something like insert blah blah blah into.
And this is a language called sequel and this is the interface to some databases the language that we'll
be using looks like this.
D-B dot dogs dot find or D-B dot dogs dot delete and will delete the dogs where we have age just 14
.
Unfortunately all my dogs have died at that age and I want to talk about it and I'll delete all dogs
where age is 14.
We'll get into the specifics of the language in the next video but I just want you to be aware.
This is what I mean when I say that there's an interface to the collection of data slash information
.
OK.
So the last thing I want to do is talk about the two broad categories or two of the broad categories
of databases and those are sequel which is also called relational databases versus no.
As you well know sequel which are called non-relational databases when I'm talking to my students about
databases a lot of them have had some experience with some sort of database whether they were in marketing
or retail or something online.
Pretty much every company out there has some database usually lots of databases with lots of information
in it whether it's a database for all of the Skewes in the gap or a database for payroll of all the
employees and what they've been paid and when or if it's a bunch of data on users like Facebook has
where it's storing all of your user information on your photos your comments your posts your likes your
tags all that stuff is stored in databases.
When I talk to students about databases the students who are familiar and have experience with databases
almost always only have experience with sequel or relational databases and that's because sequel databases
have been around the longest and they're what most people think of when they think of a database.
So now let's talk about what sequel data bases what relational means.
So sequel databases are tabular databases and they're flat.
Let me show you what I mean.
So I have some crude diagrams that I typed up and this is showing what the tables for a database involving
users and comments could look like.
Let's just start by focusing on the users.
So what we have to do in a relational database or Seacole database is we define what a user looks like
.
So one user has an ID as a name and age and a city and then every single user that we add must follow
that pattern.
So we're defining these tables and then we're adding instances of the user or of users to this table
and then let's suppose I also have comments.
So I want a user to be able to comment.
So I have a comments table and all that a comment has is an ID and some text of the comment like EHLO
or come visit Montana or I love puppies if I want there to be a relationship between users and comments
where a user can have a comment those associated with it.
So Ira I hear who's 24 from Missoula can be associated with some of these comments here.
Like come visit Montana and seriously Montana is great.
The only way to do that to express this relationship is through another table which is what we have
here.
And these are called join tables.
And what this table does is it joins a user's ID with a comment Id.
So in this case we see that the user with ID one owns the comment with ID 3.
So that means that Tim said that I love puppies and the user with the idea of two commented twice and
those comments or idea of to an idea for.
So we can see that IRA commented come visit Montana and also seriously.
Montana is great.
And yes 110 is great.
What I really want to show with all of this is that everything is tabular.
So we have to define a table ahead of time and its not very flexible at all.
Lets suppose I wanted to add another attribute to IRA that was favorite color and I wanted to set it
to be purple.
I would then have to go an add favorite color for everyone.
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
nyl or just false or something here for every single person.
So it's not flexible.
What I have to do is define exact patterns of what a user looks like and then I need to follow that
pattern very closely.
So the other type of database non-relational databases or no as well databases and you probably catch
them going here.
We don't have to define patterns ahead of time.
They're much more flexible.
So here's an example of representing irah using a non-relational database.
There are no tables.
So we don't have to define this tabular structure and things can be nested.
So it's not a flat database.
So you can see here it looks just like javascript.
In fact it's really really similar.
It's something called beats on which stands for binary javascript object notation but it's basically
javascript objects that we're familiar with and they have a bunch of key value pairs so name is Ira
.
Age is 24 city is Missoula.
And then this is the really important part.
Comments can just be nested right inside of the data.
I don't have to deal with IDS.
I don't have to define this table ahead of time.
I can just nest comments in here and just start adding objects and if I recommit something else I can
just add in another comment push into that array.
And so this comment could say why does no one care about on Tanno.
And that's a really good question.
I don't know either.
So we end up now with this very very flexible structure where if we wanted to I could just define favorite
color down here.
Fav color is purple and then I could have a whole other user who is named Tammy.
And Tammy can be 24 from a zoo as well and she won't have a fave color at all.
And instead she has favorite food which is revi.
All right.
So that's enough of that.
But I really wanted to focus on with all of this is the difference between relational and non-relational
so not the nitty gritty details of the syntax or how it actually works just the broad difference that
sequel databases or relational databases have been around the longest.
Are what most people think of when they think of databases where we have to define a table.
Everything is tabular and if we want to relate data we have to have multiple tables to relate that data
and we we often use IDs to do that with a non-relational database which is what we're going to be focusing
on for most of this course.
We don't have to define any sort of tables.
We don't have tables at all actually and instead we have a much more flexible structure.
Now this isn't to say that non-relational databases are better.
In fact in a lot of cases they're not almost in all cases but there are specific situations where they
make sense and I'll talk about why we're using a non-relational database in the next video.
But I don't need to get the impression that because this is more flexible it's inherently better.
That's not the case but it is more flexible which if that's what you're looking for then you want to
use an non-relational database.
All right so let's wrap this up.
A database is a collection of information.
It has an interface some sort of language some sort of technology or tools to interact with the data
add new things read things out of the database delete things edit things and then there are two broad
categories of databases sequel and no sequel.
OK.