+ All Categories
Home > Documents > Introduction to Databases Queries

Introduction to Databases Queries

Date post: 01-Jan-2016
Category:
Upload: ramona-hopkins
View: 30 times
Download: 3 times
Share this document with a friend
Description:
Introduction to Databases Queries. CS 146. Sample Database:. CANDY_CUSTOMER. CANDY_PURCHASE. CANDY_CUST_TYPE. CANDY_PRODUCT. Field: column of similar data values Record: row of related fields Table: set of related rows. Basic Database Vocabulary. Record. Field. - PowerPoint PPT Presentation
Popular Tags:
32
Introduction to Databases Queries CS 146
Transcript
Page 1: Introduction to Databases Queries

Introduction to Databases Queries

CS 146

Page 2: Introduction to Databases Queries

Sample Database:CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD

1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633

10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399

PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID

PROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

CUST_TYPE_IDCUST_TYPE_DESC

P Private

R Retail

W Wholesale

CANDY_CUSTOMER

CANDY_PURCHASECANDY_CUST_TYPE

CANDY_PRODUCT

Page 3: Introduction to Databases Queries

Basic Database VocabularyBasic Database Vocabulary

PROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

Field: column of similar data values

Record: row of related fields Table: set of related rows

Field Record

Page 4: Introduction to Databases Queries

Sidenote: Database HistorySidenote: Database History

All pre-1960’s systems used file-based data First database: Apollo project

Goal: to not store duplicate data in multiple locations Used a hierarchical structure Created relationships using pointers

Pointer: hardware address

Page 5: Introduction to Databases Queries

Example Hierarchical Database Example Hierarchical Database

StudentID

StudentLastName

StudentFirstName

StudentMI

Pointers* to Course Data

5000 Nelson Amber S

5001 Hernandez Joseph P

5002 Myers Stephen R

UniversityStudent

CourseID CourseName

CourseTitle

100 MIS 290 Intro. to Database Applications

101 MIS 304 Fundamentals of Business Programming

102 MIS 310 Systems Analysis & Design

UniversityCourse

*Pointer – physical location (as a number) to the start of the referenced data

Page 6: Introduction to Databases Queries

Problems with Hierarchical DatabasesProblems with Hierarchical Databases

Relationships are all one-way; to go the other way, you must create a new set of pointers

Pointers are hardware/hard drive-specific VERY hard to move to new hardware

Applications must be custom-written Usually in COBOL

Page 7: Introduction to Databases Queries

Relational DatabasesRelational Databases

Circa 1972 E.J. Codd “Normalizing” relations

Store data items only once With the exception that foreign keys can be

duplicated Stores data in a tabular format Creates relationships through sharing key

fields

Page 8: Introduction to Databases Queries

Key FieldsKey Fields Primary key: uniquely identifies a record

InstructorID InstructorLastName

InstructorFirstName

1 Black Greg

2 McIntyre Karen

3 Sarin Naj

UniversityInstructor

Primary keys

Page 9: Introduction to Databases Queries

What is the primary key of each table in the CANDY database?

How can you tell if a field is a primary key?

Class Discussion

Page 10: Introduction to Databases Queries

Special Types of Primary Keys Composite PK: made by combining 2 or more

fields to create a unique identifiero Consider the CANDY_PURCHASE table…

Surrogate PK: ID generated by the DBMS solely as a unique identifier

Page 11: Introduction to Databases Queries

Duplication ConsiderationsDuplication Considerations

When data values appear multiple times, there is duplication

Problems: Space Data becomes inconsistent over time

StudentID StudentLastName

StudentFirstName

StudentMI AdvisorLastName

AdvisorFirstName

5000 Nelson Amber S Black Anne

5001 Hernandez Joseph P Black Anne

5002 Myers Stephen R Sarin Naj

UniversityStudent

Page 12: Introduction to Databases Queries

Key Fields (continued)Key Fields (continued) Foreign key

Field that is a primary key in another table Serves to create a relationship

StudentID StudentLastName

StudentFirstName

StudentMI AdvisorID

5000 Nelson Amber S 1

5001 Hernandez Joseph P 1

5002 Myers Stephen R 3

UniversityStudent

Foreign keys

InstructorID InstructorLastName

InstructorFirstName

1 Black Greg

2 McIntyre Karen

3 Sarin Naj

UniversityInstructor

Primary keys

Page 13: Introduction to Databases Queries

What are the foreign keys in the CANDY database?

Does a table HAVE to have foreign keys?

When would you use them?

How can you tell if a field is a foreign key?

Class Discussion

Page 14: Introduction to Databases Queries

Every record has to have a non-NULL and unique PK value

Every FK value must be defined as a PK in its parent table

