Presentation on theme: "Tutorial 3 Queries and Table Relationships Review Assignment pp 3.42 to 3.43."— Presentation transcript:
Tutorial 3 Queries and Table Relationships Review Assignment pp 3.42 to 3.43
Queries Simple queries extract information from a single table Complex queries extract information from multiple tables Based on “relations” between tables
Development Standards - Queries Two purposes for queries Display information to end-users The 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 report Typically, these queries should have a special prefix to separate them from other queries.
Step 2: Students from Ireland Table: Student Fields: StudentID, FirstName, LastName Sort – ascending by LastName Criteria: Nation value is “Ireland” (not displayed) Save as: Students from Ireland Include spaces in name
Step 3 Use query to update record Note: Access queries are dynamic Find Student ID: OMA9956 Change first name to Richard
Access 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.
A one-to-many relationship Tables can be joined in two (not three) ways: one-to-one one-to-many A 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 table The table with many records is called the related table Queries can be defined to use the relationship to extract data from both tables in a single query.
An 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 10126. The Position table has two records.
Table Relationships Establish “Referential” integrity between tables Link “primary key” field in primary table to related field in the related table When you add a record to the related table, the value in the related field must already exist in the primary table When add a record to Student, the value entered in the SSN field must already exist as a value in the SSN field of Recruiter
Using 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).
Table 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 Student Cascade 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)
Use 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.
Step 4 Define relationship between Student and Recruiter Common field is SSN NOTE: plus signs in Recruiter reflect “Join”
Step 5: Wolfe Recruits Tables: Recruiter & Student Fields FirstName, LastName, City, Nation from Student BonusQuota, Salary from Recruiter SSN from Student Sort: Ascending by Nation Criteria: SSN = 977071798 (do not display SSN) Resize columns
Logical Operations in Criteria
Step 9: Bonus Info Table: Recruiter Fields: all except SSN Criteria: BonusQuota between 40 and 50 Between operator is inclusive: 40 <= BonusQuota <= 50 Could also use logical operators >= 40 and <= 50 Salary > 35,000
Step 10: Salaries with Raises Open Bonus Info query in design view Use Expression Builder to show net amount of 3 percent raise Label – Everything before the colon is part of the label RaiseAmt: Formula – start with =, then select fields and operators =[Salary]*0.03
Step 10: Salaries with Raises (cont’d) Format RaiseAmt using field properties Set the caption to "Raise Amount" Set the format to Standard Set the decimal places to 2 Use Save As to save the query
Step 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/84 Note: Access uses # to denote a date value (similar to using quotes to denote a text value)
Using 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.