SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.

Slides:



Advertisements
Similar presentations
CSC271 Database Systems Lecture # 11.
Advertisements

Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
© Pearson Education Limited, Chapter 3 SQL and QBE Transparencies.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
1 Minggu 4, Pertemuan 7 SQL: Data Manipulation Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
10/15/2012ISC239 Isabelle Bichindaritz1 SQL Queries.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Chapter 7 SQL: Data Manipulation Chapter#6 in the text book Pearson Education © 2009.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Chapter 5 SQL Data Manipulation Language Chapter 5 in Textbook.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 5 SQL: Data Manipulation. 2 Chapter - Objectives u Purpose and importance of SQL. u How to retrieve data from database using SELECT and: –Use.
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Chapter 8 Introduction to SQL
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Web Programming MySql JDBC Web Programming.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Chapter 6 SQL – Data Manipulation Pearson Education © 2014.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL and QBE Transparencies. ©Pearson Education 2009 Chapter 3 - Objectives Purpose and importance of SQL, the main language for querying relational databases.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
SQL Query Getting to the data ……..
Structured Query Language
Tahun : <<2007>> Versi : <<2/1>>
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Data Manipulation Language
SQL – Data Manipulation
Introduction to SQL Chapter 3.
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
© Pearson Education Limited, 2004
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
SQL – Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation Transparencies JOINS
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation Transparencies
Shelly Cashman: Microsoft Access 2016
Chapter Name SQL: Data Manipulation
Presentation transcript:

SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee

Introduction Structured Query Language (SQL) Writing an SQL Command Retrieving Data Building SQL Statements Performing Database Updates

What is SQL? SQL is an example of a transform-oriented language. A language designed to use relations to transform inputs into required outputs. Source: Database Systems Connolly/Begg

What is SQL? ISO SQL has two major components:  Data Definition Language (DDL)  Data Manipulation Language (DML) Source: Database Systems Connolly/Begg

Data Definition Language (DDL) Defining the database structure  Tables Controlling access to the data  What a user can legally access Source: Database Systems Connolly/Begg

Data Manipulation Language (DML) Retrieving Data  Query tables Updating Data  Populate tables Source: Database Systems Connolly/Begg

Writing SQL Commands SQL statement consists of reserved words and user-defined words  Reserved words are a fixed part of the SQL language and have a fixed meaning  User-defined words are made up by the user (according to syntax rules) Source: Database Systems Connolly/Begg

Reserved Words Are fixed part of the SQL language Have a fixed meaning Require exact spelling Kept on the same line Source: Database Systems Connolly/Begg

User-defined Words Are made up by the user Governed by a set of syntax rules Represent names of database objects such as:  Tables  Columns  Views  Indexes Source: Database Systems Connolly/Begg

Data Manipulation Select: query data in the database Insert: insert data into a table Update: updates data in a table Delete: delete data from a table Source: Database Systems Connolly/Begg

Literals Non-numeric data values must be enclosed in single quotes:  ’16 Holland Drive’  ‘CS157B’ Numeric data values must NOT be enclosed in single quotes:  6  Source: Database Systems Connolly/Begg

Writing SQL Commands Most components of an SQL statement are case insensitive, but one exception is that literal character data must be typed exactly as it appears in the database. Source: Database Systems Connolly/Begg

Simple Query Select specifies which columns are to appear in the output. From specifies the table(s) to be used. Where filters the rows subject to some condition(s). Source: Database Systems Connolly/Begg

Simple Query Group By forms groups of rows with the same column value. Having filters the groups subject to some condition. Order By specifies the order of the output. Source: Database Systems Connolly/Begg

Retrieve all columns and all rows SELECT firstColumn,…,lastColumn FROM tableName; SELECT * FROM tableName;

Use of Distinct SELECT DISTINCT columnName FROM tableName; columnName ABCDABCD AABBCDAABBCD

Calculated fields SELECT columnName/2 FROM tableName price price

Comparison Search Condition = equals is not equal to (ISO standard) != “ “ “ “ (allowed in some dialects) < is less than > is greater than <= is less than or equal to >= is greater than or equal to Source: Database Systems Connolly/Begg

Comparison Search Condition An expression is evaluated left to right. Subexpressions in brackets are evaluated first. NOTs are evaluated before ANDs and ORs. ANDs are evaluated before ORs. Source: Database Systems Connolly/Begg

Range Search Condition SELECT columnName FROM tableName WHERE columnName BETWEEN 20 AND 30; SELECT columnName FROM tableName WHERE columnName >= 20 AND columnName <= 30;

