Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems More SQL Database Design -- More SQL1.
Advanced SQL SMSU Computer Services Short Course.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
Oracle Database Administration Lecture 2 SQL language.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
SQL (DDL & DML Commands)
Program with PL/SQL. Interacting with the Oracle Server.
SQL FUNDAMENTALS SQL ( Structured Query Language )
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Technology Jing Shen.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
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.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
(SQL - Structured Query Language)
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Introduction to Structured Query Language (SQL) By Techandmate.comTechandmate.com Learn SQL Server With US.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
More SQL: Complex Queries,
Interacting with the Oracle Server
Interacting with the Oracle Server
Introduction to Database Systems, CS420
Database Management Systems 2
Interacting with the Oracle Server
Writing Correlated Subqueries
David M. Kroenke and David J
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
SQL Subquery.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
CSC 453 Database Systems Lecture
Manipulating Data Lesson 3.
Stored Procedure Language
Presentation transcript:

Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals (Exam 730) Classroom Resources Part 4: Working with DB2 data

© Copyright IBM Corporation 2008 Unit objectives After completing this unit, you should be able to: Identify the fundamentals of SQL, with a focus on SQL language elements Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage transactions, and know what constitutes a transaction boundary Create and call SQL procedures or invoke user-defined functions from the command line

© Copyright IBM Corporation 2008 Certification Exam (730) objectives Given a DML SQL statement, knowledge to identify results Ability to use SQL to SELECT data from tables Ability to use SQL to SORT or GROUP data Ability to use SQL to UPDATE, DELETE, or INSERT data Knowledge of transactions (that is, commit/rollback and transaction boundaries) Ability to call a procedure or invoke a user-defined function

© Copyright IBM Corporation 2008 The parts of speech of SQL SQL is a language that is used to define and manipulate database objects. There are several types of SQL: –DDL: Data Definition Language –DML: Data Manipulation Language –DCL: Data Control Language –Use SQL to: Define a database table (DDL) Insert data into the table (DML) Change the data in the table (DML) Retrieve data from the table (DML) Change security on database objects (DCL) In this unit, we will concentrate on DML.

© Copyright IBM Corporation 2008 Types of SQL statements Most SQL statements contain one or more of the following language elements: –Single-byte characters can be a letter, a digit (0-9), or a special character. –A token is a sequence of one or more characters. –An SQL identifier is a token that is used to form a name. –The data type of a value determines how DB2 interprets that value. –A constant specifies a value. –A special register is a storage area that is defined for an application process by the database manager and is used to store information that can be referenced in SQL statements. –A routine can be a function, a method, or a procedure. A function is a relationship between one or more input data values and one or more result values. A method is also a relationship between a set of input data values and a set of result values. However, database methods are defined, either implicitly or explicitly, as part of the definition of a user-defined structured type. A procedure is an application program that can be started by executing a CALL statement. –An expression specifies a value. –A predicate specifies a condition that is true, false, or unknown about a given row or group.

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – SELECT The SELECT statement is used to retrieve table or view data. In its simplest form, the SELECT statement can be used to retrieve all the data in a table. "SELECT * FROM staff"; SELECT * FROM staff FETCH FIRST 10 ROWS ONLY; SELECT name, salary FROM staff; SELECT DISTINCT dept, job FROM staff; SELECT name, salary + comm AS pay FROM staff;

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – WHERE Use the WHERE clause to select specific rows from a table or view by specifying one or more selection criteria, or search conditions. "SELECT name, salary FROM staff WHERE salary > 20000"; "SELECT name, job, salary FROM staff WHERE job <> 'Mgr ‘ AND name LIKE 'S%' AND salary > 20000"; Subquery "SELECT lastname FROM employee WHERE lastname IN (SELECT sales_person FROM sales WHERE sales_date < '01/01/1996')"; "SELECT e.salary FROM employee e WHERE e.salary < (SELECT AVG(s.salary) FROM staff s)";

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – ORDER BY Use the ORDER BY clause to sort the result set by values in one or more columns. "SELECT name, salary FROM staff WHERE salary > ORDER BY salary";

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – Joins A join is a query that combines data from two or more tables. SELECT deptnumb, deptname, manager, id, name, dept, job FROM org, staff; SELECT deptnumb, deptname, id AS manager_id, name AS manager FROM org, staff WHERE manager = id ORDER BY deptnumb; –Inner joins return only rows from the cross product that meet the join condition. –Outer joins return rows that are generated by an inner join operation, plus rows that would not be returned by the inner join operation. Left outer join Right outer join Full outer join

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – Set operators Use set operators to combine two or more queries into a single query –The UNION set operator generates a result table by combining two or more other result tables. –The EXCEPT set operator generates a result table by including all rows that are returned by the first query, but not by the second or any subsequent queries. –The INTERSECT set operator generates a result table by including only rows that are returned by all the queries. "SELECT sales_person FROM sales WHERE region = 'Ontario-South' UNION SELECT sales_person FROM sales WHERE sales > 3";

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – GROUP BY Use the GROUP BY clause to organize rows in a result set. SELECT sales_date, MAX(sales) AS max_sales FROM sales GROUP BY sales_date; Grouping sets can be used to analyze data at different levels of aggregation in a single pass (not available in DB2 z/OS). SELECT YEAR(sales_date) AS year, region, SUM(sales) AS tot_sales FROM sales GROUP BY GROUPING SETS (YEAR(sales_date), region, () ); The HAVING clause is often used with a GROUP BY clause to retrieve results for groups that satisfy only a specific condition. SELECT sales_person, SUM(sales) AS total_sales FROM sales GROUP BY sales_person HAVING SUM(sales) > 25;

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – INSERT The INSERT statement is used to add new rows to a table or a view. INSERT INTO staff VALUES (1212,'Cerny',20,'Sales',3, , ); INSERT INTO staff VALUES (1213,'Wolfrum',20,'Sales',2, , ); –Equivalent (not available on DB2 z/OS): INSERT INTO staff (id, name, dept, job, years, salary, comm) VALUES (1212,'Cerny',20,'Sales',3, , ), (1213,'Wolfrum',20,'Sales',2, , ); A fullselect is a statement that generates a result table. CREATE TABLE pers LIKE staff; INSERT INTO pers SELECT id, name, dept, job, years, salary, comm FROM staff WHERE dept = 38;

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – UPDATE The UPDATE statement is used to change the data in a table or a view. UPDATE staff SET dept = 51, salary = WHERE id = 750; Equivalent (not available on DB2 z/OS): UPDATE staff SET (dept, salary) = (51, 70000) WHERE id = 750; If you don't specify a WHERE clause, DB2 updates each row in the table or view!

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – DELETE The DELETE statement is used to delete entire rows of data from a table. DELETE FROM staff WHERE id IN (1212, 1213); If you don't specify a WHERE clause, DB2 deletes all the rows in the table or view!

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – MERGE Use the MERGE statement to combine conditional update, insert, or delete operations MERGE INTO employee AS e USING (SELECT empno, firstnme, midinit, lastname, workdept, phoneno, hiredate, job, edlevel, sex, birthdate, salary FROM my_emp) AS m ON e.empno = m.empno WHEN MATCHED THEN UPDATE SET (salary) = (m.salary) WHEN NOT MATCHED THEN INSERT (empno, firstnme, midinit, lastname, workdept, phoneno, hiredate, job, edlevel, sex, birthdate, salary) VALUES (m.empno, m.firstnme, m.midinit, m.lastname, m.workdept, m.phoneno, m.hiredate, m.job, m.edlevel, m.sex, m.birthdate, m.salary); Correlation names have been assigned to both the source and the target table to avoid ambiguous table references in the search condition.

