Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.

Similar presentations


Presentation on theme: "Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004."— Presentation transcript:

1 Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004

2 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 2 High-level agenda Review keys, FDs Install Oracle Start SQL Lab on Oracle system info/SQL

3 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 3 Key/FD review Product(name, price, category, color) name, category  price category  color Keys are: {name, category} Enrollment(student, address, course, room, time) student  address room, time  course student, course  room, time Keys are: [in class]

4 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 4 Next topic: SQL (6.1) Standard language for querying and manipulating data Structured Query Language Many standards: ANSI SQL, SQL92/SQL2, SQL3/SQL99 Vendors support various subsets/extensions We’ll do Oracle’s version Basic form (many more bells and whistles in addition): SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections) SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)

5 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 5 Data Types in SQL Characters:  CHAR(20)-- fixed length  VARCHAR(40)-- variable length Numbers:  BIGINT, INT, SMALLINT, TINYINT  REAL, FLOAT -- differ in precision  MONEY Times and dates:  DATE  DATETIME-- SQL Server

6 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 6 “Tables” PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Attribute names Table name Tuples or rows

7 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 7 Simple SQL Query PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT * FROM Product WHERE category=‘Gadgets’ Product PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks “selection”

8 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 8 Simple SQL Query PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100 Product PNamePriceManufacturer SingleTouch$149.99Canon MultiTouch$203.99Hitachi “selection” and “projection”

9 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 9 A Notation for SQL Queries SELECT Name, Price, Manufacturer FROM Product WHERE Price > 100 Product(PName, Price, Category, Manfacturer) (PName, Price, Manfacturer) Input Schema Output Schema

10 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 10 SQL SQL SELECT  Sometimes called a “projection” What goes in the WHERE clause: x = y, x < y, x <= y, etc.  For number, they have the usual meanings  For CHAR and VARCHAR: lexicographic ordering Expected conversion between CHAR and VARCHAR  For dates and times, what you expect

