28
ESSM 464: Spa*al Project Management Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing Structured Query Language Dr. Michelle Lawing Ecosystem Science and Management Texas A&M University College Sta*on, TX 77843 [email protected]

Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

  • Upload
    lexuyen

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Structured  Query  Language  Dr.  Michelle  Lawing  Ecosystem  Science  and  Management  Texas  A&M  University  College  Sta*on,  TX  77843  [email protected]  

 

Page 2: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Today  In  Class  

•  Announcements  

•  Quiz  

•  Lecture  (sql)  

•  Break  

•  Lab    

Page 3: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Announcements  

•  Website  is  updated  

•  ASPRS  for  Thursday,  April  09  at  5:30  in  214  Centeq  

•  Jenny  Oakley  (Family  emergency)  –  work  on  case  study  regarding  EIH  

 •  Homework  –  read  case  studies  (I  will  post  online)  

Page 4: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Job  Announcement:  GIS  Analyst    Loca*on:  Tyler,  TX    Job  Purpose    -  E.L.S.  Surveying  &  Mapping,  Inc.  is  an  all-­‐inclusive  surveying  firm  specializing  in  oil  and  gas.  

Primary  du*es  for  this  posi*on  include:  crea*ng  geodatabase  deliverables  using  ArcGIS,  CAD  and  Microsog  Excel.  This  posi*on  is  intended  to  provide  our  clients  with  survey  grade  data  in  a  polished  deliverable  format.    

Du:es  and  Responsibili:es  -  Create  geodatabase  deliverables  using  ESRI  sogware    -  Entering  data  efficiently  and  accurately    -  Using  CAD  in  conjunc*on  with  ESRI  sogware    -  Maintain  survey  grade  precision  in  ArcMap  environment    Qualifica:ons    -  Proficient  with  ESRI  sogware  (ArcMap,  ArcCatalog,  etc.)    -  Microsog  Office  experience    -  Knowledge  of  CAD  preferred    -  Programming  skills  are  a  plus.    -  Ability  to  work  well  with  others  in  a  team  oriented  environment    

To  apply,  please  submit  resume  to  [email protected]    

Page 5: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

ESSM  Recep*on  

•  April  17th  6pm  •  hjp://essm.tamu.edu/  

Page 6: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Footprints  5K  

•  April  18th  •  hjp://essm.tamu.edu/footprints/  

Page 7: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Updates  on  Team  Progress  (your  role+)  

•  Overview  of  project  

•  Overall  project  status  

•  The  objec*ves  accomplished  since  the  last  week    •  Upcoming  ac*vi*es  

Page 8: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Quiz  

Page 9: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Structured  Query  Language  (SQL)  

Page 10: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

SQL  

•  standard  computer  language  for  accessing  and  managing  databases  

•  SQL  expressions  are  used  in  many  parts  of  ArcGIS  •  define  a  subset  of  data  on  which  to  perform  some  opera*on  

•  You  use  it  in  ArcMap  to  select  features  with  the  Select  by  Ajributes  dialog  box  

•  You  use  it  in  ArcMap  in  the  Query  Builder  dialog  box  to  set  a  layer  defini*on  query  

Page 11: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

SQL  

•  You  use  it  in  geoprocessing  to  define  a  subset  of  features  •  You  use  it  in  geoprocessing  to  define  records  to  perform  

an  opera*on  on  •  You  also  use  SQL  to  select  features  programma*cally  •  We  will  review  the  basic  WHERE  clause  expressions  •  For  the  SQL  beginner  •   Detailed  reference  here  (hjp://webhelp.esri.com/

arcgisdesktop/9.2/index.cfm?TopicName=SQL_Reference)  

Page 12: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

A  simple  SQL  expression  

•  SELECT  *  FROM  forms  the  first  part  of  the  SQL  expression  

•  This  is  usually  automa*cally  supplied  for  you  •  The  hard-­‐coded  SELECT  *  syntax  means  that  you  must  select  ALL  the  columns  

•  DISTINCT,  ORDER  BY,  GROUP  BY,  and  so  on,  cannot  be  used  readily  in  a  SQL  query  except  when  using  subqueries  

Page 13: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

A  simple  SQL  expression  

