rss
twitter
  • Showcase
  • Carrières
  • Support
  • GitLab
  • Espace client
  • Contact

Changer l’interclassement de plusieurs tables MySQL à la fois (ou presque

0
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE your_collation_name_here;") AS    ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="YourDatabaseName"
AND TABLE_TYPE="BASE TABLE"

Reste à copier-coller les requêtes générées… Exemple pour WordPress :

ALTER TABLE wp_commentmeta COLLATE utf8_general_ci;
ALTER TABLE wp_comments COLLATE utf8_general_ci;
ALTER TABLE wp_links COLLATE utf8_general_ci;
ALTER TABLE wp_options COLLATE utf8_general_ci;
ALTER TABLE wp_postmeta COLLATE utf8_general_ci;
ALTER TABLE wp_posts COLLATE utf8_general_ci;
ALTER TABLE wp_term_relationships COLLATE utf8_general_ci;
ALTER TABLE wp_term_taxonomy COLLATE utf8_general_ci;
ALTER TABLE wp_termmeta COLLATE utf8_general_ci;
ALTER TABLE wp_terms COLLATE utf8_general_ci;
ALTER TABLE wp_usermeta COLLATE utf8_general_ci;
ALTER TABLE wp_users COLLATE utf8_general_ci;


Une autre possibilité issue de StackOverlow, que je ne prends même pas la peine de traduire :

I have a solution that will convert databases and tables by running a few commands. It also converts all columns of the type varchar, text, tinytext, mediumtext, longtext. You can add other types like char by simply extending the queries in the following code. You should also backup your database in case something breaks.

Copy the following code into a file called it preAlterTables.sql:

use information_schema;
SELECT concat("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8 COLLATE = utf8_unicode_ci;") as _sql 
FROM `TABLES` where table_schema like "yourDbName" group by table_schema;
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name,"` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;") as _sql 
FROM `TABLES` where table_schema like "yourDbName" group by table_schema, table_name;
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8 COLLATE utf8_unicode_ci;") as _sql 
FROM `COLUMNS` where table_schema like "yourDbName" and data_type in ('varchar');
SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8 COLLATE utf8_unicode_ci;") as _sql 
FROM `COLUMNS` where table_schema like "yourDbName" and data_type in ('text','tinytext','mediumtext','longtext');
Replace all occurences of "yourDbName" with the database you want to convert. Then run:

mysql -uroot < preAlterTables.sql | egrep '^ALTER' > alterTables.sql
This will generate a new file alterTables.sql, with all the queries you need to convert the database. Run the following command to start the conversion:

mysql -uroot < alterTables.sql
You can also adapt this to run through multiple databases, by changing the condition for the table_schema. For example table_schema like "wiki_%" will convert all databases with the name prefix wiki_. To convert all databases replace the condition with table_type!='SYSTEM VIEW'.

An issue that might arise. I had some varchar(255) columns in mysql keys. This causes an error:

ERROR 1071 (42000) at line 2229: Specified key was too long; max key length is 767 bytes
If that happens you can simply change the column to be smaller, like varchar(150), and rerun the command.

Please note: This answer converts the database to utf8_unicode_ci instead of utf8_bin, asked in the question. But you can simply replace this.

Source: https://dba.stackexchange.com/a/104866/93872
DATE 16 Mar 2016
by : Germain
Author / Auteur

Social Share / Partager

    Leave a Reply / Répondre Annuler la réponse

    *
    *

    Search the blog

    Blog categories

    • Front-end : HTML, CSS (13)
    • Workflow (1)
    • Front-end : javascript, jQuery (33)
    • Back-end : PHP, CMS (42)
    • Back-end : SQL, MySQL (20)
    • Sysadmin : Linux, Apache, GIT (75)
    • Designers : tools, advice (2)
    • Desktop : OS X (37)
    • Desktop : Microsoft (12)
    • Uncategorized (8)

    ITALIC™ Resources

    • Paramètres de connexion au serveur mail
    • Graphistes : séduire un développeur web
    • Dév front : nos bonnes pratiques
    • Mailing : nos (bonnes ?) pratiques
    • Environnement de travail Mac

    Recent comments

    • Germain dans Inserer un motif dans une forme InDesign
    • Fannie J dans Inserer un motif dans une forme InDesign
    • Rapide benchmarks de clients Git pour Linux (et un peu Windows et Mac aussi) – Haha. dans Git avec SourceTree : ouvrir dans TextMate / ouvrir dans Sublime Text 2
    • Germain dans Ajouter des contacts non-Facebook à Spotify
    • cc dans Ajouter des contacts non-Facebook à Spotify

    Meanwhile, in the office…

    UNEP @ Paysalia

    01 Déc 2015

    Liebig

    22 Sep 2015

    Mercedes-Benz

    17 Sep 2015

    Office National du Tourisme Tunisien

    21 Août 2015

    Serious Game

    17 Juil 2015

    Engie

    22 Juin 2015

    UN Guiding Principles Reporting Framework

    21 Mar 2015

    Dites-le avec Nutella®

    21 Fév 2015

    HEC

    21 Déc 2014

    AREVA

    21 Déc 2014
    © 2008-2017 ITALIC™ • 8 bis rue Abel • 75012 PARIS • Tel +33 (0)1 48 44 46 35 • RCS PARIS 508 228 772 • Powered by WordPress & GoodLayers
    Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site web.
    Cookie settingsACCEPTER
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Toujours activé
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    Enregistrer & appliquer