Set membership search condition SELECT columnName FROM tableName WHERE columnName IN (‘name1’, ‘name2’); SELECT columnName FROM tableName WHERE columnName = ‘name1’ OR columnName = ‘name2’;

Pattern matching symbols % represents any sequence of zero or more characters (wildcard). _ represents any single character Source: Database Systems Connolly/Begg

Pattern match search condition ‘h%’ : begins with the character h. ‘h_ _ _’ : four character string beginning with the character h. ‘%e’ : any sequence of characters, of length at least 1, ending with the character e. ‘%CS157B%’ : any sequence of characters of any length containing CS157B Source: Database Systems Connolly/Begg

Pattern match search condition LIKE ‘h%’ begins with the character h. NOT LIKE ‘h%’ does not begin with the character h. Source: Database Systems Connolly/Begg

Pattern match search condition To search a string that includes a pattern-matching character ‘15%’ Use an escape character to represent the pattern-matching character. LIKE ‘15#%’ ESCAPE ‘#’ Source: Database Systems Connolly/Begg

NULL search condition DOES NOT WORK comment = ‘ ’ comment != ‘ ’ DOES WORK comment IS NULL comment IS NOT NULL

Sorting The ORDER BY clause  consists of list of column identifiers that the result is to be sorted on, separated by commas.  Allows the retrieved rows to be ordered by ascending (ASC) or descending (DESC) order Source: Database Systems Connolly/Begg

Sorting Column identifier may be  A column name  A column number (deprecated) Source: Database Systems Connolly/Begg

Sorting SELECT type, rent FROM tableName ORDER BY type, rent ASC; Source: Database Systems Connolly/Begg typerent Apt Flat typerent Flat Apt Flat Apt

Aggregate Functions COUNT returns the number … SUM returns the sum … AVG returns the average … MIN returns the smallest … MAX returns the largest … value in a specified column. Source: Database Systems Connolly/Begg

Use of COUNT( * ) How many students in CS157B? SELECT COUNT( * ) AS my count FROM CS157B my count 40

GROUP BY clause When GROUP BY is used, each item in the SELECT list must be single-valued per group. The SELECT clause may contain only  Column names  Aggregate functions  Constants  An expression involving combinations of the above Source: Database Systems Connolly/Begg

Grouping SELECT dept, COUNT(staffNo) AS my count SUM(salary) FROM tableName GROUP BY dept ORDER BY dept deptmy count Salary ABCABC deptstaffNoSalary ABCABABCAB

Restricting Grouping HAVING clause  is with the GROUP BY clause.  filters groups into resulting table.  includes at least one aggregate function. WHERE clause  filters individual rows into resulting table.  Aggregate functions cannot be used. Source: Database Systems Connolly/Begg

SELECT dept, COUNT(staffNo) AS my count, SUM(salary) AS my sum FROM Staff GROUP BY dept HAVING COUNT(staffNo) > 1 ORDER BY dept; Source: Database Systems Connolly/Begg deptmy count my sum ABAB deptstaffNoSalary ABCABABCAB

Subqueries SELECT columnNameA FROM tableName1 WHERE columnNameB = (SELECT columnNameB FROM tableName2 WHERE condition); Source: Database Systems Connolly/Begg result from inner SELECT applied as a condition for the outer SELECT

Subquery with Aggregate Function SELECT fName, salary – ( SELECT AVG(salary) FROM Staff ) AS salDiff FROM Staff WHERE salary > ( SELECT AVG(salary) FROM Staff ); Source: Database Systems Connolly/Begg List all staff whose salary is greater than the average salary, show by how much their salary is greater than the average.

Nested Subqueries: Use of IN SELECT property FROM PropertyForRent WHERE staff IN( SELECT staff FROM Staff WHERE branch = ( SELECT branch FROM Branch WHERE street = ‘112 A St’)); Source: Database Systems Connolly/Begg Selects branch at 112 A St

Nested Subqueries: Use of IN SELECT property FROM PropertyForRent WHERE staff IN( SELECT staff FROM Staff WHERE branch = ( branch ) ); Source: Database Systems Connolly/Begg Select staff members who works at branch.

Nested Subqueries: Use of IN SELECT property FROM PropertyForRent WHERE staff IN( staffs who works at branch on ‘112 A St’); Source: Database Systems Connolly/Begg Since there are more than one row selected, “=“ cannot be used.

Use of ANY/SOME SELECT name, salary FROM Staff WHERE salary > SOME( SELECT salary FROM Staff WHERE branch = ‘A’ ); Source: Database Systems Connolly/Begg Result:{2000,3000,4000} Result: {list of staff with salary greater than 2000.}

