Back to Knowledge Base
Knowledge Base Database How to change all MySQL database table collation

How to change all MySQL database table collation

Database 07/11/2026
<p><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Solution</span></p>
<p><span style="border-collapse: collapse; line-height: 16px;"><span style="font-family: arial, helvetica, sans-serif; font-size: small;">After change the collation of a database, only the new tables will be created with the new collation. To change all the database collation, you can use the php script as below as it can change all the <strong></strong>collation for all the existing table. <br /><br />&lt;?php<br />$db = mysql_connect('localhost','<strong>database_user</strong>','<strong>Password</strong>');<br />if(!$db) echo "Cannot connect to the database - incorrect details";<br />mysql_select_db('<strong>database_name</strong>'); $result=mysql_query('show tables');<br />while($tables = mysql_fetch_array($result)) {<br />foreach ($tables as $key =&gt; $value) {mysql_query("ALTER TABLE $value COLLATE <strong>Collation</strong>");<br />}}<br />echo "The collation of your database has been successfully changed!";<br />?&gt;<br /><br /><em><strong>Note: </strong></em><br /><strong>database_user</strong> : Database Username<br /><strong>Password</strong>: Database user password<br /><strong>database_name</strong>: Database Name<br /><strong>Collation</strong> : new collation</span><br /></span></p>
Was this article helpful?