The Structured Query Language

Slides:



Advertisements
Similar presentations
Lecture plan Information retrieval (from week 11)
Advertisements

From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
CS 405G: Introduction to Database Systems
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
Database Conceptual and Logical Design Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 4, 2005 Some slide content.
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 23, 2004.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2004 Some slide content.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Advanced SQL Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 29, 2005 Some slide content courtesy of Susan.
Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
The Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 27, 2005 Some slide content.
Advanced SQL Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 30, 2004 Some slide content courtesy of Susan.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 21, 2004 Some slide content.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Advanced SQL Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 18, 2003 Some slide content courtesy of Susan.
Relational Algebra Wrap-up and Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 11, 2003.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Midterm Exam Chapters 1,2,3,5, 6,7 (closed book) March 11, 2014.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 12, 2007 Some slide content.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 17, 2007 Some slide content courtesy.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Relational Schema and SQL Queries James Wang.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 19, 2007.
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.
Database Conceptual and Logical Design Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems October 2, 2004 Some slide content.
The Structured Query Language Zachary G. Ives / Nicholas Taylor University of Pennsylvania CIS 550 – Database & Information Systems September 26, 2007.
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.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Connecting Databases to the Web
Connecting Databases to the Web
WWW and HTTP King Fahd University of Petroleum & Minerals
MySQL Subquery Source: Dev.MySql.com
COP Introduction to Database Structures
SQL The Query Language R & G - Chapter 5
Relational Model & Algebra
Basic SQL Lecture 6 Fall
Relational Model & Algebra
The Structured Query Language
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
SQL: The Query Language
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Relational Calculus Zachary G. Ives November 15, 2018
Relational Algebra & Calculus
Database Application Development
Chapter 27 WWW and HTTP.
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Tutorial 6 PHP & MySQL Li Xu
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
SQL: Structured Query Language
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

The Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 28, 2004 Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan

Administrivia Homework 2 handed out today Please sign up for Oracle accounts ASAP http://www.seas.upenn.edu/ora/ Will be useful in testing your HW2’s Google talk at 3:00 this afternoon – Craig Neville-Manning, “Finding needles in a 20 TB haystack, 200 million times per day” Wu and Chen auditorium, Levine 101

Recall Basic SQL SELECT [DISTINCT] {T1.attrib, …, T2.attrib} FROM {relation} T1, {relation} T2, … WHERE {predicates} SELECT * All STUDENTs AS As a “range variable” (tuple variable): optional As an attribute rename operator select-list from-list qualification

Expressions in SQL Can do computation over scalars (int, real or string) in the select-list or the qualification Show all student IDs decremented by 1 Strings: Fixed (CHAR(x)) or variable length (VARCHAR(x)) Use single quotes: ’A string’ Special comparison operator: LIKE Not equal: <> Typecasting: CAST(S.sid AS VARCHAR(255))

Set Operations Set operations default to set semantics, not bag semantics: (SELECT … FROM … WHERE …) {op} (SELECT … FROM … WHERE …) Where op is one of: UNION INTERSECT, MINUS/EXCEPT (many DBs don’t support these last ones!) Bag semantics: ALL

Exercise Find all students who have taken DB but not AI Hint: use EXCEPT

Example Data Instance STUDENT Takes COURSE PROFESSOR Teaches sid name 1 Jill 2 Qun 3 Nitin 4 Marty sid exp-grade cid 1 A 550-0103 700-1003 3 C 500-0103 4 cid subj sem 550-0103 DB F03 700-1003 AI S03 501-0103 Arch PROFESSOR Teaches fid name 1 Ives 2 Saul 8 Roth fid cid 1 550-0103 2 700-1003 8 501-0103

Nested Queries in SQL Simplest: IN/NOT IN Example: Students who have taken subjects that have (at any point) been taught by Roth

Correlated Subqueries Most common: EXISTS/NOT EXISTS Find all students who have taken DB but not AI

Universal and Existential Quantification Generally used with subqueries: {op} ANY, {op} ALL Find the students with the best expected grades

