db implementation - knukiyang/teaching/gdb/s20/... · 2020. 5. 7. · access queries: sort & filter...

17
DB Implementation: MS Access Queries

Upload: others

Post on 31-Jan-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

  • DB Implementation:

    MS Access Queries

  • MS Access Queries Database Queries

    Core DBA skill → A way of searching for & extracting data from tablestable에서 data를 검색하거나 추출하는 방법

    What does it do? Find target information → Retrieve, Filter, Sort, Aggregate/Summarize Manipulate data → Add, Change, Delete, Combine (& calculate) data in tables Assemble/Supply data → for Forms and Reports

    How does it work? Query object ⇒ SQL & a dynaset (“live” view of table)

    • Data changes made in query is reflected in underlying tables

    Database Systems Seminar 2

  • Access Queries: Views Datasheet view

    For displaying the result of the query → Useful for reviewing/validating the query Design View

    For creating/modifying a query via drag & drop GUI

    Database Systems Seminar 3

    Object Relationship Pane1. Add tables/queries2. Add fields to the Design Grid

    Design Grid3. Sort row → ascending/descending sort4. Show row → show/hide fields5. Criteria row → apply data filter6. Totals row → compute aggregate stats

  • Access Queries: Basic Types Simple Query

    Uses a single table/query• To generate a subset (row/column) of a table

    Multi-table Query Joins multiple tables/queries

    • Joining tables in Query Design does not permanently link tables

    Select Query Selects records that meet given criteria

    • Criteria row ← Selection Criteria

    Parameter Query Prompts for query criteria values

    (parameters) to run a dynamic query• Criteria row ← [Query Prompt]

    (or Search Form control name)Database Systems Seminar 4

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-intro.mp4http://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-intro.mp4

  • Access Queries: Sort & Filter Design View

    Sort using the Sort Row• Sort priority is from left to right for multiple sort

    Filter using the Criteria Row• AND query ← criteria in a single row• OR query ← criteria in multiple rows• Criteria in a single cell → AND/OR

    Query Criteria• Determines which record to include• “expressions” that consist of

    → Constants (e.g., 2000, “Adams”)→ Operators (e.g., >, +, *, AND)→ Functions (e.g., DateDiff)→ Field references (e.g., [Lastname])

    Database Systems Seminar 5

    - Gcflearnfree.org -

  • Access Queries: Query Criteria Date/Time fields

    Number fields

    Database Systems Seminar 6

    Expression Returns

    Between #1/1/15# and #12/31/15# Dates from 1/1/2015 to 12/31/2015

    > #1/1/15# After 1/1/2015

    < Date() Before today

    DateDiff(“yyyy”,Date(),[Birthdate]) > 60 Over 60 years old

    DatePart(“m”, [Birthdate]) = 12 Born in December

    Expression Returns

    Between 10 and 20 Number from 10 to 20

    >= 10 Greater than or equal to 10

    10 Equal to 10

    Not 10 Not equal to 10

    Is Null No value is entered

    In (5,10,15) Number equal to 5, 10, or 15

    MS Access: Query Criteria

    https://support.office.com/en-us/article/examples-of-query-criteria-3197228c-8684-4552-ac03-aba746fb29d8

  • Access Queries: Query Criteria Text fields

    Database Systems Seminar 7

    Expression Returns

    In (“John”, “Mary”, “Jane”) Equal to John, Mary, or Jane

    Like “J*” Starts with “j”

    Like “*s” Ends with “s”

    Like “*oh*” Contains “oh”

    Like “[A-C]??” Starts with A through C and has two more characters

    ???? Any 4 characters

    Len([LastName]) = Val(4) Any LastName of 4 characters

    Right([LastName],2) = “es” Any LastName ending in “es” (same as Like “*es”)

    Left([LastName],2)= “Ja” Any LastName starting with “Ja” (same as Like “Ja*”)

    Is Null No value is entered

    Is Not Null Value is not missing

    Not “James” Not equal to “James”

  • Access Queries: Expressions Using expressions to create a Calculated Field

    Enter expressions in blank column of query design view• NAME: [Field1] operator [Field2]

    Examples Simple math

    • DiscountPrice: [Discount] * [StandardPrice]

    String Concatenation• Author: [FirstName] & “ ” & [Lastname]

    Date & Time math• HireAge: DateDiff(“yyyy”,[BirthDate],[HireDate])

    ← DateDiff(Interval, BeginDate, EndDate)

    • RetireDate: DateAdd(“yyyy”,25,[HireDate])← DateAdd(Interval, Number, Date)

    Customized Sorting/Format• Branch: Switch([City]=“Seattle”, 1, [City]=“Redmond”, 2, [City]=“New York”, 3)

    ← Switch(expr1, value1, expr2, value2, etc.)

    Database Systems Seminar 8

  • Access Queries: Sort & Filter Datasheet View

    Common Filters → Filter for specific values1. Click the icon on the right side of the field label2. Check the values for the filter OR3. Click the Range Filter (e.g. Date Filter) & specify the filter value

    Filter by Selection → Filter by selected value1. Select a specific value in the datasheet2. Right-click & select a filtering option

    Database Systems Seminar 9

  • Access Queries: Sort & Filter Datasheet View

    Filter by Form→ Filter on several fields on a form1. Click Advanced in Sort & Filter group of Home tab2. Select Filter by Form3. Select multiple filter values as needed

    Advanced Filter→ Define custom filters→ Learn how to write query criteria1. Create a filter by Common Filter, Selection, or Form2. Click Advanced in Sort & Filter group of Home tab3. Select Advanced Filter/Sort

    Database Systems Seminar 10

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-expression.mp4http://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-expression.mp4

  • Access Queries: Dynamic Criteria Parameter Queries

    Dynamic query based on varying criteria value Enter the parameter name in square brackets in criteria

    • Do not use existing field names• Can use expressions → e.g. Like "*" & [Last Name] & "*“

    Database Systems Seminar 11

  • Access Queries: Dynamic Criteria Query Criteria from Form Entries

    Use a form to enter query parameter values → good for multiple parameter entries1. Create a parameter query2. Create a search form with unbound input control for each of query parameters3. Add a command button that will run the parameter query

    → Run Query Action of Miscellaneous Category

    4. Change the query criteria to search form control names

    Database Systems Seminar 12

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-dynamic.mp4http://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-dynamic.mp4

  • Access Queries: Joins Inner Join

    Default join in Access (i.e. Natural Join)

    Returns only the records where joined fields are equal in both tables

    Outer join Returns all records from

    one of the tables

    Database Systems Seminar 13

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-join.mp4http://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-join.mp4

  • Database Systems Seminar 14

    Access Queries: Advanced Types Action Queries

    ► Append Query• Appends table rows to an existing table

    ► Delete Query• Deletes table rows

    ► Update Query• Modifies the values of particular fields for particular records

    ► Make Table Query• Creates a new table from rows of other tables/queries

    → will overwrite existing table of the same name

    Crosstab Query ► Performs mathematical operations on intersection of two fields

    SQL-Specific Queries ► Data-definition Query

    • Defines/Changes the definition of a database object (e.g., create/modify a table)

    ► Union Query• Combines multiple SELECT queries

    ► Pass-through Query• Send command directly to ODBC database server to run server-side SQL

  • Database Systems Seminar 15

    Access Queries: Action Queries Append Query → Appends table rows to an existing table

    1. Create a select query.2. Convert the select query to an Append query

    • Append Tool in Query Type group of Design tab3. Choose the destination fields for each column in the append query.4. Run the query to append rows/records.

    Delete Query → Deletes table rows1. Create a select query.2. Convert the select query to a Delete query.

    • Delete Tool in Query Type group of Design tab3. Run the query to delete rows/records.

    Update Query → Modifies the values of particular fields for particular records1. Create a select query.2. Convert the select query to an Update query.

    • Update Tool in Query Type group of Design tab3. Set Update To: (and Criteria when appropriate) rows4. Run the query to update fields/columns.

    Make Table Query → Creates a new table from rows of other tables/queries1. Create a select query.2. Convert the select query to a Make Table query.

    • Make Table Tool in Query Type group of Design tab3. Run the query to update fields/columns.

    Access Action Queries

    https://www.tutorialspoint.com/ms_access/ms_access_action_queries.htmhttp://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-action.mp4

  • Database Systems Seminar 16

    Access Queries: Crosstab Query What is a crosstab query?

    ► To present summary data in a cross-tabulation format

    What does it do?► Calculates aggregate functions (e.g., sum, average, count, etc.) by two fields (row by

    column).

    How to create a crosstab query1. Select the Crosstab Query Wizard.

    (Ribbon → Create tab → Query Wizard → Crosstab Wizard)

    2. Choose a table or query.3. Select fields to be the “row headings”.4. Select fields to be the “column headings”.5. Select the field and function to calculate.

    MS Access: Crosstab Query

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-crosstab.mp4https://support.microsoft.com/en-us/office/make-summary-data-easier-to-read-by-using-a-crosstab-query-8465b89c-2ff2-4cc8-ba60-2cd8484667e8?ui=en-us&rs=en-us&ad=ushttp://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-crosstab.mp4

  • Database Systems Seminar 17

    Access Queries: SQL Queries Data-definition Query → Defines/Changes the definition of a database object

    Create a table• CREATE TABLE table_name

    ( field1 type(size), field2 type(size), … , PRIMARY KEY (field))

    Modify a table• ALTER TABLE table_name

    ADD/ALTER COLUMN field type(size)DROP COLUMN field

    Union Query → Combines multiple SELECT queries Select queries must have the same fields (i.e., Union compatible)

    • SELECT field1, field2, …. FROM table1UNIONSELECT fieldA, fieldB, …. FROM table2

    1. Create the select queries in Design view2. Copy & paste SQL statements into a union query

    Pass-through Query → Send command directly to ODBC database server to run server-side SQL1. Configure the server DBMS as an ODBC data source2. Create a Pass-through query

    MS Access: Data-Definition Query MS Access: Union Query

    http://widit2.knu.ac.kr/%7Ekiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-sql.mp4https://support.microsoft.com/en-ie/office/create-or-modify-tables-or-indexes-by-using-a-data-definition-query-d935e129-229b-48d8-9f2d-1d4ee87f418e?ui=en-us&rs=en-ie&ad=ie#tophttps://support.office.com/en-us/article/use-a-union-query-to-combine-multiple-queries-into-a-single-result-1f772ec0-cc73-474d-ab10-ad0a75541c6ehttp://widit2.knu.ac.kr/~kiyang/teaching/gDB/s20/showpageLN.cgi?course=gDB&semester=s20&inf=vid/query-sql.mp4

    DB Implementation:MS Access QueriesAccess Queries: ViewsAccess Queries: Basic TypesAccess Queries: Sort & FilterAccess Queries: Query CriteriaAccess Queries: Query CriteriaAccess Queries: ExpressionsAccess Queries: Sort & FilterAccess Queries: Sort & FilterAccess Queries: Dynamic CriteriaAccess Queries: Dynamic CriteriaAccess Queries: JoinsAccess Queries: Advanced TypesAccess Queries: Action QueriesAccess Queries: Crosstab QueryAccess Queries: SQL Queries