How to Use the Most Common MySql String Data Types
Strings in databases are used to hold names, addresses, comments and other series of letters and numbers. You use different String Data Types depending on the length of the strings and how you want them handled. Here are a few of the more common MySQL String Data Types and how to use them when you're creating your tables.
Instructions
-
-
1
CHAR(length) is a string data type that can store a string up to 255 characters long. The length attribute specifies the maximum number of characters that column can store. So CHAR(25) stores a string 25 characters long. If the data that's stored is shorter than 25 characters long, it's padded with spaces to the right of the string to make up the difference.
CHAR is a good data type to use if you anticipate the data to be a consistent length like two-letter state abbreviations, zip codes or social security numbers.
-
2
VARCHAR(length) is a string much like CHAR but can be of variable length. Specifying VARCHAR(25) will allow you to store a string up to 25 characters long, but will not pad the string with spaces if it's below 25 characters. In some versions of MySQL, the maximum size of a VARCHAR is 255, however in the most recent versions, this is expanded to 65,535. Be aware that trailing spaces will be stored as part of the string.
VARCHAR is a good data type to use if you're storing data that's variable in length like names, addresses, book titles, etc.
-
-
3
TEXT(length) is also a variable-length character data type like VARCHAR, but can store 65,538 characters. Other variations of TEXT include MEDIUMTEXT and LONGTEXT which both allow for even more storage.
TEXT works well with long strings of text like comments, instructions and product descriptions.
-
1