<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="spatial-type-overview">
      </a>
      13.4.1 Spatial Data Types
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045212175648">
  </a>
  <a class="indexterm" name="idm46045212174576">
  </a>
  <a class="indexterm" name="idm46045212173504">
  </a>
  <a class="indexterm" name="idm46045212172432">
  </a>
  <a class="indexterm" name="idm46045212171360">
  </a>
  <a class="indexterm" name="idm46045212170288">
  </a>
  <a class="indexterm" name="idm46045212169200">
  </a>
  <a class="indexterm" name="idm46045212168128">
  </a>
  <a class="indexterm" name="idm46045212167040">
  </a>
  <a class="indexterm" name="idm46045212165552">
  </a>
  <a class="indexterm" name="idm46045212164064">
  </a>
  <a class="indexterm" name="idm46045212162576">
  </a>
  <a class="indexterm" name="idm46045212161088">
  </a>
  <a class="indexterm" name="idm46045212159600">
  </a>
  <a class="indexterm" name="idm46045212158112">
  </a>
  <a class="indexterm" name="idm46045212156624">
  </a>
  <a class="indexterm" name="idm46045212155136">
  </a>
  <a class="indexterm" name="idm46045212153648">
  </a>
  <p>
   MySQL has spatial data types that correspond to OpenGIS classes.
        The basis for these types is described in
   <a class="xref" href="opengis-geometry-model.html" title="13.4.2 The OpenGIS Geometry Model">
    Section 13.4.2, “The OpenGIS Geometry Model”
   </a>
   .
  </p>
  <p>
   Some spatial data types hold single geometry values:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       GEOMETRY
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       POINT
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       LINESTRING
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       POLYGON
      </code>
     </p>
    </li>
   </ul>
  </div>
  <p>
   <code class="literal">
    GEOMETRY
   </code>
   can store geometry values of any
        type. The other single-value types (
   <code class="literal">
    POINT
   </code>
   ,
   <code class="literal">
    LINESTRING
   </code>
   , and
   <code class="literal">
    POLYGON
   </code>
   )
        restrict their values to a particular geometry type.
  </p>
  <p>
   The other spatial data types hold collections of values:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       MULTIPOINT
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       MULTILINESTRING
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       MULTIPOLYGON
      </code>
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       GEOMETRYCOLLECTION
      </code>
     </p>
    </li>
   </ul>
  </div>
  <p>
   <code class="literal">
    GEOMETRYCOLLECTION
   </code>
   can store a collection of
        objects of any type. The other collection types
        (
   <code class="literal">
    MULTIPOINT
   </code>
   ,
   <code class="literal">
    MULTILINESTRING
   </code>
   , and
   <code class="literal">
    MULTIPOLYGON
   </code>
   ) restrict collection members to
        those having a particular geometry type.
  </p>
  <p>
   Example: To create a table named
   <code class="literal">
    geom
   </code>
   that
        has a column named
   <code class="literal">
    g
   </code>
   that can store values of
        any geometry type, use this statement:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa6408629"><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> geom <span class="token punctuation">(</span>g <span class="token keyword">GEOMETRY</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Columns with a spatial data type can have an
   <code class="literal">
    SRID
   </code>
   attribute, to explicitly indicate the
        spatial reference system (SRS) for values stored in the column.
        For example:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa63676419"><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> geom <span class="token punctuation">(</span>
    p <span class="token keyword">POINT</span> <span class="token keyword">SRID</span> <span class="token number">0</span><span class="token punctuation">,</span>
    g <span class="token keyword">GEOMETRY</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">SRID</span> <span class="token number">4326</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   <code class="literal">
    SPATIAL
   </code>
   indexes can be created on spatial
        columns if they are
   <code class="literal">
    NOT NULL
   </code>
   and have a
        specific SRID, so if you plan to index the column, declare it
        with the
   <code class="literal">
    NOT NULL
   </code>
   and
   <code class="literal">
    SRID
   </code>
   attributes:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa1679370"><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> geom <span class="token punctuation">(</span>g <span class="token keyword">GEOMETRY</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">SRID</span> <span class="token number">4326</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   <code class="literal">
    InnoDB
   </code>
   tables permit
   <code class="literal">
    SRID
   </code>
   values for Cartesian and geographic SRSs.
   <code class="literal">
    MyISAM
   </code>
   tables permit
   <code class="literal">
    SRID
   </code>
   values for Cartesian SRSs.
  </p>
  <p>
   The
   <code class="literal">
    SRID
   </code>
   attribute makes a spatial column
        SRID-restricted, which has these implications:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The column can contain only values with the given SRID.
            Attempts to insert values with a different SRID produce an
            error.
     </p>
    </li>
    <li class="listitem">
     <p>
      The optimizer can use
      <code class="literal">
       SPATIAL
      </code>
      indexes on
            the column. See
      <a class="xref" href="spatial-index-optimization.html" title="10.3.3 SPATIAL Index Optimization">
       Section 10.3.3, “SPATIAL Index Optimization”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
  <p>
   Spatial columns with no
   <code class="literal">
    SRID
   </code>
   attribute are
        not SRID-restricted and accept values with any SRID. However,
        the optimizer cannot use
   <code class="literal">
    SPATIAL
   </code>
   indexes on
        them until the column definition is modified to include an
   <code class="literal">
    SRID
   </code>
   attribute, which may require that the
        column contents first be modified so that all values have the
        same SRID.
  </p>
  <p>
   For other examples showing how to use spatial data types in
        MySQL, see
   <a class="xref" href="creating-spatial-columns.html" title="13.4.6 Creating Spatial Columns">
    Section 13.4.6, “Creating Spatial Columns”
   </a>
   . For
        information about spatial reference systems, see
   <a class="xref" href="spatial-reference-systems.html" title="13.4.5 Spatial Reference System Support">
    Section 13.4.5, “Spatial Reference System Support”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
