1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

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.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
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. Selection SELECT * FROM r WHERE A=B AND D>5.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
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 Slide 8- 1.
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.
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.
CS 380 Introduction to Database Systems (Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views)
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
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.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: 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.
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.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
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.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Introduction to Database Systems
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.
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 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
The SQL Database Grammar
6/22/2018.
CS580 Advanced Database Topics
Retrieval Queries in SQL(DML)
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
11/9/2018.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
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
Presentation transcript:

1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook

2 Review SQL DDL DML CREATEDROPALTER UPDATERETRIEVAL INSERTDELETEUPDATE SELECT FROM WHERE GROUP BY HAVING ORDER BY

3 SQL DML (Updates) l There are three SQL commands to update the database state –INSERT –DELETE –UPDATE

4 COMPANY Database Schema

5 INSERT l Add one or more tuples to a relation –Attribute values must be listed in the same order as the attributes specified in the CREATE TABLE command INSERT INTO EMPLOYEE VALUES ('Richard','K','Marini',' ','30-DEC-52', '98 Oak Forest,Katy,TX','M',37000,' ',4)

6 INSERT l An alternate form of INSERT specifies explicitly the attribute names that correspond to values in the new tuple –Attributes with NULL values can be left out INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES ('Richard', 'Marini', ' ')

7 Disable Foreign Key in MySQL l Set foreign_key_checks = 0; l Useful to insert a tuple for subordinate before inserting the tuple for supervisor INSERT INTO EMPLOYEE (FNAME, LNAME, SSN, SUPER_SSN) VALUES ('Rob', ‘Stanley', ' ', ' ')

8 INSERT l Insertion of multiple tuples resulting from a query into a relation –Example: Suppose we want to create a temporary table that has the name, number of employees, and total salaries for each department. CREATE TABLE DEPTS_INFO (DEPT_NAMEVARCHAR(10), NO_OF_EMPSINTEGER, TOTAL_SALINTEGER); INSERT INTO DEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) SELECTDNAME, COUNT (*), SUM (SALARY) FROMDEPARTMENT, EMPLOYEE WHEREDNUMBER=DNO GROUP BYDNAME ;

9 DELETE l Removes tuples from a relation –Includes a WHERE-clause to select the tuples to be deleted l Examples: DELETE FROM EMPLOYEE WHERELname='Brown’; DELETE FROM EMPLOYEE WHERESSN=' ’; DELETE FROM EMPLOYEE WHEREDno IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname='Research'); DELETE FROM EMPLOYEE;

10 UPDATE l Used to modify attribute values of one or more selected tuples UPDATE table_name SETset-clause WHERE where-clause –WHERE-clause selects the tuples to be modified –SET-clause specifies the attributes to be modified and their new values –Each command modifies tuples in the same relation

1 UPDATE l Example: Change the location and controlling department number of project number 10 to 'Bellaire' and 5, respectively UPDATE PROJECT SET PLOCATION = 'Bellaire', DNUM = 5 WHERE PNUMBER = 10

12 UPDATE Example: Give all employees in the 'Research' department a 10% raise in salary UPDATE EMPLOYEE SETSALARY = SALARY *1.1 WHEREDNO IN (SELECTDNUMBER FROMDEPARTMENT WHEREDNAME='Research')

13 Retrieval Queries in SQL l Basic form of the SQL retrieval queries: SELECT FROM WHERE – is a list of attribute names whose values are to be retrieved by the query – is a list of the relation names required to process the query – is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query

14 Simple SQL Queries (from 1 Table) l Query: Retrieve the birthdate and address of the employee 'John B. Smith‘ SELECT FROM EMPLOYEE WHERE

15 Simple SQL Queries (from 1 Table) l Query: Retrieve the birthdate and address of the employee 'John B. Smith‘ SELECT Bdate, Address FROM EMPLOYEE WHERE

16 Simple SQL Queries (from 1 Table) l Query: Retrieve the birthdate and address of the employee 'John B. Smith‘ SELECT Bdate, Address FROM EMPLOYEE WHERE Fname='John' AND Minit='B' AND Lname='Smith';

17 Simple SQL Queries (from 1 Table) SELECT Bdate, Address FROM EMPLOYEE WHERE Fname='John' AND Minit='B' AND Lname='Smith'; For each row in Employee table If row.Fname='John' AND row.Minit='B' AND row.Lname='Smith' then print row.Bdate, row.Address Another way to interpret this:

18 Simple SQL Queries (from 1 Table) l Query: Retrieve the name and address of employees who work for department number 5 SELECT Fname, Lname, Address FROM Employee WHERE Dno = 5;

19 Simple SQL Queries (from 1 Table) l Query: Retrieve all the rows and columns in the Employee table SELECT * FROM Employee; Wildcard (*) in the SELECT clause means retrieve all columns No WHERE clause means all the rows will be retrieved

20 Simple SQL Queries (from 2 Tables) l Query: Retrieve the first name, last name and address of all employees who work for the 'Research' department

