Next up, we're going to focus our time on learning to create data.
How do we add or insert data into the tables that we've already made?
This is another one of the most important and most commonly used commands when you're working with databases.
Obviously, it's really important to have a way to get your data in the database.
So we'll be using what we learn here throughout the rest of the course.
And you can say that for everything else that we've learned so far, but in particular, this command
insert or insert into.
So the syntax here is insert into and then the name of the table cats in our case.
And then it gets a little weird after that.
So let's just focus on that first insert into and then the table name.
Then afterwards in parentheses, we're specifying the columns that we are going to insert.
So I'm saying I'm going to give you a name comma, I'm going to give you an age.
And then down here I have values, which is another keyword specifying that I told you I'm giving you
a name and an age.
Here they are.
The name is Jetson and the age is seven.
So these do need to match.
Name is first Jetson.
Age a second seven.
Before we go, try it out in cloud nine, I just want to show you that I wrote it this way, mainly
because it looks best on a slide.
You can do it on one line.
So insert into cat's name and age the values Jetson and seven or you can do it like this and this is
actually a lot of people's preferred way of writing it.
But this is a huge pain to enter into the shell this way where you have to space things manually.
But when we start working with individual SQL files where we'll save our own code basically into these
separate files in a text editor and we can space them out how we want.
This is definitely a preferable way, but we're going to work with this.
I just like this when I'm presenting and when I'm teaching it.
It's a mix of readability, but also it's not so laborious like this.
And just to hit this point home, the order that we write these column names matters.
So if I say insert into cats and I say Age is coming first and then name, I have to provide age first
and then name.
So it doesn't matter which order you use, but whatever you establish up here you have to follow through
with.
Otherwise it will try.
If you had this reversed, it would try and aside.
It would try and assign Victoria to age and 12 to name.
And that's going to be a problem because we have these strict data types where things are supposed to
be numbers and strings.
And so if we mix them up, it's problematic.
Not to mention that it makes no sense.
So let's give it a shot in cloud nine.
I'll make some space here.
And the first thing we need to do is actually recreate a table, because right now I don't have a cat's
table.
I deleted it because I showed you how delete works.
So let's recreate that create table cat and it's going to have name so far char 50 comma age is going
to be an int just like that.
Perfect.
So now we'll use the insert into command we just learned.
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
a new line.
I don't have to, but I like to values and the values will put in, let's say for name.
Let's work with my cat, blue comma.
And her age is somewhere between one and two.
We'll just leave it as one semicolon.
We'll hit enter.
We get our query.
Okay.
This time it says one row affected.
You may have noticed that everything else we've done so far has said zero rows affected and I think
that's pretty self explanatory.
We haven't had any rows of data yet, so all of these commands have returned, zero rows affected.
Now we actually have created something.
We've added data to a table and it says one row affected.
And before we move on, let's insert one more cat here.
And this time we'll switch up the order, insert into cats and I'll do it on one line, AJJ, comma,
name and then our values.
Let's say the age for this cat will be 11 and the name will be Draco.
All right.
And we also get one row affected.
So the next logical question might be, how do I see the data?
How do I know that it's in there correctly?
Basically, the same thing that you might have asked when we saw how to create a table.
How do we check our work?
Well, we've created data in a table.
How do we check our work?
And we'll see that in the next video.
Sorry for the cliffhanger.