1 / 82

Chapter 5: Introduction to SQL

Slides 40, 70, 78, and 80 have been updated 8/3. Chapter 5: Introduction to SQL. Modern Database Management 13 th Edition Jeff Hoffer, Ramesh Venkataraman , Heikki Topi. Greatly improved 2018; 2019. If some figures have “6-#”, please treat them as “ 5 -#”.

gretaf
Download Presentation

Chapter 5: Introduction to SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Slides 40, 70, 78, and 80 have been updated 8/3 Chapter 5:Introduction to SQL Modern Database Management 13th Edition Jeff Hoffer, Ramesh Venkataraman, HeikkiTopi Greatly improved 2018; 2019 If some figures have “6-#”, please treat them as “5-#” #70: “SEVEN SINS” // #71: Flowchart of SELECT // #73-81: The new SELECT-clause examples

  2. Define terms • Interpret history and role of SQL • Define a database using SQL data definition language • Write single table queries using SQL • No subquery, yet • Establish referential integrity using SQL Objectives Focus: Syntax

  3. Structured Query Language – often pronounced “Sequel” • The standard for relational database management systems (RDBMS) • RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables SQL Overview

  4. 1970–E. F. Codd develops relational database concept • 1974-1979–System R with Sequel (later SQL) created at IBM Research Lab • 1979–Oracle markets first relational DB with SQL • 1981 – SQL/DS first available RDBMS system on DOS/VSE • Others followed: INGRES (1981), IDM (1982), DG/SGL (1984), Sybase (1986) • 1986–ANSI SQL standard released • 1989, 1992, 1999, 2003, 2006, 2008, 2011–Major ANSI standard updates • Current–SQL is supported by most major database vendors History of SQL

  5. Specify syntax/semantics for data definition and manipulation Define data structures and basic operations Enable portability of database definition and application modules Specify minimal (level 1) and complete (level 2) standards Allow for later growth/enhancement to standard (referential integrity, transaction management, user-defined functions, extended join operations, national character sets) Purpose of SQL Standard

  6. Catalog • A set of schemas that constitute the description of a database • Schema • The structure that contains descriptions of objects created by a user (base tables, views, constraints) • Data Definition Language (DDL) • Commands that define a database, including creating, altering, and dropping tables and establishing constraints • Data Manipulation Language (DML) • Commands that maintain and query a database • Data Control Language (DCL) • Commands that control a database, including administering privileges and committing data Course priority: 2, 1, 3 SQL Environment

  7. Display order of columns specified here 1 2 3 4 5 6 SELECT Major, AVERAGE(GPA) FROM STUDENT WHERE ExpGraduateYr = “2018” GROUP BY Major HAVING AVERAGE(GPA) >=3.0 ORDER BY Major; Fig 5-2 Query syntax p.212 Sequence of clauses! No comma between clauses; semi-colon at the end Line breaks NOT needed!

  8. SELECT Major, AVG(GPA) , AVG(YrGrad-YrAdm) AS Duration, AdvsrName FROM STUDENT, FACULTY AS FAC WHERE Major=“ACCT” ANDFAC.FacID = STUDENT.FacID GROUP BY Major, AdvsrName HAVINGAVG(GPA) >=3.0 ORDER BY AVG(GPA) DESC, AdvsrName; Fig 6-2 Query syntax More than 85% of usage of SELECT-clause is here

  9. Visit later SQL Data Types

  10. 10 Figure 6-3: sample PVFC data

  11. Figure 5-4 P.215 DDL, DML, DCL, and the database development process

  12. Data Definition Language (DDL) • Major CREATE statements: • CREATE SCHEMA–defines a portion of the database owned by a particular user • CREATE TABLE–defines a new table and its columns • CREATE VIEW–defines a logical table from one or more tables or views • Other CREATE statements: CHARACTER SET, COLLATION, TRANSLATION, ASSERTION, DOMAIN Visit later SQL Database Definition

  13. Visit later Steps in Table Creation • Identify data types for attributes • Identify columns that can and cannot be null • Identify columns that must be unique (candidate keys) • Identify primary key–foreign key mates • Determine default values • Identify constraints on columns (domain specifications) • Create the table and associated indexes

  14. Figure 6-5 General syntax for CREATE TABLE statement used in data definition language Visit later

  15. The following slides create tables for this enterprise data model (from Chapter 1, Figure 1-3)

  16. Figure 6-6 SQL database definition commands for PVF Company (Oracle 12c) Visit later Overall table definitions Interpreta-tions next 5 slides

  17. Defining attributes and their data types Visit later

  18. Non-nullable specification Visit later Primary keys can never have NULL values Identifying primary key Constraint name Cnstr Type Field

  19. Non-nullable specifications Visit later Composite key Some primary keys are composite– composed of multiple attributes

  20. Controlling the values in attributes Visit later Default value Domain constraint validation rule

  21. Identifying foreign keys and establishing relationships Visit later Primary key of parent table Foreign key of dependent table Constraint name CnstrntType Field REFERENCESTbl(Key)

  22. Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1:M relationships • Restricting: • Deletes of primary records • Updates of primary records • Inserts of dependent records Visit later Data Integrity Controls

  23. Figure 6-7 Ensuring data integrity through updates Visit later Relational integrity is enforced via the primary-key to foreign-key match

  24. ALTER TABLE statement allows you to change column specifications: Table Actions: Example (adding a new column with a default value): Changing Tables - Alter table-clause ADD COLUMN NameType Value Default Value

  25. DROP TABLE statement allows you to remove tables from your schema: • DROP TABLE CUSTOMER_T Removing Tables

  26. Adds one or more rows to a table Inserting into a table Inserting a record that has some null attributes requires identifying the fields that actually get data Inserting from another table Two methods: full-row & partial-row insertions Insert Statement INSERT INTO TableVALUES(value-list) (field-list) VALUES (value-list) • A SUBSET from another table

  27. Creating Tables with Identity Columns Introduced with SQL:2008 • Inserting into a table does not require explicit customer ID entry or field list • INSERT INTO CUSTOMER_T VALUES ( 'Contemporary Casuals', '1355 S. Himes Blvd.', 'Gainesville', 'FL', 32601);

  28. Removes rows from a table • Delete certain row(s) • DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = 'HI'; • DELETE FROM CUSTOMER_T WHERE CUSTOMER_ID = ‘C123'; • Delete all rows • DELETE FROM CUSTOMER_T; • Careful!!! Delete Statement

  29. Modifies data in existing rows Note: the WHERE clause may be a subquery (Chap 7) Update Statement • UPDATETable-name • SETAttribute = Value • WHERECriteria-to-apply-the-update

  30. ALTER TABLE: change the columns[structure] of the table • ALTER TABLE CUSTOMER_T ADD column… • INSERT: addrecords based on the existing table [did not change/alter table] • INSERT INTO CUSTOME_T VALUES (… ) • UPDATE: change the values of some fields in SOMEexisting records [did not add a record] • UPDATE CUSTOMER_T SET field = value …WHERE… Comparison of ALTER, INSERT, and UPDATE

  31. Degree of impact: • ALTER TABLE – wide (whole table); deep (nature/structure) • INSERT – local (add rows; only make a table longer) • UPDATE – very minor (only change values of some fields of some rows) Comparison of ALTER, INSERT, and UPDATE (Cont) Decrease Common confusion

  32. Merge Statement Interpretation? Makes it easier to update a table…allows combination of Insert and Update in one statement Useful for updating master tables with new data

  33. Control processing/storage efficiency: • Choice of indexes • File organizations for base tables • File organizations for indexes • Data clustering • Statistics maintenance • Creating indexes • Speed up random/sequential access to base table data • Example • CREATE INDEX NAME_IDX ON CUSTOMER_T(CUSTOMERNAME) • This makes an index for the CUSTOMERNAME field of the CUSTOMER_T table Schema Definition

  34. This is the CENTER section of the chapter, and the CENTER of this course Many slides contain multiple points of concepts/skills – do NOT miss the text, and pay attention to color code Query: select statement Data manipulation

  35. Used for queries on single or multiple tables • Clauses of the SELECT statement: • SELECT • List the columns (& expressions) to be returned from the query • FROM • Indicate the table(s) or view(s) from which data will be obtained • WHERE • Indicate the conditions under which a row will be included in the result • GROUP BY • Indicate categorization of results (*collapsed* into groups) • HAVING • Indicate the conditions under which a category (group) will be included • ORDER BY • Sorts the result according to specified criteria on named fields SELECT Statement Memorizing clause order: 6-39 “No GROUP BY, no HAVING!!!!”; HAVING always AFTER Grp By

  36. SELECT Major, AVG(GPA) , AVG(YrGrad-YrAdm) AS Duration, AdvsrName FROM STUDENT, FACULTY AS FAC ---- [Missing table join here] WHERE Major=“ACCT” ANDFAC.FacID = STUDENT.FacID GROUP BY Major, AdvsrName HAVINGAVG(GPA) >=3.0 ORDER BY AVG(GPA) DESC, AdvsrName; Fig 6-2 Query syntax More than 80% of usage of SELECT-clause is here

  37. HAVING and WHERE are both condition-clause: states conditions/criteria for selection • WHERE states cri for INDIVIDUAL rows • HAVING states cri for GROUPS • Because HAVING states cri for groups, • It must be preceded by GROUP BY: • No GROUP BY, no HAVING • HAVING always comes after GROUP BY • “A lower-level bullet w big fonts” HAVING vs where; group by and having Remem-ber slide 6-38 >1/4 made this mistake

  38. Figure 5-10 P.242 • SQL statement processing order (based on van der Lans, 2006 p.100) Figure 5-2 General syntax of the SELECT statement used in DML Insert #71 here 1st SELECT 2nd FROM 3rd Row condition 4th Grouping 5th Group condition 6th Finally display order 1st SELECT 2nd FROM tables 3rd Row condition – row first 4th Grouping – then group 5th No grping, no group cond 6th Finally display order – this does NOT involve/affect logic!

  39. SELECT Example • Find products with standard price less than $275 P. 226 Every SESLECT statement returns a result table - Can be used as part of another query - subquery SELECT DISTINCT: no duplicate rows SELECT * : all columns selected

  40. SELECT Example Using Alias P. 227 Table alias1Column alias2 • Alias is an alternative tableorcolumn name SELECT CUST.CUSTOMER_NAME AS NAME, CUST.CUSTOMER_ADDRESS FROM CUSTOMER_V [AS] CUST WHERE NAME = ‘Home Furnishings’; Note1:Specifying source table Note2:Column alias is used as display heading ONLY ; NOT for comparison/calculation, Grp, Order (PP. 227~228) Used in SELECT while defined in FROM Avoid !**

  41. Example: • ProductStandardPrice*1.1 AS Plus10Percent • Which contents does it look like that we learned in Access (in IS 312)? • YearInBiz:(NOW()-DateOpened)/365 • Observe the result on P. 229 Using expressions PP. 228~9

  42. More functions: P. 229 • Using the COUNT aggregate function to find totals SELECT COUNT(*) FROM ORDERLINE_T WHERE ORDERID = 1004; Note: COUNT (*) and COUNT – different • COUNT(*) counts num rows • COUNT(field) counts rows that have not-null val for the field • What is average standard price for each [examine!!] product in inventory? SELECT AVG (STANDARD_PRICE)AS AVERAGE FROM PROCUCT_V GROUP BY Product_ID; • Meaning *IF* w/o GROUP BY clause? • - w/o GROUP BY, the AVG is performed over the whole table. SELECT Example Using a Function Functions that can ONLY be used w numeric columns? Can be w or w/o GROUP BY, but w different meanings

  43. Using a Function (Cont) SELECT COUNT(*)Output: COUNT(*) FROM OoderLine_T2 WHERE OrderID= 1004; Note: With aggregate functions (“set values”) you can’t have single-valued columns (“row values”) included in the SELECT clause SELECT PRODUCT_ID, COUNT(*) FROM ORDER_LINE_V WHERE ORDER_ID = 1004; ***Examine the error message on P. 230 PP. 230~1 A field name in SELECT must be either (1) in aggrfunc, or (2) the GROUP BY field

  44. Insert #72 here Using a Function (Cont) With aggregate functions (“set values” – AVG, SUM, COUNT, MIN/MAX, etc) you can’t have single-valued columns (“row values”) included in the SELECT clause • More example (error): SELECT ProductStandardPrice– AVG(ProductStandardPrice) FROM Product_T; ***!!! “Row values and set values do NOT belong to the same SELECT clause” If both really need to be in the same SELECT statement, use subquery (P. 231, top-middle) Row value Setvalue

  45. If no aggrfunc in SELECT: nothing to worry • If ALL aggregate function, nothing to worry • If there’s a mixture of fields & aggfunc(s) in SELECT, error msg will result. • In this case (fields, w aggfunc) if a field (attribute) is to be in SELECT, then that attribute must be • either in an aggregate function, • or it is the GROUP BY field • Note: A GROUP BY field doesn’t have to be in SELECT SELECT involving Aggregate function

  46. Top of P. 231: SQL cannot return botha row value (such as Product_ID) and a set value (such as COUNT/AVG/SUM of a group); • users must run two separate queries, one that returns row info and one that returns set info • SELECT S_ID FROM STUDENT - Row • SELECT AVG(GPA) FROM STUDENT - Set • SELECT S_ID, AVG(GPA)FROM STUDENT – row & set: ERROR! Issues about row value and aggregates

  47. SELECT S_ID, MAJOR, GPA FROM STUDENT SELECT S_ID, MAJOR, AVG(GPA) FROM STUDENT SELECT S_ID, MAJOR, AVG(GPA) FROM STUDENT GROUP BY MAJOR SELECT COUNT(S_ID), MAJOR, AVG(GPA) FROM STUDENT GROUP BY MAJOR row value and aggregates (cont) See middle of P. 230, SQL Server error message

  48. Wildcards: P. 232 • LIKE ‘%Desk’ • LIKE ‘*Desk’ • Comparison operators: • NULL values: • IS NULL • IS NOT NULL Wildcards; comparison operators; null values

  49. IS NULL • IS NULL: the field has a null value – not 0, not space (_), but no value • Useful in scenarios as follows: • Sold_date in a real estate property table • Paid_date in a student registration table • Transaction_amount in a auction table • … • Syntax: WHERE field_name IS NULL • The opposite is: WHERE field_nameIS NOT NULL

  50. AND, OR, and NOT Operators for customizing conditions in WHERE clause SELECT Example–Boolean Operators Must be very, very, very, VERY careful about Boolean operations Note: The LIKEoperator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed. In MS Access wildcard is “*”

More Related