Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | ramona-hopkins |
View: | 30 times |
Download: | 3 times |
Introduction to Databases Queries
CS 146
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
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
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
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
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
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
Key FieldsKey Fields Primary key: uniquely identifies a record
InstructorID InstructorLastName
InstructorFirstName
1 Black Greg
2 McIntyre Karen
3 Sarin Naj
UniversityInstructor
Primary keys
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
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
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
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
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
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)
Structure of a DatabaseStructure of a Database
DBData
DBMS
Client WorkstationsDatabase Server
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.
Query BrowserQuery Browser Example: Oracle SQL developer
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
MySQL Query Browser MySQL Query Browser Type query: Click Execute:
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
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
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
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;
Search ConditionsSearch Conditions General format:
FieldName Operator TargetValue
Operators: =, <, >, <=, >=, <> or != Examples:
PROD_ID = 1 POUNDS > 5 STATUS != 'PAID'
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’
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
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'
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
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
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'
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'
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'