Вы находитесь на странице: 1из 7

Outline of Lecture

Review of Database Organization


Storing Content in Databases

COMPSCI 111/111G

Retrieving Content with Queries

An Introduction to Practical Computing

Structured Query Language


Graphical Queries in Access

Creating Reports in Access


Review of Database Concepts

Databases Queries and Reports

30/04/2014

Aspects of Databases

COMPSCI 111/111G - Databases 02

Microsoft Access

Before we can use any database, physical or digital, we must


first:

One very widely used database management system is


Microsoft Access.

Determine how to organize the database

Drawing on this software, the user can:

Enter content into the database

Create database tables and relations (already covered)

Retrieve that content selectively when needed

Store content in the resulting database

Present the retrieved content in a useful form

Query the database for specific information

We will examine each of these subtasks during our two lectures


on this topic.

Generate reports from these data

Access adopts a relational database model that consists of


multiple tables with primary and foreign keys.

30/04/2014

COMPSCI 111/111G - Databases 02

30/04/2014

COMPSCI 111/111G - Databases 02

Review Exercises

Review Exercises

Exercise 1: What is the primary key of the tblAlbum table?

Exercise 3: Given the relationship diagram, can an artist have


more than one album? Explain your reasoning.

Exercise 2: What foreign keys does tblAlbum table have (if any)?

Exercise 4: Given the relationship diagram, can an album have


more than one artist? Explain your reasoning.

30/04/2014

COMPSCI 111/111G - Databases 02

30/04/2014

Storing Content in Access

COMPSCI 111/111G - Databases 02

Retrieving Content in Access

Once you have created a set of related tables in Access, you can
populate your database with content.

Once you have stored content in your Access database, you can
use queries to retrieve it selectively.

Here is one simple way to achieve this:

The software offers a number of mechanisms:


Using the Access graphical user interface (Query Wizard/Query
Design View). This is known as query by design (QBE).

Enter the Datasheet View


Double click on one of the tables

Using the textual Structured Query Language (SQL).

Enter one record at a time by specifying field values

These lend themselves to queries of different complexity, but


all let you retrieve content selectively.

When done, close the current table


Repeat this process with the other tables

Note: The term query sometimes refers to methods that add,


remove, or edit content, but its original sense was limited to
retrieval.

Remember that you must enter content to which foreign keys


refer before the content that points to them.

30/04/2014

COMPSCI 111/111G - Databases 02

30/04/2014

COMPSCI 111/111G - Databases 02

Queries In Design View

Queries in Design View


3. Choose fields
and add criteria
for filtering results

1. Select Query Design


from the Create Menu

2. Select tables to

use in query
5. Results in table form

4. Click exclamation
point to run query

30/04/2014

COMPSCI 111/111G - Databases 02

30/04/2014

More Exercises

COMPSCI 111/111G - Databases 02

10

Calculated Fields in Access

Exercise 5: Complete this Query by Example form so that it will return


the first name, surname, and grade (in that order) of all students who
have received an A+.

Access users can also define fields as functions of others.

We can use the Access


Expression Builder to
create derived fields
that are calculated when
queries are made.
Results

30/04/2014

COMPSCI 111/111G - Databases 02

11

30/04/2014

COMPSCI 111/111G - Databases 02

12

Aggregate Queries in Access

Structured Query Language


The Structured Query Language (SQL):
Pronounced as sequel.
Developed by IBM in the 1970s.

Detail
Default in Access
Gives individual, detailed results

A textual or command-line language for database


queries.
Is the de facto standard language for databases.

Aggregate
Group data
E.g. sums, averages, counts
Done by selecting the Sigma:
Access calls this a Totals Query

30/04/2014

COMPSCI 111/111G - Databases 02

13

30/04/2014

COMPSCI 111/111G - Databases 02

SELECT Statement
SELECT statement:
Selects rows from a table.
We can specify which table and which fields we want to
select.
We can also group or sort data and do some calculations.

14

Examples
Select all records from a given table:
SELECT * FROM <TableName>;

SELECT *
FROM Students;

Selecting records using multiple fields from a table


SELECT <Field1>, , <FieldN> FROM <TableName>;
Use square brackets when
names contain punctuation
marks or spaces

General syntax:

SELECT ID, [Last Name], Address


FROM Students;

SELECT [comma separated field list] FROM tableName;

Selecting records from a table and sorting them by a field


