Database Programming Using Oracle 11g

Slides:



Advertisements
Similar presentations
SQL2-ch2 管理綱要物件.
Advertisements

BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Triggers.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
Program with PL/SQL. Interacting with the Oracle Server.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
Autonomous Transactions: Extending the Possibilities Michael Rosenblum Dulcian, Inc. April 14, 2008 Presentation #419.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
SQL FUNDAMENTALS SQL ( Structured Query Language )
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
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.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
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.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Using SQL in PL/SQL ITEC 224 Database Programming.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
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.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
ITEC 224 Database Programming
Interacting with the Oracle8 Server
Creating Database Triggers
Interacting with the Oracle Server
Interacting with the Oracle Server
Manipulating Data.
Database Management Systems 2
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
Working with Composite Datatypes
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Data Control Language Grant, Revoke.
(SQL) Manipulating Data
Manipulating Data.
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Triggers in SQL’99 CS561.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Database Programming Using Oracle 11g SQL in PL/SQL

SQL in PL/SQL Declare Id number(10):=0; Current_date date; Begin Select 4982,sysdate into id, current_date From dual; dbms_output.put_line('Values are : ' || id || current_date); End; /

SQL in PL/SQL Select into Syntax Number of rows to be return from select can be one at max or 0 Need to have unique row check in where clause Number of column before and after into clause to be same

SQL in PL/SQL Implementing Select into Syntax - I

SQL in PL/SQL Declare Id number(10):=0; Current_date date; Begin Select 4982,sysdate into id, current_date From dual; dbms_output.put_line('Values are : ' || id || current_date); End; /

SQL in PL/SQL Implementing Select into Syntax - II

SQL in PL/SQL Declare Id number(10):=0; Hire_date date; Name emp.ename%type; Begin Select empno, ename, dob into id, name, hire_date from emp; dbms_output.put_line('Values are : ' || id || current_date); End; / Error: ORA-01422: exact fetch returns more than requested number of rows

SQL in PL/SQL Implementing Select into Syntax - III

SQL in PL/SQL Declare Id number(10):=0; date_hire date; Name emp.ename%type; Begin Select empno, ename, hiredate into id, name, date_hire from emp where empno=7369; dbms_output.put_line('Values are : ' || id || date_hire||name); End; /

SQL in PL/SQL Implementing Select into Syntax - IV

SQL in PL/SQL To Do Questions: Write a PL/SQL block to retrieve maximum salary of the employee who is not working in department no 10 but have been associated with organization for past 5 years

SQL in PL/SQL Solution: declare sal number(10):=0; begin select max(sal) into sal from emp where deptno!=10 and months_between (sysdate,hiredate)>=60; dbms_output.put_line('Maximum Salary is : ' || sal); end; /

SQL in PL/SQL To Do Question: Write a PL/SQL block to retrieve maximum, minumum comm of the employee who is not working as Analyst but belong to department having at least employees.

SQL in PL/SQL DML in PL/SQL It is possible to use insert, update and delete in PL/SQL There is no limitation on number of rows to be effected

SQL in PL/SQL Implementing DML into PL/SQL - I

SQL in PL/SQL Declare No number(10):=2; Begin Insert into emp(empno) values (no); dbms_output.put_line('Row Successfully added'); End;

SQL in PL/SQL Implementing DML and SQL in PL/SQL - I

SQL in PL/SQL Question: Write a PL/SQL block the set the salary of employee no 7369 increment by 25% of the maximum salary earned by any employee Practice Questions Website for SQL: https://www.hackerrank.com/domains/sql/select/difficulty/all/page/1

SQL in PL/SQL Declare No number(10):=2; salary emp.sal%type; Begin select max (sal) into salary from emp; update emp set sal = salary +salary*0.25 where empno=7369; dbms_output.put_line('Row Updated'); select sal into salary from emp where empno=7369; dbms_output.put_line('Updated Salary' || salary); End;

SQL in PL/SQL DML and SQL in PL/SQL - II

SQL in PL/SQL Question: Write a PL/SQL block to insert a new row in employee table (empno, ename) only and code should not violate primary key constraint assuming empno is PK and for ename any ‘ALLEN’ can be used.

