35
Nov 27 fit100-23-databases © 2006 University of Washington 1 The Information School of the University of Washington Database Intro INFO/CSE 100, Fall 2006 Fluency in Information Technology http://courses.washington.edu/info100/

1 The Information School of the University of Washington Nov 27fit100-23-databases © 2006 University of Washington Database Intro INFO/CSE 100, Fall 2006

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Nov 27 fit100-23-databases © 2006 University of Washington 1

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton Database Intro

INFO/CSE 100, Fall 2006

Fluency in Information Technology

http://courses.washington.edu/info100/

Nov 27 fit100-23-databases © 2006 University of Washington 2

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Readings and References

• Reading» Fluency with Information Technology

• Chapter 14, Introduction to Database Concepts

• References» Access Database: Design and Programming

• by Steve Roman, published by O'Reilly

Nov 27 fit100-23-databases © 2006 University of Washington 3

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Why Study Databases?

• Some of us want to compute, but all of us want information …

• Much of the archived information is in tables

• Databases enhance applications, e.g. Web

• Once you know how to create databases, you can use them to personal advantage

• Databases introduce interesting ideas

Nov 27 fit100-23-databases © 2006 University of Washington 4

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Relational Databases

• Information is stored in tables» Tables store information about entities» Entities have characteristics called attributes» Each row in a table represents a single entity

• Each row is a set of attribute values

• Every row must be unique, identified by a key

» Relationships -- associations among the data values are stored

Table structure = schema Table contents = instance

Nov 27 fit100-23-databases © 2006 University of Washington 5

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Schema for Example table: ID number unique number(Key) Last text person’s last name First text person’s first name JobCode number current position Hire date first day on job ...

A Table in a Database

Tables have names, attributes {fields}, entities {rows}

instance

schema

Nov 27 fit100-23-databases © 2006 University of Washington 6

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Two tables in a database

Nov 27 fit100-23-databases © 2006 University of Washington 7

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Redundancy in a database is Very Bad • Not every assembly of tables is a good database• Repeating data is a bad idea

» Replicated data can differ in its different locations, e.g. multiple addresses can differ• Inconsistent data is worse than no data• Cut down on the typos and mis-keyed entries

» Keep a single copy of any data• Reduces memory and data processing costs• if it is needed in multiple places, associate it with a key and

store key rather than the data

» Effort to update is high

Nov 27 fit100-23-databases © 2006 University of Washington 8

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Relationships between tables

Nov 27 fit100-23-databases © 2006 University of Washington 9

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Query to a database (set of tables) produces a new table

“You can look it up”• When looking for information, a single

item might be the answer, but a table is more likely» Which employees live in Kirkland?

• Table of employees

» Who is taking INFO/CSE 100?• Table of students

» Whose mile run time 4:00?• Table of runners

Nov 27 fit100-23-databases © 2006 University of Washington 10

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Select Operation

• Select creates a table from the rows of another table meeting a criterion

Select * from Perms Where Hire < 1993

Nov 27 fit100-23-databases © 2006 University of Washington 11

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

More Select Operation

• Select creates a table from the columns of another table

Select Last, First From Perms

This is a select from 9 dimensions to 2 dimensions

Nov 27 fit100-23-databases © 2006 University of Washington 12

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Union

• Union combines two tables with same attributesSelect * From Perms Union Select * From Temps

Join

• Combines rows if common field matchesSelect * From Perms inner join JobCodes on

Perms.JobID = JobCodes.JobID

Nov 27 fit100-23-databases © 2006 University of Washington 14

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

DB Operations

• These various DB Operations can create any table from a given set of tables

• All modern database systems are built on these relational operations

• The operations are not usually used directly, but are used indirectly from other languages

• Structured Query Language (SQL) is the language that we talk to the database in

Nov 27 fit100-23-databases © 2006 University of Washington 15

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Database Structure

• A database contains one or more tables» Tables include entities with attributes» There are relationships defined between the entities in

the various tables» Retrieve information from the tables using queries» Create GUI front ends (forms and reports) for users

• First, design the database or create the schema» What are the entities?» What are the attributes of each entity? Are they

atomic?» What are the relationships between tables?

Nov 27 fit100-23-databases © 2006 University of Washington 16

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Build a Library Database Schema

• What are the entities?

• What are the attributes of each entity? Are they atomic?

• What are the relationships between tables?

Books

Publishers

Authors

ISBN Title

Price

ID Phone

Name

ID Phone

Name

PublisherOf

1

WrittenBy∞ ∞

entity-relationship diagram for Library database

Nov 27 fit100-23-databases © 2006 University of Washington 18

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Create a new database

Nov 27 fit100-23-databases © 2006 University of Washington 19

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Create a new table in the database

Nov 27 fit100-23-databases © 2006 University of Washington 20

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Creating a table in Design view

Nov 27 fit100-23-databases © 2006 University of Washington 21

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Entering Table Data

Nov 27 fit100-23-databases © 2006 University of Washington 22

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Build another table

Nov 27 fit100-23-databases © 2006 University of Washington 23

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Add publisher ID to books

Nov 27 fit100-23-databases © 2006 University of Washington 24

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Create the link between the tables

Books

Publishers

Authors

ISBN Title

Price

ID Phone

Name

ID Phone

Name

PublisherOf

1

WrittenBy∞ ∞

Hey presto, we have a database!

Nov 27 fit100-23-databases © 2006 University of Washington 26

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Two tables with a relationship

Nov 27 fit100-23-databases © 2006 University of Washington 27

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Create a query

Nov 27 fit100-23-databases © 2006 University of Washington 28

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

The query produces a new (virtual) table

Nov 27 fit100-23-databases © 2006 University of Washington 29

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Select particular columns

Nov 27 fit100-23-databases © 2006 University of Washington 30

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Select particular rows

Nov 27 fit100-23-databases © 2006 University of Washington 31

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

SQL behind the scenes

Nov 27 fit100-23-databases © 2006 University of Washington 32

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Summary

• Databases : built on tables» Tables represent entities» Tables/Entities have attributes (fields)» Tables have a primary key (unique to that table)

• Related tables are “linked” using primary keys• Structured Query Language (SQL) used to ask

questions of database» SQL typically “called” from other programming

languages» Can limit rows, columns, and join tables

Nov 27 fit100-23-databases © 2006 University of Washington 33

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Questions

• A(n) ______ is a unique identifier for any row in a database table.

• The ______ operation takes rows from a table to create a new table.

Nov 27 fit100-23-databases © 2006 University of Washington 34

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Questions

• For the following, either indicate that the field is atomic or divide the field to make the result atomic.

Field ContentsStudentNo 47385633Phone (206) 555-1212Name Maria MurrayClass INFO100 : ABCity Seattle, WA 98115DOB September 26, 1983

Nov 27 fit100-23-databases © 2006 University of Washington 35

Th

e I

nfo

rmati

on

Sch

ool

of

the U

niv

ers

ity o

f W

ash

ing

ton

Questions

• Suppose I wanted a database to run a wine review web site.

» What would some entities be?

» What would some attributes be of those entities?