<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="charset-examples">
      </a>
      12.3.9 Examples of Character Set and Collation Assignment
     </h3>
    </div>
   </div>
  </div>
  <p>
   The following examples show how MySQL determines default
        character set and collation values.
  </p>
  <p>
   <span class="bold">
    <strong>
     Example 1: Table and Column
        Definition
    </strong>
   </span>
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa63150703"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1
<span class="token punctuation">(</span>
    c1 <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin1 <span class="token keyword">COLLATE</span> latin1_german1_ci
<span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin2 <span class="token keyword">COLLATE</span> latin2_bin<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Here we have a column with a
   <code class="literal">
    latin1
   </code>
   character
        set and a
   <code class="literal">
    latin1_german1_ci
   </code>
   collation. The
        definition is explicit, so that is straightforward. Notice that
        there is no problem with storing a
   <code class="literal">
    latin1
   </code>
   column in a
   <code class="literal">
    latin2
   </code>
   table.
  </p>
  <p>
   <span class="bold">
    <strong>
     Example 2: Table and Column
        Definition
    </strong>
   </span>
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa47052632"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1
<span class="token punctuation">(</span>
    c1 <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin1
<span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin1 <span class="token keyword">COLLATE</span> latin1_danish_ci<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   This time we have a column with a
   <code class="literal">
    latin1
   </code>
   character set and a default collation. Although it might seem
        natural, the default collation is not taken from the table
        level. Instead, because the default collation for
   <code class="literal">
    latin1
   </code>
   is always
   <code class="literal">
    latin1_swedish_ci
   </code>
   , column
   <code class="literal">
    c1
   </code>
   has a collation of
   <code class="literal">
    latin1_swedish_ci
   </code>
   (not
   <code class="literal">
    latin1_danish_ci
   </code>
   ).
  </p>
  <p>
   <span class="bold">
    <strong>
     Example 3: Table and Column
        Definition
    </strong>
   </span>
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa30132718"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1
<span class="token punctuation">(</span>
    c1 <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin1 <span class="token keyword">COLLATE</span> latin1_danish_ci<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   We have a column with a default character set and a default
        collation. In this circumstance, MySQL checks the table level to
        determine the column character set and collation. Consequently,
        the character set for column
   <code class="literal">
    c1
   </code>
   is
   <code class="literal">
    latin1
   </code>
   and its collation is
   <code class="literal">
    latin1_danish_ci
   </code>
   .
  </p>
  <p>
   <span class="bold">
    <strong>
     Example 4: Database, Table, and Column
        Definition
    </strong>
   </span>
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa56628653"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">DATABASE</span> d1
    <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> latin2 <span class="token keyword">COLLATE</span> latin2_czech_cs<span class="token punctuation">;</span>
<span class="token keyword">USE</span> d1<span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> t1
<span class="token punctuation">(</span>
    c1 <span class="token datatype">CHAR</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   We create a column without specifying its character set and
        collation. We're also not specifying a character set and a
        collation at the table level. In this circumstance, MySQL checks
        the database level to determine the table settings, which
        thereafter become the column settings.) Consequently, the
        character set for column
   <code class="literal">
    c1
   </code>
   is
   <code class="literal">
    latin2
   </code>
   and its collation is
   <code class="literal">
    latin2_czech_cs
   </code>
   .
  </p>
 </div>
 <br/>
</div>
