Store CSS color values in DB Store CSS color values in DB mysql mysql

Store CSS color values in DB


save the color with a char(6) without the # if the column is not null... if the column is nullable use a varchar(6) to saving size


If you care about disk space, you should use BINARY(3)

This answer is for advanced usage, and won't be relevant to most devs (the accepted answer is good). I'm posting here because this is the first result on Google.

There's a trade off between maintainability and disk space. A colour represented by css hex i.e. #f8f8f8 can also be represented as BINARY(3)

The difficulty with BINARY(3) is that you have to convert the hex strings back and forth to binary. You can do this with HEX() and UNHEX() but anyone maintaining your database will need to learn about binary types if they want to make changes. It's easy to underestimate the problem this will cause for future devs.

However there are benifits to using BINARY(3). The data saved to the disk for #f8f8f8 if saved as CHAR(6) is 101010101011100011010001110101001001111111110 where as if it is saved as BINARY(3) it'll be 111110001111100011111000