What is the Difference Between char and varchar

The main difference between char and varchar is that char stores values in fixed lengths and are padded with space characters to match the specified length, while varchar stores values in variable length along with 1 byte or 2-byte length prefix and are not padded with any characters.

Generally, RDBMS is software that helps to store and manage data which is based on the relational model. An RDBMS consist of databases and each database has a set of tables. These tables are connected to each other using various constraints such as foreign keys. Additionally, developers can perform operations on the databases using Structured Query Language (SQL). Overall, MySQL is a popular RDBMS used in personal as well as enterprise level. Each data stored in the database has a specific data type. Two of them are char and varchar, and both are ASCII data types.

Key Areas Covered

1. What is char
     -Definition, Functionality
2. What is varchar
     -Definition, Functionality
3. Difference Between char and varchar
     -Comparison of key differences

Key Terms

char, MySQL, SQL, varchar

Difference Between char and varchar - Comparison Summary

What is char

char is a data type that helps to store fixed-length data or string. The syntax is char[n].  Here, n denotes the string length in bytes. Hence, this data type can store values of fixed length. If the length is less than the defined size, the rest is padded with space characters to match the specified length. Furthermore, the maximum number of characters it can store is 255.

Difference Between char and varchar

An example statement is as follows. It creates a table called student. Moreover, it has two columns, and both of them are capable of storing char values.  

create table student (firstname char(30),

lastname char(50));

What is varchar

varchar is a data type that helps to store variable length data or string. The syntax is varchar [(n|max)]. Here, n denotes the string length in bytes, while max denotes the maximum storage size. Hence, this data type is capable of storing data of variable length along with 1 or byte length prefix. Unlike in char, there is no padding with any characters. Furthermore, it can store a maximum of 65535 characters.

An example statement is as follows. It creates a table called student. Moreover, it has two columns, and both of them are capable of storing varchar values. 

create table student (firstname varchar(20),

lastname varchar(50));

Difference Between char and varchar

Definition

The main difference between char and varchar is that char is a data type available in SQL that helps to store characters, while varchar is a data type available in SQL that helps to store variable characters. 

Stands for

While ‘char’ denotes character, ‘varchar’ denotes variable character.

Main Functionality

Moreover, ‘char’ stores values in fixed lengths, whereas varchar stores values in variable length long with 1 byte or 2-byte length prefix. Thus, this is also a difference between char and varchar.

Maximum characters

Furthermore, while ‘char’ holds a maximum of 255 characters, ‘varchar’ holds a maximum of 65535 characters.

Memory Allocation

Besides, another difference between char and varchar is that ‘char’ uses static memory allocation, whereas ‘varchar’ uses dynamic memory allocation.

Usage

The programmer can use char when the sizes of the column data entries are consistent. On the other hand, the programmer can use varchar when the sizes of the column data entries change considerably.

Conclusion

In brief, MySQL is a widely used RDBMS. Each data stored in the database has a specific data type. Two of them are char and varchar.  The main difference between char and varchar is that char stores values in fixed lengths and are padded with space characters to match the specified length, while varchar stores values in variable length along with 1 byte or 2-byte length prefix and are not padded with any characters. In brief, char represents fixed length data, while varchar represents variable length data. 

References:

1.MikeRayMSFT. “Char and Varchar (Transact-SQL) – SQL Server.” SQL Server | Microsoft Docs, Available here.

Image Courtesy:

1.” Logo of MySQL, only the dolphin” By Oracle – Unknown (Public Domain) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply