mysql character set conversion - mysql

I have the following UTF-8 table:
CREATE TABLE `table` (
`id` int(11) NOT NULL DEFAULT '0',
`description` mediumtext,
`description_info` mediumtext,
`rights` mediumtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
inside a UTF-8 database:
CREATE DATABASE `DB` /*!40100 DEFAULT CHARACTER SET utf8 */
in this table there are rows with LATIN1 characters, like: è or ò
My question is: how can I convert those characters?
THX!

Use a CONVERT:
SELECT
description,
CONVERT(description USING utg8)
FROM
`table`

I have found a solution here: http://www.percona.com/blog/2013/10/16/utf8-data-on-latin1-tables-converting-to-utf8-without-downtime-or-double-encoding/
Those are the steps I have applied to convert latin1 text inside the 'description' column to UTF8:
ALTER TABLE table CONVERT TO CHARACTER SET latin1 -- this is because my table is in UTF8
ALTER TABLE table CHANGE description description BLOB;
ALTER TABLE table CONVERT TO CHARACTER SET utf8, CHANGE description description mediumtext;
I don't know if this is the most effective method to this... but it works!

Related

MySQL utf8 multibyte (utf8mb4) insert duplicate entry problem

I have two words ('বাঁধা' and 'বাধা') to be inserted in a mysql (8.0.12 - MySQL Community Server - GPL) table. The word 'বাঁধা' is inserted correctly. But when inserting 'বাধা', mysql produces an error:
INSERT INTO lc6_words(jp_word, jp_fcharascii) VALUES('বাঁধা', 2476);
/*Query OK*/
INSERT INTO lc6_words(jp_word, jp_fcharascii) VALUES('বাধা', 2476);
/*#1062 - Duplicate entry 'বাধা' for key 'jp_word'*/
The table structure:
CREATE TABLE IF NOT EXISTS `lc6_words` (
`jp_wkey` BIGINT NOT NULL AUTO_INCREMENT,
`jp_word` varchar(255) NOT NULL,
`jp_fcharascii` int UNSIGNED NOT NULL,
`jp_word_occ` BIGINT UNSIGNED NOT NULL DEFAULT 1,
UNIQUE(`jp_word`),
PRIMARY KEY (`jp_wkey`)
) ENGINE=MyISAM DEFAULT CHARSET=UTF8MB4 COLLATE=utf8mb4_bin;
Relevant queries and their output:
SELECT jp_wkey FROM lc6_words WHERE BINARY jp_word='বাঁধা';
/* 1 */
SELECT jp_wkey FROM lc6_words WHERE BINARY jp_word='বাধা';
/* Empty */
Thanks for reading this far. And some more too if you share your thoughts :).
There seems to be problem in collation. After running the command below, all worked perfectly:
ALTER TABLE lc6_words MODIFY jp_word VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
Note: The VARCHAR size changed from 255 to 191.

Incorrect string value: '\xF0\x9F\x92\x8F\x0A#...for column in mysql?

I am new to MySQL.
I have a column which stores Unicode character in MySQL table.
I have a code which gets the posts from the twitter,facebook etc. and
insert the text/symbol 'as is' into the table.
CREATE TABLE `tbl_unicode` (
`_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`uninoce_Column` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8;
/* This query WONT work due to SYMBOL:💏 */
INSERT INTO `tbl_unicode`
(`_id`,
`first_name`,
`uninoce_Column`)
VALUES
(2,
'rakesh',
'最高の休日💏
#京都#宮津#天の橋立#');
/* This query WORKS FINE */
INSERT INTO `tbl_unicode`
(`_id`,
`first_name`,
`uninoce_Column`)
VALUES
(1,
'shekhar',
'最高の休日
#京都#宮津#天の橋立#');
enter code here
Live Demo in SQLFiddle
So, which datatype or configuration should i do to make it work.
Any suggestions?
CHARACTER SET utf8
MySQL's utf8 charset is not UTF-8. That would be too easy! Instead, it's a limited subset of UTF-8 where only code points up to U+00FFFF, which can be stored in three UTF-8 bytes, are supported. 💏 is U+01F48F, so it doesn't fit in the table's charset.
The real UTF-8 character set is known in MySQL (5.5 and later) as utf8mb4.
Use nvarchar as datatype for your table fields. varchar only stores 8-bit characters while nvarchar also stores unicode characters.

How to convert a column with "character set latin1 collate latin1_bin" into utf8 in Mysql?

Ok, in the existing table,
CREATE TABLE tb(id int, `text` varchar(255) character set latin1 collate latin1_bin default NULL)ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to make the text column to be utf8 and it supports Case Sensitive for select query
Suppose there is "ã" & "Ã" which is utf8 in the text column
id - text
1 - ã
2 - Ã
, and when user select * from tb where text='Ã'it should show record with id=2 only
Is this query the correct one
alter table tb modify column text varchar(255) character set utf8 collate utf8_general_ci default NULL
I have no idea what utf8_general_ci means? am i doing cortrectly?
This can be done by using the Alter Command.
Like ALTER TABLE urtable MODIFY col1 VARCHAR(50) CHARACTER SET utf8;
See column charset conversion here
this is the correct one
alter table tb modify text varchar(255) character set utf8 collate utf8_bin default NULL

Changing default charset to latin

When I change the default charset from UTF-8 to latin1, it changes the text, varchar columns to utf8
So the new table looks something like this...
...
`include_zoneclass` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
...
PRIMARY KEY (`campaignid`)
) ENGINE=InnoDB AUTO_INCREMENT=2082 DEFAULT CHARSET=latin1
How do I make the table totally latin1 and no trace of utf8 should be there.
Try using the ALTER TABLE command with CONVERT TO CHARACTER SET
See here for more:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

MySQL UPPER() and LOWER() not working with utf-8 characters

I have a search function, I want it to be case insensitive, including characters like éüò etc.
So, i transform the input to uppercase before querying the database. But MySQL doesn't convert the accented characters right.
SELECT * FROM items WHERE UPPER(description) = $input
I have MySQL 5.1.32, i have tried different collations but none seem to work right. Same with LOWER().
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_bin,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=187 DEFAULT CHARSET=utf8"
The description field contains 'hellö'. Mysql converts it to 'HELLö'. I want 'HELLÖ'.
This works for me:
CREATE TABLE items (id INT NOT NULL, name VARCHAR(100) COLLATE UTF8_GENERAL_CI) ENGINE=InnoDB;
INSERT
INTO items
VALUES (1, 'Eyjafjallajökull');
SELECT *
FROM items
WHERE name = 'EYJAFJALLAJOKULL';
--
1 Eyjafjallajökull
SELECT UPPER('Eyjafjallajökull')
FROM items;
--
EYJAFJALLAJÖKULL