Table Expressions Can substitute a subquery for any relation in the FROM clause: SELECT S.sid FROM (SELECT sid FROM STUDENT WHERE sid = 5) S WHERE S.sid = 4 Notice that we can actually simplify this query! What is this equivalent to?

Aggregation GROUP BY SELECT {group-attribs}, {aggregate-operator}(attrib) FROM {relation} T1, {relation} T2, … WHERE {predicates} GROUP BY {group-list} Aggregate operators AVG, COUNT, SUM, MAX, MIN DISTINCT keyword for AVG, COUNT, SUM

Some Examples Number of students in each course offering Number of different grades expected for each course offering Number of (distinct) students taking AI courses

Example Data Instance STUDENT Takes COURSE PROFESSOR Teaches sid name 1 Jill 2 Qun 3 Nitin 4 Marty sid exp-grade cid 1 A 550-0103 700-1003 3 C 501-0103 4 cid subj sem 550-0103 DB F03 700-1003 AI S03 501-0103 Arch PROFESSOR Teaches fid name 1 Ives 2 Saul 8 Roth fid cid 1 550-0103 2 700-1003 8 501-0103

What If You Want to Only Show Some Groups? The HAVING clause lets you do a selection based on an aggregate (there must be 1 value per group): SELECT C.subj, COUNT(S.sid) FROM STUDENT S, Takes T, COURSE C WHERE S.sid = T.sid AND T.cid = C.cid GROUP BY subj HAVING COUNT(S.sid) > 5 Exercise: For each subject taught by at least two professors, list the minimum expected grade

Aggregation and Table Expressions (aka Derived Relations) Sometimes need to compute results over the results of a previous aggregation: SELECT subj, AVG(size) FROM ( SELECT C.cid AS id, C.subj AS subj, COUNT(S.sid) AS size FROM STUDENT S, Takes T, COURSE C WHERE S.sid = T.sid AND T.cid = C.cid GROUP BY cid, subj) GROUP BY subj

Thought Exercise… Tables are great, but… Not everyone is uniform – I may have a cell phone but not a fax We may simply be missing certain information We may be unsure about values How do we handle these things?

One Answer: Null Values We designate a special “null” value to represent “unknown” or “N/A” But a question: what does: do? Name Home Fax Sam 123-4567 NULL Li 234-8972 234-8766 Maria 789-2312 789-2121 SELECT * FROM CONTACT WHERE Fax < “789-1111”

Three-State Logic Need ways to evaluate boolean expressions and have the result be “unknown” (or T/F) Need ways of composing these three-state expressions using AND, OR, NOT: Can also test for null-ness: attr IS NULL, attr IS NOT NULL Finally: need rules for arithmetic, aggregation T AND U = U F AND U = F U AND U = U T OR U = T F OR U = U U OR U = U NOT U = U

Nulls and Joins Sometimes need special variations of joins: I want to see all courses and their students … But what if there’s a course with no students? Outer join: Most common is left outer join: SELECT C.subj, C.cid, T.sid FROM COURSE C LEFT OUTER JOIN Takes T ON C.cid = T.cid WHERE …

Revised Example Data Instance STUDENT Takes COURSE sid name 1 Jill 2 Qun 3 Nitin 4 Marty sid exp-grade cid 1 A 550-0103 700-1003 3 C 501-0103 4 cid subj sem 550-0103 DB F03 700-1003 AI S03 501-0103 Arch 555-0103 Food PROFESSOR Teaches fid name 1 Ives 2 Saul 8 Roth fid cid 1 550-0103 2 700-1003 8 501-0103

Warning on Outer Join Oracle doesn’t support standard SQL syntax here: SELECT C.subj, C.cid, T.sid FROM COURSE C , Takes T WHERE C.cid =(+) T.cid

Beyond Null Can have much more complex ideas of incomplete or approximate information Probabilistic models (tuple 80% likely to be an answer) Naïve tables (can have variables instead of NULLs) Conditional tables (tuple IF some condition holds) … And what if you want “0 or more”? In relational databases, create a new table and foreign key But can have semistructured data (like XML)

