Your name here
description
Transcript of Your name here
Your name hereThe lecture notes are based on using Microsoft Access
interactively as part of the lecture
Manipulating Database Content with Relational Algebra and Microsoft Access
• How to manipulate information in relational databases – Using relational algebra and Microsoft Access
• How to define simple one-table queries– Projection queries – Selection queries
• How to define queries that combine multiple tables – Product queries – Join queries– Queries with multiple joins
• How to combine relational operations to create complex queries– Defining complex queries with Microsoft Access
• How to define queries with set operators – Union, intersection, difference
• How to create user interfaces in Access – A video rental checkout form
Manipulating Information in Relational Databases
• A relational database table– Consists of a set of rows (also called records)– each row in a table has the same number and types of
attributes.
• When you send a query to the database, it– Finds the appropriate rows of information in the stored tables – Performs the requested operations on the data– Represents the results in a new temporary table– Delivers the results table to the user – Destroys the table when the user no longer needs it
Queries and Query Languages
• Queries can be represented in many ways– Relational algebra (explained in Chapter 8)– Query by example diagrams (as in Microsoft Access)– SQL, the standard query language (explained in Chapter 9)
• 4 basic types of queries– A projection operation produces a result table with
• Only some of the columns of its input table. – A selection operation produces a result table with
• All of the columns of the input table• Only those rows of its input table that satisfy some criteria.
– A join or product operation produces a result table by• Combining the columns of two input tables.
– A set operation produces a result table by• Combining rows from one or the other of its input tables
• This chapter focuses on manipulating relational tables with relational algebra and Microsoft Access
Projection Queries• A projection query selects some of the columns of the input table
– project Customer onto (firstName, lastName) • Relational algebra form
firstName,lastName(Customer)
• Notice that the result table has fewer rows– Duplicate rows have been removed
firstName lastName
Anita Morehouse
Jane Block
Carroll Breaux
Cherry Hamilton
Catherine Harrison
Jane Doe
Joseph Greaves
accountId lastName firstName street city state zipcode balance
101 Block Jane 345 Randolph Circle Apopka FL 30458- $0.00
102 Hamilton Cherry 3230 Dade St. Dade City FL 30555- $3.00
103 Harrison Katherine 103 Landis Hall Bratt FL 30457- $31.00
104 Breaux Carroll 76 Main St. Apopka FL 30458- $35.00
106 Morehouse Anita 9501 Lafayette St. Houma LA 44099- $0.00
111 Doe Jane 123 Main St. Apopka FL 30458- $0.00
201 Greaves Joseph 14325 N. Bankside St. Godfrey IL 43580- $0.00
444 Doe Jane Cawthon Dorm, room 642 Tallahassee FL 32306- $10.55
Projection in Access
Query wizarddialog
Source table Select button(right arrow)
Selected fields
Selection Queries• A selection query selects rows
that match a selection criteria– select from Customer where
lastName = ‘Doe.’ • Relational algebra form
lastName=‘Doe’(Customer)
accountId firstName lastName street city state zipcode balance
111 Jane Doe 123 Main St. Apopka FL 34331 0.00
444 Jane Doe Cawthon Dorm, room 642
Tallahassee FL 32306 10.55
Selection query in Access
Show button unselected
Selection criterialastName = “Doe”
All fields selected
More complex criteria• The selection criterion can be any expression
– select from TimeCard where ssn = ‘376-77-0099’ and date < ‘01-mar-2002’
– Shaded rows are result rows• Relational algebra form
ssn=’376-77-0099’ and date<’01-mar-2002’(TimeCard)
ssn date startTime endTime storeId paid
145-09-0967 01/ 14/ 2002 8:15 12:00 3 yes
245-11-4554 01/ 14/ 2002 8:15 12:00 3 yes
376-77-0099 02/ 23/ 2002 14:00 22:00 5 yes
376-77-0099 03/ 21/ 2002 14:00 22:00 5 yes
145-09-0967 01/ 16/ 2002 8:15 12:00 3 yes
376-77-0099 01/ 03/ 2002 10:00 14:00 5 yes
376-77-0099 01/ 03/ 2002 15:00 19:00 5 yes
Complex criteria in Access
Field selection list
Selection criteriondate<01-mar-2002
Product Queries• A product query produces a
result table from 2 inputs– Combines the attributes from
two different tables – Produces a new table with
more attributes than either of the original ones
– is product of Employee and TimeCard
• Relational algebra form– Employee TimeCard
• Combines every row of one table with every row of other table
• We really want to combine an employee and a timecard for a single employee
Employee.ssn lastName firstName TimeCard.ssn date startTime endTime storeId paid
145-09-0967 Uno Jane 145-09-0967 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
245-11-4554 Toulouse Jie 145-09-0967 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
376-77-0099 Threat Ayisha 145-09-0967 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
479-98-0098 Fortune Julian 145-09-0967 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
579-98-8778 Fivozinsky Bruce 145-09-0967 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
145-09-0967 Uno Jane 145-09-0967 1/ 16/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
245-11-4554 Toulouse Jie 145-09-0967 1/ 16/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
376-77-0099 Threat Ayisha 145-09-0967 1/ 16/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
479-98-0098 Fortune Julian 145-09-0967 1/ 16/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
579-98-8778 Fivozinsky Bruce 145-09-0967 1/ 16/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
145-09-0967 Uno Jane 245-11-4554 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
245-11-4554 Toulouse Jie 245-11-4554 1/ 14/ 2002 8:15:00 AM 12:00:00 PM 3 Yes
Rows from second time card
Rows from first time card
Join queries• A join query is a product with a restriction on the result rows
– The join condition determines which rows match– Only matching rows are in the result table
• Typical join condition is equality of attributes– join Employee and TimeCard where Employee.ssn = TimeCard.ssn
• Relational algebra form– Employee ⋈Employee.ssn=TimeCard.ssn TimeCard
• Some rows of result shown in this table
Employee. ssn
last Name
first Name
TimeCard. ssn date start Time
storeId paid end Time
145-09-0967 Uno Jane 145-09-0967 01/ 14/ 2002 8:15 3 no 12:00
145-09-0967 Uno Jane 145-09-0967 01/ 16/ 2002 8:15 3 no 12:00
245-11-4554 Toulouse Jie 245-11-4554 01/ 14/ 2002 8:15 3 no 12:00
376-77-0099 Threat Ayisha 376-77-0099 02/ 23/ 2002 14:00 5 no 22:00
Join query in Access
Join line
Queries with Multiple Joins• This query has 2 joins
– Join Rental and Video– Join that table with Movie
• Relational algebra form accountId, videoId, dateRented, dateDue, title, cost ((Rental ⋈videoId Video) ⋈movieId Movie)
Combining Relational Operations
• Combine selection, projection, and join in one query– project videoId, title, and dateDue from Rental join Video on videoId join
Movie on movieId where accountId=113 and• Relational algebra form
videoId, title, dateDue(( accountId=113(Rental) ⋈videoId Video) ⋈movieId Movie)
Defining Complex Queries with Microsoft Access
Four source tables and three joins
Selecting by dateRented
Projecting 4 fields
Applying Set Operators to Tables
• Set operations include– Union, intersection, and difference
• Set operations can be applied to any tables with the same shape– The same order and type of attributes– Attribute names do not have to agree
• For example– Rental (accountId number, videoId number,
dateRented date, dateDue date, cost currency)– PreviousRental (accountId number, videoId number,
dateRented date, dateReturned date, cost currency)
• Agree in order and type, but not in name– Rental.dateRented vs. PreviousRental.dateReturned
Simple User Interfaces in Access
Customer navigation buttons
Videos rented by Jane Block
Customer information
A Video Rental Checkout Form
• See Access database for details of forms design