23
CSC 110 - Intro. to Computing Lecture 10: Databases

CSC 110 - Intro. to Computing Lecture 10: Databases

Embed Size (px)

Citation preview

Page 1: CSC 110 - Intro. to Computing Lecture 10: Databases

CSC 110 -Intro. to Computing

Lecture 10:

Databases

Page 2: CSC 110 - Intro. to Computing Lecture 10: Databases

Announcements

Enjoy the long weekend!

Page 3: CSC 110 - Intro. to Computing Lecture 10: Databases

Bob Jones 7 Elvis Gates 4Sam Smith 12 Steve Young 12Tom Jones 10 Bob Bledsoe 7Jim Mower 8 Michael Jones 4Bob DeNiro 7 Jack Woods 10Greg Martin 6 Josh Jones 9Mike Marino 2 Bob Martin 7Jim Woods 6 Fran McLean 12Bob Denver 7 Clive Deeker 4Xavier Jobs 11 Matthew Hertz 8Sam Els 3 Zoe Elton 9Ernie Smith 9 Kate Bolter 10

Page 4: CSC 110 - Intro. to Computing Lecture 10: Databases

Bob Jones 7 Elvis Gates 4Sam Smith 12 Steve Young 12Tom Jones 10 Bob Bledsoe 7Jim Mower 8 Michael Jones 4Bob DeNiro 7 Jack Woods 10Greg Martin 6 Josh Jones 9Mike Marino 2 Bob Martin 7Jim Woods 6 Fran McLean 12Bob Denver 7 Clive Deeker 4Xavier Jobs 11 Matthew Hertz 8Sam Els 3 Zoe Elton 9Ernie Smith 9 Kate Bolter 10

Page 5: CSC 110 - Intro. to Computing Lecture 10: Databases

Problems With Humans

Humans cannot process large amounts of dataGet overwhelmed, lost, and miss patterns

(Other) people may also make mistakes

Patterns and trends are important, however Suggest methods of improving business Identify new possibilities that can be exploited Hints at unknown relationships

Page 6: CSC 110 - Intro. to Computing Lecture 10: Databases

One Solution: Databases

Databases are programs that store and manage information automaticallyTypically used when data is frequently added,

modified, or searched forExamples of databases:

Prices & orders from amazon.com Student records in a college Phone calls made/received from a phone number Card catalogue in a library

Page 7: CSC 110 - Intro. to Computing Lecture 10: Databases

Why Databases Are Useful

Computers process 1 data item as easily as they process 10 or 10 billion itemsFilter data to return only certain elements

E.g., Only consider results for people named “Bob”

Evaluate results on a minute-by-minute basis E.g. Have we made our sales goals yet?

Combine data that are somehow related Let the user look for missed relationships

Page 8: CSC 110 - Intro. to Computing Lecture 10: Databases

Relational Databases

Most common databases today Uses relationships to organize data Starts with a table

Each table contains a collection of records Every record uses identical fields, but may have different

values Fields must include at least one unique identifier

Database usually contain multiple tables And define how to combine data between tables

Page 9: CSC 110 - Intro. to Computing Lecture 10: Databases

Commonly Used Database

Page 10: CSC 110 - Intro. to Computing Lecture 10: Databases

Database Organization

Databases contain several different piecesPhysical database – File(s) holding the tablesDatabase engine – Software enabling programs

and users to read, write, and modify database contents

Database schema – Files specifying what type of data each field in a table stores; also specifies what fields enable tables to relate to one another

Page 11: CSC 110 - Intro. to Computing Lecture 10: Databases

Physical Database

Specifies how data are storedHas little/no relation to anything else about the

databaseUsually uses many files to store all the data

Little relationship between files and the data

Physical database is only meant to be accessed by database engineMay not make sense to actual people

Page 12: CSC 110 - Intro. to Computing Lecture 10: Databases

Database Schema

Specifies what the type of data each field stores

Used to interpret the file(s) storing each tableExample schema for Movie table:

Movie (MovieId: key; Title, Genre: text; Rating: Enum)

Page 13: CSC 110 - Intro. to Computing Lecture 10: Databases

Database Engine

Program which must be used to access database Interprets database schema to get/store

information in physical databaseEnsures requests match what is expected by

databaseVerifies program/user requesting or modifying

data is allowed to do so

Page 14: CSC 110 - Intro. to Computing Lecture 10: Databases

SQL

Stands for Structured Query LanguageUsually pronounced “sequel”

Language used by database engines Includes commands retrieve, add, remove,

and modify records Expects/requires commands to be given in

a very specific format

Page 15: CSC 110 - Intro. to Computing Lecture 10: Databases

SQL Query Format

select Name from StudentReturns Name in each record in Student table

select Name from Student where StudentId =2009348Return the Name of any Student with a StudentId of

2009348 select Name from Student order by NameReturns sorted list of Names from Student

Page 16: CSC 110 - Intro. to Computing Lecture 10: Databases

SQL Query Format

select * from StudentReturn all records from Movie table

select * from Student where name like ‘John’ order by StudentIdReturn all records from the Student table,

sorted by their id numbers, whose name includes a word like “John”

Page 17: CSC 110 - Intro. to Computing Lecture 10: Databases

Database Design

Designing good databases is a tricky, complex processNo single solution works for all dataOften the “best” design is a matter of

interpretation But, bad designs limits data’s usefulness

May limit what data can be storedCannot access information across tables

Page 18: CSC 110 - Intro. to Computing Lecture 10: Databases

ER Modeling

ER stands for Entity-Relationship Common way of designing databases

Ignores “real-world” issues, implementationStarts at important data to storeEvaluates relationships between the data

Builds tables and fields from there

One popular tool for ER modeling is ER diagram

Page 19: CSC 110 - Intro. to Computing Lecture 10: Databases

ER Diagram

Start with boxes Each box captures single concept we wish

to consider/captureBox labels are usually singular nounShould describe important actor in

relationship being modeled

Problem Use Homework

Page 20: CSC 110 - Intro. to Computing Lecture 10: Databases

ER Diagram

Add ovals around each of the boxesRepresent important attributes of each box

Labels should be simple, declarative noun

Include all details we might want to captureEach oval should define single piece of data

Problem Use Homework

ProblemId

Answer

Question Number

PctCorrect

HomeworkId

DateDue Avg Score

DateGiven

Page 21: CSC 110 - Intro. to Computing Lecture 10: Databases

ER Diagram

Show relationships between boxesDraw line whenever we may wish to connect

dataTurn boxes that in middle of relationship into

diamonds

Problem Homework

ProblemId

Answer

Question Number

PctCorrect

HomeworkId

DateDue Avg Score

DateGiven

Use

Page 22: CSC 110 - Intro. to Computing Lecture 10: Databases

ER Diagram

Now build databaseBoxes and diamonds become tablesOvals become fields of the tableLinks between tables suggest tables should

share at least 1 field in common

Problem Homework

ProblemId

Answer

Question Number

PctCorrect

HomeworkId

DateDue Avg Score

DateGiven

Use

Page 23: CSC 110 - Intro. to Computing Lecture 10: Databases

For Next Lecture

Finish reading Chapter 12 Be prepared to discuss:

Spreadsheets