ItSolutionStuff.com

Mysql Hexadecimal Color Code Store in Binary Datatype Example

By Hardik Savani • January 5, 2023
PHP MySql

If you have thousands of record store, that time database size would be large. but you can some field can save your number of bites in the database.

you have color field then most of you choose string(6) data type and you wast 6 bites on every record, but you can save number of bites data by using BINARY(3) data type. following example through you can set data type for hexa color code with only allocate binary(3) on every records. first 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`)

);

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

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

when you fetch or select all the records at that using HEX() that function would convert hexadecimal color code with full size.

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

Try this and save db size......

Tags: MySql
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP AngularJS MySQL Pagination Example

Read Now →

PHP MySQL Column Sorting Example Tutorial

Read Now →

PHP MySQL Integrate Fullcalendar Example Tutorial

Read Now →

PHP Import Excel File into MySQL Database Tutorial

Read Now →

How to Execute MySQL Query in Laravel?

Read Now →

How to Add MySQL Trigger from Migration in Laravel?

Read Now →

How to fetch this week records in MySql ?

Read Now →

How to Check Empty or Null Data using MySQL Query?

Read Now →

How to Copy One Table Data into Another Table using MySQL?

Read Now →

How to count unique domains from email address field in MySQL ?

Read Now →

Mysql procedure with pagination in laravel?

Read Now →

Which MySQL Datatype use for Store an IP Address?

Read Now →