Database Programming Using Oracle 11g

Slides:



Advertisements
Similar presentations
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
Triggers.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
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.
Program with PL/SQL. Interacting with the Oracle Server.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
Subqueries.
Autonomous Transactions: Extending the Possibilities Michael Rosenblum Dulcian, Inc. April 14, 2008 Presentation #419.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
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.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic SQL These slides are licensed under.
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.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Interacting with the Oracle8 Server
Aggregating Data Using Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
SQL PL/SQL Presented by: Dr. Samir Tartir
Introduction to Database Systems, CS420
Aggregating Data Using Group Functions
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
كتابة الجمل التنفيذية في PL/SQL
Writing Correlated Subqueries
Part B Entity Relationship Diagram TITLE #* ID * title * description
(SQL) Aggregating Data Using Group Functions
DAYS OF THE WEEK.
جملة الاستعلام الأساسية
Working with Composite Datatypes
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
(SQL) Manipulating Data
PL/SQL week10.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Triggers.
Time 1.
Contact
Subqueries Schedule: Timing Topic 25 minutes Lecture
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
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
Database Programming Using Oracle 11g
Presentation transcript:

Database Programming Using Oracle 11g Conditional Control-1

Conditional Control-1 IF-Then Statement

Conditional Control-1 Online Help Material: http:// www.stackoverflow.com http://Community.oracle.com

Conditional Control-1 IF- Then Statement It is used to executed statement when condition is true. It is independent construct One End if per if condition

Conditional Control-1 begin If condition then Statement -1; End if; End;

Conditional Control-1 Implementing IF- Then Statement - I

Conditional Control-1 Write a PL/SQL block to insert a row in emp table if total number of employees in department # 20 are more less than 50 because as policy maximum number of employees can be 50 in any department Solution: Not Possible with SQL only. Need for control structure

Conditional Control-1 declare total number(10):=0; max_id number(10):=0; begin select count(*) into total from emp where deptno=20; select max (empno) into max_id from emp; dbms_output.put_line('Table is not empty'); insert into emp(empno, deptno) values (max_id+1,20); dbms_output.put_line('Row is inserted in Department - 20'); end if; end;

Conditional Control-1 if total <=50 then dbms_output.put_line('Table is not empty'); insert into emp(empno, deptno, ename) values (max_id+1,20, ‘Asim’); dbms_output.put_line('Row is inserted in Department - 20'); end if; end;

Conditional Control-1 Implementing IF- Then Statement-II

Conditional Control-1 Write a PL/SQL block to update the salary of all the employees if there are atleast 15 employees in the company and out of at least 5 have been attached with organization for more than 5 years then raise the salary of all the employee by 10% .

Conditional Control-1 declare total_emp number(10):=0; total number(10):=0; percentage real(10):=0.10; begin select count(months_between (sysdate, hiredate)) into total_emp from emp where months_between (sysdate, hiredate)>=60; -- return total number of employees in the company select count(*) into total from emp; if total_emp > 5 and total > 15 then dbms_output.put_line('In the If Condition'); update emp set sal = sal + sal*percentage ; dbms_output.put_line('Increment given to all the employees'); end if; end; dbms_output.put_line('Table is not empty'); insert into emp(empno, deptno) values (max_id+1,20); dbms_output.put_line('Row is inserted in Department - 20');

