Presentation on theme: "Tutorial 3 Queries and Table Relationships"— Presentation transcript:
1Tutorial 3 Queries and Table Relationships Review Assignment pp 3.42 to 3.43
2Queries Simple queries extract information from a single table Complex queries extract information from multiple tablesBased on “relations” between tables
3Development Standards - Queries Two purposes for queriesDisplay information to end-usersThe name of the query must informative (since the end user will select the query from a list) and the displayed column names should be grammatically correct (use captions)Process data or prepare data for a reportTypically, these queries should have a special prefix to separate them from other queries.
4Step 2: Students from Ireland Table: StudentFields: StudentID, FirstName, LastNameSort – ascending by LastNameCriteria: Nation value is “Ireland” (not displayed)Save as: Students from IrelandInclude spaces in name
5Step 3 Use query to update record Note: Access queries are dynamic Find Student ID: OMA9956Change first name to Richard
6Access is a relational database A database is almost always a collection of tables. Access is a relational database management system that allows you to form relationships between the tables.When you form a relationship between tables, you are joining the tables.Tables are joined on common fields between the tables.When tables are joined, you can view data from both tables as if the tables were one combined table.
7A one-to-many relationship Tables can be joined in two (not three) ways:one-to-oneone-to-manyA one-to-many relationship exists when one table has many records associated with a given value but the related table has only one record for that value.The table with one record is called the primary tableThe table with many records is called the related tableQueries can be defined to use the relationship to extract data from both tables in a single query.
8An example of a one-to-many relationship The Employer table is related to the Position table via the common field EmployerID.The Employer table has one record for EmployerID value The Position table has two records.
9Table Relationships Establish “Referential” integrity between tables Link “primary key” field in primary table to related field in the related tableWhen you add a record to the related table, the value in the related field must already exist in the primary tableWhen add a record to Student, the value entered in the SSN field must already exist as a value in the SSN field of Recruiter
10Using referential integrity When dealing with related tables, you need to decide if you want to enforce referential integrity.Referential integrity allows you to maintain the integrity between related tables.The rules associated with referential integrity specify that when you add a record to a related table, there must be a matching record in the primary table.If you choose to enforce referential integrity, you can insure that you will not have orphaned records (records that have no matching record in the primary table).
11Table Relationships – cont’d In Access, have option to cascade update and delete.Cascade update: if change value of SSN field in Recruiter, will automatically update values in the SSN field of StudentCascade delete: if delete a record in Recruiter, will automatically delete all related records in Student (not the value in the SSN field, but the entire student record)
12Use cascaded updates and cascaded deletes In addition to referential integrity, you can also tell Access to implement cascaded updates and cascaded deletes.If you choose cascaded updates, making a change in a field that is common to two related tables will cause the update to be made in both tables.Cascade deletes is similar. If you delete a field that is common to two tables, the deletion will take place in both tables.You should carefully consider whether you want to implement these features, as they can have dramatic effects on your data.
13Step 4 Define relationship between Student and Recruiter Common field is SSNNOTE: plus signs in Recruiter reflect “Join”
14Step 5: Wolfe Recruits Tables: Recruiter & Student Fields FirstName, LastName, City, Nation from StudentBonusQuota, Salary from RecruiterSSN from StudentSort: Ascending by NationCriteria: SSN = (do not display SSN)Resize columns
16Step 9: Bonus Info Table: Recruiter Fields: all except SSN Criteria: BonusQuota between 40 and 50Between operator is inclusive:40 <= BonusQuota <= 50Could also use logical operators>= 40 and <= 50Salary > 35,000
17Step 10: Salaries with Raises Open Bonus Info query in design viewUse Expression Builder to show net amount of 3 percent raiseLabel – Everything before the colon is part of the labelRaiseAmt:Formula – start with =, then select fields and operators=[Salary]*0.03
18Step 10: Salaries with Raises (cont’d) Format RaiseAmt using field propertiesSet the caption to "Raise Amount"Set the format to StandardSet the decimal places to 2Use Save As to save the query
19Step 11: Students from Holland Plus Younger Students Copy Students from Ireland – rename when paste.Modify to:Criteria: Nation of Holland (and show nation)BirthDate: > 1/1/84Note: Access uses # to denote a date value (similar to using quotes to denote a text value)
20Using Aggregate Functions Access has several Aggregate Functions that can be used to calculate various statistical information.Aggregate functions are specified in the Total row of the design grid. They can be assigned by clicking the Totals button on the Query Design toolbar.