AIN102.1 Microsoft Access Queries Module 1

776 views

Published on

AIN102 Microsoft Access Queries. Basic select queries, filter conditions, sorting, calculations, concatenation, special operators.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
776
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

AIN102.1 Microsoft Access Queries Module 1

  1. 1. AIN102 Access Query Design P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com
  2. 2. AIN102 Contact Information Admissions AIN102 Module 1 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-20011 All rights reserved.
  3. 3. AIN102 Notes <ul><li>This course is based on an earlier course, SQL200A, which was focused on using SQL in the Access environment. As such the screenshots still reflect SQL view as well the Query Designer view. This is actually a plus. </li></ul>Admissions AIN102 Module 1
  4. 4. AIN102 Resources <ul><li>Manuals can be downloaded from SlideShare. </li></ul><ul><li>Slides can be viewed on SlideShare… </li></ul><ul><ul><li>http://www.slideshare.net/OCDatabases </li></ul></ul>Admissions AIN102 Module 1
  5. 5. AIN102 Access Query Design Module 1 – Relational Database Background, Basic Single Table Retrieval Operations
  6. 6. AIN102 <ul><li>Session 1 – Review of Relational Databases and Single Table Retrieval, Functions and Operators </li></ul><ul><li>Session 2 – Multi-table retrieval, Subqueries, Unions, Action Queries, Indexes </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  7. 7. AIN102 <ul><li>Lecture/discussion with demonstrations </li></ul><ul><ul><li>Mostly practical, but a dose of “theory” </li></ul></ul><ul><li>Assumes some familiarity with MS Access </li></ul><ul><li>Hands-on </li></ul><ul><li>Exercises (Mostly cumulative) </li></ul><ul><li>About 3 hours per module </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  8. 8. Module 1 <ul><li>Concepts </li></ul><ul><ul><li>Relational databases </li></ul></ul><ul><ul><li>SQL </li></ul></ul><ul><ul><li>Access Query Designer </li></ul></ul><ul><li>SELECT Query </li></ul><ul><ul><li>Basic </li></ul></ul><ul><ul><li>Filter criteria </li></ul></ul><ul><ul><li>Complex criteria </li></ul></ul>AIN102 Module 1 Admissions AIN102 Module 1 <ul><li>Field Operations </li></ul><ul><ul><li>Functions </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Calculated fields </li></ul></ul><ul><ul><li>Concatenation </li></ul></ul>
  9. 9. Microsoft Access Query Design <ul><li>Course focus is Microsoft Access Query Design </li></ul><ul><li>Queries are widely used for: </li></ul><ul><ul><li>Database administration </li></ul></ul><ul><ul><li>Enterprise application development </li></ul></ul><ul><ul><li>Data driven web sites </li></ul></ul><ul><ul><li>Retrieving and modifying data in MS Access databases </li></ul></ul><ul><li>A foundation skill for eBusiness and almost all major business applications that use relational databases </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  10. 10. Access Query Design and SQL <ul><li>Class database will be MS Access – the SQL engine Access uses is called JET </li></ul><ul><li>A basic knowledge of Access is assumed </li></ul><ul><li>Focus of this class will be on the query designer, with SQL used from time-to-time to explain concepts </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  11. 11. Relational Database Basics <ul><li>Storage </li></ul><ul><li>Databases </li></ul><ul><li>Tables </li></ul><ul><li>Rows </li></ul><ul><li>Columns </li></ul><ul><li>Indexes </li></ul><ul><li>Views </li></ul><ul><li>SQL interface </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  12. 12. Storage <ul><li>In Access one file with extension mdb or accdb. I.e mydatabase.accdb . </li></ul><ul><li>Database splitter can be used to split it into two parts – a front end and a back end. </li></ul><ul><li>Interface to physical storage via the “JET” database engine. This is the behind-the scenes-workhorse. </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  13. 13. Databases <ul><li>In the Access world refers to all objects stored in the mdb. </li></ul><ul><ul><li>Tables </li></ul></ul><ul><ul><li>Queries </li></ul></ul><ul><ul><li>From </li></ul></ul><ul><ul><li>Reports </li></ul></ul><ul><ul><li>Macros </li></ul></ul><ul><ul><li>Code modules </li></ul></ul>AIN102 Module 1 Admissions AIN102 Module 1
  14. 14. AIN102 Module 1 Admissions AIN102 Module 1 Relational Database Table
  15. 15. AIN102 Module 1 Admissions AIN102 Module 1 Sample Access Database
  16. 16. AIN102 Module 1 Admissions AIN102 Module 1 Database Relationships
  17. 17. Approaching Query Design <ul><li>Try to build queries a step at a time </li></ul><ul><li>Save existing queries that are working as you like; modify them with a new name when building a modified query </li></ul><ul><li>Try looking at the generated SQL code from time-to-time </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  18. 18. Query Types <ul><li>Select </li></ul><ul><li>Action </li></ul><ul><ul><li>Update </li></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>Maketable </li></ul></ul><ul><li>Special use queries </li></ul><ul><ul><li>Crosstab </li></ul></ul><ul><ul><li>Data Definition Language (DDL) </li></ul></ul><ul><ul><li>Union </li></ul></ul><ul><ul><li>SQL pass-through </li></ul></ul>AIN102 Module 1 Admissions AIN102 Module 1
  19. 19. DDL <ul><li>Data definition language (DDL) to create and modify tables, etc. </li></ul><ul><ul><li>Create, alter, drop, etc. </li></ul></ul><ul><ul><li>Must be “hand-coded” in Access </li></ul></ul><ul><ul><li>But can be useful for database administration </li></ul></ul><ul><li>Done in MS Access via SQL Specific Queries – Will not be covered in the course </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  20. 20. MS Access DDL Queries AIN102 Module 1 Admissions AIN102 Module 1
  21. 21. DCL <ul><li>Data Control Language (DCL) to control user rights, etc. </li></ul><ul><ul><li>Grant </li></ul></ul><ul><ul><li>Revoke </li></ul></ul><ul><ul><li>Constraints </li></ul></ul><ul><li>Used primarily in enterprise databases </li></ul><ul><li>Can be done in Access but not covered in this course </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  22. 22. DML <ul><li>Data Manipulation Language (DML) </li></ul><ul><ul><li>Select Query </li></ul></ul><ul><ul><li>Append Query </li></ul></ul><ul><ul><li>Update Query </li></ul></ul><ul><ul><li>Delete Query </li></ul></ul><ul><ul><li>Maketable Query (a special type of Select) </li></ul></ul><ul><ul><li>Union Query (really combined Selects) </li></ul></ul><ul><li>MS Access Query Designer will create these statements “behind the scenes” </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  23. 23. MS Access Action Queries AIN102 Module 1 Admissions AIN102 Module 1
  24. 24. Other Access Queries <ul><li>Crosstab </li></ul><ul><li>Pass Through – used to pass an SQL statement directly to a back end database w/out processing by Access </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  25. 25. Sample Database <ul><li>Before we continue… </li></ul><ul><li>Load the sample database if you haven’t already </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  26. 26. Query Designer AIN102 Module 1 Admissions AIN102 Module 1 Right click anywhere for SQL View Double click or drag to add a field Double click to add a table
  27. 27. SELECT Queries AIN102 Module 1 Admissions AIN102 Module 1 Basic Syntax: Select column-list or * From table-list * Means all columns
  28. 28. MS Access SELECT Query AIN102 Module 1 Admissions AIN102 Module 1 Drag and Drop or double click
  29. 29. MS Access SELECT SQL AIN102 Module 1 Admissions AIN102 Module 1
  30. 30. AIN102 Module 1 Admissions AIN102 Module 1 Simple Select Query Results
  31. 31. SELECT w/ Where AIN102 Module 1 Admissions AIN102 Module 1 Filters retrieved rows Syntax: SELECT column-list FROM table-list WHERE selection-criteria
  32. 32. Comparison Operators <ul><li>< less than </li></ul><ul><li>> greater than </li></ul><ul><li><= less than or equal </li></ul><ul><li>>= greater than or equal </li></ul><ul><li>= equal </li></ul><ul><li><> not equal </li></ul>Admissions AIN102 Module 1
  33. 33. SELECT w/ WHERE AIN102 Module 1 Admissions AIN102 Module 1
  34. 34. SELECT w/ WHERE AIN102 Module 1 Admissions AIN102 Module 1
  35. 35. SELECT w/ WHERE Results AIN102 Module 1 Admissions AIN102 Module 1
  36. 36. Complex Filters AIN102 Module 1 Admissions AIN102 Module 1 Follows normal boolean logic Select PatID, LastName, etc… From patients Where (LastName = “Thomas” or birthdate < #1/1/95#) and city = “Corona”)
  37. 37. Complex Filter AIN102 Module 1 Admissions AIN102 Module 1 AND
  38. 38. Select w/ Complex Where AIN102 Module 1 Admissions AIN102 Module 1
  39. 39. Complex Filter Results AIN102 Module 1 Admissions AIN102 Module 1
  40. 40. Special Operators <ul><li>LIKE </li></ul><ul><li>IN </li></ul><ul><li>BETWEEN </li></ul><ul><li>IS NULL </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  41. 41. Like (“Wild Card Matches”) <ul><li>Where customer_last_name like “Jo*” </li></ul><ul><ul><li>*= string of characters </li></ul></ul><ul><li>Where customer_last_name like “Jo?” </li></ul><ul><ul><li>? = exactly one character </li></ul></ul>AIN102 Module 1 Admissions AIN102 Module 1 Access actually allows more sophisticated Unix style search patterns as well – see help.
  42. 42. LIKE AIN102 Module 1 Admissions AIN102 Module 1 Names ending in “S”
  43. 43. LIKE AIN102 Module 1 Admissions AIN102 Module 1 Names ending in “S”
  44. 44. LIKE Results AIN102 Module 1 Admissions AIN102 Module 1
  45. 45. IS NULL AIN102 Module 1 Admissions AIN102 Module 1 Select columnns From clients Where zip IS NULL SQL (including JET) uses three valued logic. Must use IS NULL to test for unknowns. A null is NOT the same as blank or empty.
  46. 46. IS NULL AIN102 Module 1 Admissions AIN102 Module 1
  47. 47. IS NULL AIN102 Module 1 Admissions AIN102 Module 1
  48. 48. IS NULL Results AIN102 Module 1 Admissions AIN102 Module 1 OK. Now try finding all patients with a blank address. Was the above row retrieved? Why or why not?
  49. 49. IN AIN102 Module 1 Admissions AIN102 Module 1 Select * From patients Where lastname IN (“Smith”, “Thomas”, “Juarez”) Note: what is inside parentheses is a list. Later we will replace the list with a subquery which generates the list items.
  50. 50. IN AIN102 Module 1 Admissions AIN102 Module 1
  51. 51. IN AIN102 Module 1 Admissions AIN102 Module 1 List of values
  52. 52. SELECT w/in IN Results AIN102 Module 1 Admissions AIN102 Module 1
  53. 53. BETWEEN AIN102 Module 1 Admissions AIN102 Module 1 SELECT * FROM admissions WHERE admit_date BETWEEN #10/1/2001# and #12/31/2001# Note: between is inclusive
  54. 54. BETWEEN AIN102 Module 1 Admissions AIN102 Module 1
  55. 55. BETWEEN Results AIN102 Module 1 Admissions AIN102 Module 1
  56. 56. Removing Duplicates AIN102 Module 1 Admissions AIN102 Module 1 SELECT DISTINCT PatNo FROM admissions List once each employee who has a labor ticket Removes duplicate rows from result set
  57. 57. DISTINCT AIN102 Module 1 Admissions AIN102 Module 1
  58. 58. Distinct Results AIN102 Module 1 Admissions AIN102 Module 1 W/out Distinct With Distinct
  59. 59. Sorting – ORDER BY AIN102 Module 1 Admissions AIN102 Module 1 DESC will sort in descending order Basic syntax : SELECT column list FROM table list WHERE selection criteria ORDER BY column list [DESC]
  60. 60. Sorting – ORDER BY AIN102 Module 1 Admissions AIN102 Module 1 Select * From patients Where state in(“CA”, “OR”, “WA”) Order by birthdate desc Example: List patients in Pacific Coast sorted by youngest to oldest
  61. 61. Sorting – ORDER BY AIN102 Module 1 Admissions AIN102 Module 1 Note: modify the “select_in” query
  62. 62. Sorting – ORDER BY AIN102 Module 1 Admissions AIN102 Module 1
  63. 63. Sorting – Results AIN102 Module 1 Admissions AIN102 Module 1
  64. 64. Query/SQL Exercises <ul><li>List all patient numbers, etc. for patients who co-paid more than $20; sort by co-pay amount, largest first </li></ul><ul><li>List diagnostics that contain b, d, or q in the diagnostic code </li></ul><ul><li>List the patient id’s for admissions in the fall (Sep, Oct, Nov) of 2001 and 2002 </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  65. 65. Calculated Fields AIN102 Module 1 Admissions AIN102 Module 1 Example: Select a + b as result Syntax: SELECT expression AS new column name Result is a “column alias”
  66. 66. Calculated Fields AIN102 Module 1 Admissions AIN102 Module 1 New column name Follow name with “:”
  67. 67. Calculated Fields AIN102 Module 1 Admissions AIN102 Module 1
  68. 68. Calculated Fields - Results AIN102 Module 1 Admissions AIN102 Module 1
  69. 69. Concatenation <ul><li>“ Adds” two character fields using “&” symbol </li></ul><ul><li>Useful for formatting names, addresses, etc. </li></ul><ul><li>Example: </li></ul><ul><ul><li>Select firstname & “ “ & lastname as fullname </li></ul></ul>AIN102 Module 1 Admissions AIN102 Module 1
  70. 70. Concatenation AIN102 Module 1 Admissions AIN102 Module 1 Sort, but don’t show
  71. 71. Concatenation AIN102 Module 1 Admissions AIN102 Module 1
  72. 72. Concatenation AIN102 Module 1 Admissions AIN102 Module 1 Single column with full name, sorted by last name Tip: handy for reports
  73. 73. SQL Exercises <ul><li>List all the patients with a “fullname” field; sorted by last name. Add a calculated field which shows their loaded copay (copay *1.5). Show the fullname, but not the individual names. </li></ul>AIN102 Module 1 Admissions AIN102 Module 1
  74. 74. End Session 1 AIN102 Module 1 Admissions AIN102 Module 1 Next session : multi-table operations, subqueries, unions, action queries, indexes
  75. 75. Notes Admissions AIN102 Module 1
  76. 76. Notes Admissions AIN102 Module 1

×