01-09-2007NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index.

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
The Relational Calculus
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
COMPANY schema EMPLOYEE
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
The Relational Algebra
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Introduction to Standard Query Language Erik Zeitler UDBL
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization.
Chapter 19 Query Processing and Optimization
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
Structured Query Language
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
Introduction to Database Systems
Query Processing – Implementing Set Operations and Joins Chap. 19.
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 7 More SQL: Complex Queries, Triggers, Views, and Schema Modification Slide 7- 1.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
The SQL Database Grammar
CS580 Advanced Database Topics
6/22/2018.
Company Requirements.
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

NOEA/IT FEN - Databases/SQL1 More about SQL Nested SELECT Other Joins Views Index

NOEA/IT FEN - Databases/SQL2 SQL2 - DML (Q16): Sub queries (nested SELECTs) SELECTE.FNAME, E.LNAME FROMEMPLOYEE E WHEREE.SSNIN(SELECTESSN FROMDEPENDENT WHEREESSN = E.SSN ANDE.FNAME = DEPENDENT_NAME ANDSEX = E.SEX) Also ANY (SOME) and ALL in combination with comparison operators (>, >=, = and <>).

NOEA/IT FEN - Databases/SQL3 SQL2 - DML ( Q16): Sub queries (nested SELECTs) SELECTE.FNAME, E.LNAME FROMEMPLOYEE E WHEREE.SSN IN(SELECTESSN FROMDEPENDENT WHEREESSN = E.SSN ANDE.FNAME = DEPENDENT_NAME ANDSEX = E.SEX) For each row in outer table (E), the inner SELECT is executed. If E.SSN is contained in the result of the inner SELECT, then E is included in the result table for the outer SELECT.

NOEA/IT FEN - Databases/SQL4 SQL2 - DML (Q7): Existential quantifier - EXISTS: SELECTFNAME, LNAME FROMEMPLOYEE WHEREEXISTS (SELECT* FROMDEPENDENT WHERESSN = ESSN) AND EXISTS (SELECT* FROMDEPARTMENT WHERESSN = MGRSSN)

NOEA/IT FEN - Databases/SQL5 SQL2 - DML (Q6): NOT EXISTS: SELECTFNAME, LNAME FROMEMPLOYEE WHERENOT EXISTS(SELECT* FROMDEPENDENT WHERESSN =ESSN)

NOEA/IT FEN - Databases/SQL6 SQL2 - DML For All i SQL Although SQL is supposed to be an implementation of first order predicate logic, it does not support the universal qualifier (FORALL), only the existential quantifier (EXISTS) is supported. A well known (?) result from predicate logic can be used in a workaround: –Retrieving all elements satisfying some predicate is equivalent to retrieving elements that are not in the set of elements that do not satisfy the predicate: SELECT * FROM --- WHERE NOT EXISTS (SELECT * FROM --- WHERE NOT EXISTS --- -

NOEA/IT FEN - Databases/SQL7 Let x be an arbitrary element in some set and p a predicate stating some condition on x: De Morgan’s Law:  (  x: p(x))   x:  p(x) Apply to  p(x):  (  x:  p(x))   x:  (  p(x)) Reduce the right hand side:  x: p(x)   (  x:  p(x)) “it is not true that there exists x, so p(x) is not true” – that is: “p is true for all x” SELECT * FROM --- WHERE NOT EXISTS (SELECT * FROM --- WHERE NOT EXISTS ---- A Side: Predicate Logic

NOEA/IT FEN - Databases/SQL8 SQL2 - DML (Q3B): ”Retrieve the name of each employee who works on all projects controlled by department number 5” SELECTLNAME, FNAME FROMEMPLOYEE WHERENOT EXISTS (SELECT* FROMWORKS_ON B WHERE(B.PNO IN(SELECTPNUMBER FROMPROJECT WHEREDNUM = 5)) AND NOT EXISTS(SELECT* FROMWORKS_ON C WHEREC.ESSN = SSN AND C.PNO=B.PNO))

NOEA/IT FEN - Databases/SQL9 SQL2 – DML: SELECT Queries: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] [...]:WHERE, GROUP BY, HAVING and ORDER BY may be omitted.

NOEA/IT FEN - Databases/SQL10 SQL2 - DML SELECT DISTINCTESSN FROMWORKS_ON WHEREPNO IN (1,2,3) SELECTFNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL

NOEA/IT FEN - Databases/SQL11 SQL2 - DML SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.SUPERSSN = S.SSN New coulomb names in the resulting table. AS may be omitted in FROM part.

NOEA/IT FEN - Databases/SQL12 SQL2 - DML Alternative notations for join: SELECTFNAME, LNAME, ADDRESS FROM(EMPLOYEE JOIN DEPARTMENT ON DNO=DNUMBER) WHEREDNAME = ’Research’ Provides a more clear syntax and opens for more specialised joins.

NOEA/IT FEN - Databases/SQL13 SQL2 - DML Natural join (not MS SQL Server): (Q1B): SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN (DEPARTMENT AS DEPT (DNAME,DNO,MSSN,MSDATE))) WHERE DNAME = ’Research’ DEPARTMENT.DNUMBER must be rename to DNO in order to match EMPLOYEE.DNO. Natural join is over two attributes with the same name (EMPLOYEE.DNO = DEPT.DNO).

NOEA/IT FEN - Databases/SQL14 SQL2 - DML Outer join: SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.SUPERSSN = S.SSN Retrieves only employees who have a supervisor. Left Outer Join retrieves all employees and inserts NULL in the S- attributes for employees with no supervisor. SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROM(EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN) Also RIGTH OUTER JOIN and FULL OUTER JOIN.

NOEA/IT FEN - Databases/SQL15 SQL2 - DML