•  Depending  on  the  dialog  box  in  which  you  are  building  your  query,  the  name  of  the  layer  or  table  may  be  supplied  for  you  or  you  may  need  to  choose  it  from  a  drop-­‐down  list  

•  The  next  part  of  the  expression  is  the  WHERE  clause,  which  is  the  part  you  must  build.    

•  A  basic  SQL  WHERE  clause  would  look  like            STATE_NAME  =  ‘Texas’    

 

Page 14: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

SQL  Syntax  

•  The  syntax  you  use  to  build  a  SQL  expression  differs  depending  on  the  data  source.    

•  SQL  is  a  standard,  but  not  all  database  sogware  implements  the  same  dialect  of  SQL.  

•  To  query  personal  geodatabases,  you  use  the  same  syntax  used  in  Microsog  Access.    

•  To  query  an  ArcSDE  geodatabase,  you  use  the  SQL  syntax  of  the  underlying  database  management  system  (DBMS)—either  Oracle,  SQL  Server,  DB2,  or  Informix.  

Page 15: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

SQL  Syntax  

•  The  dialog  boxes  you  create  SQL  WHERE  clauses  with  help  you    –  use  the  correct  syntax  for  the  data  you're  querying  –  list  the  field  names  and  values  with  the  appropriate  delimiters  

–  select  the  relevant  keywords  and  operators  for  you  

Page 16: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Searching  Strings  •  Strings  must  always  be  enclosed  within  single  quotes.    •  For  example          

       "STATE_NAME"  =  ‘California’  

•  Strings  in  expressions  are  case  sensi*ve  •  Use  an  SQL  func*on  to  convert  all  values  to  the  same  

case  •  UPPER  or  LOWER  func*on  •  For  example,  the  following  will  select  a  last  name  stored  

as  either  Jones  or  JONES:          UPPER("LAST_NAME")  =  'JONES'        

Page 17: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Searching  Strings  

•  UCASE  and  LCASE  perform  the  same  opera*on  for  a  personal  geodatabase  

•  Use  the  LIKE  operator  (instead  of  the  =  operator)  to  build  a  par*al  string  search.    

•  For  example,  this  expression  would  select  Mississippi  and  Missouri  among  USA  state  names:          "STATE_NAME"  LIKE  'Miss%'    

 

Page 18: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Search  Strings  

•  %  means  that  anything  is  acceptable  in  its  place  –  one  character  –  a  hundred  characters  –  no  character  

•  Alterna*vely,  if  you  want  to  search  with  a  wildcard  that  represents  one  character,  use  _  

•  For  example,  this  expression  would  find  Catherine  Smith  and  Katherine  Smith:      "OWNER_NAME"  LIKE  '_atherine  smith'    

Page 19: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Search  Strings  

•  The  wildcards  work  for  any  file-­‐based  data  or  mul*user  geodatabase  data.    

•  The  wildcards  you  use  to  query  personal  geodatabases  are  *  for  any  number  of  characters  and  ?  for  one  character.    

•  Wildcard  characters  appear  as  bujons  on  the  Select  by  Ajributes  and  Query  Builder  dialog  boxes.    

•  If  you  use  a  wildcard  character  in  a  string  with  the  =  operator,  the  character  is  treated  as  part  of  the  string,  not  as  a  wildcard.    

Page 20: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Search  Strings  

•  Select  based  on  sor*ng  order  –  greater  than  (>)  –  less  than  (<)  –  greater  than  or  equal  (>=)  –  less  than  or  equal  (<=)  –  BETWEEN  –  not  equal  (<>)  

•  For  example,  this  expression  will  select  all  the  ci*es  in  a  coverage  with  names  star*ng  with  the  lejers  M  to  Z:              "CITY_NAME"  >=  'M'    

Page 21: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

The  Null  Keyword  

•  Select  null  values  for  a  specified  field.    •  The  NULL  keyword  is  always  preceded  by  IS  or  IS  NOT.    

•  For  example,  to  find  ci*es  whose  1996  popula*on  has  not  been  entered,  you  can  use          "POPULATION96"  IS  NULL    

•  Alterna*vely,  to  find  ci*es  whose  1996  popula*on  has  been  entered,  you  can  use                "POPULATION96"  IS  NOT  NULL  

Page 22: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Searching  Numbers  •  Query  numbers  by  

