Querying Database - San Francisco State University
Download
Report
Transcript Querying Database - San Francisco State University
Introduction to Access
BUS 782
Creating a New Database
• MS Office button/New
– Blank database
– New database name and location
Access Objects
• Tables
– Create a new table: Create/Table Design
– Open a table:
• Double click the table name
– Home/View:
• Datasheet view
• Design view
• Queries
– Create a new query: Create/Query Design
• Others: Forms, Reports, Pages
Creating a Table
• Create/Table Design:
– Field name
– Field data type
• Create table by using table templates
– Create/Table Templates/select template
• Create a primary key
– Never let Access to create a primary key for
you.
– How to create a composite key?
• Enter data
Create a new Query
• Create/Query design/Show table
– Select fields
– Add criteria
• Sorting
• Total and subtotal
– Query design tools/Total
– A Total row is added to the design view
– Select function from the total row’s dropdown
list
Creating a New Form
• Using Form Wizard
– Create/More Forms/Form Wizard
Querying Database
Basic Query Language Operations
•
•
•
•
Selection
Projection
Join
Aggregates: Max, Min, Sum, Avg, Count
– Totals and SubTotals
– GroupBy
• Calculated fields
Selection
• Selection operation retrieves records that
satisfy user’s criteria.
Projection
• Projection operation defines a vertical
subset of a table and retrieves only the
specified fields.
Join
• The two tables must have common
attributes:
– Key and foreign key.
• Combines two tables to form a new table
where records of the two tables are
combined if the common attributes have
the same value.
Join Example
Faculty File:
FID
F1
F2
F5
Student File:
SID
S1
S2
S3
Fname
Chao
Smith
Boxer
Sname
Peter
Paul
Smith
Faculty Join Student =
FID
F1
F2
F1
Join Example
Student File:
SID
S1
S2
S3
Sname
Peter
Paul
Smith
StudentCourse File:
SID
CID
S1
ISYS263
FID
F1
F2
F1
Course File:
CID
Cname
ISYS263 IS Intro 3
ISYS363 MIS Intro
Acct101 accounting
Fin350 Finance Intro
S1
S3
S2
S2
S2
Units
3
3
3
(Student Join StudentCourse) Join Course
Acct101
ISYS363
ISYS263
Fin350
Acct101
Aggregate Functions
• Max, Min, Sum, Count, Avg
• QueryTools/Totals
• Ex. Student: SID,Sname, GPA, Sex, Major
– How many students in this University?
– What is the overall average GPA?
Aggregates by Group
– How many students in each major?
– Compare male students and female students
average GPA.
Examples
• Customer: CID, Cname, City, Rating
• Orders: OID, Odate, SalesPerson, CID
• Queries:
– Find customers live in San Francisco.
– Produce a customer report that shows CID, Cname,
and Rating.
– Number of customers in each city
• City, NumbeOfCustomers
– Produce a report that shows the number of orders for
each customer:
• CID, Cname, TotalNumberOfOrders
University Database
ERD Notations
Sname
SID
Major
SID
1
Student
M
M
Advise
Has
1
Balance
Account
M
Enroll
Grade
1
Faculty
FID
Fname
M
Phone
Teach
M
Course
CID
Units
Cname
Examples
• Q1: Display students’ ID, name and account
balance who owe university more than
$2000.
• Q2: Display student’s ID, name and total
units.
• Q3: Find students taking at least 9 units and
display their ID, Name and total units.
• Q4: Display CID, Cname, SID, Sname
• Q5: Display CID, Cname, number of
students in each course.
• Q6: Display faculty’s name and phone if the
faculty advises at least three students.
Order Processing Database
Examples
• Order Processing Database:
– Customer, Orders, OrderDetail, Product
– MIS report
– Total amount for each order
– Criteria applied to subtotal
Calculated Fields
• Rename a field:
– NewName:OldName
• Define a calculated field:
– Tax:salary*.15
– Age:Year(Now()) – Year(DOB)
– IIF function
Criteria
• >, >=, <, <=, =, <>
• Range: BETWEEN 1/1/03 AND 12/31/03
• Wildcard:
– ? – match any one character
• “K?NG”
– * - Match any number of characters
• “C*”
Sorting
• One field sorting
• Two fields sorting
Other Queries
• Update query:
– Query tools/Design/Query type/Update
• Delete query
• Parameter query
• CrossTab query
– Crosstab row
Access Tools for Import/Export
• Import from Excel:
– The first row of Excel’s list should contain field
names
– External Data/Import/Excel
• Export to Excel:
– External Data/Export/Excel
Open Database Connectivity
(ODBC)
• Provide a standard to retrieve data from a
database.
• It manages one or more "database drivers“
that enables the communication between
database and applications.
• To access a database, we use ODBC
facilities to define a ODBC data source
name for the database.