320 likes | 455 Views
Data Query. Peter McCartney. RDIFS Training Workshop Sevilleta LTER October 28-30, 2002. Goals. Retrieve data from storage Join tables as needed to produce matrix output (denormalize) Transform data to meet analytic needs Filter, aggregate, select. Structured Query Language.
E N D
Data Query Peter McCartney RDIFS Training WorkshopSevilleta LTEROctober 28-30, 2002 Center for Environmental Studies Arizona State University
Goals • Retrieve data from storage • Join tables as needed to produce matrix output (denormalize) • Transform data to meet analytic needs • Filter, aggregate, select Center for Environmental Studies Arizona State University
Structured Query Language • Universal syntax for querying relational datbases. • Provides means for specifying • Attributes required • The source tables and how to join them • Criteria for limiting the result (filtering) • Instructions for ordering the result set Center for Environmental Studies Arizona State University
A Simple SQL Query SELECT personnel.first_name, personnel.last_name, personnel.country_code FROM personnel WHERE personnel.country_code="usa“ Center for Environmental Studies Arizona State University
A Slightly More Complex Query SELECT personnel.first_name, personnel.last_name, country.country_name, interests.interest_area FROM country INNER JOIN (personnel INNER JOIN interests ON personnel.person_ID = interests.person_ID) ON country.country_code = personnel.country_code WHERE (((personnel.country_code)="usa")) Center for Environmental Studies Arizona State University
Result Center for Environmental Studies Arizona State University
Views • Views are query statements that are stored in the database. • Views return a matrix and may be treated as if they were another table (you can use a view in a query) • Only the query statement is stored; the result set is reproduced each time you call the view • Views let you hide much of the complicated table joins from your users Center for Environmental Studies Arizona State University
Query a View SELECT myFirstView.* FROM myFirstView WHERE (((myFirstView.interest_area)="data management")); Center for Environmental Studies Arizona State University
Querying Remote Databases • The beauty of relational databases is that you can use a variety of client applications to query them. • Your users do NOT need to learn Access in order to use your database. Center for Environmental Studies Arizona State University
Accessing Databases • Native protocol • Every database has its own native protocol. Applications must be written to this. • Open Data Base Connectivity • Windows, Unix, Mac • Java Data Base Connectivity • Cross platform • Designed for Java Language Center for Environmental Studies Arizona State University
A Typical Client Server Connection Connectivity layer translates applications requests into database‘s native syntax. Database (Access) ConnectionLayers (ODBC) Application(Excel) NativeSyntax StandardSQL Center for Environmental Studies Arizona State University
Define an ODBC Source Center for Environmental Studies Arizona State University
Query • Once an odbc source is defined, you may access it from any application that supports ODBC • Excel, SPSS, ArcView, MapInfo, Active Server Pages • Range of capabilities and user friendly features will vary Center for Environmental Studies Arizona State University
Query from Arc View Center for Environmental Studies Arizona State University
Programming the Web • Client side • ECMCA languages – Javascript • Code executed by client browser. All data must be transferred to browser • Server side • Template “page” scripting • CGI programs Center for Environmental Studies Arizona State University
Web Query • HTTPD/CGI programs • External program receives form input, generates entire page to be returned. • Server side Java • Server scripting • Permits embedding script within html pages • Html code is returned as is, script code is executed by script engine prior to page being returned. • Active Server Pages (MS IIS) • Java Server Pages ( Apache tomcat, BEA WebLogic, IBM WebSphere, Allaire Jrun Center for Environmental Studies Arizona State University
Three-tiered Query using ASP • Declare a connection object and at least one recordset object. • Open the connection to your ODBC source • Parse the form request parameters • Build an sql query • Execute the query putting the result in your recordset • Access the data in your recordset and write html output to display the data in the page Center for Environmental Studies Arizona State University
Lab Assignment • Add some data to your database • Create some views and store them in your database • (or use the samples at: \\alamillo\inetpub\wwwroot\dbweb) • Create an ODBC data source • Query your data from Access • Create an asp page to display some data following templates: • http://alamillo/dbweb Center for Environmental Studies Arizona State University