Date post: | 17-Jul-2015 |
Category: |
Technology |
Upload: | dan-durso |
View: | 145 times |
Download: | 0 times |
Accelerated Computer Training for Working Professionals Admissions Case
Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com
Orange CoastDatabase Associates
Specializing in Microsoft Office,
Access, SQL, and related technologiesClasses custom designed forWorking Professionals
http://ocdatabases.itgo.com
San Juan Capistrano, CA
(800)355-9855
SQL200A Focus on MicrosoftAccess SQL
About Us1
Accelerated Computer Training forWorking Professionals
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com
Welcome to Orange Coast DatabaseAssociates, Inc.
About Us2
Orange Coast Database Associates
Loose association of local University instructors and ITProfessionals.
Training– Open Enrollment (public classes in San Juan Capistrano using
standardized material, some of it proprietary)
– On-site (classes for groups, may be custom tailored)
– Private desk side training for individuals
Consulting & Programming (incl. offshore)
About Us3
About Us4
Contact Information
Contact: Dan D’Urso
Phone: 800-355-9855
Fax: 949-485-6284 Email: [email protected]
Course listing:
– http://www.dhdursoassociates.com/courses.shtml
Course calendar:
– http://www.dhdursoassociates.com/xcalendar/calendar.htm
Admissions SQL200A1
SQL200A – Access SQL
Introduction to Access SQL forexperienced Windows users
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.dhdursoassociates.com
AdmissionsSQL200A2
SQL200A – Access SQL
Introduction (s)
Facilities
Course Packet– Student questionnaire
– PowerPoint handouts for all sessions
– Evaluation form
– Training Certificate
AdmissionsSQL200A3
Quick pace for experienced windows users
Assumes some prior knowledge of Access
End-user, not programmer, oriented
Somewhat structured towards personal use foranalysis and reporting
SQL200A – Access Query Design (SQL)
AdmissionsSQL200A4
Select Queries– Filter Criteria
– Joins
– Grouping
Action Queries– Maketable
– Insert (Append)
– Update
– Delete
Union
Course Topics:
SQL200A – Access Query Design
AdmissionsSQL200A5
Approach:
SQL View Used in code; but helpful inunderstanding what a querydoes
Query Designer Use to construct queries
Access has many ways to accomplish tasks. Queries can beconstructed using SQL View or the Query Designer. Thiscourse focuses on the former.
SQL200A – Access Query Design
AdmissionsSQL200A6
2 Sessions
Lecture
Demo
Extensive student “hands-on”
Exercises are cumulative – laterexamples build on objects createdearlier
Course Format:
SQL200A – Access Query Design
AdmissionsSQL200A7
Session 1
Session 2
Course Schedule (3 hour sessions):
SQL200A – Access Query Design
Notes
AdmissionsSQL200A8
1
SQL200A
Access SQL Query Design
Module 1 – Relational Database Background,Basic Single Table Retrieval Operations
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com
Admissions SQL200A Module 12
SQL200A
Session 1 – Review of Relational Databasesand Single Table Retrieval, Functions andOperators
Session 2 – Multi-table retrieval, Subqueries,Unions, Action Queries, Indexes
2
Admissions SQL200A Module 13
SQL200A
Lecture/discussion with demonstrations
– Mostly practical, but a dose of “theory”
Assumes some familiarity with MS Access
Hands-on
Exercises (Mostly cumulative)
About 3 hours per module
Frequent breaks
Admissions SQL200A Module 14
Module 1
Concepts– Relational databases
– SQL
– Access QueryDesigner
SELECT Query– Basic
– Filter criteria
– Complex criteria
– Parameters
• Field Operations• Functions
• Operators
• Calculated fields
• Concatenation
3
Admissions SQL200A Module 15
Microsoft Access Query Design
Focus of this class will be on SQL view, with thequery designer used from time-to-time to explainconcepts
SQL is widely used for:– Database administration– Enterprise application development– Data driven web sites– Retrieving and modifying data in MS Access databases
A foundation skill for eBusiness and almost all majorbusiness applications that use relational databases
Admissions SQL200A Module 16
Access Query Design and SQL
The SQL engine Access uses is called JET
It support both MS Access and Visual Basic
It uses a dialect of SQL which is very close tostandard SQL
4
Admissions SQL200A Module 17
Relational Database Basics
Storage
Databases
Tables
Rows
Columns
Indexes
Views
SQL interface
Admissions SQL200A Module 18
Storage
In Access one file with extension mdb. I.emydatabase.mdb.
Database splitter can be used to split it intotwo parts – a front end and a back end.
Interface to physical storage via the “JET”database engine. This is the behind-thescenes-workhorse.
5
Admissions SQL200A Module 19
Databases
In the Access world refers to all objectsstored in the mdb.– Tables
– Queries
– From
– Reports
– Macros
– Code modules
Admissions SQL200A Module 110
Relational Database Table
6
Admissions SQL200A Module 111
Sample Access Database
Admissions SQL200A Module 112
Database Relationships
7
Admissions SQL200A Module 113
Approaching SQL Query Design
Try to build queries a step at a time
Save existing queries that are workingas you like; modify them with a newname when building a modified query
Try looking at the design view fromtime-to-time
Admissions SQL200A Module 114
Query Types
Select Action
– Update– Delete– Insert– Maketable
Special use queries– Data Definition Language (DDL)– Union– SQL pass-through
8
Admissions SQL200A Module 115
DDL
Data definition language (DDL) to create andmodify tables, etc.
– Create, alter, drop, etc.
– Must be “hand-coded” in Access
– But can be useful for database administration
Done in MS Access via SQL Specific Queries– Will not be covered in the course
Admissions SQL200A Module 116
MS Access DDL Queries
9
Admissions SQL200A Module 117
DCL
Data Control Language (DCL) to control userrights, etc.– Grant
– Revoke
– Constraints
Used primarily in enterprise databases
Can be done in Access but not covered inthis course
Admissions SQL200A Module 118
DML
Data Manipulation Language (DML)– Select Query
– Append Query
– Update Query
– Delete Query
– Maketable Query (a special type of Select)
– Union Query (really combined Selects)
MS Access Query Designer will create thesestatements “behind the scenes”
10
Admissions SQL200A Module 119
MS Access Action Queries
Admissions SQL200A Module 120
Other Access Queries
Crosstab
Pass Through – used to pass an SQLstatement directly to a back end databasew/out processing by Access
11
Admissions SQL200A Module 121
Sample Database
Before we continue…
Load the sample database if you haven’talready
Admissions SQL200A Module 122
Query DesignerRight click anywhere
for SQL View
Double click or dragto add a field
Double click to add atable
12
Admissions SQL200A Module 123
SELECT Queries
Basic Syntax:
Select column-list or *
From table-list
* Means all columns
Admissions SQL200A Module 124
MS Access SELECT SQL
13
Admissions SQL200A Module 125
MS Access SELECT Query
Drag and Dropor double click
Admissions SQL200A Module 126
Simple Select Query Results
14
Admissions SQL200A Module 127
SELECT w/ Where
Filters retrieved rows
Syntax:
SELECT column-list
FROM table-list
WHERE selection-criteria
Admissions SQL200A Module 128
SELECT w/ WHERE
15
Admissions SQL200A Module 129
SELECT w/ WHERE (Filter)
Admissions SQL200A Module 130
SELECT w/ WHERE Results
16
Admissions SQL200A Module 131
Complex Filters
Follows normal boolean logic
Select PatID, LastName, etc…
From patients
Where (LastName = “Thomas” orbirthdate < #1/1/95#) and city =“Corona”)
Admissions SQL200A Module 132
Select w/ Complex Where
17
Admissions SQL200A Module 133
Complex Filter
AND
Admissions SQL200A Module 134
Complex Filter Results
18
Admissions SQL200A Module 135
Special Operators
LIKE
IN
BETWEEN
IS NULL
Admissions SQL200A Module 136
Like (“Wild Card Matches”)
Where customer_last_name like “Jo*”– *= string of characters
Where customer_last_name like “Jo?”– ? = exactly one character
Access actually allows more sophisticated Unix stylesearch patterns as well – see help.
19
Admissions SQL200A Module 137
LIKE
Names ending in “S”
Admissions SQL200A Module 138
LIKE
Names ending in “S”
20
Admissions SQL200A Module 139
LIKE Results
Admissions SQL200A Module 140
IS NULL
Select columnnsFrom clientsWhere zip IS NULL
SQL (including JET) uses three valued logic.Must use IS NULL to test for unknowns. A null isNOT the same as blank or empty.
21
Admissions SQL200A Module 141
IS NULL
Admissions SQL200A Module 142
IS NULL
22
Admissions SQL200A Module 143
IS NULL Results
OK. Now try finding all patients with a blank address. Wasthe above row retrieved? Why or why not?
Admissions SQL200A Module 144
IN
Select *From patientsWhere lastname IN (“Smith”,“Thomas”, “Juarez”)
Note: what is inside parentheses is a list. Later wewill replace the list with a subquery whichgenerates the list items.
23
Admissions SQL200A Module 145
IN
List of values
Admissions SQL200A Module 146
IN
24
Admissions SQL200A Module 147
SELECT w/in IN Results
Admissions SQL200A Module 148
BETWEENSELECT *FROM admissionsWHERE admit_date BETWEEN#10/1/2001# and #12/31/2001#
Note: between is inclusive
25
Admissions SQL200A Module 149
BETWEEN
Admissions SQL200A Module 150
BETWEEN Results
26
Admissions SQL200A Module 151
Removing Duplicates
SELECT DISTINCT PatNoFROM admissions
List once each employee who has alabor ticket
Removes duplicate rows from result set
Admissions SQL200A Module 152
DISTINCT
27
Admissions SQL200A Module 153
Distinct Results
W/out Distinct With Distinct
Admissions SQL200A Module 154
Sorting – ORDER BY
DESC will sort in descending order
Basic syntax:
SELECT column listFROM table listWHERE selection criteriaORDER BY column list [DESC]
28
Admissions SQL200A Module 155
Sorting – ORDER BY
Select *From patientsWhere state in(“CA”, “OR”, “WA”)Order by birthdate desc
Example:List patients in Pacific Coast sortedby youngest to oldest
Admissions SQL200A Module 156
Sorting – ORDER BY
29
Admissions SQL200A Module 157
Sorting – ORDER BY
Note: modify the “select_in” query
Admissions SQL200A Module 158
Sorting – Results
30
Admissions SQL200A Module 159
Query/SQL Exercises
List all patient numbers, etc. for patients who co-paid more than $20; sort by co-pay amount,largest first
List diagnostics that contain b, d, or q in thediagnostic code
List the patient id’s for admissions in the fall (Sep,Oct, Nov) of 2001 and 2002
Admissions SQL200A Module 160
Parameter Queries
Can parameterize queries
Use unknown names, example:
Select *
From patients
Where patno =[please input patient no]
31
Admissions SQL200A Module 161
Calculated Fields
Example:Select a + b as result
Syntax:SELECT expression AS newcolumn name
Result is a “columnalias”
Admissions SQL200A Module 162
Calculated Fields
32
Admissions SQL200A Module 163
Calculated Fields
New column name
Follow name with “:”
Admissions SQL200A Module 164
Calculated Fields - Results
33
Admissions SQL200A Module 165
Concatenation
“Adds” two character fields using “&” symbol
Useful for formatting names, addresses, etc.
Example:
– Select firstname & “ “ & lastname as fullname
Admissions SQL200A Module 166
Concatenation
34
Admissions SQL200A Module 167
Concatenation
Sort, but don’tshow
Admissions SQL200A Module 168
ConcatenationSingle column with full
name, sorted by last name
Tip: handy for reports
35
Admissions SQL200A Module 169
SQL Exercises
List all the patient names sorted by last name. Add acalculated field which shows their address concatenatedtogether in “written” format with commas and blanks.
Create a second query by modifying the above.Parameterize the query with a wildcard parameter onstreet.
Admissions SQL200A Module 170
End Session 1
Next session: grouping andaggregates, multi-table operations,subqueries, unions, action queries
36
Notes
Admissions SQL200A Module 171
Notes
Admissions SQL200A Module 172
1
Admissions SQL200A Module 21
SQL200A
Access SQL Query DesignModule 2
P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.dhdursoassociates.com
Admissions SQL200A Module 22
Module 2
Part 1– Miscellaneous Functions
Strings Dates
– Summaries (Grouping)
Part 2 – Joins,subqueries
– Inner join– Outer joins– Subqueries
Multi-valued Single-valued
• Part 3 – Unions,Action Queries,Indexes• Unions
• Maketable Query
• Delete
• Append
• Update
• Indexes
2
Admissions SQL200A Module 23
String Manipulation
Trim
Substring
UCase, LCase
Left, Right
See help for others
Admissions SQL200A Module 24
String Manipulation
3
Admissions SQL200A Module 25
String Manipulation
Admissions SQL200A Module 26
String Example
4
Admissions SQL200A Module 27
Date Functions
Numerous date functions– DatePart– DateDiff– DateAdd– Etc.
Often used:– Year– Month
Ex: where year(birthdate) = 1999
Admissions SQL200A Module 28
DateDiff How long did patients stay in weeks?
5
Admissions SQL200A Module 29
DateDiff results
Note: Access has many date functions with many options.
Admissions SQL200A Module 210
Ex: Date Function – Month()
6
Admissions SQL200A Module 211
Ex: Date Function – Month()
Admissions SQL200A Module 212
Result of Month Function
7
Admissions SQL200A Module 213
Summary Functions
Count
Sum
Min
Max
Avg
Often used in conjunctionwith grouping
Admissions SQL200A Module 214
Summary Functions in Access
Click the sum symbol
Adds a total row
8
Admissions SQL200A Module 215
Summary Functions - SyntaxBasic syntax:
SELECT function(column)FROM tableWHERE filter-conditionGROUP BY column-listHAVING group-filter
Group by all columns to left of one(s)you to want aggregate
Admissions SQL200A Module 216
Simple Column Summaries
This query counts patient admissions In the year 2001
9
Admissions SQL200A Module 217
Simple Column Summaries
Admissions SQL200A Module 218
Simple Record Count
10
Admissions SQL200A Module 219
“The COUNTS”
Count(*) – counts records
Count(fieldname) – counts non–nulloccurrences of field name
Count (distinct fieldname) – counts distinctoccurrences, but not supported in access
Admissions SQL200A Module 220
Grouping
Organizes results into summary rows, one pergroup
Groups can have sub groups which have subgroups and so on….
11
Admissions SQL200A Module 221
GROUP BY in Query Designer
Admissions SQL200A Module 222
GROUP BY Problem
Not an aggregateor group
You will see this error a lot. Not to worry. It happens to everyone!
12
Admissions SQL200A Module 223
Group By Results
Admissions SQL200A Module 224
Having
Restricts the groups returned
Operates on the groups after they have beenformed
Having the count > 1
13
Admissions SQL200A Module 225
Having – SQL View
Restricts the groups returned
Operates on the groups after they have been formed
Admissions SQL200A Module 226
HAVING
14
Admissions SQL200A Module 227
HAVING Results
Admissions SQL200A Module 228
SQL200A
Access SQL Query DesignPart 2 – Joins, Subqueries
15
Admissions SQL200A Module 229
Database DesignDiagram of the database for reference in joins
Admissions SQL200A Module 230
Joins
Used to combine columns from morethan one table
Several types– Inner– Outer
Left Right
– Others (not covered) Full Outer (Not supported in Access) Cross Self Non equal
16
Admissions SQL200A Module 231
Inner Join
Pairs each row from first table withcorresponding row from second table overthe “join column” or “linking column”
The result only contains rows where there isa match over the join column in both tables
The default join in most databases
Admissions SQL200A Module 232
Inner Join Syntax
Basic SQL 92 Syntax:
SELECT column-list
FROM table1 [AS alias]
INNER JOIN table2 [AS alias]
ON join-condition
17
Admissions SQL200A Module 233
Table Aliases
Shorthand name for a table
Used in more complex queries
Select t.id, s.lnameFrom _traveler as tInner join xLU_Staff as sOn t.staffid = s.staffid
Table alias
Admissions SQL200A Module 234
Inner Join Basic SQL Example
Basic Example: Add patient names to
admissions data
Two join tables
Join condition
18
Admissions SQL200A Module 235
Inner Join Query Design
Admissions SQL200A Module 236
Inner Join Results
19
Admissions SQL200A Module 237
Inner Join over Multiple columns
Note that that the join condition can apply tomultiple columns if desired
Used with composite keysSelect ….From tablea as taInner join tableb as tbOn ta.key1 = tb.key1And ta.key2 = tb.key2
Admissions SQL200A Module 238
Joining More than Two Tables
Can join several tables in one select
Try to limit to three or four
Demonstration example
20
Admissions SQL200A Module 239
3 Table Query Results
Admissions SQL200A Module 240
More on Aliases
Can be set in QueryDesigner, too, byright clicking on atable
Useful for debuggingtop level queries aswe shall see later
21
Admissions SQL200A Module 241
Outer Joins
Left– selects all rows from the left or first table, even if no match
exists in the other table
– Widely used in commercial practice, esp. for reporting
Right– same idea but all rows from right table
Full
– all rows from both tables; not supported in Access
Admissions SQL200A Module 242
Left Outer Join
Basic SQL 92 Syntax:
SELECT column-list
FROM table1
LEFT JOIN table2
ON join-condition
22
Admissions SQL200A Module 243
Left Outer Join(Right click on relationship to edit)
Admissions SQL200A Module 244
Left Outer Join
Modify your prior inner join touse a left join
Save as qryLeft_Admit
Now run both the inner andleft joins
What is the difference?
23
Admissions SQL200A Module 245
Left Outer Join Results
Admissions SQL200A Module 246
Subqueries
One select statement embedded in another
Can be nested multiple levels deep
In Access query designer can be placed incriteria row or field row
24
Admissions SQL200A Module 247
Multi-valued Subquery
A type of subquery that compares to a list Ex: find all diagnostic codes with no admissions Commonly encountered in commercial practice
Admissions SQL200A Module 248
Multi-valued Subquery
Place subquery in criteria row
25
Admissions SQL200A Module 249
Multi-valued Subquery Result
No one was ever admitted with these diagnoses.
Admissions SQL200A Module 250
Single-valued Subquery
Subquery that returns a single value
Find all admissions with stays greater than the average stay
26
Admissions SQL200A Module 251
Single-valued Subquery
Subquery that returns a single value
Admissions SQL200A Module 252
Single-valued Subquery Result
27
Admissions SQL200A Module 253
Queries using Queries
Queries can be read just like a table
Some are updateable
Using a query as a source for another queryvery common in Access – especially forcomplicated queries
In a “back end” database such as Oracle orSQL Sever you would use a View
Admissions SQL200A Module 254
Queries using QueriesFilter the prior query ( a little on the complicated side)for the year 2001
28
Admissions SQL200A Module 255
Queries using Queries Result
Admissions SQL200A Module 256
SQL200A
Access Query Design
Part 3 – Unions, Action Queries
29
Admissions SQL200A Module 257
Special Queries
Most are “Action Queries”
Reached through Query pull-down menu
Admissions SQL200A Module 258
Special Queries
Special action queries have distinct symbols
30
Admissions SQL200A Module 259
Admissions Database
Diagram of the database for reference in part 3
Admissions SQL200A Module 260
Data Modification Queries
SQL Action Query
INSERT Append
UPDATE Update
DELETE Delete
SELECT…INTO Maketable
31
Admissions SQL200A Module 261
Creating Action Queries
First set up the select query based on thetable you are selecting from
Test select query
Then convert to action query
When you run an action query there is noresult displayed – must look at theaffected table to see the result
Admissions SQL200A Module 262
Select Into…Maketable
Creates a new table “on the fly”
32
Admissions SQL200A Module 263
Select Into…Maketable
New table
Existing table
Admissions SQL200A Module 264
Archived Labor History
33
Admissions SQL200A Module 265
Delete
Deletes one or more rows
Basic Syntax:
DELETE FROM table-name
WHERE filter-criteria
Admissions SQL200A Module 266
Delete
Example: Delete all archivedadmissions_history newer than 12/31/01
34
Admissions SQL200A Module 267
Delete in Query Designer
Example:Delete allarchivedrecordsnewer than12/31/2001
Admissions SQL200A Module 268
Archive after DELETE
35
Admissions SQL200A Module 269
More complex DELETE with FROMclause
Can delete based on matchingrecords in other tables
Uses from for criteria instead ofwhere – uses the matched rows asan implicit filter condition
Covered in advanced class
Admissions SQL200A Module 270
Insert – Append Query
Adds new rows to an existing table
Two forms:
– Single Row
– Multi-Row
36
Admissions SQL200A Module 271
Single Row Insert
Basic Syntax:
Insert [into] table-name
Values (value-list)
Admissions SQL200A Module 272
Single Row Insert
Basic Example:
INSERT into xLU_BuildStyle
values(6, “Thick”)
Not really used this way in MS Access. Typical MSAccess use would be Insert…Select as shown infollowing slides.
37
Admissions SQL200A Module 273
Multi-row Insert
Basic Syntax:
INSERT [INTO] table-name
SELECT select-statement
Admissions SQL200A Module 274
Append Query (Multi-row insert)
38
Admissions SQL200A Module 275
Multi-row Insert (Append)
Add back archived admissions_historynewer than 12/31/01
Admissions SQL200A Module 276
Multi-Row Insert
39
Admissions SQL200A Module 277
Insert (Append) Results
Admissions SQL200A Module 278
Update
Updates fields in an existing row
Basic Syntax:
UPDATE table-name
SET field1 = new value, field2 = new value,…
WHERE selection-criteria
40
Admissions SQL200A Module 279
Update
Increase the admission_history copays by 10%(unrealistic to change history but we don’t want to step on ourgood tables)
Admissions SQL200A Module 280
Update in Query Designer
New value
41
Admissions SQL200A Module 281
Update Results
New values
Remember: you have to look at the affected table.No “result” is displayed.
Admissions SQL200A Module 282
Unions
Combines the results of two queries– Ex: current records and history
Tables must be union compatible (at least in theory!) There is no designer in Access; must be done in SQL view
42
Admissions SQL200A Module 283
Union Syntax
SELECT column-list
FROM table1
UNION [ALL]
SELECT same-columns
FROM table2
Admissions SQL200A Module 284
Union Example
43
Admissions SQL200A Module 285
Union Result
If you want to know more
This is the last class in our standard offerings
However, we are happy to provide customclasses or tutoring specifically for your needs
You can also outsource some or all of yourMicrosoft Access project work to us
Admissions SQL200A Module 286
44
Admissions SQL200A Module 287
End Session 2
End of Class!
Thank you for your time.
Notes
Admissions SQL200A Module 288
Accelerated Computer Training for Working Professionals
Orange Coast Database Associates Course (800)355-9855 or http://www.d2associates.com
Orange CoastDatabase Associates
Specializing in Microsoft Office,
Access, SQL, and related technologiesComputer Training, Programming & Consulting
32422 Alipaz St., Suite A-15
San Juan Capistrano, CA
(800)355-9855 (Toll Free) | (949)489-1472 (Direct) | (949)485-6284 (Fax)
http://www.d2associates.com | [email protected]
Accelerated Computer Training