CHAR vs VARCHAR in SQL

1. CHAR Datatype:
It is a datatype in SQL which is used to store character string of fixed length specified. If the length of string is less than set or fixed length then it is padded with extra blank spaces so that its length became equal to the set length. Storage size of CHAR datatype is of n bytes(set length). We should use this datatype when we expect the data values in a column are of same length.

Example:

Consider the Query:

CREATE TABLE Student(Name VARCHAR(30), Gender CHAR(6));
INSERT into Student VALUES('Herry', 'Male');
INSERT into Student VALUES('Mahi', 'Female');
SELECT LENGTH(Gender) FROM Student;

OUTPUT:

LENGTH(Gender)
6
6

2. VARCHAR Datatype:
It is a datatype in SQL which is used to store character string of variable length but maximum of set length specified. If the length of string is less than set or fixed length then it will store as it is without padded with extra blank spaces. Storage size of VARCHAR datatype is equal to the actual length of the entered string in bytes. We should use this datatype when we expect the data values in a column are of variable length.



Example:

Consider the Query:

CREATE TABLE Student(Name VARCHAR(20), Gender CHAR(6));
INSERT into Student VALUES('Herry', 'Male');
INSERT into Student VALUES('Mahi', 'Female');
SELECT LENGTH(Name) FROM Student;

OUTPUT:

LENGTH(Name)
5
4

Difference between CHAR and VARCHAR dataypes

SR.NO. CHAR VARCHAR
1. CHAR datatype is used to store character string of fixed length VARCHAR datatype is used to store character string of variable length
2. In CHAR, If the length of string is less than set or fixed length then it is padded with extra memory space. In VARCHAR, If the length of string is less than set or fixed length then it will store as it is without padded with extra memory spaces.
3. CHAR stands for “Character” VARCHAR stands for “Variable Character”
4. Storage size of CHAR datatypes is equal to n bytes i.e. set length Storage size of VARCHAR datatype is equal to the actual length of the entered string in bytes.
5. We should use CHAR datatype when we expect the data values in a column are of same length. We should use VARCHAR datatype when we expect the data values in a column are of variable length.
6. CHAR take 1 byte for each character VARCHAR take 1 byte for each character and some extra bytes for holding length information
9. Better performance than VARCHAR Performance is not good as compared to CHAR

Conclusion : VARCHAR saves space when there is variation in length of values, but CHAR might be performance wise better.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.