<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No pain no gain &#187; Database</title>
	<atom:link href="http://www.dikant.de/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dikant.de</link>
	<description>Personal blog of Peter Dikant</description>
	<lastBuildDate>Sat, 28 Jan 2012 12:25:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Converting MySQL character sets</title>
		<link>http://www.dikant.de/2007/07/17/converting-mysql-character-sets/</link>
		<comments>http://www.dikant.de/2007/07/17/converting-mysql-character-sets/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 11:31:11 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/07/17/converting-mysql-character-sets/</guid>
		<description><![CDATA[MySQL uses character set definitions on different levels. There is a default character set for the MySQL server, one character set for each database, one for each table and even one for a single column. You should generally take care &#8230; <a href="http://www.dikant.de/2007/07/17/converting-mysql-character-sets/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>MySQL uses character set definitions on different levels. There is a default character set for the MySQL server, one character set for each database, one for each table and even one for a single column. You should generally take care that you use a consistent dataset for all columns and tables inside a database and the database itself. There exists no SQL-command or command line utility to convert a database from one character set to another. Therefore I will describe a quick way to accomplish this.</p>
<p><span id="more-17"></span></p>
<p>Let&#8217;s assume we have a database called <em>testdb</em> with a current character set of <em>utf8</em>. We want to convert it to <em>latin1</em>.<br />
The first step is to export the database in the currently used character set via mysqldump:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">--opt</span> <span style="color: #660033;">-umyusername</span> <span style="color: #660033;">-p</span> testdb <span style="color: #000000; font-weight: bold;">&gt;</span> testdb.sql</pre></div></div>

<p>Now open the exported file and replace all occurrences of <em>=utf8</em> with the new character set <em>=latin1</em>. Here is an example how to do this in  the vi texteditor:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">:<span style="color: #000000; font-weight: bold;">%</span>s<span style="color: #000000; font-weight: bold;">/</span>=utf8<span style="color: #000000; font-weight: bold;">/</span>=latin1<span style="color: #000000; font-weight: bold;">/</span>g</pre></div></div>

<p>The next step is to set the new collation for the database <em>testdb</em>. Connect to the database with a mysql client and run the following SQL command:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">alter</span> <span style="color: #990099; font-weight: bold;">database</span> testdb <span style="color: #CC0099; font-weight: bold;">collate</span> latin1_swedish_ci<span style="color: #000033;">;</span></pre></div></div>

<p>We are using <em>latin1_swedish_ci</em> as that is the default collation for the latin1 character set. You can check if the changes are working with these commands:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">use</span> testdb<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">show</span> variables <span style="color: #CC0099; font-weight: bold;">like</span> <span style="color: #008000;">&quot;character<span style="color: #008080; font-weight: bold;">_</span>set<span style="color: #008080; font-weight: bold;">_</span>database&quot;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">show</span> variables <span style="color: #CC0099; font-weight: bold;">like</span> <span style="color: #008000;">&quot;collation<span style="color: #008080; font-weight: bold;">_</span>database&quot;</span><span style="color: #000033;">;</span></pre></div></div>

<p>The last step is to reimport our modified dump into the database. Here you need to make sure that you use the old character set (in this case utf8) during the import and not the new character set:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql <span style="color: #660033;">--default-character-set</span>=utf-<span style="color: #000000;">8</span> <span style="color: #660033;">-umyusername</span> <span style="color: #660033;">-p</span> testdb <span style="color: #000000; font-weight: bold;">&lt;</span> testdb.sql</pre></div></div>

<p>Now you&#8217;re done. The database stores its data consistently with the new character set.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/07/17/converting-mysql-character-sets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

