How to change collation of database, table, column?












163















The database is latin1_general_ci now and I want to change collation to utf8mb4_general_ci.



Is there any setting in PhpMyAdmin to change collation of database, table, column? Rather than changing one by one?










share|improve this question




















  • 3





    See this for the answer: stackoverflow.com/questions/5906585/…

    – Timo Huovinen
    Jul 20 '15 at 5:36
















163















The database is latin1_general_ci now and I want to change collation to utf8mb4_general_ci.



Is there any setting in PhpMyAdmin to change collation of database, table, column? Rather than changing one by one?










share|improve this question




















  • 3





    See this for the answer: stackoverflow.com/questions/5906585/…

    – Timo Huovinen
    Jul 20 '15 at 5:36














163












163








163


64






The database is latin1_general_ci now and I want to change collation to utf8mb4_general_ci.



Is there any setting in PhpMyAdmin to change collation of database, table, column? Rather than changing one by one?










share|improve this question
















The database is latin1_general_ci now and I want to change collation to utf8mb4_general_ci.



Is there any setting in PhpMyAdmin to change collation of database, table, column? Rather than changing one by one?







mysql collation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 23:44









terales

1,7221526




1,7221526










asked Aug 18 '09 at 14:06









user158469user158469

818274




818274








  • 3





    See this for the answer: stackoverflow.com/questions/5906585/…

    – Timo Huovinen
    Jul 20 '15 at 5:36














  • 3





    See this for the answer: stackoverflow.com/questions/5906585/…

    – Timo Huovinen
    Jul 20 '15 at 5:36








3




3





See this for the answer: stackoverflow.com/questions/5906585/…

– Timo Huovinen
Jul 20 '15 at 5:36





See this for the answer: stackoverflow.com/questions/5906585/…

– Timo Huovinen
Jul 20 '15 at 5:36












16 Answers
16






active

oldest

votes


















220














You need to either convert each table individually:



ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 


(this will convert the columns just as well), or export the database with latin1 and import it back with utf8mb4.






share|improve this answer





















  • 10





    But i want to change collation of columns. This is will change only the table collation..

    – user158469
    Aug 18 '09 at 14:22






  • 6





    @rsensan: CONVERT will change the collation of columns as well.

    – Quassnoi
    Aug 18 '09 at 14:33






  • 18





    ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

    – stormwild
    Jul 17 '15 at 10:10






  • 5





    @stormwild: this won't affect existing tables

    – Quassnoi
    Jul 17 '15 at 16:03






  • 38





    My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

    – Kapitein Witbaard
    Sep 18 '15 at 9:54





















116














I am contributing here, as the OP asked:




How to change collation of database, table, column?




The selected answer just states it on table level.





Changing it database wide:



ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


Changing it per table:



ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


Good practice is to change it at table level as it'll change it for columns as well. Changing for specific column is for any specific case.



Changing collation for a specific column:



ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;





share|improve this answer



















  • 5





    This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

    – Parapluie
    Feb 3 '17 at 19:13






  • 7





    The best answer for this.

    – jubi4dition
    Aug 3 '17 at 15:41



















62














You can run a php script.



               <?php
$con = mysql_connect('localhost','user','password');
if(!$con) { echo "Cannot connect to the database ";die();}
mysql_select_db('dbname');
$result=mysql_query('show tables');
while($tables = mysql_fetch_array($result)) {
foreach ($tables as $key => $value) {
mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
}}
echo "The collation of your database has been successfully changed!";
?>





share|improve this answer


























  • thank you very much bro. :)

    – Ömer Faruk AK
    Feb 10 '13 at 22:00











  • Nice work. Saved life... Thanks

    – Ranjith Siji
    Feb 13 '13 at 18:43



















36














To change collation for tables individually you can use,



ALTER TABLE mytable CONVERT TO CHARACTER SET utf8


To set default collation for the whole database,



ALTER DATABASE  `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


or else,



Goto PhpMyAdmin->Operations->Collation.



There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column . No need of select collation while creating new columns.






share|improve this answer
























  • Thanks so much, this was helpful

    – JoZ3
    Feb 8 '15 at 17:05



















9














you can set default collation at several levels:



http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html



1) client
2) server default
3) database default
4) table default
5) column






share|improve this answer































    9














    If you run phpMyAdmin >> select database >> select table >> go to "Operations" tab >> in "Table options" section >> you can pick Collation from the drop down list >> and once you press {Go} at the top of the screen you will see a message:




    Your SQL query has been executed successfully




    and a script



    ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 


    But it will NOT change the collations of existing columns.
    To do so you can use this script (this one also came from phpMyAdmin)



    ALTER TABLE  `tableName` CHANGE  `Name`  `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL





    share|improve this answer

































      9














      The following query will generate ALTER queries that change the collation for all appropriate columns in all tables to a certain type (utf8_general_ci in my example below).



      SELECT concat
      (
      'ALTER TABLE ',
      t1.TABLE_SCHEMA,
      '.',
      t1.table_name,
      ' MODIFY ',
      t1.column_name,
      ' ',
      t1.data_type,
      '(' ,
      CHARACTER_MAXIMUM_LENGTH,
      ')',
      ' CHARACTER SET utf8 COLLATE utf8_general_ci;'
      )
      from
      information_schema.columns t1
      where
      t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
      t1.COLLATION_NAME IS NOT NULL AND
      t1.COLLATION_NAME NOT IN ('utf8_general_ci');





      share|improve this answer
























      • +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

        – Pierre
        Jan 31 at 16:39





















      4














      I was surprised to learn, and so I had to come back here and report, that the excellent and well maintained Interconnect/it SAFE SEARCH AND REPLACE ON DATABASE script has some options for converting tables to utf8 / unicode, and even to convert to innodb. It's a script commonly used to migrate a database driven website (Wordpress, Drupal, Joomla, etc) from one domain to another.



      interconnect script buttons




      • https://github.com/interconnectit/Search-Replace-DB

      • https://interconnectit.com/products/search-and-replace-for-wordpress-databases/






      share|improve this answer































        3














        You can change the CHARSET and COLLATION of all your tables through PHP script as follows. I like the answer of hkasera but the problem with it is that the query runs twice on each table. This code is almost the same except using MySqli instead of mysql and prevention of double querying. If I could vote up, I would have voted hkasera's answer up.



        <?php
        $conn1=new MySQLi("localhost","user","password","database");
        if($conn1->connect_errno){
        echo mysqli_connect_error();
        exit;
        }
        $res=$conn1->query("show tables") or die($conn1->error);
        while($tables=$res->fetch_array()){
        $conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
        }
        echo "The collation of your database has been successfully changed!";

        $res->free();
        $conn1->close();

        ?>





        share|improve this answer































          3














          You can simple add this code to script file



          //Database Connection
          $host = 'localhost';
          $db_name = 'your_database_name';
          $db_user = 'your_database_user_name';
          $db_pass = 'your_database_user_password';

          $con = mysql_connect($host,$db_user,$db_pass);

          if(!$con) { echo "Cannot connect to the database ";die();}

          mysql_select_db($db_name);

          $result=mysql_query('show tables');

          while($tables = mysql_fetch_array($result)) {
          foreach ($tables as $key => $value) {
          mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
          }
          }

          echo "The collation of your database has been successfully changed!";





          share|improve this answer































            3














            I read it here, that you need to convert each table manually, it is not true. Here is a solution how to do it with a stored procedure:



            DELIMITER $$

            DROP PROCEDURE IF EXISTS changeCollation$$

            -- character_set parameter could be 'utf8'
            -- or 'latin1' or any other valid character set
            CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
            BEGIN
            DECLARE v_finished INTEGER DEFAULT 0;
            DECLARE v_table_name varchar(255) DEFAULT "";
            DECLARE v_message varchar(4000) DEFAULT "No records";

            -- This will create a cursor that selects each table,
            -- where the character set is not the one
            -- that is defined in the parameter

            DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
            FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
            AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');

            -- This handler will set the value v_finished to 1
            -- if there are no more rows

            DECLARE CONTINUE HANDLER
            FOR NOT FOUND SET v_finished = 1;

            OPEN alter_cursor;

            -- Start a loop to fetch each rows from the cursor
            get_table: LOOP

            -- Fetch the table names one by one
            FETCH alter_cursor INTO v_table_name;

            -- If there is no more record, then we have to skip
            -- the commands inside the loop
            IF v_finished = 1 THEN
            LEAVE get_table;
            END IF;

            IF v_table_name != '' THEN

            IF v_message = 'No records' THEN
            SET v_message = '';
            END IF;

            -- This technic makes the trick, it prepares a statement
            -- that is based on the v_table_name parameter and it means
            -- that this one is different by each iteration inside the loop

            SET @s = CONCAT('ALTER TABLE ',v_table_name,
            ' CONVERT TO CHARACTER SET ', character_set);
            PREPARE stmt FROM @s;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;

            SET v_message = CONCAT('The table ', v_table_name ,
            ' was changed to the default collation of ', character_set,
            '.n', v_message);

            SET v_table_name = '';

            END IF;
            -- Close the loop and the cursor
            END LOOP get_table;
            CLOSE alter_cursor;

            -- Returns information about the altered tables or 'No records'
            SELECT v_message;

            END $$

            DELIMITER ;


            After the procedure is created call it simply:



            CALL changeCollation('utf8');


            For more details read this blog.






            share|improve this answer

































              2














              if you want to update the default charset on a schema:



               ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;





              share|improve this answer































                1














                I used the following shell script. It takes database name as a parameter and converts all tables to another charset and collation (given by another parameters or default value defined in the script).



                #!/bin/bash

                # mycollate.sh <database> [<charset> <collation>]
                # changes MySQL/MariaDB charset and collation for one database - all tables and
                # all columns in all tables

                DB="$1"
                CHARSET="$2"
                COLL="$3"

                [ -n "$DB" ] || exit 1
                [ -n "$CHARSET" ] || CHARSET="utf8mb4"
                [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

                echo $DB
                echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql

                echo "USE $DB; SHOW TABLES;" | mysql -s | (
                while read TABLE; do
                echo $DB.$TABLE
                echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB
                done
                )





                share|improve this answer































                  1














                  Just run this SQL. Change your COLLATION to what you need and databaseName.



                  SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS    ExecuteTheString
                  FROM INFORMATION_SCHEMA.TABLES
                  WHERE TABLE_SCHEMA="databaseName"
                  AND TABLE_TYPE="BASE TABLE";





                  share|improve this answer































                    0














                    Quick way - export to SQL file, use search and replace to change the text you need to change. Create new database, import the data and then rename the old database and the new one to the old name.






                    share|improve this answer































                      0














                      My solution is a combination of @Dzintars and @Quassnoi Answer.



                      SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS    ExecuteTheString
                      FROM INFORMATION_SCHEMA.TABLES
                      WHERE TABLE_SCHEMA="<your-database>"
                      AND TABLE_TYPE="BASE TABLE";


                      By using CONVERT TO, this generates a scripts, which converts all the Tables of <your-database> to your requested encoding. This also changes the encoding of every column!






                      share|improve this answer























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1294117%2fhow-to-change-collation-of-database-table-column%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        16 Answers
                        16






                        active

                        oldest

                        votes








                        16 Answers
                        16






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        220














                        You need to either convert each table individually:



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 


                        (this will convert the columns just as well), or export the database with latin1 and import it back with utf8mb4.






                        share|improve this answer





















                        • 10





                          But i want to change collation of columns. This is will change only the table collation..

                          – user158469
                          Aug 18 '09 at 14:22






                        • 6





                          @rsensan: CONVERT will change the collation of columns as well.

                          – Quassnoi
                          Aug 18 '09 at 14:33






                        • 18





                          ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                          – stormwild
                          Jul 17 '15 at 10:10






                        • 5





                          @stormwild: this won't affect existing tables

                          – Quassnoi
                          Jul 17 '15 at 16:03






                        • 38





                          My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                          – Kapitein Witbaard
                          Sep 18 '15 at 9:54


















                        220














                        You need to either convert each table individually:



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 


                        (this will convert the columns just as well), or export the database with latin1 and import it back with utf8mb4.






                        share|improve this answer





















                        • 10





                          But i want to change collation of columns. This is will change only the table collation..

                          – user158469
                          Aug 18 '09 at 14:22






                        • 6





                          @rsensan: CONVERT will change the collation of columns as well.

                          – Quassnoi
                          Aug 18 '09 at 14:33






                        • 18





                          ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                          – stormwild
                          Jul 17 '15 at 10:10






                        • 5





                          @stormwild: this won't affect existing tables

                          – Quassnoi
                          Jul 17 '15 at 16:03






                        • 38





                          My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                          – Kapitein Witbaard
                          Sep 18 '15 at 9:54
















                        220












                        220








                        220







                        You need to either convert each table individually:



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 


                        (this will convert the columns just as well), or export the database with latin1 and import it back with utf8mb4.






                        share|improve this answer















                        You need to either convert each table individually:



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4 


                        (this will convert the columns just as well), or export the database with latin1 and import it back with utf8mb4.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 23 '18 at 15:44









                        terales

                        1,7221526




                        1,7221526










                        answered Aug 18 '09 at 14:14









                        QuassnoiQuassnoi

                        331k70521559




                        331k70521559








                        • 10





                          But i want to change collation of columns. This is will change only the table collation..

                          – user158469
                          Aug 18 '09 at 14:22






                        • 6





                          @rsensan: CONVERT will change the collation of columns as well.

                          – Quassnoi
                          Aug 18 '09 at 14:33






                        • 18





                          ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                          – stormwild
                          Jul 17 '15 at 10:10






                        • 5





                          @stormwild: this won't affect existing tables

                          – Quassnoi
                          Jul 17 '15 at 16:03






                        • 38





                          My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                          – Kapitein Witbaard
                          Sep 18 '15 at 9:54
















                        • 10





                          But i want to change collation of columns. This is will change only the table collation..

                          – user158469
                          Aug 18 '09 at 14:22






                        • 6





                          @rsensan: CONVERT will change the collation of columns as well.

                          – Quassnoi
                          Aug 18 '09 at 14:33






                        • 18





                          ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                          – stormwild
                          Jul 17 '15 at 10:10






                        • 5





                          @stormwild: this won't affect existing tables

                          – Quassnoi
                          Jul 17 '15 at 16:03






                        • 38





                          My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                          – Kapitein Witbaard
                          Sep 18 '15 at 9:54










                        10




                        10





                        But i want to change collation of columns. This is will change only the table collation..

                        – user158469
                        Aug 18 '09 at 14:22





                        But i want to change collation of columns. This is will change only the table collation..

                        – user158469
                        Aug 18 '09 at 14:22




                        6




                        6





                        @rsensan: CONVERT will change the collation of columns as well.

                        – Quassnoi
                        Aug 18 '09 at 14:33





                        @rsensan: CONVERT will change the collation of columns as well.

                        – Quassnoi
                        Aug 18 '09 at 14:33




                        18




                        18





                        ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                        – stormwild
                        Jul 17 '15 at 10:10





                        ALTER SCHEMA database DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci ;

                        – stormwild
                        Jul 17 '15 at 10:10




                        5




                        5





                        @stormwild: this won't affect existing tables

                        – Quassnoi
                        Jul 17 '15 at 16:03





                        @stormwild: this won't affect existing tables

                        – Quassnoi
                        Jul 17 '15 at 16:03




                        38




                        38





                        My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                        – Kapitein Witbaard
                        Sep 18 '15 at 9:54







                        My query: ALTER TABLE MYTABLE CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; Please don't use utf8_general_ci anymore ;-)

                        – Kapitein Witbaard
                        Sep 18 '15 at 9:54















                        116














                        I am contributing here, as the OP asked:




                        How to change collation of database, table, column?




                        The selected answer just states it on table level.





                        Changing it database wide:



                        ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Changing it per table:



                        ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Good practice is to change it at table level as it'll change it for columns as well. Changing for specific column is for any specific case.



                        Changing collation for a specific column:



                        ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;





                        share|improve this answer



















                        • 5





                          This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                          – Parapluie
                          Feb 3 '17 at 19:13






                        • 7





                          The best answer for this.

                          – jubi4dition
                          Aug 3 '17 at 15:41
















                        116














                        I am contributing here, as the OP asked:




                        How to change collation of database, table, column?




                        The selected answer just states it on table level.





                        Changing it database wide:



                        ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Changing it per table:



                        ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Good practice is to change it at table level as it'll change it for columns as well. Changing for specific column is for any specific case.



                        Changing collation for a specific column:



                        ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;





                        share|improve this answer



















                        • 5





                          This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                          – Parapluie
                          Feb 3 '17 at 19:13






                        • 7





                          The best answer for this.

                          – jubi4dition
                          Aug 3 '17 at 15:41














                        116












                        116








                        116







                        I am contributing here, as the OP asked:




                        How to change collation of database, table, column?




                        The selected answer just states it on table level.





                        Changing it database wide:



                        ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Changing it per table:



                        ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Good practice is to change it at table level as it'll change it for columns as well. Changing for specific column is for any specific case.



                        Changing collation for a specific column:



                        ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;





                        share|improve this answer













                        I am contributing here, as the OP asked:




                        How to change collation of database, table, column?




                        The selected answer just states it on table level.





                        Changing it database wide:



                        ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Changing it per table:



                        ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


                        Good practice is to change it at table level as it'll change it for columns as well. Changing for specific column is for any specific case.



                        Changing collation for a specific column:



                        ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 17 '16 at 11:51









                        Nabeel AhmedNabeel Ahmed

                        8,34723544




                        8,34723544








                        • 5





                          This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                          – Parapluie
                          Feb 3 '17 at 19:13






                        • 7





                          The best answer for this.

                          – jubi4dition
                          Aug 3 '17 at 15:41














                        • 5





                          This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                          – Parapluie
                          Feb 3 '17 at 19:13






                        • 7





                          The best answer for this.

                          – jubi4dition
                          Aug 3 '17 at 15:41








                        5




                        5





                        This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                        – Parapluie
                        Feb 3 '17 at 19:13





                        This one actually answers the question directly. I needed this, as my table-level changes were NOT updating the columns. I'll investigate that later; but this is the information that got me through the hard times. Thanks.

                        – Parapluie
                        Feb 3 '17 at 19:13




                        7




                        7





                        The best answer for this.

                        – jubi4dition
                        Aug 3 '17 at 15:41





                        The best answer for this.

                        – jubi4dition
                        Aug 3 '17 at 15:41











                        62














                        You can run a php script.



                                       <?php
                        $con = mysql_connect('localhost','user','password');
                        if(!$con) { echo "Cannot connect to the database ";die();}
                        mysql_select_db('dbname');
                        $result=mysql_query('show tables');
                        while($tables = mysql_fetch_array($result)) {
                        foreach ($tables as $key => $value) {
                        mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                        }}
                        echo "The collation of your database has been successfully changed!";
                        ?>





                        share|improve this answer


























                        • thank you very much bro. :)

                          – Ömer Faruk AK
                          Feb 10 '13 at 22:00











                        • Nice work. Saved life... Thanks

                          – Ranjith Siji
                          Feb 13 '13 at 18:43
















                        62














                        You can run a php script.



                                       <?php
                        $con = mysql_connect('localhost','user','password');
                        if(!$con) { echo "Cannot connect to the database ";die();}
                        mysql_select_db('dbname');
                        $result=mysql_query('show tables');
                        while($tables = mysql_fetch_array($result)) {
                        foreach ($tables as $key => $value) {
                        mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                        }}
                        echo "The collation of your database has been successfully changed!";
                        ?>





                        share|improve this answer


























                        • thank you very much bro. :)

                          – Ömer Faruk AK
                          Feb 10 '13 at 22:00











                        • Nice work. Saved life... Thanks

                          – Ranjith Siji
                          Feb 13 '13 at 18:43














                        62












                        62








                        62







                        You can run a php script.



                                       <?php
                        $con = mysql_connect('localhost','user','password');
                        if(!$con) { echo "Cannot connect to the database ";die();}
                        mysql_select_db('dbname');
                        $result=mysql_query('show tables');
                        while($tables = mysql_fetch_array($result)) {
                        foreach ($tables as $key => $value) {
                        mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                        }}
                        echo "The collation of your database has been successfully changed!";
                        ?>





                        share|improve this answer















                        You can run a php script.



                                       <?php
                        $con = mysql_connect('localhost','user','password');
                        if(!$con) { echo "Cannot connect to the database ";die();}
                        mysql_select_db('dbname');
                        $result=mysql_query('show tables');
                        while($tables = mysql_fetch_array($result)) {
                        foreach ($tables as $key => $value) {
                        mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                        }}
                        echo "The collation of your database has been successfully changed!";
                        ?>






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 4 '12 at 11:22

























                        answered Oct 4 '12 at 6:23









                        hkaserahkasera

                        1,42421730




                        1,42421730













                        • thank you very much bro. :)

                          – Ömer Faruk AK
                          Feb 10 '13 at 22:00











                        • Nice work. Saved life... Thanks

                          – Ranjith Siji
                          Feb 13 '13 at 18:43



















                        • thank you very much bro. :)

                          – Ömer Faruk AK
                          Feb 10 '13 at 22:00











                        • Nice work. Saved life... Thanks

                          – Ranjith Siji
                          Feb 13 '13 at 18:43

















                        thank you very much bro. :)

                        – Ömer Faruk AK
                        Feb 10 '13 at 22:00





                        thank you very much bro. :)

                        – Ömer Faruk AK
                        Feb 10 '13 at 22:00













                        Nice work. Saved life... Thanks

                        – Ranjith Siji
                        Feb 13 '13 at 18:43





                        Nice work. Saved life... Thanks

                        – Ranjith Siji
                        Feb 13 '13 at 18:43











                        36














                        To change collation for tables individually you can use,



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8


                        To set default collation for the whole database,



                        ALTER DATABASE  `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


                        or else,



                        Goto PhpMyAdmin->Operations->Collation.



                        There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column . No need of select collation while creating new columns.






                        share|improve this answer
























                        • Thanks so much, this was helpful

                          – JoZ3
                          Feb 8 '15 at 17:05
















                        36














                        To change collation for tables individually you can use,



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8


                        To set default collation for the whole database,



                        ALTER DATABASE  `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


                        or else,



                        Goto PhpMyAdmin->Operations->Collation.



                        There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column . No need of select collation while creating new columns.






                        share|improve this answer
























                        • Thanks so much, this was helpful

                          – JoZ3
                          Feb 8 '15 at 17:05














                        36












                        36








                        36







                        To change collation for tables individually you can use,



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8


                        To set default collation for the whole database,



                        ALTER DATABASE  `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


                        or else,



                        Goto PhpMyAdmin->Operations->Collation.



                        There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column . No need of select collation while creating new columns.






                        share|improve this answer













                        To change collation for tables individually you can use,



                        ALTER TABLE mytable CONVERT TO CHARACTER SET utf8


                        To set default collation for the whole database,



                        ALTER DATABASE  `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin


                        or else,



                        Goto PhpMyAdmin->Operations->Collation.



                        There you an find the select box which contains all the exsiting collations. So that here you can change your collation. So here after database table will follows this collation while you are creating new column . No need of select collation while creating new columns.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 16 '13 at 8:05









                        jeevajeeva

                        1,01611322




                        1,01611322













                        • Thanks so much, this was helpful

                          – JoZ3
                          Feb 8 '15 at 17:05



















                        • Thanks so much, this was helpful

                          – JoZ3
                          Feb 8 '15 at 17:05

















                        Thanks so much, this was helpful

                        – JoZ3
                        Feb 8 '15 at 17:05





                        Thanks so much, this was helpful

                        – JoZ3
                        Feb 8 '15 at 17:05











                        9














                        you can set default collation at several levels:



                        http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html



                        1) client
                        2) server default
                        3) database default
                        4) table default
                        5) column






                        share|improve this answer




























                          9














                          you can set default collation at several levels:



                          http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html



                          1) client
                          2) server default
                          3) database default
                          4) table default
                          5) column






                          share|improve this answer


























                            9












                            9








                            9







                            you can set default collation at several levels:



                            http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html



                            1) client
                            2) server default
                            3) database default
                            4) table default
                            5) column






                            share|improve this answer













                            you can set default collation at several levels:



                            http://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html



                            1) client
                            2) server default
                            3) database default
                            4) table default
                            5) column







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 18 '09 at 14:15









                            andersonbd1andersonbd1

                            2,149113757




                            2,149113757























                                9














                                If you run phpMyAdmin >> select database >> select table >> go to "Operations" tab >> in "Table options" section >> you can pick Collation from the drop down list >> and once you press {Go} at the top of the screen you will see a message:




                                Your SQL query has been executed successfully




                                and a script



                                ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 


                                But it will NOT change the collations of existing columns.
                                To do so you can use this script (this one also came from phpMyAdmin)



                                ALTER TABLE  `tableName` CHANGE  `Name`  `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL





                                share|improve this answer






























                                  9














                                  If you run phpMyAdmin >> select database >> select table >> go to "Operations" tab >> in "Table options" section >> you can pick Collation from the drop down list >> and once you press {Go} at the top of the screen you will see a message:




                                  Your SQL query has been executed successfully




                                  and a script



                                  ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 


                                  But it will NOT change the collations of existing columns.
                                  To do so you can use this script (this one also came from phpMyAdmin)



                                  ALTER TABLE  `tableName` CHANGE  `Name`  `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL





                                  share|improve this answer




























                                    9












                                    9








                                    9







                                    If you run phpMyAdmin >> select database >> select table >> go to "Operations" tab >> in "Table options" section >> you can pick Collation from the drop down list >> and once you press {Go} at the top of the screen you will see a message:




                                    Your SQL query has been executed successfully




                                    and a script



                                    ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 


                                    But it will NOT change the collations of existing columns.
                                    To do so you can use this script (this one also came from phpMyAdmin)



                                    ALTER TABLE  `tableName` CHANGE  `Name`  `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL





                                    share|improve this answer















                                    If you run phpMyAdmin >> select database >> select table >> go to "Operations" tab >> in "Table options" section >> you can pick Collation from the drop down list >> and once you press {Go} at the top of the screen you will see a message:




                                    Your SQL query has been executed successfully




                                    and a script



                                    ALTER TABLE `tableName` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 


                                    But it will NOT change the collations of existing columns.
                                    To do so you can use this script (this one also came from phpMyAdmin)



                                    ALTER TABLE  `tableName` CHANGE  `Name`  `Name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 4 '15 at 4:44

























                                    answered Sep 4 '15 at 4:34









                                    Yevgeniy AfanasyevYevgeniy Afanasyev

                                    8,68455271




                                    8,68455271























                                        9














                                        The following query will generate ALTER queries that change the collation for all appropriate columns in all tables to a certain type (utf8_general_ci in my example below).



                                        SELECT concat
                                        (
                                        'ALTER TABLE ',
                                        t1.TABLE_SCHEMA,
                                        '.',
                                        t1.table_name,
                                        ' MODIFY ',
                                        t1.column_name,
                                        ' ',
                                        t1.data_type,
                                        '(' ,
                                        CHARACTER_MAXIMUM_LENGTH,
                                        ')',
                                        ' CHARACTER SET utf8 COLLATE utf8_general_ci;'
                                        )
                                        from
                                        information_schema.columns t1
                                        where
                                        t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
                                        t1.COLLATION_NAME IS NOT NULL AND
                                        t1.COLLATION_NAME NOT IN ('utf8_general_ci');





                                        share|improve this answer
























                                        • +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                          – Pierre
                                          Jan 31 at 16:39


















                                        9














                                        The following query will generate ALTER queries that change the collation for all appropriate columns in all tables to a certain type (utf8_general_ci in my example below).



                                        SELECT concat
                                        (
                                        'ALTER TABLE ',
                                        t1.TABLE_SCHEMA,
                                        '.',
                                        t1.table_name,
                                        ' MODIFY ',
                                        t1.column_name,
                                        ' ',
                                        t1.data_type,
                                        '(' ,
                                        CHARACTER_MAXIMUM_LENGTH,
                                        ')',
                                        ' CHARACTER SET utf8 COLLATE utf8_general_ci;'
                                        )
                                        from
                                        information_schema.columns t1
                                        where
                                        t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
                                        t1.COLLATION_NAME IS NOT NULL AND
                                        t1.COLLATION_NAME NOT IN ('utf8_general_ci');





                                        share|improve this answer
























                                        • +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                          – Pierre
                                          Jan 31 at 16:39
















                                        9












                                        9








                                        9







                                        The following query will generate ALTER queries that change the collation for all appropriate columns in all tables to a certain type (utf8_general_ci in my example below).



                                        SELECT concat
                                        (
                                        'ALTER TABLE ',
                                        t1.TABLE_SCHEMA,
                                        '.',
                                        t1.table_name,
                                        ' MODIFY ',
                                        t1.column_name,
                                        ' ',
                                        t1.data_type,
                                        '(' ,
                                        CHARACTER_MAXIMUM_LENGTH,
                                        ')',
                                        ' CHARACTER SET utf8 COLLATE utf8_general_ci;'
                                        )
                                        from
                                        information_schema.columns t1
                                        where
                                        t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
                                        t1.COLLATION_NAME IS NOT NULL AND
                                        t1.COLLATION_NAME NOT IN ('utf8_general_ci');





                                        share|improve this answer













                                        The following query will generate ALTER queries that change the collation for all appropriate columns in all tables to a certain type (utf8_general_ci in my example below).



                                        SELECT concat
                                        (
                                        'ALTER TABLE ',
                                        t1.TABLE_SCHEMA,
                                        '.',
                                        t1.table_name,
                                        ' MODIFY ',
                                        t1.column_name,
                                        ' ',
                                        t1.data_type,
                                        '(' ,
                                        CHARACTER_MAXIMUM_LENGTH,
                                        ')',
                                        ' CHARACTER SET utf8 COLLATE utf8_general_ci;'
                                        )
                                        from
                                        information_schema.columns t1
                                        where
                                        t1.TABLE_SCHEMA like 'you_db_name_goes_here' AND
                                        t1.COLLATION_NAME IS NOT NULL AND
                                        t1.COLLATION_NAME NOT IN ('utf8_general_ci');






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 14 '16 at 1:26









                                        Parampal PooniParampal Pooni

                                        1,32652133




                                        1,32652133













                                        • +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                          – Pierre
                                          Jan 31 at 16:39





















                                        • +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                          – Pierre
                                          Jan 31 at 16:39



















                                        +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                        – Pierre
                                        Jan 31 at 16:39







                                        +1 I like this answer the most. Not everyone has PHP magically somewhere. Some of use use other languages with MySQL. This was easy to run in MySQL Workbench, copy the rows and paste. I just did the extra step to run the above for information_schema.tables and the code in concat ALTER TABLE 'schema'.'table' CHARACTER SET = utf8mb4 , COLLATE = utf8mb4_bin ;

                                        – Pierre
                                        Jan 31 at 16:39













                                        4














                                        I was surprised to learn, and so I had to come back here and report, that the excellent and well maintained Interconnect/it SAFE SEARCH AND REPLACE ON DATABASE script has some options for converting tables to utf8 / unicode, and even to convert to innodb. It's a script commonly used to migrate a database driven website (Wordpress, Drupal, Joomla, etc) from one domain to another.



                                        interconnect script buttons




                                        • https://github.com/interconnectit/Search-Replace-DB

                                        • https://interconnectit.com/products/search-and-replace-for-wordpress-databases/






                                        share|improve this answer




























                                          4














                                          I was surprised to learn, and so I had to come back here and report, that the excellent and well maintained Interconnect/it SAFE SEARCH AND REPLACE ON DATABASE script has some options for converting tables to utf8 / unicode, and even to convert to innodb. It's a script commonly used to migrate a database driven website (Wordpress, Drupal, Joomla, etc) from one domain to another.



                                          interconnect script buttons




                                          • https://github.com/interconnectit/Search-Replace-DB

                                          • https://interconnectit.com/products/search-and-replace-for-wordpress-databases/






                                          share|improve this answer


























                                            4












                                            4








                                            4







                                            I was surprised to learn, and so I had to come back here and report, that the excellent and well maintained Interconnect/it SAFE SEARCH AND REPLACE ON DATABASE script has some options for converting tables to utf8 / unicode, and even to convert to innodb. It's a script commonly used to migrate a database driven website (Wordpress, Drupal, Joomla, etc) from one domain to another.



                                            interconnect script buttons




                                            • https://github.com/interconnectit/Search-Replace-DB

                                            • https://interconnectit.com/products/search-and-replace-for-wordpress-databases/






                                            share|improve this answer













                                            I was surprised to learn, and so I had to come back here and report, that the excellent and well maintained Interconnect/it SAFE SEARCH AND REPLACE ON DATABASE script has some options for converting tables to utf8 / unicode, and even to convert to innodb. It's a script commonly used to migrate a database driven website (Wordpress, Drupal, Joomla, etc) from one domain to another.



                                            interconnect script buttons




                                            • https://github.com/interconnectit/Search-Replace-DB

                                            • https://interconnectit.com/products/search-and-replace-for-wordpress-databases/







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Oct 19 '16 at 20:10









                                            Adam NofsingerAdam Nofsinger

                                            2,69223040




                                            2,69223040























                                                3














                                                You can change the CHARSET and COLLATION of all your tables through PHP script as follows. I like the answer of hkasera but the problem with it is that the query runs twice on each table. This code is almost the same except using MySqli instead of mysql and prevention of double querying. If I could vote up, I would have voted hkasera's answer up.



                                                <?php
                                                $conn1=new MySQLi("localhost","user","password","database");
                                                if($conn1->connect_errno){
                                                echo mysqli_connect_error();
                                                exit;
                                                }
                                                $res=$conn1->query("show tables") or die($conn1->error);
                                                while($tables=$res->fetch_array()){
                                                $conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
                                                }
                                                echo "The collation of your database has been successfully changed!";

                                                $res->free();
                                                $conn1->close();

                                                ?>





                                                share|improve this answer




























                                                  3














                                                  You can change the CHARSET and COLLATION of all your tables through PHP script as follows. I like the answer of hkasera but the problem with it is that the query runs twice on each table. This code is almost the same except using MySqli instead of mysql and prevention of double querying. If I could vote up, I would have voted hkasera's answer up.



                                                  <?php
                                                  $conn1=new MySQLi("localhost","user","password","database");
                                                  if($conn1->connect_errno){
                                                  echo mysqli_connect_error();
                                                  exit;
                                                  }
                                                  $res=$conn1->query("show tables") or die($conn1->error);
                                                  while($tables=$res->fetch_array()){
                                                  $conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
                                                  }
                                                  echo "The collation of your database has been successfully changed!";

                                                  $res->free();
                                                  $conn1->close();

                                                  ?>





                                                  share|improve this answer


























                                                    3












                                                    3








                                                    3







                                                    You can change the CHARSET and COLLATION of all your tables through PHP script as follows. I like the answer of hkasera but the problem with it is that the query runs twice on each table. This code is almost the same except using MySqli instead of mysql and prevention of double querying. If I could vote up, I would have voted hkasera's answer up.



                                                    <?php
                                                    $conn1=new MySQLi("localhost","user","password","database");
                                                    if($conn1->connect_errno){
                                                    echo mysqli_connect_error();
                                                    exit;
                                                    }
                                                    $res=$conn1->query("show tables") or die($conn1->error);
                                                    while($tables=$res->fetch_array()){
                                                    $conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
                                                    }
                                                    echo "The collation of your database has been successfully changed!";

                                                    $res->free();
                                                    $conn1->close();

                                                    ?>





                                                    share|improve this answer













                                                    You can change the CHARSET and COLLATION of all your tables through PHP script as follows. I like the answer of hkasera but the problem with it is that the query runs twice on each table. This code is almost the same except using MySqli instead of mysql and prevention of double querying. If I could vote up, I would have voted hkasera's answer up.



                                                    <?php
                                                    $conn1=new MySQLi("localhost","user","password","database");
                                                    if($conn1->connect_errno){
                                                    echo mysqli_connect_error();
                                                    exit;
                                                    }
                                                    $res=$conn1->query("show tables") or die($conn1->error);
                                                    while($tables=$res->fetch_array()){
                                                    $conn1->query("ALTER TABLE $tables[0] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") or die($conn1->error);
                                                    }
                                                    echo "The collation of your database has been successfully changed!";

                                                    $res->free();
                                                    $conn1->close();

                                                    ?>






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Feb 2 '15 at 5:36









                                                    mtmehdimtmehdi

                                                    313




                                                    313























                                                        3














                                                        You can simple add this code to script file



                                                        //Database Connection
                                                        $host = 'localhost';
                                                        $db_name = 'your_database_name';
                                                        $db_user = 'your_database_user_name';
                                                        $db_pass = 'your_database_user_password';

                                                        $con = mysql_connect($host,$db_user,$db_pass);

                                                        if(!$con) { echo "Cannot connect to the database ";die();}

                                                        mysql_select_db($db_name);

                                                        $result=mysql_query('show tables');

                                                        while($tables = mysql_fetch_array($result)) {
                                                        foreach ($tables as $key => $value) {
                                                        mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                                                        }
                                                        }

                                                        echo "The collation of your database has been successfully changed!";





                                                        share|improve this answer




























                                                          3














                                                          You can simple add this code to script file



                                                          //Database Connection
                                                          $host = 'localhost';
                                                          $db_name = 'your_database_name';
                                                          $db_user = 'your_database_user_name';
                                                          $db_pass = 'your_database_user_password';

                                                          $con = mysql_connect($host,$db_user,$db_pass);

                                                          if(!$con) { echo "Cannot connect to the database ";die();}

                                                          mysql_select_db($db_name);

                                                          $result=mysql_query('show tables');

                                                          while($tables = mysql_fetch_array($result)) {
                                                          foreach ($tables as $key => $value) {
                                                          mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                                                          }
                                                          }

                                                          echo "The collation of your database has been successfully changed!";





                                                          share|improve this answer


























                                                            3












                                                            3








                                                            3







                                                            You can simple add this code to script file



                                                            //Database Connection
                                                            $host = 'localhost';
                                                            $db_name = 'your_database_name';
                                                            $db_user = 'your_database_user_name';
                                                            $db_pass = 'your_database_user_password';

                                                            $con = mysql_connect($host,$db_user,$db_pass);

                                                            if(!$con) { echo "Cannot connect to the database ";die();}

                                                            mysql_select_db($db_name);

                                                            $result=mysql_query('show tables');

                                                            while($tables = mysql_fetch_array($result)) {
                                                            foreach ($tables as $key => $value) {
                                                            mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                                                            }
                                                            }

                                                            echo "The collation of your database has been successfully changed!";





                                                            share|improve this answer













                                                            You can simple add this code to script file



                                                            //Database Connection
                                                            $host = 'localhost';
                                                            $db_name = 'your_database_name';
                                                            $db_user = 'your_database_user_name';
                                                            $db_pass = 'your_database_user_password';

                                                            $con = mysql_connect($host,$db_user,$db_pass);

                                                            if(!$con) { echo "Cannot connect to the database ";die();}

                                                            mysql_select_db($db_name);

                                                            $result=mysql_query('show tables');

                                                            while($tables = mysql_fetch_array($result)) {
                                                            foreach ($tables as $key => $value) {
                                                            mysql_query("ALTER TABLE $value CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci");
                                                            }
                                                            }

                                                            echo "The collation of your database has been successfully changed!";






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered May 5 '16 at 11:41









                                                            Chandra KumarChandra Kumar

                                                            3,0721817




                                                            3,0721817























                                                                3














                                                                I read it here, that you need to convert each table manually, it is not true. Here is a solution how to do it with a stored procedure:



                                                                DELIMITER $$

                                                                DROP PROCEDURE IF EXISTS changeCollation$$

                                                                -- character_set parameter could be 'utf8'
                                                                -- or 'latin1' or any other valid character set
                                                                CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
                                                                BEGIN
                                                                DECLARE v_finished INTEGER DEFAULT 0;
                                                                DECLARE v_table_name varchar(255) DEFAULT "";
                                                                DECLARE v_message varchar(4000) DEFAULT "No records";

                                                                -- This will create a cursor that selects each table,
                                                                -- where the character set is not the one
                                                                -- that is defined in the parameter

                                                                DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
                                                                FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
                                                                AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');

                                                                -- This handler will set the value v_finished to 1
                                                                -- if there are no more rows

                                                                DECLARE CONTINUE HANDLER
                                                                FOR NOT FOUND SET v_finished = 1;

                                                                OPEN alter_cursor;

                                                                -- Start a loop to fetch each rows from the cursor
                                                                get_table: LOOP

                                                                -- Fetch the table names one by one
                                                                FETCH alter_cursor INTO v_table_name;

                                                                -- If there is no more record, then we have to skip
                                                                -- the commands inside the loop
                                                                IF v_finished = 1 THEN
                                                                LEAVE get_table;
                                                                END IF;

                                                                IF v_table_name != '' THEN

                                                                IF v_message = 'No records' THEN
                                                                SET v_message = '';
                                                                END IF;

                                                                -- This technic makes the trick, it prepares a statement
                                                                -- that is based on the v_table_name parameter and it means
                                                                -- that this one is different by each iteration inside the loop

                                                                SET @s = CONCAT('ALTER TABLE ',v_table_name,
                                                                ' CONVERT TO CHARACTER SET ', character_set);
                                                                PREPARE stmt FROM @s;
                                                                EXECUTE stmt;
                                                                DEALLOCATE PREPARE stmt;

                                                                SET v_message = CONCAT('The table ', v_table_name ,
                                                                ' was changed to the default collation of ', character_set,
                                                                '.n', v_message);

                                                                SET v_table_name = '';

                                                                END IF;
                                                                -- Close the loop and the cursor
                                                                END LOOP get_table;
                                                                CLOSE alter_cursor;

                                                                -- Returns information about the altered tables or 'No records'
                                                                SELECT v_message;

                                                                END $$

                                                                DELIMITER ;


                                                                After the procedure is created call it simply:



                                                                CALL changeCollation('utf8');


                                                                For more details read this blog.






                                                                share|improve this answer






























                                                                  3














                                                                  I read it here, that you need to convert each table manually, it is not true. Here is a solution how to do it with a stored procedure:



                                                                  DELIMITER $$

                                                                  DROP PROCEDURE IF EXISTS changeCollation$$

                                                                  -- character_set parameter could be 'utf8'
                                                                  -- or 'latin1' or any other valid character set
                                                                  CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
                                                                  BEGIN
                                                                  DECLARE v_finished INTEGER DEFAULT 0;
                                                                  DECLARE v_table_name varchar(255) DEFAULT "";
                                                                  DECLARE v_message varchar(4000) DEFAULT "No records";

                                                                  -- This will create a cursor that selects each table,
                                                                  -- where the character set is not the one
                                                                  -- that is defined in the parameter

                                                                  DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
                                                                  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
                                                                  AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');

                                                                  -- This handler will set the value v_finished to 1
                                                                  -- if there are no more rows

                                                                  DECLARE CONTINUE HANDLER
                                                                  FOR NOT FOUND SET v_finished = 1;

                                                                  OPEN alter_cursor;

                                                                  -- Start a loop to fetch each rows from the cursor
                                                                  get_table: LOOP

                                                                  -- Fetch the table names one by one
                                                                  FETCH alter_cursor INTO v_table_name;

                                                                  -- If there is no more record, then we have to skip
                                                                  -- the commands inside the loop
                                                                  IF v_finished = 1 THEN
                                                                  LEAVE get_table;
                                                                  END IF;

                                                                  IF v_table_name != '' THEN

                                                                  IF v_message = 'No records' THEN
                                                                  SET v_message = '';
                                                                  END IF;

                                                                  -- This technic makes the trick, it prepares a statement
                                                                  -- that is based on the v_table_name parameter and it means
                                                                  -- that this one is different by each iteration inside the loop

                                                                  SET @s = CONCAT('ALTER TABLE ',v_table_name,
                                                                  ' CONVERT TO CHARACTER SET ', character_set);
                                                                  PREPARE stmt FROM @s;
                                                                  EXECUTE stmt;
                                                                  DEALLOCATE PREPARE stmt;

                                                                  SET v_message = CONCAT('The table ', v_table_name ,
                                                                  ' was changed to the default collation of ', character_set,
                                                                  '.n', v_message);

                                                                  SET v_table_name = '';

                                                                  END IF;
                                                                  -- Close the loop and the cursor
                                                                  END LOOP get_table;
                                                                  CLOSE alter_cursor;

                                                                  -- Returns information about the altered tables or 'No records'
                                                                  SELECT v_message;

                                                                  END $$

                                                                  DELIMITER ;


                                                                  After the procedure is created call it simply:



                                                                  CALL changeCollation('utf8');


                                                                  For more details read this blog.






                                                                  share|improve this answer




























                                                                    3












                                                                    3








                                                                    3







                                                                    I read it here, that you need to convert each table manually, it is not true. Here is a solution how to do it with a stored procedure:



                                                                    DELIMITER $$

                                                                    DROP PROCEDURE IF EXISTS changeCollation$$

                                                                    -- character_set parameter could be 'utf8'
                                                                    -- or 'latin1' or any other valid character set
                                                                    CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
                                                                    BEGIN
                                                                    DECLARE v_finished INTEGER DEFAULT 0;
                                                                    DECLARE v_table_name varchar(255) DEFAULT "";
                                                                    DECLARE v_message varchar(4000) DEFAULT "No records";

                                                                    -- This will create a cursor that selects each table,
                                                                    -- where the character set is not the one
                                                                    -- that is defined in the parameter

                                                                    DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
                                                                    FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
                                                                    AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');

                                                                    -- This handler will set the value v_finished to 1
                                                                    -- if there are no more rows

                                                                    DECLARE CONTINUE HANDLER
                                                                    FOR NOT FOUND SET v_finished = 1;

                                                                    OPEN alter_cursor;

                                                                    -- Start a loop to fetch each rows from the cursor
                                                                    get_table: LOOP

                                                                    -- Fetch the table names one by one
                                                                    FETCH alter_cursor INTO v_table_name;

                                                                    -- If there is no more record, then we have to skip
                                                                    -- the commands inside the loop
                                                                    IF v_finished = 1 THEN
                                                                    LEAVE get_table;
                                                                    END IF;

                                                                    IF v_table_name != '' THEN

                                                                    IF v_message = 'No records' THEN
                                                                    SET v_message = '';
                                                                    END IF;

                                                                    -- This technic makes the trick, it prepares a statement
                                                                    -- that is based on the v_table_name parameter and it means
                                                                    -- that this one is different by each iteration inside the loop

                                                                    SET @s = CONCAT('ALTER TABLE ',v_table_name,
                                                                    ' CONVERT TO CHARACTER SET ', character_set);
                                                                    PREPARE stmt FROM @s;
                                                                    EXECUTE stmt;
                                                                    DEALLOCATE PREPARE stmt;

                                                                    SET v_message = CONCAT('The table ', v_table_name ,
                                                                    ' was changed to the default collation of ', character_set,
                                                                    '.n', v_message);

                                                                    SET v_table_name = '';

                                                                    END IF;
                                                                    -- Close the loop and the cursor
                                                                    END LOOP get_table;
                                                                    CLOSE alter_cursor;

                                                                    -- Returns information about the altered tables or 'No records'
                                                                    SELECT v_message;

                                                                    END $$

                                                                    DELIMITER ;


                                                                    After the procedure is created call it simply:



                                                                    CALL changeCollation('utf8');


                                                                    For more details read this blog.






                                                                    share|improve this answer















                                                                    I read it here, that you need to convert each table manually, it is not true. Here is a solution how to do it with a stored procedure:



                                                                    DELIMITER $$

                                                                    DROP PROCEDURE IF EXISTS changeCollation$$

                                                                    -- character_set parameter could be 'utf8'
                                                                    -- or 'latin1' or any other valid character set
                                                                    CREATE PROCEDURE changeCollation(IN character_set VARCHAR(255))
                                                                    BEGIN
                                                                    DECLARE v_finished INTEGER DEFAULT 0;
                                                                    DECLARE v_table_name varchar(255) DEFAULT "";
                                                                    DECLARE v_message varchar(4000) DEFAULT "No records";

                                                                    -- This will create a cursor that selects each table,
                                                                    -- where the character set is not the one
                                                                    -- that is defined in the parameter

                                                                    DECLARE alter_cursor CURSOR FOR SELECT DISTINCT TABLE_NAME
                                                                    FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE()
                                                                    AND COLLATION_NAME NOT LIKE CONCAT(character_set, '_%');

                                                                    -- This handler will set the value v_finished to 1
                                                                    -- if there are no more rows

                                                                    DECLARE CONTINUE HANDLER
                                                                    FOR NOT FOUND SET v_finished = 1;

                                                                    OPEN alter_cursor;

                                                                    -- Start a loop to fetch each rows from the cursor
                                                                    get_table: LOOP

                                                                    -- Fetch the table names one by one
                                                                    FETCH alter_cursor INTO v_table_name;

                                                                    -- If there is no more record, then we have to skip
                                                                    -- the commands inside the loop
                                                                    IF v_finished = 1 THEN
                                                                    LEAVE get_table;
                                                                    END IF;

                                                                    IF v_table_name != '' THEN

                                                                    IF v_message = 'No records' THEN
                                                                    SET v_message = '';
                                                                    END IF;

                                                                    -- This technic makes the trick, it prepares a statement
                                                                    -- that is based on the v_table_name parameter and it means
                                                                    -- that this one is different by each iteration inside the loop

                                                                    SET @s = CONCAT('ALTER TABLE ',v_table_name,
                                                                    ' CONVERT TO CHARACTER SET ', character_set);
                                                                    PREPARE stmt FROM @s;
                                                                    EXECUTE stmt;
                                                                    DEALLOCATE PREPARE stmt;

                                                                    SET v_message = CONCAT('The table ', v_table_name ,
                                                                    ' was changed to the default collation of ', character_set,
                                                                    '.n', v_message);

                                                                    SET v_table_name = '';

                                                                    END IF;
                                                                    -- Close the loop and the cursor
                                                                    END LOOP get_table;
                                                                    CLOSE alter_cursor;

                                                                    -- Returns information about the altered tables or 'No records'
                                                                    SELECT v_message;

                                                                    END $$

                                                                    DELIMITER ;


                                                                    After the procedure is created call it simply:



                                                                    CALL changeCollation('utf8');


                                                                    For more details read this blog.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Nov 22 '18 at 14:07









                                                                    Seybsen

                                                                    9,56532962




                                                                    9,56532962










                                                                    answered Jun 9 '15 at 13:07









                                                                    András OttóAndrás Ottó

                                                                    6,56112134




                                                                    6,56112134























                                                                        2














                                                                        if you want to update the default charset on a schema:



                                                                         ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;





                                                                        share|improve this answer




























                                                                          2














                                                                          if you want to update the default charset on a schema:



                                                                           ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;





                                                                          share|improve this answer


























                                                                            2












                                                                            2








                                                                            2







                                                                            if you want to update the default charset on a schema:



                                                                             ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;





                                                                            share|improve this answer













                                                                            if you want to update the default charset on a schema:



                                                                             ALTER SCHEMA MYSCHEMA DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;






                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered Jul 31 '17 at 3:44









                                                                            Mircea StanciuMircea Stanciu

                                                                            2,00722229




                                                                            2,00722229























                                                                                1














                                                                                I used the following shell script. It takes database name as a parameter and converts all tables to another charset and collation (given by another parameters or default value defined in the script).



                                                                                #!/bin/bash

                                                                                # mycollate.sh <database> [<charset> <collation>]
                                                                                # changes MySQL/MariaDB charset and collation for one database - all tables and
                                                                                # all columns in all tables

                                                                                DB="$1"
                                                                                CHARSET="$2"
                                                                                COLL="$3"

                                                                                [ -n "$DB" ] || exit 1
                                                                                [ -n "$CHARSET" ] || CHARSET="utf8mb4"
                                                                                [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

                                                                                echo $DB
                                                                                echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql

                                                                                echo "USE $DB; SHOW TABLES;" | mysql -s | (
                                                                                while read TABLE; do
                                                                                echo $DB.$TABLE
                                                                                echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB
                                                                                done
                                                                                )





                                                                                share|improve this answer




























                                                                                  1














                                                                                  I used the following shell script. It takes database name as a parameter and converts all tables to another charset and collation (given by another parameters or default value defined in the script).



                                                                                  #!/bin/bash

                                                                                  # mycollate.sh <database> [<charset> <collation>]
                                                                                  # changes MySQL/MariaDB charset and collation for one database - all tables and
                                                                                  # all columns in all tables

                                                                                  DB="$1"
                                                                                  CHARSET="$2"
                                                                                  COLL="$3"

                                                                                  [ -n "$DB" ] || exit 1
                                                                                  [ -n "$CHARSET" ] || CHARSET="utf8mb4"
                                                                                  [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

                                                                                  echo $DB
                                                                                  echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql

                                                                                  echo "USE $DB; SHOW TABLES;" | mysql -s | (
                                                                                  while read TABLE; do
                                                                                  echo $DB.$TABLE
                                                                                  echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB
                                                                                  done
                                                                                  )





                                                                                  share|improve this answer


























                                                                                    1












                                                                                    1








                                                                                    1







                                                                                    I used the following shell script. It takes database name as a parameter and converts all tables to another charset and collation (given by another parameters or default value defined in the script).



                                                                                    #!/bin/bash

                                                                                    # mycollate.sh <database> [<charset> <collation>]
                                                                                    # changes MySQL/MariaDB charset and collation for one database - all tables and
                                                                                    # all columns in all tables

                                                                                    DB="$1"
                                                                                    CHARSET="$2"
                                                                                    COLL="$3"

                                                                                    [ -n "$DB" ] || exit 1
                                                                                    [ -n "$CHARSET" ] || CHARSET="utf8mb4"
                                                                                    [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

                                                                                    echo $DB
                                                                                    echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql

                                                                                    echo "USE $DB; SHOW TABLES;" | mysql -s | (
                                                                                    while read TABLE; do
                                                                                    echo $DB.$TABLE
                                                                                    echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB
                                                                                    done
                                                                                    )





                                                                                    share|improve this answer













                                                                                    I used the following shell script. It takes database name as a parameter and converts all tables to another charset and collation (given by another parameters or default value defined in the script).



                                                                                    #!/bin/bash

                                                                                    # mycollate.sh <database> [<charset> <collation>]
                                                                                    # changes MySQL/MariaDB charset and collation for one database - all tables and
                                                                                    # all columns in all tables

                                                                                    DB="$1"
                                                                                    CHARSET="$2"
                                                                                    COLL="$3"

                                                                                    [ -n "$DB" ] || exit 1
                                                                                    [ -n "$CHARSET" ] || CHARSET="utf8mb4"
                                                                                    [ -n "$COLL" ] || COLL="utf8mb4_general_ci"

                                                                                    echo $DB
                                                                                    echo "ALTER DATABASE $DB CHARACTER SET $CHARSET COLLATE $COLL;" | mysql

                                                                                    echo "USE $DB; SHOW TABLES;" | mysql -s | (
                                                                                    while read TABLE; do
                                                                                    echo $DB.$TABLE
                                                                                    echo "ALTER TABLE $TABLE CONVERT TO CHARACTER SET $CHARSET COLLATE $COLL;" | mysql $DB
                                                                                    done
                                                                                    )






                                                                                    share|improve this answer












                                                                                    share|improve this answer



                                                                                    share|improve this answer










                                                                                    answered Oct 11 '17 at 10:52









                                                                                    Petr StastnyPetr Stastny

                                                                                    313




                                                                                    313























                                                                                        1














                                                                                        Just run this SQL. Change your COLLATION to what you need and databaseName.



                                                                                        SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS    ExecuteTheString
                                                                                        FROM INFORMATION_SCHEMA.TABLES
                                                                                        WHERE TABLE_SCHEMA="databaseName"
                                                                                        AND TABLE_TYPE="BASE TABLE";





                                                                                        share|improve this answer




























                                                                                          1














                                                                                          Just run this SQL. Change your COLLATION to what you need and databaseName.



                                                                                          SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS    ExecuteTheString
                                                                                          FROM INFORMATION_SCHEMA.TABLES
                                                                                          WHERE TABLE_SCHEMA="databaseName"
                                                                                          AND TABLE_TYPE="BASE TABLE";





                                                                                          share|improve this answer


























                                                                                            1












                                                                                            1








                                                                                            1







                                                                                            Just run this SQL. Change your COLLATION to what you need and databaseName.



                                                                                            SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS    ExecuteTheString
                                                                                            FROM INFORMATION_SCHEMA.TABLES
                                                                                            WHERE TABLE_SCHEMA="databaseName"
                                                                                            AND TABLE_TYPE="BASE TABLE";





                                                                                            share|improve this answer













                                                                                            Just run this SQL. Change your COLLATION to what you need and databaseName.



                                                                                            SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," COLLATE utf8_general_ci;") AS    ExecuteTheString
                                                                                            FROM INFORMATION_SCHEMA.TABLES
                                                                                            WHERE TABLE_SCHEMA="databaseName"
                                                                                            AND TABLE_TYPE="BASE TABLE";






                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered Oct 23 '17 at 15:13









                                                                                            DzintarsDzintars

                                                                                            12918




                                                                                            12918























                                                                                                0














                                                                                                Quick way - export to SQL file, use search and replace to change the text you need to change. Create new database, import the data and then rename the old database and the new one to the old name.






                                                                                                share|improve this answer




























                                                                                                  0














                                                                                                  Quick way - export to SQL file, use search and replace to change the text you need to change. Create new database, import the data and then rename the old database and the new one to the old name.






                                                                                                  share|improve this answer


























                                                                                                    0












                                                                                                    0








                                                                                                    0







                                                                                                    Quick way - export to SQL file, use search and replace to change the text you need to change. Create new database, import the data and then rename the old database and the new one to the old name.






                                                                                                    share|improve this answer













                                                                                                    Quick way - export to SQL file, use search and replace to change the text you need to change. Create new database, import the data and then rename the old database and the new one to the old name.







                                                                                                    share|improve this answer












                                                                                                    share|improve this answer



                                                                                                    share|improve this answer










                                                                                                    answered Nov 9 '15 at 14:25









                                                                                                    kickoff3pmkickoff3pm

                                                                                                    1




                                                                                                    1























                                                                                                        0














                                                                                                        My solution is a combination of @Dzintars and @Quassnoi Answer.



                                                                                                        SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS    ExecuteTheString
                                                                                                        FROM INFORMATION_SCHEMA.TABLES
                                                                                                        WHERE TABLE_SCHEMA="<your-database>"
                                                                                                        AND TABLE_TYPE="BASE TABLE";


                                                                                                        By using CONVERT TO, this generates a scripts, which converts all the Tables of <your-database> to your requested encoding. This also changes the encoding of every column!






                                                                                                        share|improve this answer




























                                                                                                          0














                                                                                                          My solution is a combination of @Dzintars and @Quassnoi Answer.



                                                                                                          SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS    ExecuteTheString
                                                                                                          FROM INFORMATION_SCHEMA.TABLES
                                                                                                          WHERE TABLE_SCHEMA="<your-database>"
                                                                                                          AND TABLE_TYPE="BASE TABLE";


                                                                                                          By using CONVERT TO, this generates a scripts, which converts all the Tables of <your-database> to your requested encoding. This also changes the encoding of every column!






                                                                                                          share|improve this answer


























                                                                                                            0












                                                                                                            0








                                                                                                            0







                                                                                                            My solution is a combination of @Dzintars and @Quassnoi Answer.



                                                                                                            SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS    ExecuteTheString
                                                                                                            FROM INFORMATION_SCHEMA.TABLES
                                                                                                            WHERE TABLE_SCHEMA="<your-database>"
                                                                                                            AND TABLE_TYPE="BASE TABLE";


                                                                                                            By using CONVERT TO, this generates a scripts, which converts all the Tables of <your-database> to your requested encoding. This also changes the encoding of every column!






                                                                                                            share|improve this answer













                                                                                                            My solution is a combination of @Dzintars and @Quassnoi Answer.



                                                                                                            SELECT CONCAT("ALTER TABLE ", TABLE_SCHEMA, '.', TABLE_NAME," CONVERT TO CHARACTER SET utf8mb4 ;") AS    ExecuteTheString
                                                                                                            FROM INFORMATION_SCHEMA.TABLES
                                                                                                            WHERE TABLE_SCHEMA="<your-database>"
                                                                                                            AND TABLE_TYPE="BASE TABLE";


                                                                                                            By using CONVERT TO, this generates a scripts, which converts all the Tables of <your-database> to your requested encoding. This also changes the encoding of every column!







                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered Nov 27 '18 at 23:22









                                                                                                            Florian KirmaierFlorian Kirmaier

                                                                                                            263




                                                                                                            263






























                                                                                                                draft saved

                                                                                                                draft discarded




















































                                                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                                                But avoid



                                                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                To learn more, see our tips on writing great answers.




                                                                                                                draft saved


                                                                                                                draft discarded














                                                                                                                StackExchange.ready(
                                                                                                                function () {
                                                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1294117%2fhow-to-change-collation-of-database-table-column%23new-answer', 'question_page');
                                                                                                                }
                                                                                                                );

                                                                                                                Post as a guest















                                                                                                                Required, but never shown





















































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown

































                                                                                                                Required, but never shown














                                                                                                                Required, but never shown












                                                                                                                Required, but never shown







                                                                                                                Required, but never shown







                                                                                                                Popular posts from this blog

                                                                                                                Feedback on college project

                                                                                                                Futebolista

                                                                                                                Albești (Vaslui)