Our blog

 

MySQL Bulk Update Collation and Character Set for Entire Database

If you need to change the character set and collation for all columns and tables on an entire database then check out this little PHP script.

It's currently Zend Framework based, though only for the database adapter - you could easily modify this to suit your own database connection.

PHP:
  1. <?php
  2. /* collationChange
  3. * Edmonds Commerce
  4. * www.edmondscommerce.co.uk
  5. * info@edmondscommerce.co.uk
  6. * 0844 357 0201
  7. *
  8. * Description:
  9. *
  10. * Bulk Change the Collation and Character Set for an Entire Database
  11. *
  12. * Inspiration Came from Here:
  13. * http://serverfault.com/questions/65043/alter-charset-and-collation-in-all-columns-in-all-tables-in-mysql
  14. *
  15. */
  16.  
  17. //get Db connection etc
  18. require('../_top.php');
  19. $db = Zend_Registry::get('dbAdapter');
  20.  
  21. $dbName='SET_THE_DB_NAME';
  22. $character_set = 'utf8';
  23. $collation = 'utf8_general_ci';
  24.  
  25. $stmt = $db->query("SELECT distinct CONCAT( 'alter table ', TABLE_SCHEMA, '.', TABLE_NAME, '  CONVERT TO CHARACTER SET $character_set COLLATE $collation;' ) as query, TABLE_NAME as t FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$dbName';");
  26. while($r=$stmt->fetch()){
  27.     $db->query($r['query']);
  28.     echo "<h3>Done {$r['t']}</h3>";
  29. }

More Reading:

 

Leave a Reply