Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql change default table charset to database charset

Tags:

mysql

I have problem with mysql table charset. Every table in my database has default charset. For example:

CREATE TABLE privacy_settings (
  id_privacy_setting int(11) NOT NULL AUTO_INCREMENT,
  id_account int(11) NOT NULL,
  setting_name varchar(255) NOT NULL DEFAULT '0',
  privacy_level int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (id_privacy_setting),
  KEY fk_privacy_settings_accounts (id_account),
  CONSTRAINT fk_privacy_settings_accounts FOREIGN KEY (id_account) REFERENCES accounts (id_account) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8  

I want to remove DEFAULT CHARSET block, so table could use database default charset:

CREATE TABLE privacy_settings (
      id_privacy_setting int(11) NOT NULL AUTO_INCREMENT,
      id_account int(11) NOT NULL,
      setting_name varchar(255) NOT NULL DEFAULT '0',
      privacy_level int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (id_privacy_setting),
      KEY fk_privacy_settings_accounts (id_account),
      CONSTRAINT fk_privacy_settings_accounts FOREIGN KEY (id_account) REFERENCES accounts (id_account) ON DELETE NO ACTION ON UPDATE NO ACTION
    ) ENGINE=InnoDB   

Is there any way to do this without recreating the table?

like image 723
Ris90 Avatar asked Aug 11 '11 15:08

Ris90


People also ask

How do I change the default charset of a MySQL table?

You can change the default with an alter table set default charset but that won't change the charset of the existing columns. To change that you need to use a alter table modify column . Changing the charset of a column only means that it will be able to store a wider range of characters.

How do I set MySQL database to utf8?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

How do I change MySQL from utf8 to latin1?

Similarly, here's the command to change character set of MySQL table from latin1 to UTF8. Replace table_name with your database table name. mysql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; Hopefully, the above tutorial will help you change database character set to utf8mb4 (UTF-8).

How do I find the default charset for MySQL?

To see the default character set and collation for a given database, use these statements: USE db_name; SELECT @@character_set_database, @@collation_database; Alternatively, to display the values without changing the default database: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.


2 Answers

To change a table's character set, from MySQL Documentation:

If you want 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;

If you want to convert an entire database to use a different default character set, you can issue this statement: (from Default Character Set and Collation)

ALTER DATABASE db_name DEFAULT CHARACTER SET charset_name;
like image 53
JYelton Avatar answered Oct 20 '22 01:10

JYelton


You can use the following to change collation and default character set for tables and schemas:

alter table TABLENAME convert to CHARACTER SET utf8 COLLATE utf8_unicode_ci;
alter database SCHEMA default character set utf8 COLLATE utf8_unicode_ci;
like image 22
Abdo Avatar answered Oct 19 '22 23:10

Abdo