What should one use CHAR data type or VARCHAR data type in MySQL?


Actually, both of these data types in MySQL store strings and can be set with a maximum length. The use of these data types purely depends on the need. Followings are some points which will make us clear that when one should CHAR and when VARCHAR −

  • Suppose if we have fixed size of data, such as flags of “Y” and “N” then it is better to use CHAR rather than VARCHAR. It is because 1 byte of length prefix also used with VARCHAR. In other words, for the above kind of data, CHAR will store the only 1byte which is the data only and VARCHAR will store 2 bytes (1 byte for data and 1 byte for length prefix).
  • If we want to store the data such as usernames and passwords or any other variable-length data then it is better to use the VARCHAR data type because then we would be able to store data without wasting extra space.

Updated on: 19-Jun-2020

242 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements