Databases - cs.auckland.ac.nz

38
7/15/2005 COMPSCI 111 S2C - Lecture 7/8 1 Databases ETDD Chapter 7 (pp 122- 135)

Transcript of Databases - cs.auckland.ac.nz

Page 1: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 1

Databases

ETDD Chapter 7 (pp 122- 135)

Page 2: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 2

DatabasesDatabases

Database– A (large) collection of data about a particular topic

- Maintains information- Makes the information available on demand

Examples of databases:– Library book collection– Dr. Office patient files– Car dealer customer records– Video Store movie collections– Supermarket daily transactions– An address book– Student marks– Wine cellar inventory

Page 3: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 3

DatabasesDatabases

Databases:– Entities (e.g., students, classes)– Relationships (e.g., Ann is enrolled in CS111)

A Database Management System (DBMS) – software package designed to store and manage databases

Page 4: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 4

Relational DatabasesRelational Databases

Relational Model

• Introduced in the late 1970’s

• Provides a conceptual view of the database

• User removed from the “storage” of information

• Information seen as Tables with rows and columns

• Most widely used model today

Page 5: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 5

Relational DatabaseRelational Database

Relation• A table with rows(records) and columns(fields)• Every relation has a schema, which describes the columns, or fields.

Table field

record

Page 6: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 6

RelationsRelations

Primary key• Each table must have a primary key. This is a (non null) field or

combination of fields that makes each record unique. You cannot have two records in a table with the same primary key.

Foreign Key• This is a field in a table that is related to the primary key in

another table. We can use the foreign key and primary key to join information in the two tables where there are common values. This field can hold null values.

Referential Integrity• All values in the foreign key field of a table must also be

contained in the table to which it is related or be null.

Page 7: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 7

ExampleExample

In this example the first table (inventory) has a primary key called ID.The second table (movies) has a primary key called Movie.

The inventory table also has a foreign key (name). This is related to theprimary key in the movies table. Every value in this field in the inventory table must also have an entry in the table movies.

Page 8: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 8

Looking at the DataLooking at the Data

If we look at the data in this database:Movie Category Director Length in Minutes

Blade II ACTION Guillermo del Toro 116

Lord of the Rings FANTASY Peter Jackson 178

Minority Report ACTION Steven Spielberg 145

Monsters, Inc. COMEDY Peter Docter 92

The Graduate COMEDY Mike Nichols 105

ID Name Rental_Price Rental Period Num_Copies

1 Lord of the Rings $3.00 1 5

2 Lord of the Rings $7.00 3 1

3 Monsters, Inc. $5.00 2 2

4 Blade II $6.00 2 3

5 Minority Report $4.00 1 2

6 Minority Report $6.00 3 1

We can see that there is onlyone row for each movie title in this table. Movie is our primarykey. But there can be many rows for the movies in the second table.

This is because the movie name does not make a record unique inthis table. This table has a primarykey that is a unique ID.

Page 9: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 9

RelationshipsRelationships

In this example we can have more than one record in the inventorytable with the same value in the name field (I.e. “Star Wars” can occurseveral times).

But we can only have one record with this value in the movies table.This is because Movie is a primary key.

Therefore we have a many-to-one relationship between the inventoryand movies tables.

Page 10: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 10

Using Microsoft AccessUsing Microsoft Access

• MS Access is a database management program.

• It allows us to create relational databases

• It gives us a powerful query tool to access the data

• It has a report writer to create reports from this data

• It has a form tool to allow us a graphical data entry environment (we will not use the form tool in this course)

• Like Excel and Word, it has a Macro language that allows us to automate our work.

Page 11: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 11

Using Microsoft AccessUsing Microsoft Access

Choose Tables, Queries,Reports

Select which view

Page 12: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 12

Creating a TableCreating a Table

We must define a field name and field type for each field in our tableThe description is optional, but useful documentation

Page 13: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 13

Creating a Primary KeyCreating a Primary Key

You can select one or more fields to use as a primary key. Access will enforce the integrity of this field. It will not allow a nullvalue and will not allow duplicates.

Page 14: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 14

Adding RelationshipsAdding Relationships

Once we have created the structure for tables in our database wecan add relationships from the Tools menu. This allows us to define our foreign keys – primary key relationships.

Parts – primary key – pidCatalog – primary key – sid+pid

foreign keys – pid (parts)sid(supliers)

Suppliers – primary key - sid

Page 15: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 15

Adding DataAdding Data

We must add data to our tables in a certain order if we have created primary key/foreign key relationships. The data found in the primary key must be entered before we can add the data in the foreign key. This is because we want to enforce referential integrity.

In the example just shown we would need to populate the Parts and Suppliers tables before we populate the Catalog table.

When we add data to the Catalog table Access checks for referential integrity and rejects a record that has an sid or pid not already contained in the other tables.

Page 16: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 16

Adding DataAdding Data

Example: If our supplier table has the following information, we cannot add a record to the Catalog table with an sid of 5

sid sname address

1 Acme Widget Suppliers 1 Grub St., Potemkin Village, IL 61801

2 Big Red Tool and Die 4 My Way, Bermuda Shorts, OR 90305

