{"id":61809,"date":"2021-10-07T08:58:15","date_gmt":"2021-10-07T13:58:15","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=61809"},"modified":"2021-10-07T08:58:15","modified_gmt":"2021-10-07T13:58:15","slug":"varchar-vs-text-for-mysql-databases","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/products\/varchar-vs-text-for-mysql-databases\/","title":{"rendered":"VARCHAR vs. TEXT for MySQL Databases"},"content":{"rendered":"\n
When you\u2019re building a database for a web application, one of the most impactful decisions is the data type you choose for text data fields. MySQL provides multiple string data types, each with unique characteristics and trade-offs. The data type you choose affects how much data you can store, where it\u2019s stored, the functionality available to you, and the performance of everyday database operations.<\/p>\n\n\n\n
In this article, we\u2019ll look at one frequently asked question concerning MySQL string data types: should you use a VARCHAR column or a TEXT column to store textual website data?<\/p>\n\n\n\n
If you\u2019re looking for a TL;DR, it\u2019s this: use VARCHAR if your data is of variable length and you know it fits into VARCHAR\u2019s 65,535 character limit. In most circumstances, VARCHAR provides better performance, it\u2019s more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.<\/p>\n\n\n\n
To explain why VARCHAR is the right default choice, we\u2019ll need to look at MySQL\u2019s string data formats. For our purposes, these fall into two main groups, CHAR and VARCHAR types and the TEXT types, which are based on MySQL\u2019s binary object BLOB types. There are other string types\u2014SET and ENUM\u2014but they\u2019re not relevant to this article.<\/p>\n\n\n\n
We\u2019ll be talking about characters and the amount of space they consume. However, we\u2019re not going to discuss the precise definition of a character or the effect of using different character sets. Take a look at the MySQL documentation to learn more about character lengths and multibyte character sets<\/a>.<\/p>\n\n\n\n First, CHAR and VARCHAR:<\/p>\n\n\n\n CHARs are great for storing short strings when you know how long they are. You can also use VARCHAR to store short strings\u2014VARCHAR(40), for example\u2014but it can store any string up to the maximum column size, using a variable amount of storage space.<\/p>\n\n\n\n Next, TEXT and its variants. Text is based on the BLOB (binary large object) type. These are variable-length data types, and they come in three main variants:<\/p>\n\n\n\n In many respects, TEXT is similar to VARCHAR: it stores variable-length strings up to 65,535 bytes. For both, the amount of space consumed depends on the length of the string. However, there are some key differences:<\/p>\n\n\n\n Taken together, these qualities of TEXT mean that, for many circumstances involving a web application, it\u2019s preferable to use VARTEXT when storing variable-length text of fewer than 65,535 characters.<\/p>\n\n\n\n None of this means that you must<\/em> use VARCHAR. TEXT is a reasonable choice if the limitations we\u2019ve described are unimportant to you. However, there are circumstances where TEXT has undesirable consequences that don\u2019t arise if you use VARCHAR.<\/p>\n\n\n\n If you want to store longer variable-length strings, MEDIUMTEXT and LONGTEXT are your best option. However, you may run into network and other system limitations if you try to insert large amounts of data. Throwing multi-gigabyte BLOBs into your database is rarely wise, so be sure to check the data\u2019s size before storing it.<\/p>\n\n\n\n To see a real-life example of how these MySQL data types are used, let\u2019s explore a couple of tables in WordPress\u2019s database with cPanel\u2019s built-in phpMyAdmin tools. You can examine the data types of any MySQL database on your server by opening phpMySQL, locating the database in the left-hand column, and clicking the Structure<\/em> link adjacent to the relevant table.<\/p>\n\n\n\n If you look at the comments<\/em> table, you\u2019ll see that WordPress\u2019s developers have used VARCHAR for shorter text fields: comment type, comment agent, comment author, URL, and so on. But they have chosen to use a TEXT field for the comment content, a column unlikely to be used in an index.<\/p>\n\n\n\n In the post table, a similar pattern is evident. VARCHAR for post status, post password, and post name. The post content column, which is likely to exceed VARCHAR\u2019s space limit, is stored in a LONGTEXT column with a theoretical maximum of 4 GB.<\/p>\n\n\n\n PhpMySQL is just one of the many web hosting, database management, and automation tools built into cPanel & WHM. You can read more about managing database with cPanel in:<\/p>\n\n\n\n As always, if you have any feedback or comments, please let us know. We are here to help in the best ways we can. You\u2019ll find us on Discord<\/a>, the cPanel forums<\/a>, and Reddit<\/a>. Be sure to also follow us on Facebook<\/a>, Instagram<\/a>, and Twitter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" When you\u2019re building a database for a web application, one of the most impactful decisions is the data type you choose for text data fields. MySQL provides multiple string data types, each with unique characteristics and trade-offs. The data type you choose affects how much data you can store, where it\u2019s stored, the functionality available […]<\/p>\n","protected":false},"author":77,"featured_media":65829,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-61809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-products"],"acf":[],"yoast_head":"\nVARCHAR in Action<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n