Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL200.1 Module 1


Published on

Introduction to SQL Module 1.

Published in: Technology
  • Be the first to comment

SQL200.1 Module 1

  1. 1. SQL200 Based on SQL Clearly Explained by Jan Harrington SQL Programming Module 1 – Relational Database Background, Basic Single Table Retrieval Operations
  2. 2. SQL200 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 [email_address]
  3. 3. 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>
  4. 4. 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 GUI tools or Access “SQL View” almost exclusively </li></ul>
  5. 5. 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>
  6. 6. 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>
  7. 7. Relational Database Table
  8. 8. 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>
  9. 9. Relational Database with constraints
  10. 10. Database Management Systems Positioning Chart VLDB Enterprise Workgroup Single user Spreadsheet # Users Cost
  11. 11. System Architecture Access MDB File Server Architecture Access
  12. 12. System Architecture Oracle DB Visual Basic App Client/Server Architecture Access  SQL 
  13. 13. System Architecture Oracle DB Browser Web Architecture Web Server  SQL 
  14. 14. 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>
  15. 15. 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>
  16. 16. 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>
  17. 17. 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>
  18. 18. 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>
  19. 19. 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>
  20. 20. 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>
  21. 21. SQL Statement Processing Parse Validate Optimize Access Plan Execute
  22. 22. 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>Use Access import table feature, or </li></ul></ul><ul><ul><li>Run SQL script, or </li></ul></ul><ul><ul><li>Use Access upsizing wizard </li></ul></ul>
  23. 23. Text 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>
  24. 24. Date Conventions <ul><li>In Access date are normally surrounded by pound signs </li></ul><ul><ul><li>#12/23/2004# </li></ul></ul><ul><li>In an enterprise database such as Oracle or SQL Sever, then single quotes </li></ul><ul><ul><li>‘ 2004-12-23’ MySQL </li></ul></ul><ul><ul><li>’ 12-23-2004’ SQL Server </li></ul></ul><ul><ul><li>’ 23-DEC-04’ Oracle </li></ul></ul><ul><li>Be sensitive to the environment of your class in copying scripts from the PowerPoint slides! </li></ul>
  25. 25. SELECT Basic Syntax ( Projection ): Select <column-list> or <*> From <table-list>
  26. 26. SELECT Basic Example ( Projection ): select customer_last_name, customer_street from customers
  27. 27. MS Access SQL Query
  28. 29. SQL Server Query
  29. 30. SELECT with Where Clause Example ( Restriction plus Projection ): Select <column-list> From <table-list> Where <selection-criteria>;
  30. 31. SELECT with Where Basic Example ( Restriction plus Projection ): select customer_last_name, customer_street from customers where customer_last_name = “Jones”
  31. 32. Select with Where
  32. 33. On Your Own <ul><li>Find books written by Mark Twain </li></ul><ul><li>Show title, publisher, year </li></ul>
  33. 34. Complex Predicates 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’
  34. 35. Select with Complex Where
  35. 36. Complex Where Result
  36. 37. 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>
  37. 38. 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>
  38. 39. IN 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.
  39. 40. SQL Where Clause with IN
  40. 41. IS NULL 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.
  41. 42. 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>
  42. 43. BETWEEN Select * From orders Where order_date BETWEEN #1/1/99# and #12/31/99# Note: date formats vary from product to product. Above is Access syntax, SQL Server uses single quotes. Ex: ‘1/1/99’
  43. 44. Where with Between
  44. 45. Removing Duplicates Select DISTINCT customer_city From customers List once each city in which there are customers Removes duplicate rows from result set
  45. 46. Removing Duplicates
  46. 47. Sorting – ORDER BY DESC will sort in descending order Basic syntax : Select <column list> From <table list> Where <selection criteria> Order by <column list> [DESC]
  47. 48. Sorting – ORDER BY Select * From customers Order by customer_state, customer_city Example: List all records sorted by state, city
  48. 49. Sorting Results with Order By
  49. 50. Selecting Top Records Select Top 5 (or top 25 percent) Customer_last_name , contact_zip From customers Order by customer_zip desc ; List largest 5 zips or top 25 % of them…
  50. 51. 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 column 2 of the credit card #; sort by order date descending </li></ul>[end module]