MySQL 26 - VARCHAR Data Type

submitted by mkenny400 on 03/20/18 1

Varchar is very similar to CHAR in that it is used to store strings, but there are some pretty big differences. The first difference is that when you store data less than the max it does not pad it with spaces to make it fit. The benefit in this is that you will save storage. The downside to this is that now MySQL is going to have to keep track of how many characters you have. To do this, each value is going to have an additional byte or two that MySQL can use. The max length is 65535 bytes. That is per value in that column! But you have to keep in mind encodings. If your characters are encoded with something such as UTF-8, each character can take up multiple bytes. The max size for a character in this case is 3 bytes. That means that we really can't store that many characters. Additionally, MySQL has a row limit of 65535 bytes (potential bytes…meaning declared sizes). What does this mean? It means that if you make this too big, you are not going to be able to create other columns. Earlier I said that there will be an additional byte or two for each value you put into this column. The purpose of this is to keep track of how long the string is. Why one or two and not one? The reason being is because with one byte we can only count to 255. if we want to keep track of bytes after that we need two bytes. This means that we actually can't store quite 65535 bytes. Realistically, we can't store that much because we have to store the length of the string, and each character can take up to 3 bytes which can push us over the row limit. This means the real max is a little less than a third of that. Not every character is going to take up 3 bytes, but MySQL assumes it will as to not cut you short. This is slightly different than CHAR because char you can store up to 255 characters, not bytes. Even if you use an encoding where some characters take up multiple bytes, you can still store 255 of them. Remember that reason for this is that VARCHAR is subject to the row-limit. MySQL will not let you go past the max and will tell you the appropriate max, so don't worry about it too much. You should try not to push your limits. Plus, most of the time you will not come even close to the max for most columns. Should you use the max? Varchar will only store what is needed to store a value, but you should still try to have the max size no more than what is needed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×