SQL212.1 Introduction to SQL using Oracle Module 1

596 views

Published on

Relational database background, basic SQL retrieval

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
596
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SQL212.1 Introduction to SQL using Oracle Module 1

  1. 1. 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
  2. 2. 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.
  3. 3. SQL212 Notes <ul><li>This course is based on an earlier course, SQL200, which was cross-platform. As such the screenshots still reflect a wide variety of platforms. </li></ul>Bookstore SQL212 Module 1
  4. 4. SQL212 Resources <ul><li>Bookstore database build script found on dhdurso.org… </li></ul><ul><ul><li>http://www.dhdurso.org/downloads/sql-downloads.html </li></ul></ul><ul><li>Slides can be viewed on SlideShare… </li></ul><ul><ul><li>http://www.slideshare.net/OCDatabases </li></ul></ul>Bookstore SQL212 Module 1
  5. 5. SQL212 SQL Programming Part 1 – Relational Database Background Bookstore2 SQL212 Module 2
  6. 6. SQL Programming <ul><li>Course focus is SQL language </li></ul><ul><li>Widely used for: </li></ul><ul><ul><li>Database administration </li></ul></ul><ul><ul><li>Enterprise application development </li></ul></ul><ul><ul><li>Data driven web sites </li></ul></ul><ul><li>A foundation skill for eBusiness and almost all major business applications that use relational databases </li></ul>Bookstore SQL212 Module 1
  7. 7. SQL Programming <ul><li>A basic knowledge of query systems, perhaps via MS Access, or some programming knowledge, is desirable </li></ul><ul><li>We will use Oracle SQL Developer in this class to create scripts. </li></ul>Bookstore SQL212 Module 1
  8. 8. Relational Database Evolution <ul><li>Based on Codd’s paper </li></ul><ul><li>Early commercial efforts focused on Unix </li></ul><ul><li>First mainframe implementation by IBM - precursor to today’s DB2 </li></ul><ul><li>First PC implementation in early 80’s by Oracle </li></ul>Bookstore SQL212 Module 1
  9. 9. Relational Database Basics <ul><li>Storage </li></ul><ul><li>Databases </li></ul><ul><li>Tables </li></ul><ul><li>Rows </li></ul><ul><li>Columns </li></ul><ul><li>Indexes </li></ul><ul><li>Views </li></ul><ul><li>Cursors </li></ul><ul><li>Application interfaces </li></ul>Bookstore SQL212 Module 1
  10. 10. Bookstore SQL212 Module 1 Relational Database Table
  11. 11. Constraints <ul><li>Database </li></ul><ul><ul><li>Domain </li></ul></ul><ul><ul><li>Uniqueness </li></ul></ul><ul><ul><li>Relationship Cardinality </li></ul></ul><ul><ul><ul><li>1 to 1 </li></ul></ul></ul><ul><ul><ul><li>1 to N </li></ul></ul></ul><ul><li>Other Business Rule </li></ul><ul><ul><li>Triggers </li></ul></ul><ul><ul><li>Stored Procedures </li></ul></ul>Bookstore SQL212 Module 1
  12. 12. Bookstore SQL212 Module 1 Relational Database with constraints
  13. 13. Database Management Systems Bookstore SQL212 Module 1 Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet # Users Cost
  14. 14. System Architecture Bookstore SQL212 Module 1 Access MDB File Server Architecture Access
  15. 15. System Architecture Bookstore SQL212 Module 1 Oracle DB Visual Basic App Client/Server Architecture Access  SQL 
  16. 16. System Architecture Bookstore SQL212 Module 1 Oracle DB Browser Web Architecture Web Server  SQL 
  17. 17. Approaching SQL <ul><li>Relatively simple </li></ul><ul><li>Two main environments </li></ul><ul><ul><li>Interactive (This course) </li></ul></ul><ul><ul><li>Embedded </li></ul></ul><ul><ul><ul><li>Static (Compiled) </li></ul></ul></ul><ul><ul><ul><li>Dynamic </li></ul></ul></ul>Bookstore SQL212 Module 1
  18. 18. SQL Standardization <ul><li>ANSI standardization </li></ul><ul><ul><li>First standard in 1986 </li></ul></ul><ul><ul><li>SQL 89 </li></ul></ul><ul><ul><li>SQL 92 </li></ul></ul><ul><ul><li>SQL 99 </li></ul></ul><ul><li>Various vendor extensions </li></ul><ul><ul><li>Microsoft/Sybase: T-SQL </li></ul></ul><ul><ul><li>Oracle: PL/SQL </li></ul></ul>Bookstore SQL212 Module 1
  19. 19. SQL Conformance <ul><li>Entry </li></ul><ul><li>Intermediate </li></ul><ul><li>Advanced </li></ul><ul><li>Most are at least entry level </li></ul>Bookstore SQL212 Module 1
  20. 20. SQL Statements <ul><li>Data Manipulation Language (DML) </li></ul><ul><li>Data Control Language (DCL) </li></ul><ul><li>Data Definition Language (DDL) </li></ul><ul><li>Note: SQL 99 changes these to seven types </li></ul>Bookstore SQL212 Module 1
  21. 21. SQL DDL <ul><li>Data definition language (DDL) </li></ul><ul><ul><li>Create, alter, drop, etc. </li></ul></ul><ul><ul><li>Frequently implemented via various CASE tools: Visio, Embarcadero, ERWin, etc. </li></ul></ul><ul><ul><li>But very useful for database administration </li></ul></ul>Bookstore SQL212 Module 1
  22. 22. SQL DCL <ul><li>Data Control Language (DDL) </li></ul><ul><ul><li>Grant </li></ul></ul><ul><ul><li>Revoke </li></ul></ul><ul><ul><li>Constraints </li></ul></ul>Bookstore SQL212 Module 1
  23. 23. SQL DML <ul><li>Data Manipulation Language (DML) </li></ul><ul><ul><li>Select </li></ul></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>Update </li></ul></ul><ul><ul><li>Delete </li></ul></ul>Bookstore SQL212 Module 1
  24. 24. SQL Statement Processing Bookstore SQL212 Module 1 Parse Validate Optimize Access Plan Execute
  25. 25. SQL212 SQL Programming Part 2 – Basic SQL Retrieval Bookstore2 SQL212 Module 2
  26. 26. Bookstore Sample Database <ul><li>Before we continue (note: instructor may have already done this)… </li></ul><ul><li>Load the sample database if you haven’t already </li></ul><ul><ul><li>Run SQL script </li></ul></ul>Bookstore SQL212 Module 1
  27. 27. Conventions <ul><li>In Access character strings are normally surrounded by double quotes </li></ul><ul><ul><li>“ Jones” </li></ul></ul><ul><li>In an enterprise database such as Oracle or SQL Sever, then single quotes </li></ul><ul><ul><li>‘ Jones’ </li></ul></ul><ul><li>Be sensitive to the environment of your class in copying scripts from the PowerPoint slides! </li></ul>Bookstore SQL212 Module 1
  28. 28. SELECT Bookstore SQL212 Module 1 Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
  29. 29. SELECT Bookstore SQL212 Module 1 Basic Example ( Projection ): select customer_last_name, customer_street from customers
  30. 30. MS Access SQL Query Bookstore SQL212 Module 1
  31. 31. Bookstore SQL212 Module 1
  32. 32. Query with Results Bookstore SQL212 Module 1
  33. 33. SELECT with Where Clause Bookstore SQL212 Module 1 Example ( Restriction plus Projection ): Select <column-list> From <table-list> Where <selection-criteria>;
  34. 34. 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’
  35. 35. Select with Where Bookstore SQL212 Module 1
  36. 36. On Your Own <ul><li>Find books written by Mark Twain </li></ul><ul><li>Show title, publisher, year </li></ul>Bookstore SQL212 Module 1
  37. 37. 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’
  38. 38. Select with Complex Where Bookstore SQL212 Module 1
  39. 39. Complex Where Result Bookstore SQL212 Module 1
  40. 40. Special Operators <ul><li>Can be used in where clause </li></ul><ul><li>LIKE </li></ul><ul><li>IN </li></ul><ul><li>BETWEEN </li></ul><ul><li>IS NULL </li></ul>Bookstore SQL212 Module 1
  41. 41. Like (“Wild Card Matches”) <ul><li>ANSI </li></ul><ul><li>Where customer_last_name like “Jo%” </li></ul><ul><li>Like “Jo_” </li></ul><ul><li>Access </li></ul><ul><li>Where customer_last_name like “Jo*” </li></ul><ul><li>Like “Jo?” </li></ul>Bookstore SQL212 Module 1
  42. 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. 43. SQL Where Clause with IN Bookstore SQL212 Module 1
  44. 44. 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.
  45. 45. On Your Own <ul><li>Find all customers with an address not equal to 4592 Maple Lane </li></ul><ul><li>Was Peter Johnson selected? </li></ul><ul><li>Why or why not? </li></ul>Bookstore SQL212 Module 1
  46. 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. 47. Where with Between Bookstore SQL212 Module 1
  48. 48. 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
  49. 49. Removing Duplicates Bookstore SQL212 Module 1
  50. 50. 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]
  51. 51. Sorting – ORDER BY Bookstore SQL212 Module 1 Select * From customers Order by customer_state, customer_city Example: List all records sorted by state, city
  52. 52. Sorting Results with Order By Bookstore SQL212 Module 1
  53. 53. SQL Exercises <ul><li>List all books whose publisher name begins with “H” or “T”; sort by title [hint: use LIKE] </li></ul><ul><li>List all customers whose last name ends with “S”; sort by state, city, last name </li></ul><ul><li>Find the order numbers of orders with order dates in 1999; sort by order #. [Hint: use BETWEEN] </li></ul><ul><li>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 </li></ul>Bookstore SQL212 Module 1 [end module]
  54. 54. Notes Bookstore SQL212 Module 1
  55. 55. Notes Bookstore SQL212 Module 1
  56. 56. Notes Bookstore SQL212 Module 1

×