SQL in PL/SQL declare max_no emp.empno%type; begin select max(empno) into max_no from emp; insert into emp (empno, ename) values (max_no+1, 'ALLEN'); dbms_output.put_line ('Row added'); end;

SQL in PL/SQL DML and SQL in PL/SQL - III

SQL in PL/SQL Question: Write a PL/SQL block to increase salary by 15% as retention bonus of all those employees who have been associated with company for more than 10 years

SQL in PL/SQL begin update emp set sal = sal*0.15 + sal where months_between (sysdate, hiredate)>120; dbms_output.put_line('Salary updated'); end;

SQL in PL/SQL PL/SQL and Sequence-I

SQL in PL/SQL Question: Write a PL/SQL block which should insert primary key in the table using sequence in consistent way without unique key violation. Assuming there is no data in table

SQL in PL/SQL create sequence emp_no start with 1 increment by 1 NoCache nocycle order ;

SQL in PL/SQL declare pid number(10):=0; new_id number(10):=0; begin new_id := pid + emp_no.nextval; dbms_output.put_line ('Updated Maxid is : ' || new_id); insert into product values (new_id, 'HD'); dbms_output.put_line('Id: ' || new_id || ' Successfully added'); end;

SQL in PL/SQL PL/SQL and Sequence-II

SQL in PL/SQL Question: Write a PL/SQL block which should insert primary key in the table using sequence in consistent way without unique key violation. Assuming there is existing data in primary key column

SQL in PL/SQL create sequence emp_no start with 1 increment by 1 NoCache nocycle order ; create table product (id number(10) primary key, pname varchar2(30)); insert into product values(1, 'HD'); select emp_no.nextval, emp_no.currval from dual;

SQL in PL/SQL declare pid number(10):=0; new_id number(10):=0; begin select max (id) into pid from product; dbms_output.put_line ('Maximum id : ' || pid); dbms_output.put_line ('Next Value of PK ' || ( pid + emp_no.nextval )); insert into product values (pid + emp_no.nextval, 'HD'); dbms_output.put_line( ' Row Successfully added'); end; - Error: There will be missing values, need for control structures

SQL in PL/SQL What is Commit Every DML (Insert, update, delete) is written to permanent storage after commit After commit changes are visible to all user. Execution of block leads to Autocommit

SQL in PL/SQL Implementing PL/SQL and Commit

SQL in PL/SQL declare pid number(10):=0; new_id number(10):=0; total_rows number(10):=0; begin select max (id) into pid from product; insert into product values (pid + emp_no.nextval, 'HD'); dbms_output.put_line( ' Row Successfully added'); Commit; select count (*) into total_rows from product; dbms_output.put_line( ' Total rows inserted : ' || total_rows); end;

SQL in PL/SQL What is Rollback Every DML (Insert, update, delete) can be undone if not committed Rollback before commit will undo all the changes till last commit or start of block

SQL in PL/SQL Implementing PL/SQL and Rollback

SQL in PL/SQL declare pid number(10):=0; new_id number(10):=0; total_rows number(10):=0; begin select max (id) into pid from product; insert into product values (pid + emp_no.nextval, 'HD'); dbms_output.put_line( ' Row Successfully added'); rollback; select count (*) into total_rows from product; dbms_output.put_line( ' Total rows inserted' || total_rows); end;

SQL in PL/SQL With SavePoint part of changes are undone What is SavePoint With SavePoint part of changes are undone Rollback is possible upto SavePoint

SQL in PL/SQL Implementing PL/SQL and SavePoint

SQL in PL/SQL declare pid number(10):=0; new_id number(10):=0; total_rows number(10):=0; begin select max (id) into pid from product; insert into product values (pid + emp_no.nextval, 'HD'); savepoint a;

SQL in PL/SQL insert into product values (pid + emp_no.nextval, 'HD'); savepoint B; rollback to A; select count (*) into total_rows from product; dbms_output.put_line( ' Total rows inserted : ' || total_rows); end; Note: Should be inserting 2 but due to savepoint one row is rollback