The next group of data types will cover are numeric types.
And of course, we've already seen one.
We've been using it all over the place.
It stores whole numbers and we've been using the plain regular variety of INT, But recall from the
docs that there's integer and int, those are the same.
It's just a shorter version of the, it's a short way of referencing integer.
They mean the same thing, but there's also a small into tiny and medium int and big int.
So I don't want to dive too much into into the details, but it's actually quite simple.
The difference comes down to how large of an integer you can store with each type.
It's all about optimising storage and how much memory things take up.
So a regular integer or int the one we've been using takes up four bytes according to this table and
the maximum signed value.
Remember we talked about the signed versus unsigned signs means it could be negative or positive.
Is this number here, which is what, thousand 100,000?
Right.
So that's.
Which is 2 billion.
It's hard without the commas, but I think it's 2 billion.
147,483,647.
That is the maximum signed value.
So the minimum would then be this -2.1 billion.
And then if we compare that to some of these other integers, like tiny int, tiny int can go from -128
up to 127.
So if we were storing something small like the number of children somebody has or the number of dependents
on their tax returns or something horrible like that, tiny, there's no reason to have this much space
allotted, which would be four bytes.
Why not just stick to one byte and have a small integer or technically a tiny int?
I can show that really quickly.
I'll do it over here.
It's not going to look any different to us really, but I'll create a table.
Create table, let's call it parent.
And then we just have children, which will be a tiny int.
And if I insert into parent, we don't have any other information about a parent, just the number of
children they have.
That's fine.
We can do two or somebody else has about not seven children.
That's quite a few or zero children.
That works great.
But if I try and insert something much larger, a 200 children would get an error out of range value
for that column.
So if there is any possibility somebody could have more than 128 children or 147, then fine, maybe
we use small int or medium int.
This is all just about optimizing storage in the amount of space that each one of these values takes
to store.
So INT is a default.
That's all we've been using.
You can store quite large numbers.
There is a larger, there's big int which stores way larger numbers but takes up double the space and
then there's medium, small and tiny.
So if you really care and you're trying to be, you know, trying to optimize storage, which is a good
thing to do, I just feel like it's overkill when you're starting out.
But it's good to know about.
And if you're in a situation where you do care about it, then you can take a look at this table or
go to the docs and find the appropriate sized integer.
The last thing we'll talk about with ints is the signed versus unsigned.
So by default, we're working with signed integers.
When we say data type is int or tiny int, I can go from -128 up to 127.
So technically I could have a negative number of children in here.
Negative three maybe.
I don't want that.
So what I can do is say that we're only working with unsigned now.
We're also going to learn how to add constraints checks to say that a number has to be greater than
something or between something.
So this is not the only way or the main way that you would go about enforcing a positive number of children.
But it is a way of saying every tiny int in this table is going to be positive because it is a signed
integer versus unsigned.
So it looks like this when we create our table and we drop this table parent recreate it, and instead
of simply saying children is of type tiny int I say tiny int unsigned.
And it's going to look exactly the same if I try and insert a new child or a new parent in there with
three children, it works.
But if I try and do negative three, we're now out of range because we specified this has to be an unsigned
tiny int, which just means there's no negative sign and that allows us to store a higher possible max.
Right.
155 But the minimum is now zero, so you can use unsigned as a keyword with any of these others you
could do.
INT unsigned big int unsigned.
That's enough about integers.
I usually just stick with int unless I know for sure I'm dealing with something small.