DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Top 5 Key Features of Apache Iceberg for Modern Data Lakes
  • Generative AI vs. Machine Learning: Decoding the Distinctions
  • A Comprehensive Guide to Data Visualization: An Effective Way of Telling Stories With “Data”
  • Static Proxies or Rotating Proxies: Which One Should You Choose?

Trending

  • Rethinking Recruitment: A Journey Through Hiring Practices
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Data Engineering
  3. Data
  4. CHAR vs. VARCHAR: What's the Difference?

CHAR vs. VARCHAR: What's the Difference?

In this article, we are going to go through the similarities and differences between two MySQL data types: VARCHAR and CHAR.

By 
Lukas Vileikis user avatar
Lukas Vileikis
·
Aug. 21, 22 · Analysis
Likes (2)
Comment
Save
Tweet
Share
6.2K Views

Join the DZone community and get the full member experience.

Join For Free

If you've found yourself in the MySQL space even for a little while, you already know that MySQL and other flavors of it offer a couple of data types for developers to choose from. A wide majority of the most popular data types offered by MySQL are related to text — in this space, MySQL offers VARCHAR, CHAR, TINYTEXT, TEXT, and other options. In this article, we are going to go through the similarities and differences between two of some of the most popular options: the aforementioned VARCHAR and CHAR.

What Is CHAR and VARCHAR?

Before diving deeper into the differences between the two data types, we must first make you understand what they are, right? Well, everything in this space is pretty simple because the names of the data types themselves suggest what they are able to do — CHAR stands for "character values" and VARCHAR stands for "variable character values."

Both CHAR and VARCHAR differ from other similar data types by the fact that they both are considered to be fixed-length data types: we create both CHAR and VARCHAR data types with a "fixed" length that can range from 0 to 255 characters in size if CHAR is being used or from 1 to 65,535 characters if VARCHAR is in use. The bigger our data length is, the more space our characters will occupy (this will probably won't be a very large problem, however, if your data grows, you might want to keep an eye out on both the structure of your database and learn how to work with big data sets), though this is common sense. What's not sometimes very widely discussed though is the core differences between the two data types — after all, even their names are similar, so what's the deal? Bear with us while we guide you through their world...

CHAR vs. VARCHAR

At first glance, both CHAR and VARCHAR might seem very similar — after all, they are defined in exactly the same fashion, aren't they? Demo CHAR(5) would create a column named demo with a CHAR data type with a length of 5 on it, and demo VARCHAR(5) would create a column named demo with a VARCHAR data type.

However, when we dive deeper into these, we would notice that MySQL hides their differences inside of its architecture:

CHAR VARCHAR
Fixed length Variable length
Always takes the same amount of space regardless of what's stored Amount of space taken varies: either 1 or 2 bytes + data
Can hold a maximum of 255 characters Can hold a maximum of 65,535 characters
Uses static memory allocation Uses dynamic memory allocation
As you can see, some of the differences are apparent, some are not — for example, we would imply that VARCHAR is a variable-length data storage type because of the name, but did you know that CHAR can only hold 255 characters? One of the reasons for that might be because of the fact that CHAR is known to always take up exactly the same amount of storage space — how can MySQL ensure that a column with a CHAR data type will not take up 5MB of storage if it holds 50,000 characters? With 255, that's easy to do — the length of a CHAR column is defined when we create a table and it doesn't change no matter what happens. This has its own downsides though — we can't store very large text values. For you to imagine what CHAR can handle, head over to the Lorem Ipsum generator and observe the paragraphs. CHAR can handle approximately one-third of one paragraph — in other words, four to five sentences.

We have highlighted the text that CHAR could handle without issues in blue. See the problem? Such a data type might be a fit if we are storing very small amounts of text, but not so if we are working with larger-scale systems, or even if we need to handle a couple of login forms. (Remember, we need to hash our passwords to make them not humanly readable — do we really want to take the risk of not being able to store them?)

In a nutshell, CHAR is a fit if we are strapped on storage space and do not plan on storing much data inside of our column.

With VARCHAR however, that's a different story altogether — as you can understand, it's a variable-length data type meaning that its rows can get up to 65,535 bytes of data in size. The amount of characters that this data type can handle is not limited to 255, though that's what pretty much every developer uses if they're dealing with data — do note that VARCHAR data types can also easily hold 6,000, 10,000 characters, etc., but it all depends on the collation and character set that is being used. (For details on that topic, refer to the docs.) 

The bottom line is this — we can create tables with VARCHAR data types on columns so long as the row size doesn't exceed 65,535. Simple as that. For example, if we have 7 columns and 6 of them are of 10,000 characters in size and the seventh has 6,000, we are in trouble, because the row size combined would exceed 65,535 — we would have 66,000 instead. You get the point.

Also, keep in mind that the amount of space such a data type takes up on the disk varies from 1 to 2 bytes — keep that in mind when designing systems with bigger sets of data, but other than that, do not worry too much. Modern hard and solid state drives don't make this a problem at all. Use them and you should be fine.

The last thing is that CHAR differs from VARCHAR in the amount it allocates memory for its data — CHAR allocates memory statically meaning that once the memory is allocated, its size cannot change, and VARCHAR does things the other way around — it allocates memory at runtime.

The bottom line is this though — VARCHAR is mostly used to work with medium-sized to big projects and CHAR is generally only used when we are strapped in size or our project is very small. Sacrificing a couple thousand bytes here or there isn't an issue that should worry a database administrator — disk space is cheap and easy to acquire. However, with that being said, do keep in mind that if we are working with bigger sets of data, we should take care of the size of our data types accordingly. Other than that though, there shouldn't be any major issues!

Summary

Both CHAR and VARCHAR are data types that are used very frequently — however, to use them properly, we must understand that CHAR is more of a fit when we deal with fixed-length data that does not exceed 255 characters, and VARCHAR is a fit when we deal with projects of a bigger scale. The amount of space these data types occupy does vary as well, however, it shouldn't cause many issues when we aren't working with bigger data sets. If you find yourself working with them, though, head over to our big data tutorial, and we will walk you through everything you need to know.

Big data Virtual screening Data Types

Published at DZone with permission of Lukas Vileikis. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Top 5 Key Features of Apache Iceberg for Modern Data Lakes
  • Generative AI vs. Machine Learning: Decoding the Distinctions
  • A Comprehensive Guide to Data Visualization: An Effective Way of Telling Stories With “Data”
  • Static Proxies or Rotating Proxies: Which One Should You Choose?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!