NOEA/IT FEN - Databases/SQL16 What about employees with no supervisor?

NOEA/IT FEN - Databases/SQL17 Here they are!

NOEA/IT FEN - Databases/SQL18 SQL2 - DML

NOEA/IT FEN - Databases/SQL19 SQL2 - DML

NOEA/IT FEN - Databases/SQL20 SQL2 - DML Also: –CROSS JOIN (Cartesian Product) –UNION JOIN SQL2 provides many different ways of expressing the same join: –This can be view as an advantage: More simple expressions –Or as an disadvantage: More complicated language

NOEA/IT FEN - Databases/SQL21 SQL2 - DML Aggregate Functions: –COUNT –SUM –MAX –MIN –AVG

NOEA/IT FEN - Databases/SQL22 SQL2 - DML Ex.: ”Number of Employees in the research department” SELECTCOUNT(*) FROMEMPLOYEE, DEPARTMENT WHEREDNO = DNUMBER AND DNAME = ’Research’

NOEA/IT FEN - Databases/SQL23 SQL2 - DML (Q24)

NOEA/IT FEN - Databases/SQL24 Result of Q24

NOEA/IT FEN - Databases/SQL25 SQL2 - DML (Q26)

NOEA/IT FEN - Databases/SQL26 Result of Q26, 1

NOEA/IT FEN - Databases/SQL27 Result of Q26, 2

NOEA/IT FEN - Databases/SQL28 SQL2 - DML

NOEA/IT FEN - Databases/SQL29 SQL - VIEWS A view is virtual table which is created from one or more existing base tables. Views may be used in a layered architecture to provide different view of the database to different users. May also be used to increase efficiency of frequent queries, for instance to avoid JOINs.

NOEA/IT FEN - Databases/SQL30 SQL - VIEWS CREATE VIEWWORKS_ON1 AS SELECTFNAME, LNAME, PNAME, HOURS FROMEMPLOYEE, PROJECT, WORKS_ON WHERESSN=ESSN AND PNO=PNUMBER; Using this view, the query: SELECTFNAME, LNAME, PNAME FROMEMPLOYEE, PROJECT, WORKS_ON WHEREPNAME = 'ProductX' AND SSN = ESSN AND PNO = PNUMBER; May written as SELECTFNAME, LNAME, PNAME FROMWORKS_ON1 WHEREPNAME = 'ProductX'; And hence saving the join

NOEA/IT FEN - Databases/SQL31 SQL - VIEWS Updating through views is problematic: FX: Transfer John Smith from the project 'ProductX' to the project 'ProductY’ UPDATEWORKS_ON1 SETPNAME = ’ProductY’ WHERELNAME = ’Smith’ ANDFNAME = ’John’ ANDPNAME = ’ProductX’

NOEA/IT FEN - Databases/SQL32 SQL - VIEWS Which update of the base tables should be executed? This? Or this?

NOEA/IT FEN - Databases/SQL33 SQL - VIEWS Views and update: Updatable views Gray zone Not updatable views

NOEA/IT FEN - Databases/SQL34 SQL - VIEWS Generally: Views defined over one base table can be updated, if the primary key (ore some candidate key) is included in the view. Views defined by joining more base tables are generally not updatable. Some joined view are in principle updatable: all primary keys from the base tables must be included in the view. Views defined using aggregate or grouping functions are not updatable. SQL2 standard establishes that joined view are not updatable.

NOEA/IT FEN - Databases/SQL35 Index Index is often considered part of the DDL of SQL –index is about internal physical storage access, and –as such NOT a part of the SQL2 standard –many SQL versions include index anyway Previous versions of SQL includes a CREATE INDEX statement –Is used for speeding up queries, for instance: CREATE INDEX LNAME_INDEXON EMPLOYEE(LNAME); –Index is in some older SQL implementations also used to simulate primary keys: CREATE UNIQUE INDEX SSN_INDEX ON EMPLOYEE(SSN);

NOEA/IT FEN - Databases/SQL36 Index –Often it is possible to define one (and only one) clustering index: CREATE INDEX DNO_INDEX ONEMPLOYEE(DNO) CLUSTER; –The effect is that employees in the same department are stored physically close –Hereby queries concerning employees in the same department are speeded up. Why is that?

NOEA/IT FEN - Databases/SQL37 Databaser (SQL) og programudviklingsværktøjer ODBC/JDBC giver standardiseret tilgang til RDBMS

NOEA/IT FEN - Databases/SQL38 Databaseprogrammering fra 3GLs og OOPLs SQL og typiske programmeringssprog som Java, C, Cobol etc. er ikke ”kompatible”: Impedans-mismatch: –Programmeringssprogets datatyper er ikke afstemt med databasens domæner –SQL er mængde- (tabel-) orienteret: alle resultater er en tabel med et antal tupler. 3GLs og OOPLs er tuple- (objekt-)orienteret, dvs. der er behov for en datastruktur i programmeringssproget

NOEA/IT FEN - Databases/SQL39 Embedded SQL Databaseaccess vha. SQL fra 3GL/OOPL (værtssprog): Er baseret Cursor-begrebet: En cursor kan opfattes som en pointer, der udpeger aktiv række i tabellen, som SQL-sætningen resulterer i En cursor er nødvendig, når resultatet af en SQL-sætning er mere end een tuple (værtssproget er record- (objekt-) orienteret) Er leverandørafhængig Visse moderne værktøjer (fx Visual Studio.NET) kan tilsyneladende returnere en mængde af forekomster (dataset eller ResultSet, men reelt er der tale om en indpakket cursor ODBC (Open Data Base Connectivity): Resultset svarer til en cursor. I OOPLs kaldes en cursor ofte en iterator (Enumerator i C#).