Conditional Control-1 select count(months_between (sysdate, hiredate)) into total_emp from emp where months_between (sysdate, hiredate)>=60; select count(*) into total from emp; if total_emp > 5 and total > 15 then update emp set sal = sal + sal*percentage ; dbms_output.put_line('Increment given’ ); end if;end;

Conditional Control-1 IF- Then- else Statement It is used to executed statement when condition is true. If condition is not true then there is an alternate path available One closing end if;

Conditional Control-1 begin If condition then Statement -1; Else Statement-1; End if; End;

Conditional Control-1 Implementing IF- Then-else Statement-I

Conditional Control-1 Write a PL/SQL block to check whether you are logged in as ‘APEX_PUBLIC_USER’ and if this is true then display success message and then check whether maximum salary has been achieved against designation of ANALYST or not. As company policy maximum salary which can be paid to Analyst is not more than 5000 otherwise display message not achieved.

Conditional Control-1 declare max_sal number(10):=0; designation varchar2(30):='ANALYST'; begin select max(sal) into max_sal from emp where job=designation; if user = 'APEX_PUBLIC_USER' and max_sal <=5000 then

Conditional Control-1 dbms_output.put_line ('Current Maximum Salary for ' || designation || ' is : ' || max_sal); dbms_output.put_line ('Maximum Salary not achieved'); else dbms_output.put_line ('Maximum achieved'); end if; end;

Conditional Control-1 Implementing IF- Then-else Statement-II

Conditional Control-1 Write a PL/SQL block to proceed as follow of emp = 7369: i. Salary > 3000 and less than 6000 and designation = ‘MANAGER’ Message: Senior Management ii. Not part of Management

Conditional Control-1 declare designation emp.job%type; salary emp.sal%type; begin select job , sal into designation, salary from emp where empno=7369; if designation =‘ANALYST' and salary > 3000 and salary < 6000 then

Conditional Control-1 dbms_output.put_line(‘Senior Management'); else dbms_output.put_line(‘Not Part of Management'); end if; end;

Conditional Control-1 ELSIF Statement It can be used with IF only Dependent construct One closing end if;

Conditional Control-1 begin If condition then Statement -1; Elsif condition then Statement-1; End if; -- Only one End if per if End;

Conditional Control-1 Implementing IF- Then-elsif Statement-I

Conditional Control-1 Write a PL/SQL block to proceed as follow of emp = 7369: i. Salary > 3000 and less than 6000 and designation = ‘MANAGER’ Message: Senior Management ii. Salary > 6000 and less than 9000 designation = ‘PRESIDENT’ Message: Executive Management iii. Not eligible

Conditional Control-1 declare designation emp.job%type; salary emp.sal%type; begin select job , sal into designation, salary from emp where empno=7369; if designation =‘PRESIDENT' and salary > 6000 and salary < 9000 then

Conditional Control-1 dbms_output.put_line('Executive Management'); elsif designation='MANAGER' and salary > 3000 and salary < 6000 then dbms_output.put_line('Senior Management'); else dbms_output.put_line('Not part of Management'); end if; end;

Conditional Control-1 Implementing IF- Then-elsif Statement-II

Conditional Control-1 Write a PL/SQL block to proceed display for any given date: If day is Saturday or Sunday then display ‘ Weekend’ If day is Monday or Tuesday then display ‘ Start of week’ If Day is Wednesay or Friday then display ‘ Toward end of week’

Conditional Control-1 DECLARE v_date DATE := TO_DATE('18-Dec-2013', 'DD-MON-YYYY'); v_day VARCHAR2(15); BEGIN v_day := RTRIM(TO_CHAR(v_date, 'DAY')); DBMS_OUTPUT.PUT_LINE ('Day of the Date: ' || ' 18-Dec-2012 is : ' || v_day);

Conditional Control-1 IF v_day IN ('SATURDAY', 'SUNDAY') THEN DBMS_OUTPUT.PUT_LINE (v_date||' falls on weekend'); ELSif v_day in ('MONDAY', ' TUESDAY') then DBMS_OUTPUT.PUT_LINE (v_date||' falls on start of week'); ELSif v_day in ('WEDNESDAY', ' FRIDAY') then

Conditional Control-1 DBMS_OUTPUT.PUT_LINE (v_date||' falls toward mid week'); END IF; DBMS_OUTPUT.PUT_LINE('Done...'); END;

Conditional Control-1 Nested IF Parent and child if conditions Child if will be executed if parent is true. One end if per if keyword

Conditional Control-1 Implementing Nested-IF -I

Conditional Control-1 DECLARE a number(3) := 100; b number(3) := 200; BEGIN IF( a between 0 and 100 ) THEN IF( b between 101 and 200 ) THEN dbms_output.put_line('Value of a is 100 and b is 200' );

Conditional Control-1 END IF; dbms_output.put_line('Exact value of a is : ' || a ); dbms_output.put_line('Exact value of b is : ' || b ); END;

Conditional Control-1 Questions to Practice: Write at least 3 different version of questions solution discussed. i. Write a PL/SQL block to check whether number of employess in Emp table are odd or even, if number of employees are odd then display message ‘ Odd number of employees else display ‘ Even number of employees’