MYSQL Query to change all Collation - mysql

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"

Related

i have to set all the database charset utf8_general_ci

I am working in a multi language website I have to set all table column charset utf8_general_ci in a database.
How can I change that. Is it a short method?
Create a backup then execute following
For Database
ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
For Table
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Try This :
SELECT CONCAT('ALTER TABLE `', TABLE_NAME,'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;') AS mySQL
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA= "myschema"
AND TABLE_TYPE="BASE TABLE"

How to modify mariadb chartset at once?

I am developing with mariadb and Spring, JdbcTemplate.
At first, we made DB charset as utf8, but now we have to change it into utf8mb4 because of emojis.
Till now I update individual charset with query something like below.
ALTER TABLE WT_WORKS CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE WT_WORKS CHANGE WORKS_TITLE WORKS_TITLE VARCHAR(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE WT_WORKS CHANGE WORKS_DESC WORKS_DESC VARCHAR(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
However It is now efficient because of table relations. For example when I insert into WT_WORKS, also need to into WT_WRITERS. It looks impossible to find every tables and columns.
So I want to know change these at once.(Including Procedures and Functions). -- something like (v_name VARCHAR(10)) into (v_name VARCHAR(10) CHARSET utf8mb4).
Thanks for answer.
FYI. my my.cnf got follow settings
[client]
default-character-set=utf8mb4
[mysqld]
collation-server = utf8mb4_unicode_ci
character-set-server = utf8mb4
[client]
default-character-set = utf8mb4
SELECT DISTINCT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.COLUMNS
WHERE CHARACTER_SET_NAME = 'utf8'
will list all the tables that still have some column set to utf8. When them, do ALTER TABLE .. CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
This should generate all the ALTERs you need:
SELECT DISTINCT
CONCAT(
"ALTER TABLE ", TABLE_SCHEMA, ".", TABLE_NAME,
" CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;"
)
FROM information_schema.COLUMNS
WHERE CHARACTER_SET_NAME = 'utf8'
Then copy and paste them into the mysql command line tool.
Caveat: If you have some columns with charsets other than utf8, they will be blindly converted to utf8mb4. This would be bad for hex, ascii, etc., columns, such as country_code, uuid, md5, etc.
You could do something similar to change individual columns instead.
You do not need to do both.

change all existing collation of columns

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"

Creating a MySQL Database with a Specific Character Set and Collation

When creating a MySQL database with CREATE SCHEMA, it uses MySQL's default character set and collation. But I want to change that.
How do I create a MySQL Database with a specific character set and collation?
You can try like this:
CREATE DATABASE IF NOT EXISTS someDatabase
DEFAULT CHARACTER SET = 'utf8' DEFAULT COLLATE 'utf8_general_ci'
and if the database is already created, then you can alter is like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE SCHEMA `my_database`
DEFAULT CHARACTER SET utf8
COLLATE utf8_unicode_ci ;

Mysql: Set column charset

I have an existing table and I want to convert the charset only for one specific column to utf-8.
I know that this command ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 does it for the whole table but I'm looking for a column-specific command.
Is there a command for that?
Try this:
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8mb4;
I share that, it can always help...
I modified a database recently; moving from utf8 to utf8mb4; here is the script that allowed me to generate the alters...
Generate SQL commands to alter the tables:
SELECT CONCAT("ALTER TABLE `",`TABLE_NAME`,"` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;")
FROM `information_schema`.`TABLES`
WHERE `TABLE_SCHEMA` = 'xxxx';
Generate SQL commands to alter each column:
SELECT CONCAT("ALTER TABLE `",`TABLE_NAME`,"` MODIFY `",`COLUMN_NAME`,"` ",COLUMN_TYPE," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ",IF(`IS_NULLABLE`='YES', 'NULL', 'NOT NULL')," ",IF(`COLUMN_DEFAULT` IS NOT NULL, CONCAT(" DEFAULT '", `COLUMN_DEFAULT`, "'"), ''),";")
FROM `information_schema`.`COLUMNS`
WHERE `TABLE_SCHEMA` = 'xxx' AND `TABLE_NAME` = 'xxxx' AND (`CHARACTER_SET_NAME` IS NOT NULL OR `COLLATION_NAME` IS NOT NULL);
Note that for foreign keys and primary keys that make a relationship, you will need to disable foregin key checks before modifying the column
SET FOREIGN_KEY_CHECKS=0;
and enable afterwards.
SET FOREIGN_KEY_CHECKS=1;
Below one worked for me.
ALTER TABLE table_name
MODIFY column_name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
This one worked for me.
ALTER TABLE `table_name` CHANGE `column_name` `column_name` TEXT CHARACTER SET utf8 COLLATE utf8mb4_unicode_ci;