Mysql Hexadecimal color code store in binary datatype

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog I will show you how to mysql hexadecimal color code store in binary data type. If we have more than thousands of record to store, so that time database size would be more large. But that time we can some field can save to our number of bites in the database.

Now we have a color field then we most of choose string(6) for data type and we use 6 bites on every record, but when we can save number of bites data by using BINARY(3) data type. By following this example through we can set data type for hexa color code and with only allocate binary(3) on our every records. So first we know how to create table and assign data type with following query.

CREATE TABLE IF NOT EXISTS `colors` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `color_name` varchar(255) NOT NULL,  `color` BINARY(3) NOT NULL,  PRIMARY KEY (`id`));

Now when we want to insert then data at that time with by using UNHEX() that function would convert in small size.

INSERT INTO `colors` (color_name, color) VALUES ('Green',UNHEX('f2a709'));


When we need fetch or select all the records at that time by using HEX() that function would convert hexadecimal color code with full size.

SELECT color_name ,HEX(color) AS color FROM `colors`; 

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read this, I hope it will you. You can also follow us onĀ Facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →