Mysql -change DB,tables to utf8 - mysql

In /etc/my.cnf the following has been added
character-set-server=utf8
collation-server=utf8_general_ci
But for the database and tables created before adding the above how to convert the database and tables to utf8 with collation settings

Well, the database character set and table character set are just defaults (they don't affect anything directly). You'd need to modify each column to the proper charset. PHPMyAdmin will do this for you (just edit the column, then change the character set). If you want to do raw SQL, you'll need to know the column definition (SHOW CREATE TABLE foo will show you the definition). Then, you can use ALTER TABLE to change the definition.
To change the default charset for a table:
ALTER TABLE `tablename` DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
To change the charset of a column with the definition `foo VARCHAR(128) CHARACTER SET 'foo' COLLATE 'foo'``:
ALTER TABLE `tablename` MODIFY
`foo` VARCHAR(128) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

https://serverfault.com/questions/65043/alter-charset-and-collation-in-all-columns-in-all-tables-in-mysql
And:
http://www.mysqlperformanceblog.com/2009/03/17/converting-character-sets/

Related

Convert database to utf8

Hello I'm trying to convert my database, one table and field to utf using this script
-- Write a script that converts hbtn_0c_0 database to UTF8
-- (utf8mb4, collate utf8mb4_unicode_ci) in your MySQL server.
-- You need to convert all of the following to UTF8:
-- Database hbtn_0c_0
-- Table first_table
-- Field name in first_table
ALTER DATABASE
`hbtn_0c_0`
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE `hbtn_0c_0`;
ALTER TABLE
`first_table`
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
ALTER TABLE
`first_table`
CHANGE `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
But I have a SQL error. Please help me
black_genius#genius:~/Documents/ALX_Task/alx-higher_level_programming/0x0D-SQL_introduction$ cat 100-move_to_utf8.sql | mysql -hlocalhost -uroot -p
Enter password:
ERROR 1064 (42000) at line 22: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci' at line 4
I'm using mysql version v8.0.31 on ubuntu 22.10
When you change a column, you need to provide the old name and the new name, even if the name is the same. See the syntax in the documentation:
CHANGE [COLUMN] old_col_name new_col_name column_definition
In your case it should be
ALTER TABLE
`first_table`
CHANGE `name` `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Demonstration: . db-fiddle.com/f/qFuvFqP5PEGbsc8F8DfiaN/0
You don't need to change the individual column if you use ALTER TABLE ... CONVERT TO CHARACTER SET .... That ALTER TABLE automatically converts all string-based columns.
The documentation describes:
To change the table default character set and all character columns
(CHAR, VARCHAR, TEXT) to a new character set, use a statement like
this:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
The statement also changes the collation of all character columns.
Paul Spiegel's answer about the CHANGE COLUMN syntax is correct; that syntax allows you to change a column's name, so you need to specify the column name twice.
An alternative is to use MODIFY COLUMN instead of CHANGE COLUMN. This allows you to change the column type and options, including character set, but not to change the column name. So there's no need to include the column name twice.
ALTER TABLE
`first_table`
MODIFY `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
But again, in your example, there's no need to use either CHANGE COLUMN or MODIFY COLUMN. The character set conversion should be achieved by using the CONVERT TO CHARACTER SET action.

How to import amharic data from to mysql database?

I found this from stack overflow
ALTER DATABASE ethioVision CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
but still cannot add file
Changing the default character sets of a table or a schema does not change the data in the column itself, it only changes the default to apply the next time you add a table or add a column to a table.
To convert current data, alter one table at a time:
ALTER TABLE <name> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

Can column comment be a utf-8 in MySQL?

Here is quetions about adding comment to column for MySQL. Can this comment be utf-8? Also what encoding MySQL uses for these columns by default?
Default character set and collation is set when the database is created
CREATE DATABASE mydb
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
You can modify character set on a specific column like this
ALTER TABLE t MODIFY col1 CHAR(50) CHARACTER SET utf8;

Is CHARACTER SET redundant when specifying COLLATION in MySQL?

I've always been surprised that MySQL and related tools tend to specify both CHARACTER SET and COLLATION in CREATE TABLE AND ALTER TABLE statements:
SHOW CREATE TABLE test;
CREATE TABLE ...
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I select a collation for a column in MySQL Workbench, I get:
ALTER TABLE ...
CHARACTER SET 'latin1' COLLATE 'latin1_general_ci' ...;
I've always supposed that specifying the character set and the collation was redundant, as the collation implies the character set.
Am I wrong?
When I try to mix a collation with another charset, I get an error:
CREATE TABLE ... DEFAULT CHARACTER SET utf8 collate latin1_bin;
COLLATION 'latin1_bin' is not valid for CHARACTER SET 'utf8'
Is it safe to only ever specify the collation?
If so, why do all these tools systematically include the charset in the statement, too?
You are not required to specify character set when creating table, it's automatically set on collate as it described in the MySQL Reference Manual:
MySQL chooses the table character set and collation in the following
manner:
If both CHARACTER SET charset_name and COLLATE collation_name are specified, character set charset_name and collation collation_name are used.
If CHARACTER SET charset_name is specified without COLLATE, character set charset_name and its default collation are used. To see the default collation for each character set, use the SHOW CHARACTER SET statement or query the INFORMATION_SCHEMA CHARACTER_SETS table.
If COLLATE collation_name is specified without CHARACTER SET, the character set associated with collation_name and collation collation_name are used.
Otherwise (neither CHARACTER SET nor COLLATE is specified), the database character set and collation are used.
The table character set and collation are used as default values
for column definitions if the column character set and collation are
not specified in individual column definitions. The table character
set and collation are MySQL extensions; there are no such things
in standard SQL.
So you can create a table using this query:
CREATE TABLE `tabletest` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 COLLATE=latin1_general_ci;
This query will create a table with CHARSET=latin1 COLLATE=latin1_general_ci, so it's safe to specify COLLATE only.
As for why are there both CHARSET and COLLATE, please read the following:
What is the difference between collation and character set?

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 ;