Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.

Slides:



Advertisements
Similar presentations
What is a Database By: Cristian Dubon.
Advertisements

Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
1 A GUIDE TO ORACLE8 CHAPTER 1: Introduction to Client/Server Databases 1.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
Introduction to Structured Query Language (SQL)
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
CSC 2720 Building Web Applications Database and SQL.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CSC 2720 Building Web Applications Database and SQL.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Client/Server Databases and the Oracle Relational Database.
INTRODUCTION TO DATABASES CS 260 Database Systems.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Enhanced Guide to Oracle8i
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
SQL SELECT QUERIES CS 260 Database Systems. Overview  Introduction to SQL  Single-table select queries  Using the DBMS to manipulate data output 
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
1 Introduction to Oracle Chapter 1. 2 Before Databases Information was kept in files: Each field describes one piece of information about student Fields.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
Database Basics BCIS 3680 Enterprise Programming.
Understand Tables and How to Create Them Database Administration Fundamentals LESSON 2.2.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Chapter 5 Introduction to SQL.
CS320 Web and Internet Programming SQL and MySQL
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Chapter 8 Working with Databases and MySQL
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Presentation transcript:

Introduction to Databases Queries CS 146

Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Basic Database Vocabulary  Field: column of similar data values  Record: row of related fields  Table: set of related rows Field Record

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

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

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

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

Key Fields  Primary key: uniquely identifies a record InstructorIDInstructor LastName Instructor FirstName 1BlackGreg 2McIntyreKaren 3SarinNaj 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 identifier o Consider the CANDY_PURCHASE table…  Surrogate PK: ID generated by the DBMS solely as a unique identifier

Duplication Considerations  When data values appear multiple times, there is duplication  Problems:  Space  Data becomes inconsistent over time StudentIDStudent LastName Student FirstName StudentMIAdvisorLast Name AdvisorFirst Name 5000NelsonAmberSBlackAnne 5001HernandezJosephPBlackAnne 5002MyersStephenRSarinNaj UniversityStudent

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

 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 Database DB Data DBMS Client Workstations Database Server

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.

Query 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 Queries

MySQL Query Browser Type query:Click Execute:

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

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

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

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;

Search Conditions  General format: FieldName Operator TargetValue  Operators: =,, =, <> or !=  Examples:  PROD_ID = 1  POUNDS > 5  STATUS != 'PAID'

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, 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

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'

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

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

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, 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'

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'