William William - 3 months ago 17
SQL Question

If I have a VARCHAR(32) with no data inside it, does it take any space?

I have a MySQL database and I'd like to know if I have a VARCHAR(32) with no data inside it, does it take any space, like some

kb
?

The reason I'm asking it is because I store a hash of 32 chars to check something but after a while I'll not use it anymore, so if it takes some of DB space, I might make this field null or empty.

If so, should I make this field null or empty to save some space?

Answer

NULL values occupy space for the NULL flag. In addition, any fixed component of the data type is also stored. For a VARCHAR() that is the length component, which is either 1 or 2 bytes.

The storage requirement for VARCHAR() is explained in the documentation. For VARCHAR(32) the length is stored as a single byte.

Arrgh. I forgot that in MySQL, this depends on the storage engine. The above is true for MyISAM. It is different for InnoDB. Here is a good answer.