1.96k likes | 3.91k Vues
Access Queries. Queries. Most common type of Query is selection(projection) Specify sources for data retrieval table(s) and/or query( ies ) Specify attributes(fields) to be retrieved Selection criteria Calculations that need to be performed Action Queries Other types such as crosstabs.
E N D
Queries • Most common type of Query is selection(projection) • Specify sources for data retrieval • table(s) and/or query(ies) • Specify attributes(fields) to be retrieved • Selection criteria • Calculations that need to be performed • Action Queries • Other types such as crosstabs
Query Languages • Relational Algebra • Provides the conceptual basis for SQL and QBE • Structured Query Language (SQL) • The user enters commands according to a pre-defined syntax to retrieve desired data. • Query By Example (QBE) • The user starts with a sample of the table(s) columns and marks the fields he or she wants to include in the answer. • Defaults are available for summarizing and manipulating the data.
Creating Queries • Query Wizard • QBE Query Design • SQL design (beyond scope of our class)
Creating Query 2k7 • Wizards • Click on create tab • Select query wizard • Select simple query • Select your data source(s) • Select desired attributes from each data source • Select next • Name query • Click on finish
Wizard Click on create tab and select Query Wizard
Access creates the SQL needed to do the query for the wizard • SQL for previous query example
Design View • Uses QBE (what we will use) • Design View • Click on create tab • Select query design • Select your data sources and click on add • Select attributes from each data source and drag to grid below • Run query • Save query if it will be used on a regular basis
Select attributes from each data source and drag to grid below
Access creates the SQL needed to do the query • SQL for previous query example
Queries with selection criteria • Use design view when using selection criteria • Select your data sources and attributes if interest from your data sources enter selection criteria • Run query
Query to list Customers from Brazil Selection Criteria
Comparison operators <, >, =, <=, >=, and <> Not equal to Logical operators And, Or, and Not Queries(complex selection criteria)
Multiple Criteria • AND operator • Queries that must satisfy more than one condition, with all conditions being true, use the ANDoperator • OR operator • When any one of several criteria is all that is required for a row to be displayed in a dynaset, then you use the OR operator
And Operator Selection Criteria
OR Operator Selection Criteria
Action queries • Action Queries • create tables, • Delete/remove records, • update fields, or • Append/add new records to an existing table • Action queries alter information as opposed to selection queries, which passively display information without altering anything • No undo feature
Make Table • Use design view • Select source table • Select attributes • Select make table query symbol and enter name for your table in pop-up box • Run query • You will receive a message about the number of records being added to table • Save query if it will be used in the future
Select source table Select attributes Note use of wildcard *. Wildcard selects all attributes
Select make table query symbol and enter name for your table in pop-up box
Delete Query • Use design view • Select source table • Select records/attributes selection criteria • Select delete query symbol • Run query • You will receive a message about the number of records being deleted • Save query if it will be used in the future
Note there are currently 91 records in the Norhtwind Traders customer table
Select delete query symbol Criteria for deletion
DELETE tblCustomersUSA.*, tblCustomersUSA.Country FROM tblCustomersUSA WHERE (((tblCustomersUSA.Country)="usa")); Note there are now 78 records in the Norhtwind Traders customer table
DELETE tblCustomersUSA.*, tblCustomersUSA.Country FROM tblCustomersUSA WHERE (((tblCustomersUSA.Country)="usa"));
Update Query • Use design view • Select source table • Select attribute(s) to be updated • Select update query symbol • Fill in update to criteria • Run query • You will receive a message about the number of records being updated • Save query if it will be used in the future
Select attribute(s) to be updated Select update query symbol Update to Criteria
Append Query • Use design view • Select source table(s) • Select append query symbol • Identify destination table that source table will be appended to • Drag fields from source to grid • Access will automatically map fields from source and destination that have same name • Manual map for all others • Run query • You will receive a message about the number of records being updated
Select append Query Symbol Select Append to Table and click on ok
Select field and drag to grid below • Notice that field mapping has been done for most fields automatically • Must map to like type data