Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming Using Oracle 11g

Similar presentations


Presentation on theme: "Database Programming Using Oracle 11g"— Presentation transcript:

1 Database Programming Using Oracle 11g
SQL in PL/SQL

2 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; /

3 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

4 SQL in PL/SQL Implementing Select into Syntax - I

5 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; /

6 SQL in PL/SQL Implementing Select into Syntax - II

7 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

8 SQL in PL/SQL Implementing Select into Syntax - III

9 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; /

10 SQL in PL/SQL Implementing Select into Syntax - IV

11 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

12 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; /

13 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.

14 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

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

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

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

18 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:

19 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;

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

21 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.

22 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;

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

24 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

25 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;

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

27 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

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

29 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;

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

31 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

32 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;

33 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

34 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

35 SQL in PL/SQL Implementing PL/SQL and Commit

36 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;

37 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

38 SQL in PL/SQL Implementing PL/SQL and Rollback

39 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;

40 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

41 SQL in PL/SQL Implementing PL/SQL and SavePoint

42 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;

43 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


Download ppt "Database Programming Using Oracle 11g"

Similar presentations


Ads by Google