Introduction to SQL Basics SQL in 45 Minutes
Introduction to SQL Basics --SQL in 45 Minutes Chapter 5 Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1
The SQL Query Language Ø Ø Ø Developed by IBM (system R) in the 1970 s Need for a standard since it is used by many vendors Standards: Ø SQL-86 Ø SQL-89 (minor revision) Ø SQL-92 (major revision, current standard) Ø SQL-99 (major extensions) Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 2
R 1 Example Instances Ø Ø We will use these instances of the Sailors and Reserves relations in our examples. If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ? S 1 S 2 Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 3
Basic SQL Query Ø Ø SELECT FROM WHERE [DISTINCT] target-list relation-list qualification relation-list A list of relation names (possibly with a range -variable after each name). target-list A list of attributes of relations in relation-list qualification Comparisons (Attr op const or Attr 1 op Attr 2, where op is one of ) combined using AND, OR and NOT. DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 4
Conceptual Evaluation Strategy Ø Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Ø Ø Ø Compute the cross-product of relation-list. Discard resulting tuples if they fail qualifications. Delete attributes that are not in target-list. If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 5
Example of Conceptual Evaluation SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid AND R. bid=103 Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 6
A Note on Range Variables Ø OR Really needed only if the same relation appears twice in the FROM clause. The previous query can also be written as: SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid AND bid=103 SELECT FROM WHERE sname Sailors, Reserves Sailors. sid=Reserves. sid AND bid=103 Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke It is good style, however, to use range variables always! 7
Find sailors who’ve reserved at least one boat SELECT S. sid FROM Sailors S, Reserves WHERE S. sid=R. sid Ø Ø R Would adding DISTINCT to this query make a difference? What is the effect of replacing S. sid by S. sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference? Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 8
Dr. Eick’s Graphical Method to Design SQL Queries 1. 2. 3. 4. 5. Draw a node for each relation that is required to answer the query Write those attributes, whose values will be returned by answer the query into the node(s) Specify single node restrictions/selection conditions --attach those to nodes using ‘<>’ Assign edges that connect the involved nodes for 2 node restrictions / conditions to the graph. Label the edge with the 2 -node condition Translate the graph into an SQL-query Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 9
Example: “Give sid and name of all sailors that have reservations for a green boat and a red boat” Sailor S S-name sid Reservation R 1 sid=sid Reservation R 2 sid= sid Boat B 2 bid= bid color=green bid= bid Boat B 1 color=red Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke Remark: for corresponding SQL-query see page 12 10
Find sid’s of sailors who’ve reserved a red or a green boat Ø Ø Ø UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries). If we replace OR by AND in the first version, what do we get? Also available: EXCEPT (What do we get if we replace UNION by EXCEPT? ) SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND (B. color=‘red’ OR B. color=‘green’) SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ UNION SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 11
Find sid’s of sailors who’ve reserved a red and a green boat Ø Ø Ø INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples. Included in the SQL/92 standard, but some systems don’t support it. Contrast symmetry of the UNION and INTERSECT queries with how much the other versions differ. SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE S. sid=R 1. sid AND R 1. bid=B 1. bid AND S. sid=R 2. sid AND R 2. bid=B 2. bid AND B 1. color=‘red’ AND B 2. color=‘green’ Key field! SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ INTERSECT SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 12
- Slides: 12