Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.

Slides:



Advertisements
Similar presentations
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
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.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
SQL: Schema Definition, Constraints, & Queries and Views.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2004 Pearson Education, Inc.. Lecture-7 Chapter 8 SQL: Schema Definition, Basic Constraints, and Queries.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2004 Pearson Education, Inc.. Lecture-7 Chapter 8 SQL: Schema Definition, Basic Constraints, and Queries.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
SQL SQL-99: a. Schema Definition b. Basic Constraints c. Queries.
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.
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.
Chapter 8 SQL-99: Schema Definition, Constraints, & Queries and Views.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Lecture # 25 Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views Muhammad Emran Database Systems.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
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,
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
11/9/2018.
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في ذلك. حيث المرجع الاساسي للمادة هي الكتاب المعتمد في وصف المقرر

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 2 Short Quiz Retrieve the name of each employee who has a dependent with the same first name as the employee.

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 3 CORRELATED NESTED QUERIES If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query, the two queries are said to be correlated Query 12: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q12: SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN = E.SSN AND E.FNAME = DEPENDENT_NAME)

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 4 CORRELATED NESTED QUERIES (contd.) A query written with nested SELECT... FROM... WHERE... blocks can be expressed as a single block query. For example, Q12 may be written as in Q12A Q12A: SELECT E.FNAME, E.LNAME FROM EMPLOYEE AS E, DEPENDENT AS D WHERE E.SSN = D.ESSN AND E.FNAME = D.DEPENDENT_NAME

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 5 THE EXISTS FUNCTION EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not The results of EXISTS is a Boolean value True or False EXIST returns TRUE if there is at least one tuple in the result of select ; otherwise it return FALSE NOT EXIST returns TRUE if there is no tuple in the result of select ; otherwise it return FALSE

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 6 THE EXISTS FUNCTION (contd.) Query 12: Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT E.FNAME, E.LNAME FROMEMPLOYEE As E WHEREEXISTS (SELECT * FROM DEPENDENT WHERE E.SSN=ESSN AND E. FNAME=DEPENDENT_NAME)

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 7 THE EXISTS FUNCTION (contd.) Query 6: Retrieve the names of employees who have no dependents. SELECT FNAME, LNAME FROM EMPLOYEE WHERENOT EXISTS ( SELECT * FROM DEPENDENT WHERE SSN=ESSN) the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 8 Joined Relations Feature Can specify a "joined relation" in the FROM- clause Looks like any other relation but is the result of a join Allows the user to specify different types of joins (JOIN, NATURAL JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, Full JOIN)

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 9 Joined Relations Feature Examples: Q1: SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO could be written as: Q1: SELECT FNAME, LNAME, ADDRESS FROM ( EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO ) WHERE DNAME='Research’ or as: Q1:SELECTFNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE) WHEREDNAME='Research’

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Slide Joined Relations Feature Query 8: For each employee, retrieve the employee's name, and the name of his or her immediate supervisor. Q8:SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE AS E, EMPLOYEE AS S WHEREE.SUPERSSN=S.SSN can be written as: Q8: SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN=S.SSN)

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe Substring Pattern Matching and Arithmetic Operators LIKE comparison operator Used for string pattern matching % replaces an arbitrary number of zero or more characters underscore (_) replaces a single character Standard arithmetic operators: Addition (+), subtraction (–), multiplication (*), and division (/) BETWEEN comparison operator

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 12 SUBSTRING COMPARISON The LIKE comparison operator is used to compare partial strings Two reserved characters are used: '%' replaces an arbitrary number of characters, and '_' replaces a single arbitrary character

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 13 SUBSTRING COMPARISON (contd.) Query 25: Retrieve all employees whose address is in Houston, Texas. Here, the value of the ADDRESS attribute must contain the substring 'Houston,TX‘ in it. Q25: SELECT FNAME, LNAME FROM EMPLOYEE WHEREADDRESS LIKE '%Houston,TX%'

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 14 SUBSTRING COMPARISON (contd.) Query 26: Retrieve all employees who were born during the 1950s. ( ) = Q26: SELECT FNAME, LNAME FROM EMPLOYEE WHERE BDATE LIKE ’ ’;

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 15 ARITHMETIC OPERATIONS Query 27: Show the effect of giving all employees who work on the 'ProductX' project a 10% raise. Q27: SELECT FNAME, LNAME, 1.1*SALARY FROM EMPLOYEE, WORKS_ON, PROJECT WHERE SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX’

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 16 Between Retrieve all employees in department 5 whose salary is between $30000 and $ SELECT * FROM EMPLOYEE WHERE (salary BETWEEN AND 40000) AND Dno=5

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 17 Specifying Updates in SQL There are three SQL commands to modify the database: INSERT DELETE UPDATE

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 18 INSERT it is used to add new tuples to a relation Attribute values should be listed in the same order as the attributes were specified in the CREATE TABLE command

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 19 INSERT (contd.) Example: U1:INSERT INTO EMPLOYEE VALUES ('Richard','K','Marini', ' ', '30-DEC-52', '98 Oak Forest,Katy,TX', 'M', 37000,' ', 4 ) An alternate form of INSERT specifies explicitly the attribute names that correspond to the values in the new tuple Attributes with NULL values can be left out Example: Insert a tuple for a new EMPLOYEE for whom we only know the FNAME, LNAME, and SSN attributes. U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES ('Richard', 'Marini', ' ')

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 20 INSERT (contd.) Another variation of INSERT allows insertion of multiple tuples resulting from a query into a relation

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 21 INSERT (contd.) Example: Suppose we want to create a temporary table that has the name, number of employees, and total salaries for each department. A table DEPTS_INFO is created by U3A, and is loaded with the summary information retrieved from the database by the query in U3B. U3A:CREATE TABLE DEPTS_INFO (DEPT_NAMEVARCHAR(10), NO_OF_EMPSINTEGER, TOTAL_SALINTEGER); U3B:INSERT INTODEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) SELECTDNAME, COUNT (*), SUM (SALARY) FROMDEPARTMENT, EMPLOYEE WHEREDNUMBER=DNO GROUP BYDNAME ;

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 22 INSERT (contd.) Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the DEPARTMENT or the EMPLOYEE relations after issuing U3B. We have to create a view (see later) to keep such a table up to date.

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The DELETE Command Removes tuples from a relation Includes a WHERE clause to select the tuples to be deleted

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe The UPDATE Command Modify attribute values of one or more selected tuples Additional SET clause in the UPDATE command Specifies attributes to be modified and new values

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 25 UPDATE (contd.) Example: Give all employees in the 'Research' department a 10% raise in salary. U6: UPDATE EMPLOYEE SET SALARY = SALARY *1.1 WHERE DNO IN ( SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research') In this request, the modified SALARY value depends on the original SALARY value in each tuple The reference to the SALARY attribute on the right of = refers to the old SALARY value before modification The reference to the SALARY attribute on the left of = refers to the new SALARY value after modification

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 26 Recap of SQL Queries A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] There are three SQL commands to modify the database: INSERT, DELETE, and UPDATE