11 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 11 SQL e.g. Movies(Title,Year,Length,inColor,Studio,Prdcr#) Q: How long was Star Wars (1977), in SQL? Q: Which Fox movies are are at least 100 minutes long, in SQL?

12 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 12 SQL e.g. Reps(ssn, name, etc.) Clients(ssn, name, rssn) Q: Who are George’s clients, in SQL? Conceptually:   Clients.name (  Reps.name=“George” and Reps.ssn=rssn (Reps x Clients))

13 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 13 The LIKE operator s LIKE p: pattern matching on strings p may contain two special symbols:  _ = any single character  % = zero or more chars Product(Name, Price, Category, Manufacturer) Find all products whose name contains ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’

14 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 14 The LIKE operator Q: What it want to search for values containing a ‘%’? PName LIKE ‘%%’ won’t work Instead, must use escape chars  In C/C++/J, prepend ‘\’  In SQL, prepend an arbitrary escape char: PName LIKE ‘%x%’ ESCAPE ‘x’

15 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 15 More on escape chars SQL: no official default escape char In SQL*Plus: default escape char == ‘\’  Can set with  SQL> set escape x Other tools, DBMSs: your mileage may very SQL string literals put in ‘ ‘:  ‘mystring’ Single-quote literals escaped with single- quotes:  ‘George’’s string’

16 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 16 More on single-quotes Dates with DATE:  DATE ‘1948-05-14’ Timestamps with TIMESTAMP:  TIMESTAMP ‘1948-05-14 12:00:00’

17 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 17 Eliminating Duplicates SELECT DISTINCT category FROM Product SELECT DISTINCT category FROM Product Compare to: SELECT category FROM Product SELECT category FROM Product Category Gadgets Photography Household Category Gadgets Photography Household

18 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 18 Ordering the Results Ordering is ascending, unless you specify the DESC keyword per attribute. SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price DESC, pname ASC SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price DESC, pname ASC

19 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 19 Ordering the Results SELECTCategory FROMProduct ORDER BYPName SELECTCategory FROMProduct ORDER BYPName PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi ?

20 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 20 Ordering the Results SELECT DISTINCT category FROMProduct ORDER BYcategory SELECT DISTINCT category FROMProduct ORDER BYcategory Compare to: Category Gadgets Household Photography SELECT DISTINCT category FROMProduct ORDER BYPName SELECT DISTINCT category FROMProduct ORDER BYPName ?

21 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 21 Joins in SQL Connect two or more tables: PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CNameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan What is the connection between them?

22 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 22 Joins in SQL Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 Join between Product and Company

23 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 23 Joins in SQL PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan PNamePrice SingleTouch$149.99 SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200

24 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 24 Joins in SQL Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all countries that manufacture some product in the ‘Gadgets’ category. SELECTCountry FROMProduct, Company WHEREManufacturer=CName AND Category=‘Gadgets’

25 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 25 Joins in SQL NamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Company CnameStockPriceCountry GizmoWorks25USA Canon65Japan Hitachi15Japan Country ?? What is the problem? What’s the solution? SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’

26 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 26 Joins Product (pname, price, category, manufacturer) Purchase (buyer, seller, store, product) Person(name, phone, city) Find names of Seattleites who bought Gadgets, and the names of the stores they bought such product from. SELECT DISTINCT name, store FROM Person, Purchase, Product WHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’

27 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 27 Disambiguating Attributes Sometimes two relations have the same attr: Person(pname, address, worksfor) Company(cname, address) SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor = cname SELECT DISTINCT Person.pname, Company.address FROM Person, Company WHERE Person.worksfor = Company.cname Which address ?

28 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 28 Tuple Variables SELECT DISTINCT x.store FROM Purchase AS x, Purchase AS y WHERE x.product = y.product AND y.store = ‘BestBuy’ SELECT DISTINCT x.store FROM Purchase AS x, Purchase AS y WHERE x.product = y.product AND y.store = ‘BestBuy’ Find all stores that sold at least one product that the store ‘BestBuy’ also sold: Answer: (store) Product (pname, price, category, manufacturer) Purchase (buyer, seller, store, product) Person(persname, phoneNumber, city)

29 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 29 Tuple Variables Tuple variables introduced automatically: Product (name, price, category, manufacturer) Becomes: Doesn’t work when Product occurs more than once In that case the user needs to define variables explicitly SELECT name FROM Product WHERE price > 100 SELECT name FROM Product WHERE price > 100 SELECT Product.name FROM Product AS Product WHERE Product.price > 100 SELECT Product.name FROM Product AS Product WHERE Product.price > 100

30 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 30 SQL Query Semantics SELECT a1, a2, …, ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE Conditions 1. Nested loops: Answer = {} for x1 in R1 do for x2 in R2 do ….. for xn in Rn do if Conditions then Answer = Answer  {(a1,…,ak)} return Answer Answer = {} for x1 in R1 do for x2 in R2 do ….. for xn in Rn do if Conditions then Answer = Answer  {(a1,…,ak)} return Answer

31 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 31 SQL Query Semantics SELECT a1, a2, …, ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE Conditions 2. Parallel assignment Doesn’t impose any order! Answer = {} for all assignments x1 in R1, …, xn in Rn do if Conditions then Answer = Answer  {(a1,…,ak)} return Answer Answer = {} for all assignments x1 in R1, …, xn in Rn do if Conditions then Answer = Answer  {(a1,…,ak)} return Answer

32 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 32 First Unintuitive SQLism SELECTR.A FROMR, S, T WHERER.A=S.A OR R.A=T.A Looking for R  (S  T) But what happens if T is empty? See transcript of this in Oracle on salestranscript

33 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 33 Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C  L (  C (R 1 x … R n )

34 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 34 Set/bag ops in SQL Orthodox SQL has set operators:  UNION, INTERSECT, EXCEPT And bag operators:  UNION ALL, INTERSECT ALL, EXCEPT ALL These operators are applied to queries: (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The Bon”) (SELECT name FROM Person WHERE City=“Seattle”) UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store=“The Bon”)

35 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 35 Set/bag ops in Oracle SQL Oracle SQL support uses MINUS rather than EXCEPT Oracle SQL supports bag op UNION ALL but not INTERSECT ALL or MINUS ALL See the Ullman page on more differencesUllman

36 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 36 Disambiguation in Oracle SQL Can rename fields by  Select name as n …  Select name n … But not by  Select name=n… Can rename relations only by  … from tab t1, tab t2 Lesson: if you get errors, remove all =s, ASs

37 Matthew P. Johnson, OCL1, CISDD CUNY, F2004 37 Disambiguation in Oracle SQL Every selected field must be unambiguous For R(A,B),  Select A from R, R   Select R1.A from R R1, R R2 Consider: Why? * is shorthand for all fields, each must be unambiguous  Select * from R R1, R R2 SQL> Select * from R, R; Select * from R, R * ERROR at line 1: ORA-00918: column ambiguously defined SQL> Select * from R, R; Select * from R, R * ERROR at line 1: ORA-00918: column ambiguously defined


Download ppt "Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004."

Similar presentations


Ads by Google