SQL: Structured Query Language Data Manipulation Language

SQL: Structured Query Language Data Manipulation Language

Outline SQL: Structured Query Language Introduction slide167 Data Manipulation Language ExampleDB slide170 Data Manipulation Retrieval slide 174 Updates slide 203 QueryProcessing slide208 166 165 Query Languages Introduction SQL presentation Introduction Query Languages Functionalities : Data Definition Language Data Manipulation Language data definition and data manipulation in the relational format Formal Languages Manipulation data control relational algebra Manipulation language relational calculus (based on predicate logic) non procedural user-oriented Query languages borrowed to relational algebra and to the tuple relational calculus Structured Query Language (SQL) Power of the manipulation language QUEry Language (QUEL) Relational Algebra Query By Example (QBE) + Link with programming languages Functions-Aggregates + Sorting use of SQL statements inside a higher-level language program is known as Embedded SQL (Pascal, C, ...) A SQL query (without functions and sorting) Set of Relational Algebra operations 167 168 DBMS SQL - 1 SQL Presentation (2) Introduction Example DB : Company DB DB Example History SEQUEL language of the SYSTEM/R relational DBMS prototype (74-76) Employee(ESSN, LastName, FirstName, BirthDate, Address, IBM research lab at San José E Sex, Salary, BonusAmount, SupervSSN , DepNumber Normalization at ISO ) The SQL1 norm (1986, 1989) Department(DepNumber, DepName, MgrSSN , The SQL2 norm (1992) D MgrStartDate ) The SQL3 norm DL Query language of (quite) all relational DBMSs Dept_locat(DepNumber , DLocation ) ORACLE (Oracle Corporation - 1977) Project( ProjNumber, ProjName, PLocation, DepNumber ) P INGRES (Ingres Technology - 1980) W DB2 (IBM - 1984) Work_on(ProjNumber , ESSN , Nbh ) INFORMIX (Informix Inc - 1981) SYBASE (Sybase Inc - 1984) MySQL (1995) 169 170 ER Model of Company database DB Example 1,1 supervised_by Supervise Employee Populated Database (1) Employee ENSS LastName FirstName BirthDate Address Sex Salary BonusAmount SupervSSN dDpNumber ESSN supervises 123456789 Smith John 1967-01-09 731 Fondren, Houston, TX M 300000 10000 333445555 5 LastName 0,n 333445555 Wong Franklin 1955-12-08 638 Voss, Houston, TX M 400000 5000 888665555 5 FirstName Works_for 999887777 Zelaya Alicia 1968-07-19 3321 Castel, Spring, TX F 250000 2000 987654321 4 0,1 BirthDate 0,n Departement 967654321 Wallace Jennifer 1941-06-20 291 Berry, Bellaire, TX F 430000 9000 888665555 4 Address 0,1 DepNumber 666884444 Narayan Ramesh 1962-09-15 975 Fire Oak, Humble, TX M 380000 15000 333445555 5 453453453 English Joyce 1972-07-31 5631 Rice, Houston, TX F 250000 2500 333445555 5 Sex 0,1 DepName Salary Manages 987987987 Jabbar Ahmad 1969-03-29 980 Dallas, Houston, TX M 250000 12000 987654321 4 MgrStartDate 888665555 Borg James 1937-11-10 450 Stone, Houston, TX M 550000 8000 null 1 BonusAmount 0,n 0,n Work_on ProjNumber ESSN Nbh 0,n 1 123456789 32.5 2 123456789 7.5 3 666884444 40.0 Work_on Locate 1 453453453 20.0 nbh 2 453453453 20.0 2 333445555 10.0 Project ProjNumber ProjName PLocation DepNumber 3 333445555 10.0 1 productx Bellaire 5 10 333445555 10.0 0,n 2 producty Sugarland 5 (1,1) 20 333445555 10.0 3 productz Houston 5 30 999887777 30.0 Project Dept-Locat 10 computerization Stanford 4 10 999887777 10.0 20 reorganization Houston 1 ProjNumber DLocation 10 987987987 35.0 0,1 Control 30 newbenefits Stanford 4 ProjName 30 987987987 5.0 PLocation 30 987654321 20.0 20 987654321 15.0 20 888665555 null 171 172 DBMS SQL - 2 Populated Database (2) Data retrieval Data Retrieval Dept_Locat DepNumber DLocation General retrieval syntax slide 175 1 Houston 4 Stanford Selection and projection slide 176 5 Bellaire 5 Sugarland Join operator slide 181 5 Houston Set-Theoretical Operations slide 184 Department DepNumber DepName MgrSSN MgrStarDate Aggregate functions slide 185 5 R&D 333445555 1988-05-22 4 Administration 987654321 1995-01-01 1 Headquarters 888665555 1981-06-19 Table grouping slide 189 Predicates and division slide 193 Summary slide 198 Complete example slide 200 173 174 General Syntax of Data Retrieval Data Retrieval Selection and Projection Data Retrieval Syntax How to fill the clauses ? " Retrieve all employees who are involved on a project for more than 20 hours " which result the user wants to see, SELECT schema result? SELECT ESSN, ProjNumber, nbh <set of projected attributes > FROM Work_on WHERE Nbh > 20; ESSN ProjNumber Nbh FROM Where are the attributes that I need? 11234 1 32.5 <set of relations> 16668 3 40 29998 27 30 Are there conditions on the values 19879 26 35 [WHERE used in my rule? Do I have several <set of restrictions and join relations in my FROM clause? Number of output tuples : 4 criteria >] 175 176 DBMS SQL - 3 Projection(1) Data Retrieval Projection (2) " Find all the informations registered on the "Determine all the project locations " Project records " SELECT DISTINCT PLocation SELECT * FROM Project FROM Project Wild-card character To discard instead duplicate values of listing all columns of tuples Result PLocation Result ProjNumber ProjName PLocation DepNumber 1 productx Bellaire 5 Bellaire 2 producty Sugarland 5 Sugarland 3 productz Houston 5 10 computerization Stanford 4 Houston 20 reorganization Houston 1 Stanford 30 newbenefits Stanford 4 177 178 Selection Data Retrieval Selection and sorting Data Retrieval "Find all the employees who work for a department whose number is between 1 and 4" "Find all the employees whose lastname begins with SELECT * 'b' or 'B' " FROM Employee SELECT * WHERE DepNumber >=1 AND DepNumber <=4 FROM Employee WHERE LastName LIKE ‘b%’ OR LastName LIKE ‘B%’ SELECT * " Retrieve the list of women ordered by their salary" FROM Employee SELECT * WHERE DepNumber BETWEEN 1 AND 4 FROM Employee WHERE Sex ='F' SELECT * ORDER BY Salary FROM Employee WHERE DepNumber IN (1, 2, 3, 4) 179 180 DBMS SQL - 4 The join operation Data Retrieval Procedural Join Data Retrieval SELECT ESSN " Retrieve the ESSN values for all the employees FROM Employee working in the 'R&D' department at Houston " WHERE DepNumber IN ( SELECT DepNumber SELECT E.ESSN FROM Department FROM Employee E, Department D, Dep_Locat DL WHERE DepName = 'R&D' WHERE E.DepNumber = D.DepNumber Cartesian and DepNumber IN ( AND D.DepNumber=DL.DepNumber Product SELECT DepNumber AND D.DLocation ='Houston' FROM Dep_Locat AND E.DepName = ‘R&D’ join conditions WHERE DLocation = ‘Houston')) attribute name non ambiguous 181 182 Auto-join Data Retrieval Join (SQL2 syntax) Join a relation with itself SQL2 introduces a new syntax closest to synonyms relational algebra (joins are expressed within "Employees who earn more than their supervisor " the FROM clause) SELECT E.EmpName Supported by several DBMSs (>= Oracle 9, FROM Employee E, Employee SUP MySQL, SQLServer, …) WHERE E.supervSSN = SUP.ESSN AND E.Salary > SUP.Salary 183 184 DBMS SQL - 5 SQL2 join examples Set-Theoretical Operations Data Retrieval Union (SQL1 norm ) automatic elimination of Cross Product doubles "List of expenses of the staff SELECT * Difference (SQL2 norm !) FROM Employee CROSS JOIN project costs (salary and bonus)" SELECT Salary "Employees who are not Jointure FROM Employee supervisors" SELECT E.ESSN UNION FROM Employee E JOIN Department D ON (E.DepNumber = SELECT BonusAmount SELECT ENSS D.DepNumber) JOIN Dep_Locat DL ON FROM Employee FROM Employee (D.DepNumber=DL.DepNumber) MINUS Intersection (SQL2 norm !) SELECT supervSSN WHERE D.DLocation ='Houston' "Employees who supervise other FROM Employee AND E.DepName = ‘R&D’ employees and work on at least « natural » join one project" SELECT SupervSSN Explicit equality on attributes with same name can be replaced by FROM Employee implicite one using NATURAL JOIN, ou JOIN … USING (attrs) INTERSECT SELECT ENSS FROM Work_on 185 186 Aggregate functions Data Retrieval Examples with functions (1) Data Retrieval 5 predefined functions : COUNT, SUM, MIN, MAX, " Find the maximum salary, the minimum salary, and AVG the average salary among all employees ." Principle : applied to all values in a column of a relation SELECT MAX(Salary), MIN(Salary), AVG(Salary) FROM Employee Produces a unique value Rule without grouping (later) : only in the SELECT, never in the WHERE "Find the total salary amount and the total bonus do not mix in the SELECT functions and attributes! received by the employees" SELECT SUM(Salary), SUM(BonusAmount) FROM Employee 187 188 DBMS SQL - 6 Examples with functions (2) Data Retrieval Examples with functions (3) Data Retrieval "Retrieve the number of employees in the 'R&D' "Employees whose salary is greater than the department " average salary" SELECT COUNT (ESSN) FROM Employee E, Department D SELECT ENSS WHERE E.DepNumber=D.DepNumber FROM Employee AND DepName='R&D’ WHERE salary > ( " Retrieve the total number of employees in the SELECT AVG(salary) company" FROM Employee) SELECT COUNT (*) FROM Employee 189 190 Grouping Data Retrieval Grouping examples Data Retrieval Principle " Find the average salary for each department " SELECT DepNumber, AVG(Salary) Horizontal subgroups of tuples based on the value of FROM Employee an attribute or a group of attributes, specified in the GROUP BY DepNumber GROUP BY clause " … ordered by descending department number " The relation is (logically) fragmented in groups of tuples, where all the tuples of each group have the SELECT DepNumber, AVG(Salary) same value for the grouping attribute(s) FROM Employee GROUP BY DepNumber The function is applied to each subgroup ORDER BY 1 DESC independently " … only

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us