Download presentation
Presentation is loading. Please wait.
Published byPatience Oliver Modified over 8 years ago
1
Introduction to Databases Queries CS 146
2
Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT
3
Basic Database Vocabulary Field: column of similar data values Record: row of related fields Table: set of related rows Field Record
4
Sidenote: 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
5
Example Hierarchical Database Student ID Student LastName Student FirstName Student MI Pointers * to Course Data 5000NelsonAmberS 5001HernandezJosephP 5002MyersStephenR UniversityStudent CourseIDCourse Name Course Title 100MIS 290Intro. to Database Applications 101MIS 304Fundamentals of Business Programming 102MIS 310Systems Analysis & Design UniversityCourse *Pointer – physical location (as a number) to the start of the referenced data
6
Problems 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
7
Relational 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
8
Key Fields Primary key: uniquely identifies a record InstructorIDInstructor LastName Instructor FirstName 1BlackGreg 2McIntyreKaren 3SarinNaj UniversityInstructor Primary keys
9
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
10
Special Types of Primary Keys Composite PK: made by combining 2 or more fields to create a unique identifier o Consider the CANDY_PURCHASE table… Surrogate PK: ID generated by the DBMS solely as a unique identifier
11
Key Fields (continued) Foreign key Field that is a primary key in another table Serves to create a relationship StudentIDStudent LastName Student FirstName StudentMIAdvisorID 5000NelsonAmberS1 5001HernandezJosephP1 5002MyersStephenR3 UniversityStudent Foreign keys InstructorIDInstructor LastName Instructor FirstName 1BlackGreg 2McIntyreKaren 3SarinNaj UniversityInstructor Primary keys
12
What are the foreign keys in the CANDY database? Does a table HAVE to have foreign keys? How can you tell if a field is a foreign key? Class Discussion
13
Alternative to Foreign Keys: Repeat data values for every record Problems: Space Data becomes inconsistent over time StudentIDStudent LastName Student FirstName StudentMIAdvisorLast Name AdvisorFirst Name 5000NelsonAmberSBlackGreg 5001HernandezJosephPBlackGreg 5002MyersStephenRSarinNaj UniversityStudent
14
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)
15
Structure of a Database DB Data DBMS Client Workstations Database Server
16
Database 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.
17
Query Browser Example: MySQL SQL query utility Logging on: Server Host: stef.cs.uwec.edu Username: CS320_Student Password: C4361 Database: CS320_Student
18
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 Queries
19
MySQL Query Browser Type query:Click Execute:
20
Query 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
21
Retrieving Data From a Single Table Syntax: SELECT column1, column2, … FROM schema.tablename WHERE search_condition SELECT candycust_id, candycust_name FROM candy_customer WHERE cust_id = 1
22
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 tablename WHERE search_condition
23
How many fields and how many records will the following query retrieve? A. 7 fields and 14 records B. 14 fields and 7 records C. 7 fields and 9 records D. None of the above SELECT * FROM candy_purchase;
24
Search Conditions General format: FieldName Operator TargetValue Operators: =,, =, <> or != Examples: PROD_ID = 1 POUNDS > 5 STATUS != 'PAID'
25
Search Conditions (continued) Number: just type the number Text string: Case-sensitive Enclose in single quotes Date: Enter as a text string in 'yyyy-mm-dd' format: WHERE purch_date = '2004-10-24'
26
Which records will the following query retrieve? A. Purch_id values 2, 3, 5, 7, 8 B. Purch_id values 2, 7, 8, 9 C. Purch_id values 2, 7, 8 D. None of the above SELECT * FROM candy_purchase WHERE pounds >= 5
27
Which records will the following query retrieve? A. Purch_id values 1, 2, 3, 4, 6, 8 B. Purch_id values 5, 7, 9 C. All purch_id records will be returned D. No purch_id records will be returned E. An error will occur SELECT * FROM candy_purchase WHERE status = 'Paid'
28
Searching for NULL Values NULL: undefined Search conditions for NULL and non-NULL values: WHERE column_name IS NULL WHERE column_name IS NOT NULL
29
Combining Multiple Search Conditions AND: query only retrieves records for which both conditions are true WHERE Condition1 AND Condition2 OR: query retrieves records for which either condition is true WHERE Condition1 OR Condition2
30
Using AND and OR in Search Conditions Every expression must be well-formed: Do this: Not this: WHERE purch_date > '2004-10-28' AND purch_date < '2004-11-1' WHERE purch_date > '2004-10-28' AND < '2004-11-1'
31
Which records will the following query retrieve? A. Purch_id values 4, 6, 8 B. Purch_id values 1, 2, 3, 4, 5, 9, 12, 13 C. Purch_id values 1, 2, 3, 4, 5, 9, 10, 11, 12, 13 D. None of the above SELECT * FROM candy_purchase WHERE delivery_date IS NULL AND status = 'PAID'
32
Which records will the following query retrieve? A. Purch_id values 1, 2, 3, 4, 6, 7, 8, 12 B. Purch_id values 1, 2, 3, 4, 12 C. Purch_id values 1, 2, 3, 4, 5, 9, 12, 13 D. None of the above SELECT * FROM candy_purchase WHERE delivery_date = NOT NULL AND status = 'PAID'
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.