–  the  equal  (=)  –  not  equal  (<>)  –  greater  than  (>)  –  less  than  (<)  –  greater  than  or  equal  (>=)  –  less  than  or  equal  (<=)  –  BETWEEN  

•  For  example              "POPULATION96"  >=  5000    

•  Numeric  values  are  always  listed  using  the  point  as  the  decimal  delimiter  regardless  of  your  regional  se}ngs  (PROBLEM  IN  INTERNATIONAL  SETTING).    

•  The  comma  cannot  be  used  as  a  decimal  or  thousands  delimiter  in  an  expression.    

Page 23: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Calcula*ons  •  Calcula*ons  can  be  included  in  expressions  using  the  

arithme*c  operators:  +  -­‐  *  /    •  Calcula*ons  can  be  between  fields  and  numbers.    •  For  example:    

       "AREA"  >=  "PERIMETER"  *  100    

•  Calcula*ons  can  also  be  performed  between  fields.    •  For  example,  to  find  the  countries  with  a  popula*on  density  

of  less  than  or  equal  to  25  people  per  square  mile,  you  could  use  this  expression:            "POP1990"  /  "AREA"  <=  25      

Page 24: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Operator  precedence  •  Expressions  evaluate  according  to  standard  operator  

precedence  rules.    •  For  example,  the  part  of  an  expression  enclosed  in  

parentheses  is  evaluated  before  the  part  that  isn't  enclosed.    

               "HOUSEHOLDS"  >  "MALES"  *  "POP90_SQMI"  +  "AREA“            evaluates  differently  from    

     "HOUSEHOLDS"  >  "MALES"  *  ("POP90_SQMI"  +  "AREA")    

 

Page 25: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Combining  expressions  •  Complex  expressions  can  be  built  by  combining  expressions  with  the  AND  and  OR  operators.    

 For  example,  the  following  expression  would  select  all  the  houses  that  have  more  than  1,500  square  feet  and  a  garage  for  three  or  more  cars:        

•  "AREA"  >  1500  AND  "GARAGE"  >  3      When  you  use  the  OR  operator,  at  least  one  side  of  the  expression  of  the  two  separated  by  the  OR  operator  must  be  true  for  the  record  to  be  selected.      For  example:      "RAINFALL"  <  20  OR  "SLOPE"  >  35      Use  the  NOT  operator  at  the  beginning  of  an  expression  to  find  features  or  records  that  don't  match  the  specified  expression.  For  example:      NOT  "STATE_NAME"  =  'Colorado'      NOT  expressions  can  be  combined  with  AND  and  OR.      For  example,  this  expression  would  select  all  the  New  England  states  except  Maine:      "SUB_REGION"  =  'New  England'  AND  NOT  "STATE_NAME"  =  'Maine'  

Page 26: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Subqueries  •  A  subquery  is  a  query  nested  within  another  query  and  

are  supported  by  geodatabase  data  sources  only.  They  can  be  used  to  apply  predicate  or  aggregate  func*ons  or  to  compare  data  with  values  stored  in  another  table.  For  example,  this  query  would  select  only  the  countries  that  are  not  also  listed  in  the  table  indep_countries:        

•  "COUNTRY_NAME"  NOT  IN  (SELECT  "COUNTRY_NAME"  FROM  indep_countries)      For  more  informa*on,  see  SQL  Reference.  

Page 27: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing  

Querying  Dates  

•  Some  changes  have  been  introduced  in  ArcGIS  9  for  building  expressions  that  query  date  fields.  The  appropriate  syntax  for  the  underlying  data  source  is  automa*cally  shown  in  the  unique  values  list  or  generated  by  the  Query  Builder.  Most  of  the  *me,  you  will  only  need  to  click  the  field,  the  operator,  and  the  value  to  generate  the  proper  syntax.  

Page 28: Structured(Query(Language - people.tamu.edupeople.tamu.edu/~alawing/materials/ESSM464/SQL.pdf · Structured$Query$Language$(SQL)$ ... For$example,$the$following$expression$would$selectall$the$houses$thathave$more$than$

ESSM  464:  Spa*al  Project  Management  Ecosystem  Science  and  Management  |  Texas  A&M  University  (c)  2015,  A.  Michelle  Lawing