change all existing collation of columns - mysql

I have a table which some of its columns has collation of utf8_bin. But I need to change them to utf8_unicode_ci.
I know this query for do it:
ALTER TABLE `shn_sho_cities` CHANGE `city_name` `city_name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;
but as I want change all columns which have utf8_bin instead of writing a query for each column.
Is there any way which I could do this?

The following code will generate the required queries to change your collations, to utf8mb4 with utf8mb4_unicode_ci
SELECT CONCAT('ALTER TABLE `', TABLE_NAME,'` CONVERT TO CHARACTER SET
utf8mb4 COLLATE utf8mb4_unicode_ci;') AS mySQL
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= "YOU-DB-NAME"
AND TABLE_TYPE="BASE TABLE"

Related

find all the columns of a database with %latin1% value

How could I do this SQL query?
Name Database: database_1
Column: Collation
Value: latin1_swedish_ci
I need the list of ALL "database_1" tables that have columns whose value contains the text %latin1%.
My final goal: I need to change these values in all the tables that contain this data, for example:
CHARACTER SET latin1 COLLATE latin1_swedish_ci
Change to:
CHARACTER SET utf8 COLLATE utf8_unicode_ci
I already have the consult ready for replacement:
ALTER TABLE `blog_sitemapconf` CHANGE `value` `value` VARCHAR(100) CHARACTER SET latin1 COLLATE latin1_english_ci NOT NULL DEFAULT '';
But I need to get the name of all tables that contain columns with
CHARACTER SET latin1 COLLATE latin1_swedish_ci
You can query this information from the information_schema.columns table:
SELECT table_name, column_name
FROM information_schema.columns
WHERE collation_name LIKE '%latin1%'
First, determine how you will be doing the charset conversion. If the table is correctly encoded in latin1, then this is the desired conversion:
ALTER TABLE tbl CONVERT TO CHARACTER SET utf8;
If, instead, you have utf8 bytes stored in latin1 columns, then the 'fix' is more complex.
However, that changes all CHAR/TEXT columns in the table. Is that OK?
If that is OK, then this will generate all the ALTERs.
SELECT DISTINCT
CONCAT("ALTER TABLE ", table_schema, ".", table_name,
" CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;")
FROM information_schema.columns
WHERE character_set = 'latin1'
AND table_schema NOT IN ('mysql', 'information_schema', 'performance_schema');
You can then copy/paste them into the mysql commandline tool.
If you need to change only individual columns, then the fix is difficult because the rest of the attributes of each column must be repeated. This is not easy to do in a query.

How to change MYSQL Database columns filed Collation

I need to change below collation to utf8_general_ci
You can use Alter table clause
ALTER TABLE `change_messages` CHANGE `message` `message` TEXT CHARSET utf8 COLLATE utf8_general_ci;

MYSQL Query to change all Collation

I have one table that all Collation is set to latin1_swedish_c, i need to change them all to utf8_unicode_ci and set all newone
Is there SQL statment for that, because i have tried
ALTER DATABASE 'empinity' convert TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Try this way:
ALTER TABLE `your table` CHANGE `table_field` `table_field` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci ;
For database:
ALTER DATABASE database_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;
For changing collation of all tables as once, run the query:
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE utf8_unicode_ci")
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="your_database_name"
AND TABLE_TYPE="BASE TABLE"

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

Mysql column to utf8_bin

I have a table in mysql. I want to change one of the columns to have utf8_bin as it's collate attribute.
I know the command to do for whole table is like:
ALTER TABLE temp CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;
But how do I do it for individual columns?
Thanks for the help
It's right in the manual
Their example
ALTER TABLE t1 MODIFY
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_swedish_ci;