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;
Related
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.
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"
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"
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"
I am asked to set all table's default COLLATE to utf8_bin. How to do this?
How to do this?
One at a time, I'm afraid...
alter table <some_table> convert to character set utf8 collate utf8_bin;
There's no bulk method, unless you feel like using mysqldump to get the whole DB out, edit the resulting dump to add the required collation and then reimporting the whole DB again.
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE utf8_bin") AS String
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="YourDatabaseName"
AND TABLE_TYPE="BASE TABLE"
One query to rule all,just run the result of this query.
ALTER TABLE <table name> COLLATE utf8_bin;
If you also need to update the existing character encoding (unlikely by the sounds of things), you can use:
ALTER TABLE <table name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;