© Copyright IBM Corporation 2008 Data Manipulation Language (DML) – Reference Use the data-change-table-reference clause to retrieve intermediate result sets in the same unit of work SELECT salary FROM OLD TABLE ( UPDATE employee SET salary = salary * 1.07 WHERE empno = '000220'); SALARY record(s) selected.

© Copyright IBM Corporation 2008 Units of work and savepoints A unit of work (UOW), also known as a transaction: –Is a recoverable sequence of operations within an application process. –Starts implicitly when the first SQL statement within an application process is issued against the database. All subsequent reads and writes by the same application process are considered part of the same UOW. The application ends the UOW by issuing either a COMMIT or a ROLLBACK statement A savepoint lets you selectively roll back a subset of actions that make up a UOW without losing the entire transaction. –You can nest savepoints and have several savepoint levels active at the same time; this allows your application to roll back to a specific savepoint, as necessary.

© Copyright IBM Corporation 2008 Creating and calling an SQL procedure An SQL procedure is: –A stored procedure whose body is written in SQL. The body contains the logic of the SQL procedure. –Created when you successfully invoke a CREATE PROCEDURE (SQL) statement CREATE PROCEDURE sales_status (IN quota INTEGER, OUT sql_state CHAR(5)) DYNAMIC RESULT SETS 1 LANGUAGE SQL BEGIN DECLARE SQLSTATE CHAR(5); DECLARE rs CURSOR WITH RETURN FOR SELECT sales_person, SUM(sales) AS total_sales FROM sales GROUP BY sales_person HAVING SUM(sales) > quota; OPEN rs; SET sql_state = SQLSTATE;

© Copyright IBM Corporation 2008 Creating and using SQL user-defined functions Create user-defined functions to extend the set of built-in DB2 functions. –Suppose that you needed a function that returns the area of a circle when the radius of that circle is specified as an argument to the function. CREATE FUNCTION ca (r DOUBLE) RETURNS DOUBLE LANGUAGE SQL CONTAINS SQL NO EXTERNAL ACTION DETERMINISTIC RETURN * (r * r); Example output: db2 SELECT ca(96.8) AS area FROM sysibm.sysdummy1 AREA E record(s) selected.

© Copyright IBM Corporation 2008 User-defined table function You can also create a user-defined table function, which takes zero or more input arguments and returns data as a table. –A table function can only be used in the FROM clause of an SQL statement. A function that returns the names and employee numbers of all employees that hold a specific job: CREATE FUNCTION jobemployees (job VARCHAR(8)) RETURNS TABLE ( empno CHAR(6), firstname VARCHAR(12), lastname VARCHAR(15)) LANGUAGE SQL READS SQL DATA NO EXTERNAL ACTION DETERMINISTIC RETURN SELECT empno, firstnme, lastname FROM employee WHERE employee.job = jobemployees.job;

© Copyright IBM Corporation 2008 Unit summary Having completed this unit, you should be able to: Identify the fundamentals of SQL, with a focus on SQL language elements Use DML to select, insert, update, or delete data Use COMMIT and ROLLBACK statements to manage transactions, and know what constitutes a transaction boundary Create and call SQL procedures or invoke user-defined functions from the command line