Next, we have a bit more to cover with basic inserts.
First of all, I mentioned the order matters in the sense that if we tell my SQL that name is coming
first in this first part of the insert, then we have to make sure name comes first.
It doesn't know that this is a name and this is an age unless we follow the order that we lay out,
but we can switch that order.
There's no rule that says we have to have name and then age.
We could do age and the name.
We just want to make sure that we follow that pattern.
So if I try that insert into cats and then I'll say age first and then name and then our values will
be and you can see I typed a quote because I was just not paying attention.
We don't want to quote because we're inserting an age first, let's say a two year old cat named about
Beth.
Sure.
And if I insert that, it looks like it worked.
And if I do a select star from Cats again, we'll see that Beth was inserted as the name in that row
and age was two.
So as long as we follow the order we laid out the order overall doesn't matter.
It doesn't matter if age comes before name or name before age, as long as we are consistent with our
values.
Now, what happens if we are not consistent?
What if I did insert into cats and I say, expect the name first and then the age, and then in my values
I get lazy or I don't pay attention and I put the age first.
We have eight and then the name of Linus.
What happens?
Then we get an error.
We get an error that says incorrect integer value.
Linus For the column called Age, you can't have an integer, which is what age is supposed to be called
Linus.
That is not an integer.
So it's telling me that.
So what does that mean?
If I look at cats, do we have Linus in here?
No, we don't.
So this error prevented the row from being inserted.
Just be aware of that.
These column data types are not guidelines.
They are rules.
At least with the way everything set up right now, age has to be an integer.
There's no way to put a piece of text in there.
So we tried that and we got slapped in the face.
My SQL says, not so fast.
Okay, so that's a bit more about the order.
The next thing I want to cover is a multiple insert so we can insert multiple rows at once, which is
a common operation.
We don't have to have three separate insert into cats.
I can do one insert into cats and then have multiple values sets of values separated by commas.
So let's try that this time.
Maybe I'll do it from a file just so we have a record of it.
So I'll do insert into cat's name and then age.
We don't have to do that order, but I will.
And then values.
And the first name that will do is who should we do?
This is the hardest part of the whole thing.
How about a cat called Meatball and Meatballs?
Age is five, and then I can add a comma and just have another pair.
Let's do turkey.
And turkey is one and one more.
I guess we'll go with some food themed ones.
How about potato face?
Potato face is 15.
So one thing that you might see people do is format these so that all those insert or rather all the
parentheses line up.
You really don't have to do that.
It doesn't matter whatsoever, especially when we just have three.
Everyone can figure out what's happening here.
But if you wanted to be consistent, you totally can.
All right, so I'm going to run this execute, assuming I got Oh, I didn't put a semicolon there.
Let's see, did it complain to me?
It looked like it worked.
So it may have figured out what I was trying to do.
But let's verify over here.
Select Star from Cats.
Yeah, it did work.
So if I had done that over here in the MySQL client, I left off a semicolon.
What?
We know what happens, right?
So like star from Cats, nothing happens until I enter that semicolon to terminate the line.
But over here inside of DB gate, it can figure out.
I guess it just has some logic to add that semicolon on.
Who knows?
Anyway, I should put it there because it's technically required.
So that's how we can do a multiple insert.
This is nice.
If you have multiple rows you want to insert at once, you don't want to have a separate insert into
for every single one.
Instead you can just chain them all together as long as they all follow the same pattern you laid out
name and then h name, age, name, age, name, age.
That's multi insert and that's it for this video.