INT(11) is a lie
Creating an integer column in MySQL with INT(11) is kind of pointless. It doesn't do what you think it does, and it's also deprecated. Let's learn what it actually does! (Or... what it did)
Everything I said
If you've been creating integer columns in MySQL like this, I'm about to blow your mind because this 11, it doesn't actually do anything. It can do something, but you have to add an extra keyword. The thing that it does is probably not the thing that you're thinking it does, and as of MySQL 8.0.17, it's deprecated.
So it's going away, so it's not gonna do anything at all in the future. We're gonna create an example table, and we're gonna take a look at a few examples. Good name. We'll start with a character column. So if we said email is a character column of varying length, how long do we want it to be? Let's say 255.
That seems excessive, but that's not the point of this video. So what we're saying here is that up to 255 characters can go in this column. That's reasonable enough. This is not interesting. We all know that. However, let's move on to a number column, and we'll say it's an integer of 11.
11 what? 11 somethings. So what does this mean? Up to 11 digits? M- maybe. That seems reasonable enough, but it's 100% wrong. So what does this 11 actually mean? Right now, nothing. So if we were to create this table, and then we were to describe this table back, so describe example, we'll look and we'll see that the 11 is actually gone.
W- that information has just been lost. If we do show create table example, and we copy, we copy this out, that 11 is simply gone. It didn't matter. MySQL just threw it away because it's pointless. At least in our example, it's pointless, right? Because we created the table with it, and then we read it back, and MySQL said, "You don't need that part.
You don't need that part. I'm not even-- I didn't even remember what you told me. I just totally forgot." So unlike a character column, that 11 does not determine the underlying size of the column. If you wanna change the underlying size of the column, you would have to use one of the different data types. You could use the tinyint, which is a one-byte column, and the unsigned range is 0 to 255.
A smallint is two bytes, and that goes from 0 to 65,535. Which, interestingly enough, used to be the maximum number of rows that Microsoft Excel could support. So if you're ever at a party and you need a little tidbit, maybe try that one. That's a pretty good one. Moving up, you've got the mediumint, uh, the integer, and the bigint, and those maximum values just get truly enormous when you get up to the big integer column.
So what does the number in parentheses actually do, or what was it supposed to do? Because now it's deprecated, and it's going away forever, and so it doesn't really matter what it does. I can still tell you, it is only useful when you combine it with a zerofill keyword. So if we look at our table again, let's actually... Let's drop table example.
We'll get rid of that, and we're gonna say int 11 zerofill. And now if we run that, and we say show create table example, and we read this back, let's see if MySQL retained it. It looks like it did retain that int 11 and the zerofill.
It also forced it to be unsigned. So when you add the zerofill keyword, it forces it to be unsigned. Let's insert some data and see what this looks like. So insert into example. We'll put email and num, and we'll say values are Foo, and we'll just put 1 in there and see what happens.
We've got one row in there. Select star from example, and if we look at that, we see that number or num has been zero-padded. It's been filled with zeros until it reaches 11 characters long. That's what the 11 does. That's what the 11 does and will continue to do for who knows how long, but not very long because it is deprecated.
So I wouldn't even recommend using this whatsoever, but at least now you know what it's for and, more importantly, what it's not for. It is not for controlling the size of the column. If you wanna control the size of a numeric column, use one of the more appropriate data types. So if it's very small, tinyint, smallint. If it's huge, bigint.
But that int 11 only works if you add the zerofill keyword, and even then, I wouldn't recommend it. So hopefully this blew your mind. Hopefully, this has you questioning everything. If so, please leave a comment. We are currently accepting very nice, positive comments, subscriptions, and thumbs up. Um, if you would like to unsubscribe, leave a mean comment, or thumbs down, you'll have to wait for a future video.
Unfortunately, we're not accepting those at this moment. If you'd like to learn more about PlanetScale, you can go to planetscale.com. Until the next time, see ya.