21 Join Operation l SQL uses JOIN operation to combine information from two or more tables IDName 1John 2Mary 3Bob IDDegree 1BS 2 2MS 3 R.IDS.IDR.NameS.Degree 11JohnBS 22MaryBS 22MaryMS 33BobMS Join on R. ID = S.ID R S

2 Join Operation IDName 1John 2Mary 3Bob IDDegree 1BS 2 2MS 3 R.IDS.IDR.NameS.Degree 11JohnBS 22MaryBS 22MaryMS 33BobMS Join on R. ID = S.ID R S In this case, a row in R is “merged” with a row in S if their IDs are the same SELECT * FROM R, S WHERE R.ID = S.ID

23 Simple SQL Queries (from 2 Tables) l Query: Retrieve the first name, last name and address of all employees who work for the 'Research' department SELECT FROM Employee, Department WHERE

24 Simple SQL Queries (from 2 Tables) l Query: Retrieve the first name, last name and address of all employees who work for the 'Research' department SELECT Fname, Lname, Address FROM Employee, Department WHERE

25 Simple SQL Queries (from 2 Tables) l Query: Retrieve the first name, last name and address of all employees who work for the 'Research' department SELECT Fname, Lname, Address FROM Employee, Department WHERE Dname='Research'

26 Simple SQL Queries (from 2 Tables) l Query: Retrieve the first name, last name and address of all employees who work for the 'Research' department SELECT Fname, Lname, Address FROM Employee, Department WHERE Dname='Research' AND Dnumber=Dno;

27 Simple SQL Queries (from 2 Tables) l Query: Retrieve the name of each project and the name of the department that controls it SELECT FROM Department, Project WHERE

28 Simple SQL Queries (from 2 Tables) l Query: Retrieve the name of each project and the name of the department that controls it SELECT Pname, Dname FROM Department, Project WHERE

29 Simple SQL Queries (from 2 Tables) l Query: Retrieve the name of each project and the name of the department that controls it SELECT Pname, Dname FROM Department, Project WHERE Dnum=Dnumber;

30 Exercise List the names of all employees and their corresponding department names

31 Exercise List the names of managers for each department

32 Simple SQL Queries (from 3 Tables) l Query: For every project located in 'Stafford', list the project number, controlling department number, and the department manager's last name, address, and birthdate SELECT FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE

3 Simple SQL Queries (from 3 Tables) l Query: For every project located in 'Stafford', list the project number, controlling department number, and the department manager's last name, address, and birthdate SELECTPNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE

34 Simple SQL Queries (from 3 Tables) l Query: For every project located in 'Stafford', list the project number, controlling department number, and the department manager's last name, address, and birthdate SELECTPNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE PLOCATION='Stafford'

35 Simple SQL Queries (from 3 Tables) l Query: For every project located in 'Stafford', list the project number, controlling department number, and the department manager's last name, address, and birthdate SELECTPNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE PLOCATION='Stafford‘ AND DNUM=DNUMBER AND MGRSSN=SSN;

36 Aliases l If a query refers to two or more attributes with the same name but in different relations, we must qualify the attribute name with the relation name by prefixing the relation name to the attribute name IDName 1John 2Mary 3Bob IDDegree 1BS 2 2MS 3 R.IDS.IDR.NameS.Degree 11JohnBS 22MaryBS 22MaryMS 33BobMS R S SELECT * FROM R, S WHERE R.ID = S.ID What if we want to join the same table? Use tuple variables

37 Tuple Variables l Query: For each employee, retrieve the employee's name and the name of his or her immediate supervisor SELECT FROM EMPLOYEE E, EMPLOYEE S WHERE  E and S are tuple variables

38 Tuple Variables l Query: For each employee, retrieve the employee's name and the name of his or her immediate supervisor SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHERE  E and S are tuple variables

39 Tuple Variables l Query: For each employee, retrieve the employee's name and the name of his or her immediate supervisor SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHEREE.SuperSSN = S.SSN  E and S are tuple variables

40 Exercise l Query: Find the names of employees who earn more than their supervisors SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, Employee S WHERE E.SUPERSSN=S.SSN AND E.SALARY > S.SALARY;

41 Find the names of the department where John Smith is currently working. SELECT DNAME FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER and FNAME=‘John’ and LNAME=‘Smith’ Find names of the managers earning more than 100K SELECT FNAME, LNAME FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER and MGRSSN=SSN and SALARY> Find names of employees worked on multiple (at least 2) projects. SELECT name FROM EMPLOYEE A, EMPLOEE B, DEPARTMENT C, DEPARTMENT D WHERE A.SSN = B.SSN AND C.DNUM not= D.DNUM OR SELECT FNAME, LNAME FROM EMPLOYEE WHERE (SELECT COUNT(*) FROM PROJECT WHERE DNO=DNUM) >= 2 A MORE GENERAL METHOD LATER