Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store CSS color values in DB

I need to store a CSS HEX color value (#FFFFFF for example) into a mysql database.

Which type of data would I use and why it would be better over using another one?

like image 453
Hommer Smith Avatar asked Nov 29 '12 14:11

Hommer Smith


2 Answers

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

like image 199
Thomas Horrobin Avatar answered Oct 03 '22 07:10

Thomas Horrobin


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

like image 41
silly Avatar answered Oct 03 '22 07:10

silly