Tuesday 11 August 2015

How To Store UTF-8 Character in MySql


What is UTF-8 ?



  • UTF-8 & UTF 16 are Unicode Character Sets.
  • UTF-8 is the preferred encoding for e-mail and web pages. 
  • UTF-8 is variable-length and uses 8-bit code units.
  • UTF-8 is backwards compatible with ASCII.


How to handle with mysql?

First of all check character set and collation of  your database,to execute following query :

SHOW VARIABLES LIKE "character_set_database";

Variable_name                 Value
-----------------------------------------------    
character_set_database      latin1


SHOW VARIABLES LIKE "collation_database";

Variable_name                     Value          
-----------------------------------------------------------
collation_database            latin1_swedish_ci

OR you can also execute this query : SHOW VARIABLES LIKE  'char%';

As a result you get result like this:




  • So here default character set is UTF 8 so no big bloom..if not then whenever you create table set CHARSET=utf8 and Collation = utf8_general_ci

Let us create one table :

CREATE TABLE product (
  id bigint(20) NOT NULL AUTO_INCREMENT,
  name varchar(50) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


If you have already created table then execute following alter query

ALTER TABLE product 
MODIFY `name` VARCHAR(255) CHARACTER SET utf8 NOT NULL DEFAULT ''

Now you can insert or update utf-8 character easily..enjoy..lots..!!!!



No comments:

Post a Comment