1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
SQL Review.
111 A DMINISTRATIVE Exam Oct 19, :30pm in 3005 (usual room) No class on Oct 21 What topic to do for review? November 18 th away for conference.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
SQL: Queries, Constraints, Triggers
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
SQL: Queries, Programming, Triggers
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li

2 More notes from Quiz 3

3 Composite attributes CREATE TABLE Employee (ssn CHAR (20), name CHAR(20), sname CHAR(20), street CHAR(20), city CHAR(20), zip CHAR(10), PRIMARY KEY (ssn)) Employees name ssn address snum street city zip

4 Multi-valued attributes CREATE TABLE Employee (ssn CHAR (20), name CHAR(20), …) CREATE TABLE Phone (pid INT, ssn CHAR(20), phone CHAR(15), PRIMARY KEY (pid), FOREIGN KEY (ssn) REFERENCES Employees) Employees phone name ssn

5 Derived attributes CREATE TABLE Employee (ssn CHAR (20), name CHAR(20), bdate DATETIME, age INT, PRIMARY KEY (ssn)) Employees bdate name ssn age

6 HW1 solution Q: Why we don’t have a total participation constraint from “Flight” to “Attendant”? A: “5. Each flight is initialized with departure/arrival airports, projected departure datetime, and projected arrival datetime. Pilots, flight attendants, and an airplane can be assigned later to finalize a flight.

7 Examples in Quiz 3  The solution used two tables;  Alternatively we could use one table.  Is it a good idea to use one table for this ER?

8 Next topic: SQL!

9 SQL-Based DBMSs  Commercial RDBMS choices include  DB2 (IBM)  Oracle  SQL Server (Microsoft)  Teradata  Open source RDBMS options include  MySQL  PostgreSQL  And for so-called “Big Data”, we also have  Apache Hive (on Hadoop) + newer wannabees

10 Example Instances R1 S1 S2  We’ll use these instances of our usual 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?)

11 Example Data in MySQL Sailors Reserves Boats

12 Basic SQL Query  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 Attr1 op Attr2, 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! (Bags, not sets.) SELECT [DISTINCT] target-list FROM relation-list WHERE qualification

13 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.  Project out 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.

14 Example of Conceptual Evaluation SELECT S.sname FROM Sailors S, Reserves R  using S1 WHERE S.sid=R.sid AND R.bid=103

15 A Note on Range Variables  Really needed only if the same relation appears twice in the FROM clause. The previous query can also be written as: SELECT sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 It is good style, however, to use range variables always ! OR Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

16 Find sailors who ’ ve reserved at least one boat  Would adding DISTINCT to this query make a difference? (With our data? With possible data?)  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? SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

17 Expressions and Strings  Illustrates use of arithmetic expressions and string pattern matching: Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters.  AS provides a way to (re)name fields in result.  LIKE is used for string matching. `_ ’ stands for any one character and `% ’ stands for 0 or more arbitrary characters. SELECT S.sname, S.age, S.age+1 AS age1, 7*S.age AS age2 FROM Sailors S WHERE S.sname LIKE ‘ B_%B ’ Sailors(sid, sname, rating, age)

18 Find sid ’ s of sailors who ’ ve reserved a red or a green boat  If we replace OR by AND in this first version, what do we get?  UNION : Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).  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 ’ Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)

19 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 B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color= ‘ red ’ AND B2.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 ’ Sailors(sid, sname, rating, age) Reserves(sid, bid, day) Boats(bid, bname, color)