Modifying the Database: Inserting Data Inserting a new literal tuple is easy, if wordy: INSERT INTO PROFESSOR(fid, name) VALUES (4, ‘Simpson’) But we can also insert the results of a query! INSERT INTO PROFESSOR(fid, name) SELECT sid AS fid, name FROM STUDENT WHERE sid < 20

Deleting Tuples Deletion is a fairly simple operation: DELETE FROM STUDENT S WHERE S.sid < 25

Updating Tuples What kinds of updates might you want to do? UPDATE STUDENT S SET S.sid = 1 + S.sid, S.name = ‘Janet’ WHERE S.name = ‘Jane’

Now, How Do I Talk to the DB? Generally, apps are in a different (“host”) language with embedded SQL statements Static: SQLJ, embedded SQL in C Runtime: ODBC, JDBC, ADO, OLE DB, … Typically, predefined mappings between host language types and SQL types (e.g., VARCHAR  string or char[])

Embedded SQL in C EXEC SQL BEGIN DECLARE SECTION int sid; char name[20]; EXEC SQL END DECLARE SECTION … EXEC SQL INSERT INTO STUDENT VALUES (:sid, :name); EXEC SQL SELECT name, age INTO :sid, :name FROM STUDENT WHERE sid < 20

The Impedance Mismatch and Cursors SQL is set-oriented – it returns relations There’s no relation type in most languages! Solution: cursor that’s opened, read DECLARE sinfo CURSOR FOR SELECT sid, name FROM STUDENT … OPEN sinfo; while (…) { FETCH sinfo INTO :sid, :name … } CLOSE sinfo;

JDBC: Dynamic SQL Roughly speaking, a Java version of ODBC See Chapter 6 of the text for more info import java.sql.*; Connection conn = DriverManager.getConnection(…); PreparedStatement stmt = conn.prepareStatement(“SELECT * FROM STUDENT”); … ResultSet rs = stmt.executeQuery (); while (rs.next()) { sid = rs.getInteger(1); … }

Database-Backed Web Sites We all know traditional static HTML web sites: Web-Browser Web-Server HTTP-Request GET ... HTML-File File-System Load File All pages are static Need to generate web pages on the fly depending on user input

Common Gateway Interface (CGI) Can have the web server invoke code (with parameters) to generate HTML Web Server Web Server File-System HTTP-Request Load File HTML HTML? HTML-File Execute Program Program? Output File I/O, Network, DB

CGI: Discussion Advantages: Disadvantages: Standardized: works for every web-server, browser Flexible: Any language (C++, Perl, Java, …) can be used Disadvantages: Statelessness: query-by-query approach Inefficient: new process forked for every request Security: CGI programmer is responsible for security Updates: To update layout, one has to be a programmer

DB Access in Java Browser JVM Java Applet Java-Server-Process TCP/UDP IP Java-Server-Process JDBC Driver manager JDBC-Driver JDBC-Driver JDBC-Driver Sybase Oracle ...

Java Applets: Discussion Advantages: Can take advantage of client processing Platform independent – assuming standard java Disadvantages: Requires JVM on client; self-contained Inefficient: loading can take a long time ... Resource intensive: Client needs to be state of the art Restrictive: can only connect to server where applet was loaded from (for security … can be configured)

*SP Server Pages and Servlets (IIS, Tomcat, …) Web Server Web Server HTTP Request File-System Load File HTML HTML? HTML File File Script? Output Server Extension I/O, Network, DB

One Step Beyond: DB-Driven Web Sites (Strudel, Cocoon, …) Web Server DB-Driven Web Server HTTP Request Styles Cache HTML File HTML Script? Local Database Dynamic HTML Generation Data Other data sources

Wrapping Up We’ve seen how to query in SQL (DML) Basic foundation is TRC-based Subqueries and aggregation add extra power Nulls and outer joins add flexibility of representation We can update tables We’ve seen that SQL doesn’t precisely match standard host language semantics Embedded SQL Dynamic SQL Data-driven web sites