Difference Between Similar Terms and Objects

Difference Between Char and Varchar

Difference Between Char and Varchar

Both are data types in many programming languages and database systems where ‘char’ refers to character and ‘varchar’ refers to variable character. Char in C represents the character type that is used to store string values, mostly UTF-8 encoded characters and integers. Varchar, on the other hand, is a data type which can contain data of any type of indeterminate length. Varchar refers to a data type of a field in a database management system. While they both can store string values up to a maximum length of 8,000 characters, char requires more storage than varchar. Technically, they are both used to store same types of data but they differ in the way they are stored and retrieved. Let’s take a look at their differences in detail.

What is Char?

Char is a fixed-length data type that is used to store non-Unicode characters, hence the name (short for character). It occupies one byte of space for each character which are encoded as numbers – the ones from the ASCII encoding. The char type can also be used to declare small integers. To declare a character variable, the keyword ‘char’ is used, which means a single character is stored in one byte.

Like the integer types, char can be signed or unsigned. It can hold signed char values ranging from -128 to 127 and depending on the architectural size, it can also be unsigned, holding values ranging from 0 to 255. When char values are stored, they are right-padded with spaces to the specified length. The trailing spaces are removed when they are retrieved.

For example – if you declare a variable of char (7) data type, then it will always take 7 bytes of data regardless of whether you are storing 1 character or 7 characters, which means you can store maximum of 7 characters in the column.

What is Varchar?

Varchar, as the name suggests, is a variable-length data type which can contain any type of data with the length varying from 0 to 65,535. Varchar field can store values of any size up to a certain limit, depending on the database. It can be defined either in programming languages or on the database level. The size of the varchar field can be anything from zero to the maximum declared field length.

To declare a variable character, the ‘varchar’ keyword is used. Varchar takes a variable space, which means it will use only the number of bytes equal to the number of characters. It helps in avoiding the waste of space as it only utilizes the space required for the size of the string. In some programming languages and database systems, any extra space is removed automatically from the database.

For example – if you declare a variable of varchar (10), it will use the number of bytes equal to the number of characters. So, if you’re storing only one character, then it will take only one byte and if you’re storing 10 characters, it will take 10 bytes, thus avoiding wastage of database space.

Difference between Char and Varchar

  1. Data Type

‘Char’ is a fixed-length data type which is used to store character string value of fixed length, whereas ‘Varchar’ is a variable-length data type which is used to store variable length alphanumeric data.

  1. Storage Size

The storage size of the character value is equal to the maximum size of this column that you declare while creating the table. On the other hand, the storage size of the varchar value is the actual length of the data entered, not the maximum size for this column.

  1. Data Entries

You can use char when the data entries in a column are expected to be the same size, while on the contrary, varchar can be used when the data entries in a column are expected to vary in size.

  1. Memory Allocation

Char uses static memory allocation while varchar uses dynamic memory allocation

  1. Length

The length of a char variable can be of any value from 0 to 255, while the length of varchar variable ranges from 0 to 65,535.

  1. Application

Data entries are consistent in char which is used for storing data like phone numbers, whereas varchar is used for storing varying data like addresses.

Char vs. Varchar

Char Varchar
Used to store character string value of fixed length. Used to store alphanumeric data of variable length.
The length varies from 0 to 255. The length varies from 0 to 65,535.
Takes 1 byte per character for storage. Takes 1 byte per character plus 1 or 2 extra bytes for storing length information.
The storage size of char is the same as declared. The storage size of varchar depends on the specific string stored.
Uses static memory allocation. Uses dynamic memory allocation.
Char should be used when the length of the variable is known. Varchar should be used only when the length of the variable is not known.
It only accepts characters. It accepts both characters and numbers.
It’s 50 percent faster than Varchar. It’s slower than Char.
The storage size of the char value is equal to the maximum size for the column. The storage size of the varchar value is equal to the actual length of the data entered, not the maximum size for the column.

Summary

  • Both ‘Char’ and ‘Varchar’ are data types in programming languages and database systems that share some common traits in terms of functionality and technicality. However, they differ considerably like the way they are stored and retrieved.
  • While char actually refers to character, varchar refers to variable character. As the name suggests, char is a fixed-length data type while varchar is a variable-length data type.
  • Char takes up to 1 byte per character, whereas varchar also takes up to 1 byte per character plus extra 1 or 2 bytes to store length information. For char, the length varies from 0 to 255 and for varchar, it can be anything between 0 and 65,535.
  • As char is fixed-length, any remaining space in the field is padded with blanks. Varchar, on the other hand, is variable-length so it holds only the characters you assign to it.
  • Remaining characters are padded with white spaces when the values are stored in ‘char’ fields, whereas ‘varchar’ doesn’t add extra spaces when you provide the less data than specified length.

Search DifferenceBetween.net :

Custom Search


Help us improve. Rate this post! 1 Star2 Stars3 Stars4 Stars5 Stars
Loading...

Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.



Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Molinaro, Anthony. SQL Cookbook. Sebastopol: O’Reilly Media, 2005. Print

[1]Molinaro, Anthony. SQL Cookbook. Sebastopol: O’Reilly Media, 2005. Print

[2]Beaulieu, Alan. Learning SQL. Sebastopol: O’Reilly Media, 2005. Print

[3]Ritchie, Dennis and Brian Kernighan. The C Programming Language (1st ed.). New Jersey: Prentice Hall, 1978. Print

[4]Image Credit: https://commons.wikimedia.org/wiki/File:DB2_Character_Data_Types.png

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : ,
Protected by Copyscape Plagiarism Finder