<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="charset-unicode-utf16">
      </a>
      12.9.5 The utf16 Character Set (UTF-16 Unicode Encoding)
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045216583616">
  </a>
  <p>
   The
   <code class="literal">
    utf16
   </code>
   character set is the
   <code class="literal">
    ucs2
   </code>
   character set with an extension that
        enables encoding of supplementary characters:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      For a BMP character,
      <code class="literal">
       utf16
      </code>
      and
      <code class="literal">
       ucs2
      </code>
      have identical storage
            characteristics: same code values, same encoding, same
            length.
     </p>
    </li>
    <li class="listitem">
     <p>
      For a supplementary character,
      <code class="literal">
       utf16
      </code>
      has
            a special sequence for representing the character using 32
            bits. This is called the
      <span class="quote">
       “
       <span class="quote">
        surrogate
       </span>
       ”
      </span>
      mechanism:
            For a number greater than
      <code class="literal">
       0xffff
      </code>
      , take 10
            bits and add them to
      <code class="literal">
       0xd800
      </code>
      and put them
            in the first 16-bit word, take 10 more bits and add them to
      <code class="literal">
       0xdc00
      </code>
      and put them in the next 16-bit
            word. Consequently, all supplementary characters require 32
            bits, where the first 16 bits are a number between
      <code class="literal">
       0xd800
      </code>
      and
      <code class="literal">
       0xdbff
      </code>
      , and
            the last 16 bits are a number between
      <code class="literal">
       0xdc00
      </code>
      and
      <code class="literal">
       0xdfff
      </code>
      .
            Examples are in Section
      <a class="ulink" href="http://www.unicode.org/versions/Unicode4.0.0/ch15.pdf" target="_blank">
       15.5
            Surrogates Area
      </a>
      of the Unicode 4.0 document.
     </p>
    </li>
   </ul>
  </div>
  <p>
   Because
   <code class="literal">
    utf16
   </code>
   supports surrogates and
   <code class="literal">
    ucs2
   </code>
   does not, there is a validity check that
        applies only in
   <code class="literal">
    utf16
   </code>
   : You cannot insert a
        top surrogate without a bottom surrogate, or vice versa. For
        example:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa74666380"><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">INSERT</span> <span class="token keyword">INTO</span> t <span class="token punctuation">(</span>ucs2_column<span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span>0xd800<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* legal */</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t <span class="token punctuation">(</span>utf16_column<span class="token punctuation">)</span><span class="token keyword">VALUES</span> <span class="token punctuation">(</span>0xd800<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* illegal */</span></code></pre>
  </div>
  <p>
   There is no validity check for characters that are technically
        valid but are not true Unicode (that is, characters that Unicode
        considers to be
   <span class="quote">
    “
    <span class="quote">
     unassigned code points
    </span>
    ”
   </span>
   or
   <span class="quote">
    “
    <span class="quote">
     private use
    </span>
    ”
   </span>
   characters or even
   <span class="quote">
    “
    <span class="quote">
     illegals
    </span>
    ”
   </span>
   like
   <code class="literal">
    0xffff
   </code>
   ). For
        example, since
   <code class="literal">
    U+F8FF
   </code>
   is the Apple Logo, this
        is legal:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa67681255"><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">INSERT</span> <span class="token keyword">INTO</span> t <span class="token punctuation">(</span>utf16_column<span class="token punctuation">)</span><span class="token keyword">VALUES</span> <span class="token punctuation">(</span>0xf8ff<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* legal */</span></code></pre>
  </div>
  <p>
   Such characters cannot be expected to mean the same thing to
        everyone.
  </p>
  <p>
   Because MySQL must allow for the worst case (that one character
        requires four bytes) the maximum length of a
   <code class="literal">
    utf16
   </code>
   column or index is only half of the
        maximum length for a
   <code class="literal">
    ucs2
   </code>
   column or index.
        For example, the maximum length of a
   <code class="literal">
    MEMORY
   </code>
   table index key is 3072 bytes, so these statements create tables
        with the longest permitted indexes for
   <code class="literal">
    ucs2
   </code>
   and
   <code class="literal">
    utf16
   </code>
   columns:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa61414928"><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> tf <span class="token punctuation">(</span>s1 <span class="token datatype">VARCHAR</span><span class="token punctuation">(</span><span class="token number">1536</span><span class="token punctuation">)</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> ucs2<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">MEMORY</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> i <span class="token keyword">ON</span> tf <span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> tg <span class="token punctuation">(</span>s1 <span class="token datatype">VARCHAR</span><span class="token punctuation">(</span><span class="token number">768</span><span class="token punctuation">)</span> <span class="token keyword">CHARACTER</span> <span class="token keyword">SET</span> utf16<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">MEMORY</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> i <span class="token keyword">ON</span> tg <span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
 </div>
 <br/>
</div>
