Instructor: Jinze Liu Fall 2008. Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.

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.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
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.
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.
CS 405G: Introduction to Database Systems
SQL (2).
SQL Review.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
CS405G: Introduction to Database Systems Final Review.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
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,
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.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
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.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CMPT 258 Database Systems SQL Queries (Chapter 5).
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Relational Algebra.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
CS 405G: Introduction to Database Systems SQL III.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
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
CS 440 Database Management Systems
Basic SQL Lecture 6 Fall
EECS 647: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Lecture#7: Fun with SQL (Part 2)
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Recap – Relational languages
CS 405G: Introduction to Database Systems
Presentation transcript:

Instructor: Jinze Liu Fall 2008

Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access control Done before mid-term Transaction management Optional Optional Components (2) Data replication Indexing XML as the middle-ware between database and web interface Informational retrieval Mining method 10/21/ Jinze University of Kentucky

3 Phases of Database Design u Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.) u Logical schema is a description of the structure of the database (Relational, Network, etc.) u Physical schema is a description of the implementation (programs, tables, dictionaries, catalogs

10/21/2015Jinze University of Kentucky Cross product The only operator that adds columns Difference The only non-monotone operator Union The only operator that allows you to add rows? Selection? Projection?

10/21/2015Jinze University of Kentucky Composition of monotone operators produces a monotone query Old output rows remain “correct” when more rows are added to the input Highest-GPA query is non-monotone Current highest GPA is 4.1 Add another GPA 4.2 Old answer is invalidated  So it must use difference!

10/21/ Jinze University of Kentucky sidnameagegpa 1234John Smith Mary Carter Bob Lee222.6

10/21/2015Jinze University of Kentucky Simple A small set of core operators who semantics are easy to grasp Declarative? Yes, compared with older languages like CODASYL Though operators do look somewhat “procedural” Complete? With respect to what?

10/21/2015Luke Huan Univ. of Kansas 8

Reserves Sailors Boats Jinze University of Kentucky

Solution: Sailors  π sname Who reserved boat #103? Reserves σ bid = “103” Boat #103 Jinze University of Kentucky

Information about boat color only available in Boats; so need an extra join: Names of sailors who reserved red boat Sailors  π sname Reserve  π SID Who reserved red boats? Boat σ color = “red” Red boats Jinze University of Kentucky

Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Names of sailors who reserved red boat Sailors  π sname Reserve  π SID Who reserved red boats? Boat σ color = “red”  color = “green” Red boats Jinze University of Kentucky

10/21/2015Jinze University of Kentucky 14 SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Table expressions, subqueries More: NULL ’s, outerjoins, data modification, constraints, …

10/21/2015Jinze University of Kentucky 15 Student (SID integer, name varchar(30), age integer, GPA float); Course (CID char(10), title varchar(100)); Enroll (SID integer, CID char(10)); Create table Student (SID integer, name varchar(30), age integer, GPA float); Create table Course (CID char(10), title varchar(100)); Create table Enroll (SID integer, CID char(10));

10/21/2015Jinze University of Kentucky 16 SELECT a list of attributes FROM a list of relations WHERE condition ; Condition may have logical operators AND, OR, NOT Condition may have comparison operators:, >=, > String comparison may use “=” (exactly match) or “LIKE” (matching with regular expressions) %, _, \ (Arithmetic) expressions of attributes are allowed

10/21/2015Luke Huan Univ. of Kansas 17 SELECT... FROM … WHERE ORDER BY output_column [ ASC | DESC ], … ; ASC = ascending, DESC = descending Operational semantics After SELECT list has been computed, sort the output according to ORDER BY specification

10/21/2015Jinze University of Kentucky 18 SELECT sid, 2007 – age, FROM STUDENT WHERE name LIKE ‘%John%’ OR GPA > 3.6 ; sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim182.9 sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim182.9 sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim182.9 asdfsadf

10/21/2015Jinze University of Kentucky 19 Set and Bag operation UNION, INTERSECTION, EXCEPT DISTINCT Aggregation HAVING Nested queries SELECT age, AVG(GPA) FROM Student S ENROLL E WHERE S.SID = E.SID AND E.CID = ‘EECS108’ GROUP BY age HAVING age > 20; --Compute the average GPA for Students who are at least 20 years old and are enrolled in 108 with the same age

10/21/2015Jinze University of Kentucky 20 SQL provides the option of set semantics with DISTINCT keyword SID’s of all enrolled students SELECT SID, FROM Enroll, Say Bart takes CS700 and CS505 SELECT DISTINCT SID, FROM Enroll, With DISTINCT, all duplicate SIDs are removed from the output

10/21/2015Jinze University of Kentucky 21 SELECT [ DISTINCT ] E 1, E 2, …, E n FROM R 1, R 2, …, R m WHERE condition; For each t 1 in R 1 : For each t 2 in R 2 : … … For each t m in R m : If condition is true over t 1, t 2, …, t m : Compute and output E 1, E 2, …, E n as a row If DISTINCT is present Eliminate duplicate rows in output t 1, t 2, …, t m are often called tuple variables

10/21/2015Jinze University of Kentucky 22 UNION, EXCEPT, INTERSECT Set semantics Duplicates in input tables, if any, are first eliminated Exactly like set union, - and intersect in relational algebra UNION ALL, EXCEPT ALL, INTERSECT ALL Bag semantics Think of each row as having an implicit count (the number of times it appears in the table) Bag union: sum up the counts from two tables Bag difference: subtract the two counts (a row with negative count vanishes) Bag intersection: take the minimum of the two counts

10/21/2015Jinze University of Kentucky 23 Bag1Bag2 Bag1 UNION ALL Bag2 Bag1 EXCEPT ALL Bag2 Bag1 INTERSECT ALL Bag2 fruit Apple Orange fruit Apple Orange fruit Apple Orange Apple fruit Apple Orange

10/21/2015Jinze University of Kentucky 24 Enroll(SID, CID), ClubMember(club, SID) (SELECT SID FROM ClubMember) EXCEPT (SELECT SID FROM Enroll); SID’s of students who are in clubs but not taking any classes (SELECT SID FROM ClubMember) EXCEPT ALL (SELECT SID FROM Enroll); SID’s of students who are in more clubs than classes

10/21/2015Jinze University of Kentucky 25 Standard SQL aggregate functions: COUNT, SUM, AVG, MIN, MAX Example: number of students under 18, and their average GPA SELECT COUNT(*), AVG(GPA) FROM Student WHERE age < 18; COUNT(*) counts the number of rows

10/21/2015Jinze University of Kentucky 26 Example: How many students are taking classes? SELECT COUNT (SID) FROM Enroll; SELECT COUNT(DISTINCT SID) FROM Enroll;

10/21/2015Jinze University of Kentucky 27 SELECT … FROM … WHERE … GROUP BY list_of_columns ; Example: find the average GPA for each age group SELECT age, AVG(GPA) FROM Student GROUP BY age;

10/21/2015Jinze University of Kentucky 28 SELECT … FROM … WHERE … GROUP BY … ; Compute FROM Compute WHERE Compute GROUP BY : group rows according to the values of GROUP BY columns Compute SELECT for each group For aggregation functions with DISTINCT inputs, first eliminate duplicates within the group  Number of groups = number of rows in the final output

10/21/2015Jinze University of Kentucky 29 SELECT age, AVG(GPA) FROM Student GROUP BY age; Compute GROUP BY: group rows according to the values of GROUP BY columns Compute SELECT for each group sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim192.9 sidnameagegpa 1234John Smith Mary Carter Kevin Kim Bob Lee Susan Wong223.4 agegpa

10/21/2015Jinze University of Kentucky 30 An aggregate query with no GROUP BY clause represent a special case where all rows go into one group SELECT AVG(GPA) FROM Student; Compute aggregate over the group sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim192.9 sidnameagegpa 1234John Smith Mary Carter Bob Lee Susan Wong Kevin Kim192.9 gpa 3.24 Group all rows into one group

10/21/2015Jinze University of Kentucky 31 If a query uses aggregation/group by, then every column referenced in SELECT must be either Aggregated, or A GROUP BY column  This restriction ensures that any SELECT expression produces only one value for each group

10/21/2015Jinze University of Kentucky 32 SELECT SID, age FROM Student GROUP BY age; Recall there is one output row per group There can be multiple SID values per group SELECT SID, MAX(GPA) FROM Student; Recall there is only one group for an aggregate query with no GROUP BY clause There can be multiple SID values Wishful thinking (that the output SID value is the one associated with the highest GPA) does NOT work

10/21/2015Jinze University of Kentucky 33 Used to filter groups based on the group properties (e.g., aggregate values, GROUP BY column values) SELECT … FROM … WHERE … GROUP BY … HAVING condition; Compute FROM Compute WHERE Compute GROUP BY : group rows according to the values of GROUP BY columns Compute HAVING (another selection over the groups) Compute SELECT for each group that passes HAVING

10/21/2015Jinze University of Kentucky 34 Find the average GPA for each age group over 10 SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING age > 10; Can be written using WHERE without table expressions List the average GPA for each age group with more than a hundred students SELECT age, AVG(GPA) FROM Student GROUP BY age HAVING COUNT(*) > 100; Can be written using WHERE and table expressions

10/21/2015Jinze University of Kentucky 35 Use query result as a table In set and bag operations, FROM clauses, etc. A way to “nest” queries Example: names of students who are in more clubs than classes (SELECT SID FROM ClubMember) EXCEPT ALL (SELECT SID FROM Enroll) SELECT DISTINCT name FROM Student, ( ) AS S WHERE Student.SID = S.SID;

10/21/2015Jinze University of Kentucky 36 A query that returns a single row can be used as a value in WHERE, SELECT, etc. Example: students at the same age as Bart SELECT * FROM Student WHERE age = ( ); SELECT age FROM Student WHERE name = ’Bart’ What’s Bart’s age? Runtime error if subquery returns more than one row Under what condition will this runtime error never occur? name is a key of Student What if subquery returns no rows? The value returned is a special NULL value, and the comparison fails

10/21/2015Jinze University of Kentucky 37 x IN ( subquery ) checks if x is in the result of subquery Example: students at the same age as (some) Bart SELECT * FROM Student WHERE age IN ( ); SELECT age FROM Student WHERE name = ’Bart’ What’s Bart’s age?

10/21/2015Jinze University of Kentucky 38 EXISTS ( subquery ) checks if the result of subquery is non-empty Example: students at the same age as (some) Bart SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age); This happens to be a correlated subquery—a subquery that references tuple variables in surrounding queries

10/21/2015Jinze University of Kentucky 39 SELECT * FROM Student AS s WHERE EXISTS (SELECT * FROM Student WHERE name = ’Bart’ AND age = s.age); For each row s in Student Evaluate the subquery with the appropriate value of s.age If the result of the subquery is not empty, output s.* The DBMS query optimizer may choose to process the query in an equivalent, but more efficient way (example?)