3 Perfunctory Parts 99999 Short Pier, Terra Del Fuego, TX 41299

4 Alien Aircaft Inc. 2 Groom Lake, Rachel, NV 51902

Page 17: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 17

Querying the DatabaseQuerying the Database

The ability to perform queries on our database is what makes it so useful.

Queries must be written in a computer language. Each language has its own syntax that must be learned.

• The most universal query language is SQL (structured query language). This is a standardized language used by most DBMS’s.

• A popular, but software application specific way to query adatabase is QBE (query by example)

• Natural language query: becoming popular especially forweb based applications -- still young technology

Page 18: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 18

Querying the DatabaseQuerying the Database

Query By Example (QBE):• Access uses a method called Query by Example or QBE to

query the database. The query design view is a graphical view of the database query. You are supplied with a grid and you select fields in columns that you want to use.

Page 19: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 19

Things to KnowThings to Know

• The difference between a database and a DBMS• What tables, fields and records are• What a Primary Key of a table is• What Foreign Keys and referential integrity are• One-to-many and many-to-one relations between tables

Page 20: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 20

A One-to-One RelationshipA One-to-One Relationship

Page 21: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 21

A One-to-One RelationshipA One-to-One Relationship

Page 22: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 22

A Many-to-One RelationshipA Many-to-One Relationship

Page 23: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 23

Types of Queries in AccessTypes of Queries in Access

Select Query• Does not change the data. Is a view of data already contained

in databaseUpdate Query

• Allows us to update tables. Modifies data permanently in database.

Make Table Query• Makes a new table and fills it with the selected data. Adds new

table permanently to databaseAppend Query

• Adds records to an already created table. Adds data permanently to database.

Delete Query• Removes data from the database

Page 24: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 24

Detail vs. AggregateDetail vs. Aggregate

The default for queries are in Access is to retrieve records at the row level (individual detail). If we want to group data (i.e. calculate sums, find averages or counts) we need to create an aggregate query.

We can do this by selecting the Sigma : ∑

In Access this is called a “Totals Query”

Page 25: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 25

Select Queries in Design ViewSelect Queries in Design View

First we need to select the tables that we want to add to the query.

Next we can choose the columns andadd criteria for filtering the data

Results

The exclamation point runs our query.

Page 26: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 26

Adding Calculated FieldsAdding Calculated Fields

We can use the expressionbuilder to create a memberdiscount field.

Results

Page 27: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 27

Aggregate Queries (Σ)Aggregate Queries (Σ)

You can add a label that will show up on the resulting queryLabel: field

Results:

Page 28: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 28

SQLSQL

SQL – Structured Query Language(pronounced “sequel”)Developed by IBM in the 1970sThis is the language that is widely used to interact with

databases. The QBE language in Access generates SQL for us.

Page 29: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 29

SQLSQL

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 the data and do some calculations.

General syntax

SELECT [comma separated field list] FROM tableName;

SQL is case insensitive. Case does matter however when comparing values in a textual field of the database

Page 30: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 30

ExamplesExamples

Examples:• select all

SELECT * FROM TableName; (star means select all )• Selecting three fields

SELECT field1,field2, field3 FROM TableName ;• sorting

SELECT field1, field2 FROM TableName ORDER BY field2;

• Constraining (notice that numeric fields do not need quotations, but text fields use a single quote)

SELECT field1, field2, field3 FROM TableNameWHERE field1>3;

SELECT field1, field2, field3 FROM TableName WHERE field2=‘Myra’;• Aggregate query

SELECT field1 FROM TableName GROUP BY field1;

Page 31: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 31

Using SQL in AccessUsing SQL in Access

Movie Category Director Length in Minutes

Blade II ACTION Guillermo del Toro 116

Lord of the Rings FANTASY Peter Jackson 178

Minority Report ACTION Steven Spielberg 145

Monsters, Inc. COMEDY Peter Docter 92

The Graduate COMEDY Mike Nichols 105

SELECT movie,category from movies where category=‘ACTION’;

movie category

Blade II ACTION

Minority Report ACTION

SELECT movie,category from movies where [Length in Minutes]<110;

movie category

Monsters, Inc. COMEDY

The Graduate COMEDY

This table is called movies

A field name with spaces must beinside of brackets.

Page 32: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 32

SQL in AccessSQL in Access

We can add the table name as a qualifier (i.e. movies.category) whenwe have multiple tables and there might be confusion

SELECT movies.movie,movies.category from moviesWHERE movies.category = 'ACTION';

Page 33: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 33

Creating Reports in AccessCreating Reports in Access

We can also create reports from data in our database.The report wizard is the fastest way to create a report.From there we can modify the results in design view.

Page 34: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 34

Grouping the DataGrouping the Data

Page 35: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 35

SortingSorting

Page 36: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 36

ReportReport

Page 37: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 37

Modifying the ReportModifying the Report

We can use Design Mode to modify the reports layout:

Page 38: Databases - cs.auckland.ac.nz

7/15/2005 COMPSCI 111 S2C - Lecture 7/8 38

Modified ReportModified Report