Rules for Relational Database Tables (non-negotiable)

Page 15: Introduction to Databases Queries

Structure of a DatabaseStructure of a Database

DBData

DBMS

Client WorkstationsDatabase Server

Page 16: Introduction to Databases Queries

Database StructureDatabase Structure A database consists of multiple user accounts Your area in the database is called your user schema

Identified by your username and password Each user schema contains database objects that you

create Tables Views Stored programs Etc.

Page 17: Introduction to Databases Queries

Query BrowserQuery Browser Example: Oracle SQL developer

Page 18: Introduction to Databases Queries

Query: command to perform an operation on a database object Create Insert Modify View Delete

Structured Query Language (SQL) Standard query language for

relational databases

Database QueriesDatabase Queries

Page 19: Introduction to Databases Queries

MySQL Query Browser MySQL Query Browser Type query: Click Execute:

Page 20: Introduction to Databases Queries

Query ConventionsQuery Conventions Not case-sensitive

Convention: reserved words in all-caps, user-supplied values (table names, field names, etc.) in lower-case letters

Queries can span multiple lines Semi-colon marks the end of a line

Page 21: Introduction to Databases Queries

Retrieving Data From a Single Table Syntax:

SELECT column1, column2, …FROM schema.tablenameWHERE search_condition

SELECT candycust_id, candycust_nameFROM candy_customerWHERE cust_id = 1

Page 22: Introduction to Databases Queries

Retrieving all Fields or Records

To retrieve all fields in the table: use the "*" wildcard character

To retrieve all records in a table: omit the search condition

SELECT *FROM tablenameWHERE search_condition

Page 23: Introduction to Databases Queries

How many fields and how many records will the following query retrieve?

A. 7 fields and 14 recordsB. 14 fields and 7 recordsC. 7 fields and 9 recordsD. None of the above

SELECT * FROM candy_purchase;

Page 24: Introduction to Databases Queries

Search ConditionsSearch Conditions General format:

FieldName Operator TargetValue

Operators: =, <, >, <=, >=, <> or != Examples:

PROD_ID = 1 POUNDS > 5 STATUS != 'PAID'

Page 25: Introduction to Databases Queries

Search Conditions (continued)Search Conditions (continued) Number: just type the number Text string:

Case-sensitive Enclose in single quotes

Date: Enter as a text string in ‘dd-mon-yy' format:

WHERE purch_date = ‘28-Oct-04’

Page 26: Introduction to Databases Queries

Which records will the following query retrieve?

A. Purch_id values 2, 3, 5, 7, 8B. Purch_id values 2, 7, 8, 9C. Purch_id values 2, 7, 8D. None of the above

SELECT *FROM candy_purchaseWHERE pounds >= 5

Page 27: Introduction to Databases Queries

Which records will the following query retrieve?

A. Purch_id values 1, 2, 3, 4, 6, 8B. Purch_id values 5, 7, 9C. All purch_id records will be returnedD. No purch_id records will be returnedE. An error will occur

SELECT *FROM candy_purchaseWHERE status = 'Paid'

Page 28: Introduction to Databases Queries

Searching for NULL ValuesSearching for NULL Values NULL: undefined

Search conditions for NULL and non-NULL values:

WHERE column_name IS NULLWHERE column_name IS NOT NULL

Page 29: Introduction to Databases Queries

Combining Multiple Search ConditionsCombining Multiple Search Conditions

AND: query only retrieves records for which both conditions are trueWHERE Condition1 AND Condition2

OR: query retrieves records for which either condition is trueWHERE Condition1 OR Condition2

Page 30: Introduction to Databases Queries

Using AND and OR in Search Conditions Every expression must be well-formed:

Do this:

Not this:

WHERE purch_date > ‘28-Oct-04'AND purch_date < ‘1-Nov-04’

WHERE purch_date > ’28-Oct-04'AND < ‘1-Nov-04'

Page 31: Introduction to Databases Queries

Which records will the following query retrieve?

A. Purch_id values 4, 6, 8B. Purch_id values 1, 2, 3, 4, 5, 9, 12, 13C. Purch_id values 1, 2, 3, 4, 5, 9, 10, 11, 12, 13D. None of the above

SELECT *FROM candy_purchaseWHERE delivery_date IS NULLAND status = 'PAID'

Page 32: Introduction to Databases Queries

Which records will the following query retrieve?

A. Purch_id values 1, 2, 3, 4, 6, 7, 8, 12B. Purch_id values 1, 2, 3, 4, 12C. Purch_id values 1, 2, 3, 4, 5, 9, 12, 13D. None of the above

SELECT *FROM candy_purchaseWHERE delivery_date = NOT NULLAND status = 'PAID'


Recommended