Click here to load reader

Jan 08, 2018

ISOM Today’s Buzzwords Query Languages Formal Query Languages Procedural and Declarative Languages Relational Algebra Relational Calculus SQL Aggregate Functions Nested Queries

ISOM MIS3150 Data and Information Management Query Languages SQL Arijit Sengupta ISOM Structure of this semester Database Fundamentals Relational Model Normalization Conceptual Modeling Query Languages Advanced SQL 0. Intro 1. Design NewbieUsersProfessionalsDesigners Java DB Applications JDBC 4. Applications MIS Querying Developers Transaction Management Data Mining 3. Advanced Topics ISOM Todays Buzzwords Query Languages Formal Query Languages Procedural and Declarative Languages Relational Algebra Relational Calculus SQL Aggregate Functions Nested Queries ISOM Objectives At the end of the lecture, you should Get a formal as well as practical perspective on query languages Have a background on query language basics (how they came about) Be able to write simple SQL queries from the specification Be able to look at SQL queries and understand what it is supposed to do Be able to write complex SQL queries involving nesting Execute queries on a database system ISOM Set Theory Basics A set: a collection of distinct items with no particular order Set description: { b | b is a Database Book} {c | c is a city with a population of over a million} {x | 1 < x < 10 and x is a natural number} Most basic set operation: Membership: x S (read as x belongs to S if x is in the set S) ISOM Other Set Operations Addition, deletion (note that adding an existing item in the set does not change it) Set mathematics: Union R S = { x | x R or x S} Intersection R S = { x | x R and x S} Set Difference R S = { x | x R and x S} Cross-product R x S = { | x R and y S} You can combine set operations much like arithmetic operations: R (S T) Usually no well-defined precedence ISOM Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Strong formal foundation based on logic. Allows for much optimization. Query Languages != programming languages! QLs not expected to be Turing complete. QLs not intended to be used for complex calculations. QLs support easy, efficient access to large data sets. ISOM Formal Relational Query Languages Two mathematical Query Languages form the basis for real languages (e.g. SQL), and for implementation: Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.) * Understanding Algebra & Calculus is key to understanding SQL, query processing! ISOM Structured Query Language Need for SQL Operations on Data Types Definition Manipulation Operations on Sets Declarative (calculus) vs. Procedural (algebra) Evolution of SQL SEQUEL..SQL_92.. SQL_93 SQL Dialects Does SQL treat Relations as Sets? ISOM Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input relations for a query are fixed (but query will run regardless of instance!) The schema for the result of a given query is also fixed! Determined by definition of query language constructs. Positional vs. named-field notation: Positional notation easier for formal definitions, named-field notation more readable. Both used in SQL ISOM Example Instances R1 S1 S2 Students, Registers, Courses relations for our examples. C1 ISOM Basic operations: Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 and in reln. 2. Additional operations: Intersection, join, division, renaming: Not essential, but (very!) useful. Since each operation returns a relation, operations can be composed! (Algebra is closed.) Relational Algebra ISOM Projection Deletes attributes that are not in projection list. Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Projection operator has to eliminate duplicates! (Why??) Note: real systems typically dont do duplicate elimination unless the user explicitly asks for it. (Why not?) ISOM Vertical Slices Projection Specifying Elements No Specification List all information about Students select* fromSTUDENT; (Student) Conditional List IDs, names, and addresses of all students select StudentID, name, address from STUDENT; StudentID, name, address (Student) Algebra: projection (R) ISOM Does SQL treat Relations as Sets? What are the different salaries we pay to our employees? selectsalary fromEMPLOYEE; OR is the following better? selectDISTINCT salary fromEMPLOYEE; ISOM Selection Selects rows that satisfy selection condition. No duplicates in result! (Why?) Schema of result identical to schema of (only) input relation. Result relation can be the input for another relational algebra operation! (Operator composition.) ISOM Horizontal Slices Restriction Specifying Conditions Unconditional List all students select* fromSTUDENT; (Student) Conditional List all students with GPA > 3.0 select* fromSTUDENT whereGPA > 3.0; GPA > 3.0 (Student) Algebra: selection or restriction (R) ISOM Specifying Conditions List all students in... select* fromSTUDENT wherecity in (Boston,Atlanta); List all students in... select* fromSTUDENT wherezip not between and 60123; ISOM Pattern Matching %any string with n characters, n>=0 _any single character. x exact sequence of string x. List all CIS 3200 level courses. select* fromCOURSE wherecourse# like ? ; List all CIS courses. select* fromCOURSE wherecourse# like CIS%; ISOM Missing or Incomplete Information List all students whose address or telephone number is missing: select* fromSTUDENT whereAddress is null or GPA is null; ISOM Horizontal and Vertical Query: List all student ID, names and addresses who have GPA > 3.0 and date of birth before Jan 1, selectStudentID, Name, Address fromSTUDENT whereGPA > 3.0 and DOB < 1-Jan-80 order byName DESC; Algebra: StudentID,name, address ( GPA > 3.0 and DOB < 1-Jan-80 (STUDENT)) Calculus: {t.StudentID, t.name, t.address | t Student t.GPA > 3.0 t.DOB < 1-Jan-80} Order by sorts result in descending (DESC) order. Note: The default order is ascending (ASC) as in: order by Name; ISOM Union, Intersection, Set-Difference All of these operations take two input relations, which must be union-compatible: Same number of fields. `Corresponding fields have the same type. What is the schema of result? ISOM Union n List students who live in Atlanta or GPA > 3.0 selectStudentID, Name, DOB, Address fromSTUDENT where Address = Atlanta union selectStudentID, Name, DOB, Address fromSTUDENT where GPA > 3.0; Can we perform a Union on any two Relations ? ISOM Union Compatibility n Two relations, A and B, are union-compatible if A and B contain a same number of attributes, and The corresponding attributes of the two have the same domains Examples CIS=Student (ID: D id ; Name: D name ; Address: D addr ; Grade: D grade ); Senior-Student (SName: D name ; S#: D id ; Home: D addr ; Grade: D grade ); Course (C#: D number ; Title: D str ; Credits: D number ) Are CIS-Student and Senior-Student union compatible? Are CIS-Student and Course union compatible? What happens if we have duplicate tuples? What will be the column names in the resulting Relation? ISOM Union, Intersect, Minus selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA UNION selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA UNION selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA INTERSECT selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA INTERSECT selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA MINUS selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; selectCUSTNAME, ZIP fromCUSTOMER where STATE = MA MINUS selectSUPNAME, ZIP fromSUPPLIER whereSTATE = MA ORDER BY 2; B A B A B AA ISOM Cross-Product Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited if possible. Conflict: Both S1 and R1 have a field called sid. * Renaming operator : ISOM Joins Condition Join: Result schema same as that of cross- product. Fewer tuples than cross-product, might be able to compute more efficiently Sometimes called a theta-join. ISOM Joins Equi-Join: A special case of condition join where the condition c contains only equalities. Result schema similar to cross-product, but only one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields. ISOM Find names of students who have taken course #103 v Solution 2 : v Solution 3 : v Solution 1 : ISOM Connecting/Linking Relations List information about all students and the classes they are taking What can we use to connect/link Relations? Join: Connecting relations so that relevant tuples can be retrieved. Student Class ISOM Join Cartesian Product Student: 30 tuplesClass: 4 tuples Total Number of Tuples in the Cartesian Product. ? (match each tuple of student to every tuple of class) Select tuples having identical Student Ids. Expected number of such Tuples: Join Selectivity R1R2 ISOM Join Forms General Join Forms Equijoin Operator Dependent Natural Join Outer Join Left Right Full selects.*, c.* fromSTUDENT s, CLASS c wheres.StudentID = c.SID (+); selects.*, c.* fromSTUDENT s, CLASS c wheres.StudentID = c. SID; = x > y ... R1R2 R1R2 ISOM Find names of students who have taken a CIS course Information about departments only available in Courses; so need an extra join: v A more efficient solution: * A query optimizer can find this given the first solution! ISOM Find students who have taken an MIS or a CS course Ca

Welcome message from author

This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Related Documents