Use of ALL SELECT name, salary FROM Staff WHERE salary > ALL( SELECT salary FROM Staff WHERE branch = ‘A’ ); Source: Database Systems Connolly/Begg Result:{2000,3000,4000} Result: {list of staff with salary greater than 4000.}

Use of Any/Some and All If the subquery is empty:  ALL returns true  ANY returns false ISO standard allows SOME to be used interchangeably with ANY. Source: Database Systems Connolly/Begg

Multi-Table Queries Join Inner Join Left Outer Join Right Outer Join Full Outer Join Source: Database Systems Connolly/Begg

Join SELECT client FROM Client c, View v WHERE c.client = v.client; Source: Database Systems Connolly/Begg FROM Client c JOIN View v ON c.client = v.client (creates two identical client columns) FROM Client JOIN View USING client FROM Client NATURAL JOIN View ISO standard Alternatives

Join The join operation combines data from two tables by forming pairs of related rows where the matching columns in each table have the same value. If one row of a table is unmatched, the row is omitted from the resulting table. Source: Database Systems Connolly/Begg

Inner Join Source: Database Systems Connolly/Begg SELECT b.*,p.* FROM Branch b, Property p WHERE b.bCity = p.pCity; branchbcity B003 B004 B002 Glasgow Bristol London propertypcity P103 P204 P102 Aberdeen London Glasgow

Inner Join Result Source: Database Systems Connolly/Begg SELECT b.*,p.* FROM Branch b, Property p WHERE b.bCity = p.pCity; branchbcity B003 B002 Glasgow London propertypcity P102 P204 Glasgow London

Left Outer Join SELECT b.*,p.* FROM Branch b LEFT JOIN Property p ON b.bCity = p.pCity; Source: Database Systems Connolly/Begg branchbcity B003 B004 B002 Glasgow Bristol London propertypcity P102 NULL P204 Glasgow NULL London

Right Outer Join SELECT b.*,p.* FROM Branch b RIGHT JOIN Property p ON b.bCity = p.pCity; Source: Database Systems Connolly/Begg propertypcity P103 P204 P102 Aberdeen London Glasgow branchbcity NULL B002 B003 NULL London Glasgow

Full Outer Join SELECT b.*,p.* FROM Branch b FULL JOIN Property p ON b.bCity = p.pCity; Source: Database Systems Connolly/Begg branchbcity B004 NULL B002 B003 Bristol NULL London Glasgow propertypcity NULL P103 P204 P102 NULL Aberdeen London Glasgow

Exists and Not Exists For use only with subqueries. Produces true/false results. Source: Database Systems Connolly/Begg

EXISTS returns true IFF there exists at least one row in the resulting table returned by the subquery Source: Database Systems Connolly/Begg NOT EXISTS returns false if the subquery is empty.

Exists and Not Exists SELECT staff FROM Staff s WHERE EXISTS( SELECT * FROM Branch b WHERE s.branch = b.branch AND city = ‘London’); Source: Database Systems Connolly/Begg Find all staff who work in a London branch office.

Combining Result Tables UNION INTERSECT EXCEPT Source: Database Systems Connolly/Begg R  SR  S R – S R S SS RR

Use of UNION ( SELECT city FROM Branch WHERE city IS NOT NULL) UNION ( SELECT city FROM Property WHERE city IS NOT NULL); Source: Database Systems Connolly/Begg city London Glasgow Aberdeen Bristol List of all cities where there is either a branch or property.

Use of INTERSECT ( SELECT city FROM Branch ) INTERSECT ( SELECT city FROM Property ); Source: Database Systems Connolly/Begg city London Glasgow Aberdeen List of all cities where there is both a branch and a property.

Use of EXCEPT ( SELECT city FROM Branch ) EXCEPT ( SELECT city FROM Property ); Source: Database Systems Connolly/Begg city Bristol List of all cities where there is a branch office but no properties.

Database Updates INSERT  Adds new rows of data to a table UPDATE  Modifies existing data in a table DELETE  Removes rows of data from a table Source: Database Systems Connolly/Begg

Use of INSERT INSERT INTO TableName[(columnList)] VALUES (dataValueList) Source: Database Systems Connolly/Begg

Use of UPDATE UPDATE TableName SET columnnName1 = dataValueList [, columnName2 = dataValue2…] [WHERE searchCondition] Source: Database Systems Connolly/Begg

Use of DELETE DELETE FROM TableName [WHERE searchCondition] Source: Database Systems Connolly/Begg