<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="mysql-shell-tutorial-python-documents-in-tables">
      </a>
      22.4.5 Documents in Tables
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045126739680">
  </a>
  <p>
   In MySQL, a table may contain traditional relational data, JSON
      values, or both. You can combine traditional data with JSON
      documents by storing the documents in columns having a native
   <a class="link" href="json.html" title="13.5 The JSON Data Type">
    <code class="literal">
     JSON
    </code>
   </a>
   data type.
  </p>
  <p>
   Examples in this section use the city table in the
   <code class="literal">
    world_x
   </code>
   schema.
  </p>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="idm46045126735152">
       </a>
       city Table Description
      </h4>
     </div>
    </div>
   </div>
   <p>
    The city table has five columns (or fields).
   </p>
   <pre class="screen">+---------------+------------+-------+-------+---------+------------------+
| Field         | Type       | Null  | Key   | Default | Extra            |
+---------------+------------+-------+-------+---------+------------------+
| ID            | int(11)    | NO    | PRI   | null    | auto_increment   |
| Name          | char(35)   | NO    |       |         |                  |
| CountryCode   | char(3)    | NO    |       |         |                  |
| District      | char(20)   | NO    |       |         |                  |
| Info          | json       | YES   |       | null    |                  |
+---------------+------------+-------+-------+---------+------------------+
</pre>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="mysql-shell-tutorial-python-insert-record">
       </a>
       Insert a Record
      </h4>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045126731312">
   </a>
   <p>
    To insert a document into the column of a table, pass to the
    <code class="literal">
     values()
    </code>
    method a well-formed JSON document
        in the correct order. In the following example, a document is
        passed as the final value to be inserted into the Info column.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa95015125"><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-mysqlsh"><span class="token prompt">mysql-py&gt;</span> db<span class="token punctuation">.</span>city<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span>
None<span class="token punctuation">,</span> <span class="token string">"San Francisco"</span><span class="token punctuation">,</span> <span class="token string">"USA"</span><span class="token punctuation">,</span> <span class="token string">"California"</span><span class="token punctuation">,</span> <span class="token string">'{"Population":830000}'</span><span class="token punctuation">)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="mysql-shell-tutorial-python-select-record">
       </a>
       Select a Record
      </h4>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045126725440">
   </a>
   <p>
    You can issue a query with a search condition that evaluates
        document values in the expression.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa10168084"><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-mysqlsh"><span class="token prompt">mysql-py&gt;</span> db<span class="token punctuation">.</span>city<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"ID"</span><span class="token punctuation">,</span> <span class="token string">"Name"</span><span class="token punctuation">,</span> <span class="token string">"CountryCode"</span><span class="token punctuation">,</span> <span class="token string">"District"</span><span class="token punctuation">,</span> <span class="token string">"Info"</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span>
<span class="token string">"CountryCode = :country and Info-&gt;'$.Population' &gt; 1000000"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>
<span class="token string">'country'</span><span class="token punctuation">,</span> <span class="token string">'USA'</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 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 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> ID   <span class="token punctuation">|</span> Name           <span class="token punctuation">|</span> CountryCode <span class="token punctuation">|</span> District       <span class="token punctuation">|</span> Info                        <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 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 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> 3793 <span class="token punctuation">|</span> New York       <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> New York       <span class="token punctuation">|</span> {"Population": 8008278}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3794 <span class="token punctuation">|</span> Los Angeles    <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> California     <span class="token punctuation">|</span> {"Population": 3694820}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3795 <span class="token punctuation">|</span> Chicago        <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Illinois       <span class="token punctuation">|</span> {"Population": 2896016}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3796 <span class="token punctuation">|</span> Houston        <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Texas          <span class="token punctuation">|</span> {"Population": 1953631}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3797 <span class="token punctuation">|</span> Philadelphia   <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Pennsylvania   <span class="token punctuation">|</span> {"Population": 1517550}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3798 <span class="token punctuation">|</span> Phoenix        <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Arizona        <span class="token punctuation">|</span> {"Population": 1321045}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3799 <span class="token punctuation">|</span> San Diego      <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> California     <span class="token punctuation">|</span> {"Population": 1223400}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3800 <span class="token punctuation">|</span> Dallas         <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Texas          <span class="token punctuation">|</span> {"Population": 1188580}     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> 3801 <span class="token punctuation">|</span> San Antonio    <span class="token punctuation">|</span> USA         <span class="token punctuation">|</span> Texas          <span class="token punctuation">|</span> {"Population": 1144646}     <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 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 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">9 rows in set (0.01 sec)</span></code></pre>
   </div>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h4 class="title">
       <a name="idm46045126720288">
       </a>
       Related Information
      </h4>
     </div>
    </div>
   </div>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       See
       <a class="ulink" href="/doc/x-devapi-userguide/en/devapi-users-working-with-relational-tables-and-documents.html" target="_top">
        Working with Relational Tables and Documents
       </a>
       for more information.
      </p>
     </li>
     <li class="listitem">
      <p>
       See
       <a class="xref" href="json.html" title="13.5 The JSON Data Type">
        Section 13.5, “The JSON Data Type”
       </a>
       for a detailed description of the
            data type.
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
