sql.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!-- meta page description: SQL support in GRASS GIS -->
  2. <!-- this file is lib/db/sqlp/sql.html -->
  3. Vector points, lines and areas usually have attribute data that are
  4. stored in DBMS. The attributes are linked to each vector object using a
  5. category number (attribute ID, usually the "cat" integer column). The
  6. category numbers are stored both in the vector geometry and the
  7. attribute table.
  8. <p>
  9. GRASS GIS supports various RDBMS
  10. (<a href="http://en.wikipedia.org/wiki/Relational_database_management_system">Relational
  11. database management system</a>) and embedded databases. SQL
  12. (<a href="http://en.wikipedia.org/wiki/Sql">Structured Query
  13. Language</a>) queries are directly passed to the underlying database
  14. system. The set of supported SQL commands depends on the RDMBS and
  15. database driver selected.
  16. <h2>Database drivers</h2>
  17. The default database driver used by GRASS GIS 8 is SQLite. GRASS GIS
  18. handles multiattribute vector data by default. The <em>db.*</em> set of
  19. commands provides basic SQL support for attribute management, while the
  20. <em>v.db.*</em> set of commands operates on vector maps.
  21. <p>
  22. Note: The list of available database drivers can vary in various binary
  23. distributions of GRASS GIS:
  24. <p>
  25. <table class="border">
  26. <tr><td><a href="grass-sqlite.html">sqlite</a></td><td>Data storage in SQLite database files (default DB backend)</td>
  27. <td><a href="http://sqlite.org/">http://sqlite.org/</a></td></tr>
  28. <tr><td><a href="grass-dbf.html">dbf</a></td><td>Data storage in DBF files</td>
  29. <td><a href="http://shapelib.maptools.org/dbf_api.html">http://shapelib.maptools.org/dbf_api.html</a></td></tr>
  30. <tr><td><a href="grass-pg.html">pg</a></td><td>Data storage in PostgreSQL RDBMS</td>
  31. <td><a href="http://postgresql.org/">http://postgresql.org/</a></td></tr>
  32. <tr><td><a href="grass-mysql.html">mysql</a></td><td>Data storage in MySQL RDBMS</td>
  33. <td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
  34. <!--
  35. <tr><td><a href="grass-mesql.html">mesql</a></td><td>Data are stored in MySQL embedded database</td>
  36. <td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
  37. -->
  38. <tr><td><a href="grass-odbc.html">odbc</a></td><td>Data storage via UnixODBC (PostgreSQL, Oracle, etc.)</td>
  39. <td><a href="http://www.unixodbc.org/">http://www.unixodbc.org/</a></td></tr>
  40. <tr><td><a href="grass-ogr.html">ogr</a></td><td>Data storage in OGR files</td>
  41. <td><a href="http://gdal.org">http://gdal.org/</a></td></tr>
  42. </table>
  43. <h2>NOTES</h2>
  44. <h3>Database table name restrictions</h3>
  45. <ul>
  46. <li> No dots are allowed as SQL does not support '.' (dots) in table names.</li>
  47. <li> Supported table name characters are only: <br>
  48. <div class="code"><pre>
  49. [A-Za-z][A-Za-z0-9_]*
  50. </pre></div></li>
  51. <li> A table name must start with a character, not a number.</li>
  52. <li> Text-string matching requires the text part to be 'single quoted'.
  53. When run from the command line multiple queries should be contained
  54. in "double quotes". e.g.<br>
  55. <div class="code"><pre>
  56. d.vect map where="individual='juvenile' and area='beach'"
  57. </pre></div></li>
  58. <li> Attempts to use a reserved SQL word (depends on database backend) as
  59. column or table name will cause a "SQL syntax error".</li>
  60. <li> An error message such as &quot;<tt>dbmi: Protocol
  61. error</tt>&quot; either indicates an invalid column name or an
  62. unsupported column type (then the GRASS SQL parser needs to be
  63. extended).</li>
  64. <li> DBF column names are limited to 10 characters (DBF API definition).</li>
  65. </ul>
  66. <h3>Database table column types</h3>
  67. The supported types of columns depend on the database backend. However, all backends
  68. should support VARCHAR, INT, DOUBLE PRECISION and DATE.
  69. <h2>EXAMPLES</h2>
  70. <h3>Display of vector feature selected by attribute query</h3>
  71. Display all vector points except for <i>LAMAR</i> valley
  72. and <i>extensive trapping</i> (brackets are superfluous in this
  73. example):
  74. <div class="code"><pre>
  75. g.region vector=schools_wake -p
  76. d.mon wx0
  77. d.vect roadsmajor
  78. # all schools
  79. d.vect schools_wake fcol=black icon=basic/diamond col=white size=13
  80. # numerical selection: show schools with capacity of above 1000 kids:
  81. d.vect schools_wake fcol=blue icon=basic/diamond col=white size=13 \
  82. where="CAPACITYTO &gt; 1000"
  83. # string selection: all schools outside of Raleigh
  84. # along with higher level schools in Raleigh
  85. d.vect schools_wake fcol=red icon=basic/diamond col=white size=13 \
  86. where="ADDRCITY &lt;&gt; 'Raleigh' OR (ADDRCITY = 'Raleigh' AND GLEVEL = 'H')"
  87. </pre></div>
  88. <p>
  89. Select all attributes from table where <i>CORECAPACI</i> column values are
  90. smaller than 200 (children):
  91. <div class="code"><pre>
  92. # must be run from the mapset which contains the table
  93. echo "SELECT * FROM schools_wake WHERE CORECAPACI &lt; 200" | db.select input=-
  94. </pre></div>
  95. <p>
  96. <p>
  97. Example of subquery expressions from a list (not supported for DBF driver):
  98. <div class="code"><pre>
  99. v.db.select schools_wake where="ADDRCITY IN ('Apex', 'Wendell')"
  100. </pre></div>
  101. <h3>Example of pattern matching</h3>
  102. <div class="code"><pre>
  103. # field contains string:
  104. # for DBF driver:
  105. v.extract schools_wake out=elementary_schools where="NAMELONG LIKE 'ELEM'"
  106. # for SQLite driver:
  107. v.extract schools_wake out=rivers_noce where="DES LIKE '%NOCE%'"
  108. v.extract schools_wake out=elementary_schools where="NAMELONG LIKE '%ELEM%'"
  109. # match exactly number of characters (here: 2), does not work for DBF driver:
  110. v.db.select mysites where="id LIKE 'P__'"
  111. #define wildcard:
  112. v.db.select mysites where="id LIKE 'P%'"
  113. </pre></div>
  114. <h3>Example of null handling</h3>
  115. <div class="code"><pre>
  116. v.db.addcolumn map=roads col="nulltest int"
  117. v.db.update map=roads col=nulltest value=1 where="cat &gt; 2"
  118. d.vect roads where="nulltest is null"
  119. v.db.update map=roads col=nulltest value=2 where="cat &lt;= 2"
  120. </pre></div>
  121. <h3>Update of attributes</h3>
  122. Examples of complex expressions in updates (using <tt>v.db.*</tt>
  123. modules):
  124. <div class="code"><pre>
  125. v.db.addcolumn map=roads column="exprtest double precision"
  126. v.db.update map=roads column=exprtest value="cat/nulltest"
  127. v.db.update map=roads column=exprtest value="cat/nulltest+cat" where="cat=1"
  128. # using data from another column
  129. v.db.update map=roads column=exprtest qcolumn="(cat*100.)/SHAPE_LEN."
  130. </pre></div>
  131. <p>
  132. Examples of more complex expressions in updates (using <tt>db.*</tt>
  133. modules):
  134. <div class="code"><pre>
  135. echo "UPDATE roads SET exprtest=null"
  136. echo "UPDATE roads SET exprtest=cat/2" | db.execute
  137. echo "UPDATE roads SET exprtest=cat/2+cat/3" | db.execute
  138. echo "UPDATE roads SET exprtest=NULL WHERE cat&gt;2" | db.execute
  139. echo "UPDATE roads SET exprtest=cat/3*(cat+1) WHERE exprtest IS NULL" | db.execute"
  140. </pre></div>
  141. <p>
  142. Instead of creating and updating new columns with an expression, you
  143. can use the expression directly in a command:
  144. <div class="code"><pre>
  145. d.vect roads where="(cat/3*(cat+1))&gt;8"
  146. d.vect roads where="cat&gt;exprtest"
  147. </pre></div>
  148. <h3>Example of changing a SQL type (type casting)</h3>
  149. <i>Note: not supported for <a href="grass-dbf.html">DBF driver</a>.</i>
  150. <p>
  151. North Carolina data set: convert string column to double precision:
  152. <p>
  153. <div class="code"><pre>
  154. # first copy map into current mapset
  155. g.copy vect=geodetic_pts,mygeodetic_pts
  156. v.db.addcolumn mygeodetic_pts col="zval double precision"
  157. # the 'z_value' col contains 'N/A' strings, not to be converted
  158. v.db.update mygeodetic_pts col=zval \
  159. qcol="CAST(z_value AS double precision)" \
  160. where="z_value &lt;&gt; 'N/A'"
  161. </pre></div>
  162. <h3>Example of concatenation of fields</h3>
  163. <i>Note: not supported for <a href="grass-dbf.html">DBF driver</a>.</i>
  164. <div class="code"><pre>
  165. v.db.update vectormap column=column3 qcolumn="column1 || column2"
  166. </pre></div>
  167. <h3>Example of conditions</h3>
  168. Conditions (like if statements) are usually written as CASE statement in SQL:
  169. <div class="code"><pre>
  170. v.db.update vectormap column=species qcolumn="CASE WHEN col1 &gt;= 12 THEN cat else NULL end"
  171. # a more complex example with nested conditions
  172. v.db.update vectormap column=species qcolumn="CASE WHEN col1 &gt;= 1 THEN cat WHEN row = 13 then 0 ELSE NULL end"
  173. </pre></div>
  174. <h2>SEE ALSO</h2>
  175. <em>
  176. <a href="db.connect.html">db.connect</a>,
  177. <a href="db.select.html">db.select</a>,
  178. <a href="db.execute.html">db.execute</a>,
  179. <a href="v.db.connect.html">v.db.connect</a>,
  180. <a href="v.db.select.html">v.db.select</a>,
  181. <a href="v.db.update.html">v.db.update</a>
  182. </em>
  183. <p>
  184. <a href="databaseintro.html">Database management in GRASS GIS</a>,
  185. <a href="database.html">Help pages for database modules</a>
  186. <h2>AUTHOR</h2>
  187. Radmin Blazek