SQL212 SQL Programming Based on  SQL Clearly Explained  by Jan Harrington Module  1 – Relational Database Background, Basic Single Table Retrieval Operations Bookstore SQL212  Module 1
SQL212 Contact Information Bookstore2 SQL212  Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address]   Copyright 2001-2009. All rights reserved.
SQL212 Notes This course is based on an earlier course, SQL200, which was cross-platform. As such the screenshots still reflect a wide variety of platforms. Bookstore SQL212  Module 1
SQL212 Resources Bookstore database build script found on dhdurso.org… http://www.dhdurso.org/downloads/sql-downloads.html   Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases   Bookstore SQL212  Module 1
SQL212 SQL Programming Part 1 – Relational Database Background Bookstore2 SQL212  Module 2
SQL Programming Course focus is SQL language Widely used for: Database administration Enterprise application development Data driven web sites A foundation skill for eBusiness and almost all major business applications that use relational databases Bookstore SQL212  Module 1
SQL Programming A basic knowledge of query systems, perhaps via MS Access,  or some programming knowledge, is desirable We will use Oracle SQL Developer in this class to create scripts. Bookstore SQL212  Module 1
Relational Database Evolution Based on Codd’s paper Early commercial efforts focused on Unix First mainframe implementation by IBM  - precursor to today’s DB2 First PC implementation in early 80’s by Oracle Bookstore SQL212  Module 1
Relational Database Basics Storage Databases Tables Rows Columns Indexes Views Cursors Application interfaces Bookstore SQL212  Module 1
Bookstore SQL212  Module 1 Relational Database Table
Constraints Database Domain Uniqueness Relationship Cardinality 1 to 1 1 to N Other Business Rule Triggers Stored Procedures Bookstore SQL212  Module 1
Bookstore SQL212  Module 1 Relational Database with constraints
Database Management Systems Bookstore SQL212  Module 1 Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet # Users Cost
System Architecture Bookstore SQL212  Module 1 Access MDB File Server  Architecture Access
System Architecture Bookstore SQL212  Module 1 Oracle DB Visual Basic App Client/Server  Architecture Access  SQL 
System Architecture Bookstore SQL212  Module 1 Oracle DB Browser Web  Architecture Web Server  SQL 
Approaching SQL Relatively simple Two main environments Interactive (This course) Embedded Static (Compiled) Dynamic Bookstore SQL212  Module 1
SQL Standardization ANSI standardization First standard in 1986 SQL 89 SQL 92 SQL 99 Various vendor extensions Microsoft/Sybase: T-SQL Oracle: PL/SQL Bookstore SQL212  Module 1
SQL Conformance Entry Intermediate Advanced Most are at least entry level Bookstore SQL212  Module 1
SQL Statements Data Manipulation Language (DML) Data Control Language (DCL) Data Definition Language (DDL) Note: SQL 99 changes these to seven types Bookstore SQL212  Module 1
SQL DDL Data definition language (DDL) Create, alter, drop, etc. Frequently implemented via various CASE tools: Visio, Embarcadero, ERWin, etc. But very useful for database administration Bookstore SQL212  Module 1
SQL DCL Data Control Language (DDL) Grant Revoke Constraints Bookstore SQL212  Module 1
SQL DML Data Manipulation Language (DML) Select Insert Update Delete Bookstore SQL212  Module 1
SQL Statement Processing Bookstore SQL212  Module 1 Parse Validate Optimize Access Plan Execute
SQL212 SQL Programming Part 2 – Basic SQL Retrieval Bookstore2 SQL212  Module 2
Bookstore Sample Database Before we continue (note: instructor may have already done this)… Load the sample database if you haven’t already Run SQL script Bookstore SQL212  Module 1
Conventions In Access character strings are normally surrounded by double quotes “ Jones” In an enterprise database such as Oracle or SQL Sever, then single quotes ‘ Jones’ Be   sensitive to the environment of your class in copying scripts from the PowerPoint slides! Bookstore SQL212  Module 1
SELECT Bookstore SQL212  Module 1 Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
SELECT Bookstore SQL212  Module 1 Basic Example ( Projection ): select   customer_last_name,  customer_street from  customers
MS Access SQL Query Bookstore SQL212  Module 1
Bookstore SQL212  Module 1
Query with Results Bookstore SQL212  Module 1
SELECT with Where Clause Bookstore SQL212  Module 1 Example ( Restriction  plus   Projection ): Select <column-list> From <table-list> Where <selection-criteria>;
SELECT with Where Bookstore SQL212  Module 1 Basic Example ( Restriction plus Projection ): select   customer_last_name,  customer_street from  customers where  customer_last_name = ‘Jones’
Select with Where Bookstore SQL212  Module 1
On Your Own Find books written by Mark Twain Show title, publisher, year Bookstore SQL212  Module 1
Complex Predicates Bookstore SQL212  Module 1 Follow normal boolean logic Select   customer_last_name,  customer_street From  customers Where  (customer_last_name = ‘Jones’ or customer_last_name = ‘Smith’)and customer_state=‘NY’
Select with Complex Where Bookstore SQL212  Module 1
Complex Where Result Bookstore SQL212  Module 1
Special Operators Can be used in where clause LIKE IN BETWEEN IS NULL Bookstore SQL212  Module 1
Like (“Wild Card Matches”) ANSI Where customer_last_name like “Jo%” Like “Jo_” Access Where customer_last_name like “Jo*” Like “Jo?” Bookstore SQL212  Module 1
IN Bookstore SQL212  Module 1 Select  * From  customers Where  customer_last_name in (‘Rizzo’, ‘Jones’, ‘Garcia’) The list in parentheses can be replaced by a subquery. We will study this later.
SQL Where Clause with IN Bookstore SQL212  Module 1
IS NULL Bookstore SQL212  Module 1 Select  * From  customers Where  customer_street IS NULL  SQL uses three valued logic. Must use IS NULL to test for unknowns. A null is NOT the same as blank or empty.
On Your Own Find all customers with an address not equal to 4592 Maple Lane Was Peter Johnson selected? Why or why not? Bookstore SQL212  Module 1
BETWEEN Bookstore SQL212  Module 1 Select * From orders Where order_date BETWEEN ‘ 1-jan-99’ and ‘31-dec-99’ Note: date formats vary from product to product.
Where with Between Bookstore SQL212  Module 1
Removing Duplicates Bookstore SQL212  Module 1 Select DISTINCT  customer_city From  customers List once each city in which there are customers Removes duplicate rows from result set
Removing Duplicates Bookstore SQL212  Module 1
Sorting – ORDER BY Bookstore SQL212  Module 1 DESC will sort in descending order Basic syntax : Select <column list> From <table list> Where <selection criteria> Order by <column list> [DESC]
Sorting – ORDER BY Bookstore SQL212  Module 1 Select  * From  customers Order by  customer_state, customer_city Example: List all records sorted by state, city
Sorting Results with Order By Bookstore SQL212  Module 1
SQL Exercises List all books whose publisher name begins with “H” or “T”; sort by title [hint: use LIKE] List all customers whose last name ends with “S”; sort by state, city, last name Find the order numbers of orders with order dates in 1999; sort by order #. [Hint: use BETWEEN] Find the order numbers and order dates of all orders with a “2” in character position 2 of the credit card #; sort by order date descending Bookstore SQL212  Module 1 [end module]
Notes Bookstore SQL212  Module 1
Notes Bookstore SQL212  Module 1
Notes Bookstore SQL212  Module 1

