<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-shell-tutorial-javascript-table-insert">
      </a>
      22.3.4.1 Insert Records into Tables
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045127606400">
  </a>
  <p>
   You can use the
   <code class="literal">
    insert()
   </code>
   method with the
   <code class="literal">
    values()
   </code>
   method to insert records into an
        existing relational table. The
   <code class="literal">
    insert()
   </code>
   method accepts individual columns or all columns in the table.
        Use one or more
   <code class="literal">
    values()
   </code>
   methods to specify
        the values to be inserted.
  </p>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-javascript-insert-complete-record">
       </a>
       Insert a Complete Record
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127600192">
   </a>
   <p>
    To insert a complete record, pass to the
    <code class="literal">
     insert()
    </code>
    method all columns in the table.
          Then pass to the
    <code class="literal">
     values()
    </code>
    method one value
          for each column in the table. For example, to add a new record
          to the city table in the
    <code class="literal">
     world_x
    </code>
    schema,
          insert the following record and press
    <span class="keycap">
     <strong>
      Enter
     </strong>
    </span>
    twice.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa12736204"><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-js&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 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 function">values</span><span class="token punctuation">(</span>
None<span class="token punctuation">,</span> <span class="token string">"Olympia"</span><span class="token punctuation">,</span> <span class="token string">"USA"</span><span class="token punctuation">,</span> <span class="token string">"Washington"</span><span class="token punctuation">,</span> <span class="token string">'{"Population": 5000}'</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    The city table has five columns: ID, Name, CountryCode,
          District, and Info. Each value must match the data type of the
          column it represents.
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="mysql-shell-tutorial-javascript-insert-partial-record">
       </a>
       Insert a Partial Record
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045127591440">
   </a>
   <p>
    The following example inserts values into the ID, Name, and
          CountryCode columns of the city table.
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-mysqlsh"><div class="docs-select-all right" id="sa6756219"><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-js&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 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 punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span>
None<span class="token punctuation">,</span> <span class="token string">"Little Falls"</span><span class="token punctuation">,</span> <span class="token string">"USA"</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">"Happy Valley"</span><span class="token punctuation">,</span> <span class="token string">"USA"</span><span class="token punctuation">)</span></code></pre>
   </div>
   <p>
    When you specify columns using the
    <code class="literal">
     insert()
    </code>
    method, the number of values must match the number of columns.
          In the previous example, you must supply three values to match
          the three columns specified.
   </p>
  </div>
  <div class="simplesect">
   <div class="titlepage">
    <div>
     <div class="simple">
      <h5 class="title">
       <a name="idm46045127586256">
       </a>
       Related Information
      </h5>
     </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/crud-ebnf-table-crud-functions.html#crud-ebnf-tableinsertfunction" target="_top">
        TableInsertFunction
       </a>
       for
              the full syntax definition.
      </p>
     </li>
    </ul>
   </div>
  </div>
 </div>
 <br/>
</div>