Case sensitivity
SQL is case insensitive.
It only matters when comparing values in a textual field of
a database.

SELECT <Field1> FROM TableName ORDER BY <Field2>;


SELECT Name
FROM Students
ORDER BY [Date Of Birth];

30/04/2014

COMPSCI 111/111G - Databases 02

16

More on SQL Syntax

WHERE Clause

We can constrain SQL queries using WHERE conditions:

We can also constrain SQL queries in various other ways:


Comparisons (=, >, <, <=, >=, <>)
E.g., WHERE [City] <> Auckland' ;
BETWEEN AND
E.g., WHERE Price BETWEEN 10 AND 20;
LIKE (some pattern)
E.g., WHERE [City] LIKE San *';
IN (some list)
E.g., WHERE [City] IN (Auckland', Wellington');
AND, NOT, OR (combined with any of above)
E.g., WHERE [country] = New Zealand' AND City = Auckland';
IS NULL, IS NOT NULL
E.g., WHERE [ZIP/Postal Code] IS NOT NULL;

SELECT <Field1>, , <FieldN> FROM <TableName>


WHERE <FieldK> < <number> ;
SELECT <Field1>, , <FieldN> FROM <TableName>
WHERE <FieldK> = <string> ;

Text values need a single quote; numeric values do not.

These return only those records that satisfy the conditions.


SELECT [First Name]
FROM [Student]
WHERE [City] = Auckland'
ORDER BY [Date of Birth];

30/04/2014

SELECT [First Name]


FROM [Student]
WHERE ID < 10
ORDER BY [Date of Birth];

COMPSCI 111/111G - Databases 02

17

30/04/2014

SQL in Access

COMPSCI 111/111G - Databases 02

18

Exercises

To use SQL in Access, click SQL View in the view menu.

Exercise 6: Write an SQL command that will return the first


name, surname, and grade (in that order) of all students
who have received an A+.
To avoid ambiguity, you should use table names as qualifiers
when multiple tables are present.
E.g., SELECT Students.[First Name], Students.City
FROM Students WHERE (Students.City="Auckland");
30/04/2014

COMPSCI 111/111G - Databases 02

19

30/04/2014

COMPSCI 111/111G - Databases 02

20

Exercises

Reporting Results in Access


Once Access has generated a response to some query, you can
present the results in a report.
The database system provides two reporting options:
Report Tool (show entire table, some formatting control)
Report Wizard (table/field selection, grouping, sorting)

Exercise 7: Write an SQL command that will return the


surname and ID number of students ordered according to
their total marks.

Because these operate on tables, you can also use them to


generate reports from the database itself.

Report Wizard
Report Tool
30/04/2014

COMPSCI 111/111G - Databases 02

21

30/04/2014

Using the Report Wizard

COMPSCI 111/111G - Databases 02

22

Using the Report Wizard

The first step in using Access Report Wizard is to select the


tables and fields you want to present.

Once you have selected what content to present, you can specify
ways to group the records.

This can include summary information for each group level.


30/04/2014

COMPSCI 111/111G - Databases 02

23

30/04/2014

COMPSCI 111/111G - Databases 02

24

Using the Report Wizard

Using the Report Wizard

You can also indicate how to sort the records independently of


the fields used to group them.

Here is sample output that the Report Wizard generates.

Results grouped
using the Country
field.

Sorted in descending order


based on the number of
amphibians.

Once this is done, you are ready to generate your report.


30/04/2014

COMPSCI 111/111G - Databases 02

25

30/04/2014

Review of Key Ideas

COMPSCI 111/111G - Databases 02

26

Review of Key Ideas

A database is a collection of information, either physical or


digital, about some topic.

Many digital collections of information are organized into


relational databases that:
Include a set of tables with fields and records

A database management system is software that lets one:


Create an organization for a database

Use primary keys to ensure records are unique

Enter content into that database

Use foreign keys to specify relations between tables


Ensure referential integrity among the tables

Retrieve that content selectively when needed

These organizing assumptions influence strongly the:

Present the retrieved content in a useful form

Access is a widely used database system that supports these four


capabilities.

Manner and order in which one enters content


Statement of queries (e.g., SQL) for retrieving content
Reporting of results from those queries

Database systems like Access rely heavily on these ideas.


30/04/2014

COMPSCI 111/111G - Databases 02

27

30/04/2014

COMPSCI 111/111G - Databases 02

28

Вам также может понравиться