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"
Related
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.
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"
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 ;
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;
I know there are many settings for a language for a table and a database.
I already created the database. I believe when I created it, it was default/LATIN. I want to change everything-I mean...both the table and the database, to UTF-8.
How can I do that? thanks.
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;
Have a look at Using alter command to change character set.
Another useful link: http://dev.mysql.com/doc/refman/5.0/en/charset-table.html
The general form is
ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]
and for a specific column in a table
ALTER TABLE column COLLATE utf8_general_ci
MySQL’s character sets and collations demystified
MySQL Forums :: Change Collation
aioobe's answer tells how to change the character set of a database, table or column. You should keep in mind that
setting the character set for a table just specifies the default character set for new columns in that table. It doesn't change the character set for preexisting columns; you have to do those columns individually, OR if you want to change every single string-type column in the table to the same character set there's a command you can use to do that: "alter table ... convert to character set" ( http://dev.mysql.com/doc/refman/5.1/en/alter-table.html )
if you already have data that is stored mis-encoded in a column, then using "alter table ... modify" to change the column will not quite solve the problem. For example, if you're been storing UTF-8 data in a Latin1 column and you change the character set directly from Latin1 to UTF-8, it'll still be mis-encoded afterwards. This can be worked around by converting from Latin-1 to UTF-8 via binary.
1) Database default character set and collation:
SELECT ##character_set_database, ##collation_database;
Altered via: ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
2) Table default character set and collation:
SELECT T.table_name, CCSA.character_set_name
FROM information_schema.TABLES T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA
WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "YOUR_DB";`
Altered via: ALTER TABLE [table_name] CHARACTER SET utf8 COLLATE utf8_general_ci
3) Column character set and collation:
SELECT c.TABLE_NAME, c.COLUMN_NAME, c.CHARACTER_SET_NAME, c.COLLATION_NAME
FROM information_schema.COLUMNS c
WHERE c.table_schema = "YOUR_DB";`
Altered via: ALTER TABLE [table_name] CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
The third one requires that you disable foreign key checks for the data conversion. So putting this all together:
DELIMITER //
CREATE PROCEDURE migrate_charset_to_utf8()
BEGIN
DECLARE done TINYINT DEFAULT 0;
DECLARE curr_table VARCHAR(64);
DECLARE table_cursor CURSOR FOR
SELECT T.table_name
FROM information_schema.TABLES T
WHERE T.TABLE_TYPE = 'BASE TABLE' AND
T.TABLE_SCHEMA = 'YOUR_DB';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN table_cursor;
table_loop: LOOP
FETCH table_cursor INTO curr_table;
IF done THEN
LEAVE table_loop;
END IF;
# Convert table data(columns) charset
SET #sql_str1 = CONCAT("ALTER TABLE ", curr_table, " CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
PREPARE stmt1 FROM #sql_str1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
# Set table's default charset e.g for new columns added
SET #sql_str2 = CONCAT("ALTER TABLE ", curr_table, " CHARACTER SET utf8 COLLATE utf8_general_ci");
PREPARE stmt2 FROM #sql_str2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
END LOOP table_loop;
CLOSE table_cursor;
END//
DELIMITER ;
SET ##FOREIGN_KEY_CHECKS = 0;
CALL migrate_charset_to_utf8();
SET ##FOREIGN_KEY_CHECKS = 1;
ALTER DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci;
EDIT: look here instead
Add to your my.cnf this:
[mysqld]
character-set-server=utf8
default-collation=utf8_unicode_ci
And restart mysqld deamon.
ADDED:
ALTER DATABASE your_base_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;
and my.cnf
SET collation_connection = utf8_unicode_ci;
SET character_set_results = utf8;
SET character_set_connection = utf8;
SET character_set_client = utf8;