SQL212.1 Introduction to SQL using Oracle Module 1

  • 1.
    SQL212 SQL ProgrammingBased on SQL Clearly Explained by Jan Harrington Module 1 – Relational Database Background, Basic Single Table Retrieval Operations Bookstore SQL212 Module 1
  • 2.
    SQL212 Contact InformationBookstore2 SQL212 Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-2009. All rights reserved.
  • 3.
    SQL212 Notes Thiscourse is based on an earlier course, SQL200, which was cross-platform. As such the screenshots still reflect a wide variety of platforms. Bookstore SQL212 Module 1
  • 4.
    SQL212 Resources Bookstoredatabase build script found on dhdurso.org… http://www.dhdurso.org/downloads/sql-downloads.html Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases Bookstore SQL212 Module 1
  • 5.
    SQL212 SQL ProgrammingPart 1 – Relational Database Background Bookstore2 SQL212 Module 2
  • 6.
    SQL Programming Coursefocus is SQL language Widely used for: Database administration Enterprise application development Data driven web sites A foundation skill for eBusiness and almost all major business applications that use relational databases Bookstore SQL212 Module 1
  • 7.
    SQL Programming Abasic knowledge of query systems, perhaps via MS Access, or some programming knowledge, is desirable We will use Oracle SQL Developer in this class to create scripts. Bookstore SQL212 Module 1
  • 8.
    Relational Database EvolutionBased on Codd’s paper Early commercial efforts focused on Unix First mainframe implementation by IBM - precursor to today’s DB2 First PC implementation in early 80’s by Oracle Bookstore SQL212 Module 1
  • 9.
    Relational Database BasicsStorage Databases Tables Rows Columns Indexes Views Cursors Application interfaces Bookstore SQL212 Module 1
  • 10.
    Bookstore SQL212 Module 1 Relational Database Table
  • 11.
    Constraints Database DomainUniqueness Relationship Cardinality 1 to 1 1 to N Other Business Rule Triggers Stored Procedures Bookstore SQL212 Module 1
  • 12.
    Bookstore SQL212 Module 1 Relational Database with constraints
  • 13.
    Database Management SystemsBookstore SQL212 Module 1 Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet # Users Cost
  • 14.
    System Architecture BookstoreSQL212 Module 1 Access MDB File Server Architecture Access
  • 15.
    System Architecture BookstoreSQL212 Module 1 Oracle DB Visual Basic App Client/Server Architecture Access  SQL 
  • 16.
    System Architecture BookstoreSQL212 Module 1 Oracle DB Browser Web Architecture Web Server  SQL 
  • 17.
    Approaching SQL Relativelysimple Two main environments Interactive (This course) Embedded Static (Compiled) Dynamic Bookstore SQL212 Module 1
  • 18.
    SQL Standardization ANSIstandardization First standard in 1986 SQL 89 SQL 92 SQL 99 Various vendor extensions Microsoft/Sybase: T-SQL Oracle: PL/SQL Bookstore SQL212 Module 1
  • 19.
    SQL Conformance EntryIntermediate Advanced Most are at least entry level Bookstore SQL212 Module 1
  • 20.
    SQL Statements DataManipulation Language (DML) Data Control Language (DCL) Data Definition Language (DDL) Note: SQL 99 changes these to seven types Bookstore SQL212 Module 1
  • 21.
    SQL DDL Datadefinition language (DDL) Create, alter, drop, etc. Frequently implemented via various CASE tools: Visio, Embarcadero, ERWin, etc. But very useful for database administration Bookstore SQL212 Module 1
  • 22.
    SQL DCL DataControl Language (DDL) Grant Revoke Constraints Bookstore SQL212 Module 1
  • 23.
    SQL DML DataManipulation Language (DML) Select Insert Update Delete Bookstore SQL212 Module 1
  • 24.
    SQL Statement ProcessingBookstore SQL212 Module 1 Parse Validate Optimize Access Plan Execute
  • 25.
    SQL212 SQL ProgrammingPart 2 – Basic SQL Retrieval Bookstore2 SQL212 Module 2
  • 26.
    Bookstore Sample DatabaseBefore we continue (note: instructor may have already done this)… Load the sample database if you haven’t already Run SQL script Bookstore SQL212 Module 1
  • 27.
    Conventions In Accesscharacter strings are normally surrounded by double quotes “ Jones” In an enterprise database such as Oracle or SQL Sever, then single quotes ‘ Jones’ Be sensitive to the environment of your class in copying scripts from the PowerPoint slides! Bookstore SQL212 Module 1
  • 28.
    SELECT Bookstore SQL212 Module 1 Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
  • 29.
    SELECT Bookstore SQL212 Module 1 Basic Example ( Projection ): select customer_last_name, customer_street from customers
  • 30.
    MS Access SQLQuery Bookstore SQL212 Module 1
  • 31.
  • 32.
    Query with ResultsBookstore SQL212 Module 1
  • 33.
    SELECT with WhereClause Bookstore SQL212 Module 1 Example ( Restriction plus Projection ): Select <column-list> From <table-list> Where <selection-criteria>;
  • 34.
    SELECT with WhereBookstore SQL212 Module 1 Basic Example ( Restriction plus Projection ): select customer_last_name, customer_street from customers where customer_last_name = ‘Jones’
  • 35.
    Select with WhereBookstore SQL212 Module 1
  • 36.
    On Your OwnFind books written by Mark Twain Show title, publisher, year Bookstore SQL212 Module 1
  • 37.
    Complex Predicates BookstoreSQL212 Module 1 Follow normal boolean logic Select customer_last_name, customer_street From customers Where (customer_last_name = ‘Jones’ or customer_last_name = ‘Smith’)and customer_state=‘NY’
  • 38.
    Select with ComplexWhere Bookstore SQL212 Module 1
  • 39.
    Complex Where ResultBookstore SQL212 Module 1
  • 40.
    Special Operators Canbe used in where clause LIKE IN BETWEEN IS NULL Bookstore SQL212 Module 1
  • 41.
    Like (“Wild CardMatches”) ANSI Where customer_last_name like “Jo%” Like “Jo_” Access Where customer_last_name like “Jo*” Like “Jo?” Bookstore SQL212 Module 1
  • 42.
    IN Bookstore SQL212 Module 1 Select * From customers Where customer_last_name in (‘Rizzo’, ‘Jones’, ‘Garcia’) The list in parentheses can be replaced by a subquery. We will study this later.
  • 43.
    SQL Where Clausewith IN Bookstore SQL212 Module 1
  • 44.
    IS NULL BookstoreSQL212 Module 1 Select * From customers Where customer_street IS NULL SQL uses three valued logic. Must use IS NULL to test for unknowns. A null is NOT the same as blank or empty.
  • 45.
    On Your OwnFind all customers with an address not equal to 4592 Maple Lane Was Peter Johnson selected? Why or why not? Bookstore SQL212 Module 1
  • 46.
    BETWEEN Bookstore SQL212 Module 1 Select * From orders Where order_date BETWEEN ‘ 1-jan-99’ and ‘31-dec-99’ Note: date formats vary from product to product.
  • 47.
    Where with BetweenBookstore SQL212 Module 1
  • 48.
    Removing Duplicates BookstoreSQL212 Module 1 Select DISTINCT customer_city From customers List once each city in which there are customers Removes duplicate rows from result set
  • 49.
  • 50.
    Sorting – ORDERBY Bookstore SQL212 Module 1 DESC will sort in descending order Basic syntax : Select <column list> From <table list> Where <selection criteria> Order by <column list> [DESC]
  • 51.
    Sorting – ORDERBY Bookstore SQL212 Module 1 Select * From customers Order by customer_state, customer_city Example: List all records sorted by state, city
  • 52.
    Sorting Results withOrder By Bookstore SQL212 Module 1
  • 53.
    SQL Exercises Listall books whose publisher name begins with “H” or “T”; sort by title [hint: use LIKE] List all customers whose last name ends with “S”; sort by state, city, last name Find the order numbers of orders with order dates in 1999; sort by order #. [Hint: use BETWEEN] Find the order numbers and order dates of all orders with a “2” in character position 2 of the credit card #; sort by order date descending Bookstore SQL212 Module 1 [end module]
  • 54.
  • 55.
  • 56.