<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="charset-metadata">
      </a>
      12.2.2 UTF-8 for Metadata
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045218012320">
  </a>
  <a class="indexterm" name="idm46045218010832">
  </a>
  <a class="indexterm" name="idm46045218009344">
  </a>
  <p>
   <span class="firstterm">
    Metadata
   </span>
   is
   <span class="quote">
    “
    <span class="quote">
     the
        data about the data.
    </span>
    ”
   </span>
   Anything that
   <span class="emphasis">
    <em>
     describes
    </em>
   </span>
   the database—as opposed to
        being the
   <span class="emphasis">
    <em>
     contents
    </em>
   </span>
   of the database—is
        metadata. Thus column names, database names, user names, version
        names, and most of the string results from
   <a class="link" href="show.html" title="15.7.7 SHOW Statements">
    <code class="literal">
     SHOW
    </code>
   </a>
   are metadata. This is also
        true of the contents of tables in
   <code class="literal">
    INFORMATION_SCHEMA
   </code>
   because those tables by
        definition contain information about database objects.
  </p>
  <p>
   Representation of metadata must satisfy these requirements:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      All metadata must be in the same character set. Otherwise,
            neither the
      <a class="link" href="show.html" title="15.7.7 SHOW Statements">
       <code class="literal">
        SHOW
       </code>
      </a>
      statements
            nor
      <a class="link" href="select.html" title="15.2.13 SELECT Statement">
       <code class="literal">
        SELECT
       </code>
      </a>
      statements for
            tables in
      <code class="literal">
       INFORMATION_SCHEMA
      </code>
      would work
            properly because different rows in the same column of the
            results of these operations would be in different character
            sets.
     </p>
    </li>
    <li class="listitem">
     <p>
      Metadata must include all characters in all languages.
            Otherwise, users would not be able to name columns and
            tables using their own languages.
     </p>
    </li>
   </ul>
  </div>
  <p>
   To satisfy both requirements, MySQL stores metadata in a Unicode
        character set, namely UTF-8. This does not cause any disruption
        if you never use accented or non-Latin characters. But if you
        do, you should be aware that metadata is in UTF-8.
  </p>
  <p>
   The metadata requirements mean that the return values of the
   <a class="link" href="information-functions.html#function_user">
    <code class="literal">
     USER()
    </code>
   </a>
   ,
   <a class="link" href="information-functions.html#function_current-user">
    <code class="literal">
     CURRENT_USER()
    </code>
   </a>
   ,
   <a class="link" href="information-functions.html#function_session-user">
    <code class="literal">
     SESSION_USER()
    </code>
   </a>
   ,
   <a class="link" href="information-functions.html#function_system-user">
    <code class="literal">
     SYSTEM_USER()
    </code>
   </a>
   ,
   <a class="link" href="information-functions.html#function_database">
    <code class="literal">
     DATABASE()
    </code>
   </a>
   , and
   <a class="link" href="information-functions.html#function_version">
    <code class="literal">
     VERSION()
    </code>
   </a>
   functions have the
        UTF-8 character set by default.
  </p>
  <p>
   The server sets the
   <a class="link" href="server-system-variables.html#sysvar_character_set_system">
    <code class="literal">
     character_set_system
    </code>
   </a>
   system
        variable to the name of the metadata character set:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa46736618"><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 prompt">mysql&gt;</span> <span class="token keyword">SHOW</span> <span class="token keyword">VARIABLES</span> <span class="token operator">LIKE</span> <span class="token string">'character_set_system'</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Variable_name        <span class="token punctuation">|</span> Value   <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> character_set_system <span class="token punctuation">|</span> utf8mb3 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   Storage of metadata using Unicode does
   <span class="emphasis">
    <em>
     not
    </em>
   </span>
   mean that the server returns headers of columns and the results
        of
   <a class="link" href="describe.html" title="15.8.1 DESCRIBE Statement">
    <code class="literal">
     DESCRIBE
    </code>
   </a>
   functions in the
   <a class="link" href="server-system-variables.html#sysvar_character_set_system">
    <code class="literal">
     character_set_system
    </code>
   </a>
   character
        set by default. When you use
   <code class="literal">
    SELECT column1 FROM
        t
   </code>
   , the name
   <code class="literal">
    column1
   </code>
   itself is
        returned from the server to the client in the character set
        determined by the value of the
   <a class="link" href="server-system-variables.html#sysvar_character_set_results">
    <code class="literal">
     character_set_results
    </code>
   </a>
   system
        variable, which has a default value of
   <code class="literal">
    utf8mb4
   </code>
   . If you want the server to pass
        metadata results back in a different character set, use the
   <a class="link" href="set-names.html" title="15.7.6.3 SET NAMES Statement">
    <code class="literal">
     SET NAMES
    </code>
   </a>
   statement to force the
        server to perform character set conversion.
   <a class="link" href="set-names.html" title="15.7.6.3 SET NAMES Statement">
    <code class="literal">
     SET NAMES
    </code>
   </a>
   sets the
   <a class="link" href="server-system-variables.html#sysvar_character_set_results">
    <code class="literal">
     character_set_results
    </code>
   </a>
   and other
        related system variables. (See
   <a class="xref" href="charset-connection.html" title="12.4 Connection Character Sets and Collations">
    Section 12.4, “Connection Character Sets and Collations”
   </a>
   .) Alternatively, a client
        program can perform the conversion after receiving the result
        from the server. It is more efficient for the client to perform
        the conversion, but this option is not always available for all
        clients.
  </p>
  <p>
   If
   <a class="link" href="server-system-variables.html#sysvar_character_set_results">
    <code class="literal">
     character_set_results
    </code>
   </a>
   is set
        to
   <code class="literal">
    NULL
   </code>
   , no conversion is performed and the
        server returns metadata using its original character set (the
        set indicated by
   <a class="link" href="server-system-variables.html#sysvar_character_set_system">
    <code class="literal">
     character_set_system
    </code>
   </a>
   ).
  </p>
  <p>
   Error messages returned from the server to the client are
        converted to the client character set automatically, as with
        metadata.
  </p>
  <p>
   If you are using (for example) the
   <a class="link" href="information-functions.html#function_user">
    <code class="literal">
     USER()
    </code>
   </a>
   function for comparison or
        assignment within a single statement, don't worry. MySQL
        performs some automatic conversion for you.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa64011689"><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">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1 <span class="token keyword">WHERE</span> <span class="token function">USER</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> latin1_column<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   This works because the contents of
   <code class="literal">
    latin1_column
   </code>
   are automatically converted to
        UTF-8 before the comparison.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa16091371"><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> t1 <span class="token punctuation">(</span>latin1_column<span class="token punctuation">)</span> <span class="token keyword">SELECT</span> <span class="token function">USER</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   This works because the contents of
   <a class="link" href="information-functions.html#function_user">
    <code class="literal">
     USER()
    </code>
   </a>
   are automatically
        converted to
   <code class="literal">
    latin1
   </code>
   before the assignment.
  </p>
  <p>
   Although automatic conversion is not in the SQL standard, the
        standard does say that every character set is (in terms of
        supported characters) a
   <span class="quote">
    “
    <span class="quote">
     subset
    </span>
    ”
   </span>
   of Unicode.
        Because it is a well-known principle that
   <span class="quote">
    “
    <span class="quote">
     what applies to
        a superset can apply to a subset,
    </span>
    ”
   </span>
   we believe that a
        collation for Unicode can apply for comparisons with non-Unicode
        strings. For more information about coercion of strings, see
   <a class="xref" href="charset-collation-coercibility.html" title="12.8.4 Collation Coercibility in Expressions">
    Section 12.8.4, “Collation Coercibility in Expressions”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
