Beruflich Dokumente
Kultur Dokumente
INDEX
Sno. Lab no
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. LAB I LAB II LAB III LAB IV LAB V LAB VI (String Functions) LAB VII (SET operators and PL/SQL) LAB VIII LAB IX LAB X (Procedures and Functions) LAB XI (Triggers)
Date
8 August 2013 12 August 2013 26 August 2013 2 September 2013 12 September 2013 16 September 2013 17 September 2013 18 September 2013 23 September 2013 30 September 2013 14 October 2013
Page No
3-6 7-9 10-11 12-17 18-22 23-24 25-27 28-30 31-34 35-38 39-44
LAB I : 8 August 2013 1. Write SQL query to display all Employee who are clerks and salesman. SQL> SELECT * FROM Emp WHERE Job='CLERKS' OR Job='SALESMAN'; EMPNO ENAME DEPTNO JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ---------- -------------- ------------7499 ALLEN 7521 7654 7844 WARD MARTIN TURNER SALESMAN SALESMAN SALESMAN SALESMAN 7698 20-FEB-81 7698 22-FEB-81 1600 1250 1250 1500 300 500 1400 0 30 30 30 30
2. Write SQL query to display all the employees who are managers and earning greater than 2500. SQL> SELECT * from emp where job='MANAGER' and sal>2500; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ------------------7566 JONES 7698 BLAKE MANAGER MANAGER 7839 02-APR-81 7839 01-MAY-81 2975 2850 20 30
3. Write SQL query to display all employees who were hired in year 1981. SELECT * from Emp Where hiredate like '%81'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
------ ---------- --------- ---------- --------- ---------- ---------- ------ -----------------------------7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN SALESMAN SALESMAN MANAGER SALESMAN 7698 20-FEB-81 7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 1600 1250 2975 1250 1400 300 500 30 30 20 30
7698 BLAKE 7782 CLARK 7839 KING 7844 TURNER 7900 JAMES 7902 FORD
7839 01-MAY-81 7839 09-JUN-81 17-NOV-81 7698 08-SEP-81 7698 03-DEC-81 7566 03-DEC-81
30 10 10 30 30 20
10 rows selected.
4. Write SQL query to display the details of employees who are not working in department no 30 and 40. SQL> select * from emp where deptno not in (30,40); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
-------- ---------- --------- ---------- --------- ---------- ---------- ---------- ------------- ---------7369 SMITH 7566 JONES 7782 CLARK 7788 SCOTT 7839 KING 7876 ADAMS 7902 FORD 7934 MILLER 8 rows selected. CLERK MANAGER MANAGER ANALYST PRESIDENT CLERK ANALYST CLERK 7902 17-DEC-80 7839 02-APR-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81 7788 23-MAY-87 7566 03-DEC-81 7782 23-JAN-82 800 2975 2450 3000 5000 1100 3000 1300 20 20 10 20 10 20 20 10
5. Write SQL query to display details of employees in which s comes in their name. SQL> select * from emp where ename like '%S%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH 7566 JONES 7788 SCOTT 7876 ADAMS 7900 JAMES 5 rows selected.
7902 17-DEC-80 7839 02-APR-81 7566 19-APR-87 7788 23-MAY-87 7698 03-DEC-81
20 20 20 20 30
6. Write SQL query to Find the names of the employees in each department having highest salary? SQL> select ename from emp where sal in(select max(sal) from emp); ENAME ---------30 BLAKE
7. Write SQL query to Display all the employees who are managers and salesman and earning salary>2500. SQL> select * from emp where job = 'MANAGER' or job = 'SALESMAN' and sal>2500; EMPNO ENAME JOB CONTACTNO ADDRESS MGR HIREDATE SAL COMM DEPTNO
---------- ------------------------------ --------- ---------- -------------- -------------7369 SMITH 7566 JONES 7698 BLAKE 30 7782 CLARK 10 MANAGER MANAGER MANAGER MANAGER
30 20
8. Write SQL query to Display all the employees who are managers and earning salary >2500 as well as all the salesman SQL> select * from emp where job ='MANAGER' and sal>2500 or job= 'SALESMAN
MGR HIREDATE
SAL
COMM
DEPTNO
---------- ------------------------------ --------- ---------- -------------- -------------7369 SMITH 7499 ALLEN 30 7521 WARD 30 SALESMAN MANAGER SALESMAN
30
7698 22-FEB-81
1237.5
500
LAB II: 12 August 2013 1. Write SQL query to find out total salary of all the employees where job type is MANAGER SQL> select job,sum(sal) from emp where job='MANAGER' group by job; JOB SUM(SAL)
2. Write SQL query to find out total salary of all the employees where job type is either Manager of Clerk. SQL> select job,sum(sal) from emp where job='MANAGER' or job='CLERK' group by job; JOB SUM(SAL)
3. Write SQL query to Find the number of each job types in each department number SQL> select job,count(job) from emp group by job; JOB COUNT(JOB)
4. Write SQL Query to find the average salary of each department. SQL> select deptno,avg(sal) from emp where ename!='A%' group by deptno order by deptno asc; DEPTNO AVG(SAL) ---------- ---------10 2916.66667 20 2175
30 1566.66667
5. Find average salary for each department number and display only those department numbers whose average salary > 2500 SQL> select job,sum(sal) from emp group by job having sum(sal) >2500; JOB SUM(SAL)
--------- ---------CLERK SALESMAN PRESIDENT MANAGER ANALYST 4150 5600 5000 8275 6000a
6. Write SQL Query to find out the maximum salary in each department. Also display the name of the employee. SQL> select ename,deptno,sal from emp where sal in (select max(sal) from emp gro up by deptno) ;
ENAME
DEPTNO
SAL
20 10 20
7. Write SQL query to find the average sal of employees of each department where no of employee is greater than 3. SQL> select job,avg(sal) from emp group by job having count(*)>3; JOB AVG(SAL)
8. Write SQL query to count the no.of employee in each department. SQL> select deptno,count(job) from emp group by deptno; DEPTNO COUNT(JOB) ---------- ---------30 20 10 6 5 3
10
# Inserting into Table CLASS SQL> insert into class values(10,'Finance','D'); 1 row created. SQL> insert into class values(11,'Marketing','D'); 1 row created. SQL> insert into class values(12,'IT','C'); 1 row created.
# Creating Table Student SQL> create table student(rollno number(10) primary key,SName varchar2(35) unique, classno number(2) references class(classno)); Table created.
# Inserting into Table Student SQL> insert into student values(123,'Aman',10); 1 row created. SQL> insert into student values(124,'Harpreet',10); 1 row created. SQL> insert into student values(125,'Anchal',11); 1 row created.
11
# Deleting from table Student SQL> delete from student where classno=10; 2 rows deleted. SQL> delete from class where classno=10; 1 row deleted.
12
---------- ---------- --------- ---------- --------- ---------- ---------7369 SMITH 7499 ALLEN 7521 WARD CLERK SALESMAN SALESMAN 7902 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 800
1600 1250
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7566 JONES 7654 MARTIN 7698 BLAKE MANAGER SALESMAN MANAGER 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7782 CLARK 7788 SCOTT 7839 KING MANAGER ANALYST PRESIDENT 7839 09-JUN-81 7566 19-APR-87 17-NOV-81
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7844 TURNER SALESMAN 7698 08-SEP-81
13
CLERK CLERK
1100 950 30
20
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7902 FORD 7934 MILLER 14 rows selected. ANALYST CLERK 7566 03-DEC-81 7782 23-JAN-82
3000 1300
2. Write SQL Query to Change the width of the job attribute to 40 characters. SQL> alter table emp modify(JOB char(40)); Table altered.
3. Write SQL Query to Add a constraint unique on the attribute contact no. SQL> alter table emp add constraint un unique(contactno); Table altered.
4. Write SQL Quert to Drop the attribute contact no. SQL> alter table emp drop column contactno; Table altered.
5. Write SQL Query to Change the designation of all salesmen to Marketing Executives and give them a 20% salary hike. SQL> update emp set job='marketing executive',sal=1.2*sal where job='salesman'; 4 rows updated.
14
6. Write SQL Query to Decrease the salaries of all managers by 5%. SQL> update emp set sal=0.95*sal where job='manager'; SQL> SELECT * FROM EMP; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO ---------- ---------20 20-FEB-81 1920 300
---------- ---------- --------- ---------- --------- ---------- ---------7369 SMITH 7499 ALLEN 30 7521 WARD 30 CLERK 7902 17-DEC-80 800
MARKETING EXECUTIVE
7698 22-FEB-81
1500
500
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7566 JONES 7654 MARTIN 30 7698 BLAKE MANAGER 7839 02-APR-81
2826.25
MARKETING EXECUTIVE
7698 28-SEP-81
MANAGER
7839
01-MAY-81
2707.5
30
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7782 CLARK 7788 SCOTT 7839 KING MANAGER ANALYST PRESIDENT 7839 7566 09-JUN-81 19-APR-87 17-NOV-81
EMPNO ENAME
---------- ---------- --------- ---------- --------- ---------- ---------7844 TURNER 0 30 7876 ADAMS MARKETING EXECUTIVE 7698
CLERK 7788
23-MAY-87
1100
20
15
CLERK
7698
03-DEC-81
950
30
7. Write SQL Query to Change the designation of Scott to manager and a give him a 20% salary hike. Ans. SQL> update emp set job='manager',sal=1.2*sal where ename like 'scott'; 1 row updated.
8. Write SQL Query to display empno, dname and the location of each employee working. SQL> select empno,ename,loc,emp.deptno from emp,dept where emp.deptno=dept.deptno; EMPNO ENAME LOC DEPTNO
---------- ---------- ------------- ---------7782 CLARK 7839 KING 7934 MILLER 7566 JONES 7902 FORD 7876 ADAMS 7369 SMITH 7788 SCOTT 7521 WARD 7844 TURNER 7499 ALLEN NEW YORK NEW YORK NEW YORK DALLAS DALLAS DALLAS DALLAS DALLAS CHICAGO CHICAGO CHICAGO 20 20 20 20 20 30 30 30 10 10 10
EMPNO ENAME
LOC
DEPTNO
16
CHICAGO CHICAGO
30 30
14 rows selected.
9. Write SQL Query to display empno, salary and depttno for the employee WARD. SQL> select empno, sal,dname from emp,dept where emp.deptno=dept.deptno and enam e='WARD'; EMPNO SAL DNAME
10. Write SQL Query to display department name,empno, and salary for employees who are either Manager or Salesman. SQL> select dname,empno,sal from emp,dept where dept.deptno=emp.deptno and (job= 'MANAGER'or JOB='SALESMAN');
DNAME
EMPNO
SAL
-------------- ---------- ---------ACCOUNTING RESEARCH SALES SALES SALES SALES SALES 7 rows selected. 7782 7566 7844 7698 7521 7499 7654 2450 2975 1500 2850 1250 1600 1250
17
11. Write SQL Query to Display empno. Deptno and location of employee whose name is either SMITH or JONES. SQL> select ename,emp.deptno,loc from emp,dept where emp.deptno=dept.deptno and (ename='SMITH' or ename='JONES'); ENAME DEPTNO LOC
18
---------- --------- --------CLARK KING MILLER 09-JUN-81 07-SEP-81 17-NOV-81 15-FEB-82 23-JAN-82 23-APR-82
2. Write SQL Query to Display name, hire date and review date of employees of department 20 where review date is hire date + 6 months. SQL> select ename,hiredate,add_months(hiredate,6) from emp where deptno=20; ENAME HIREDATE ADD_MONTH
---------- --------- --------SMITH WARD JONES SCOTT ADAMS FORD 17-DEC-80 17-JUN-81 22-FEB-81 22-AUG-81 02-APR-81 02-OCT-81 19-APR-87 19-OCT-87 23-MAY-87 23-NOV-87 03-DEC-81 03-JUN-82
6 rows selected.
3. Write SQL Query to Display name, number of weeks employed for employees of department 30. SQL> select ename,hiredate, (sysdate-hiredate)/7 from emp where deptno=30;
19
ENAME
HIREDATE (SYSDATE-HIREDATE)/7
---------- --------- -------------------ALLEN MARTIN BLAKE TURNER JAMES 20-FEB-81 28-SEP-81 01-MAY-81 08-SEP-81 03-DEC-81 1698.92869 1667.50012 1688.92869 1670.35726 1658.07155
4. Write SQL Query to display the hiredate for all the employees of department 20 in the format 15th August 2013 SQL> select ename,hiredate,To_char((hiredate),'DD "of" MONTH,YYYY') from emp where deptno=20; ENAME HIREDATE TO_CHAR((HIREDATE),'
---------- --------- -------------------SMITH WARD JONES SCOTT ADAMS FORD 17-DEC-80 17 of DECEMBER ,1980 22-FEB-81 22 of FEBRUARY ,1981 02-APR-81 02 of APRIL ,1981 19-APR-87 19 of APRIL ,1987 23-MAY-87 23 of MAY ,1987
6 rows selected.
5. Write SQL Query to Produce the hiredate announcement for all employees in the format employee hired on May 20th, 1992 at 16:27 SQL> select ename,hiredate,To_char((hiredate),'"Employee hired on" Month DDTH,YY YY "at" HH:MI') from emp;
20
ENAME HIREDATE TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH ---------- --------- ---------------------------------------------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS 17-DEC-80 Employee hired on December 17TH,1980 at 12:00 20-FEB-81 Employee hired on February 20TH,1981 at 12:00 22-FEB-81 Employee hired on February 22ND,1981 at 12:00 02-APR-81 Employee hired on April 02ND,1981 at 12:00
28-SEP-81 Employee hired on September 28TH,1981 at 12:00 01-MAY-81 Employee hired on May 09-JUN-81 Employee hired on June 19-APR-87 Employee hired on April 01ST,1981 at 12:00 09TH,1981 at 12:00 19TH,1987 at 12:00
17-NOV-81 Employee hired on November 17TH,1981 at 12:00 08-SEP-81 Employee hired on September 08TH,1981 at 12:00 23-MAY-87 Employee hired on May 23RD,1987 at 12:00
ENAME HIREDATE TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH ---------- --------- ---------------------------------------------JAMES FORD MILLER 03-DEC-81 Employee hired on December 03RD,1981 at 12:00 03-DEC-81 Employee hired on December 03RD,1981 at 12:00 23-JAN-82 Employee hired on January 23RD,1982 at 12:00
14 rows selected.
6. Write a SQL Query to display the jobs of those employees who have salary 3500 as top management and rest employees as junior management. SQL> select ename,sal,decode(sal,1300,'Junior Manager',1800,'Middle Manager',300 0,'Senior Manager') from emp;
21
ENAME
SAL DECODE(SAL,130
---------- ---------- -------------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS 175.76 1920 3500 39.57 1500 379.05 325.85 3000 Senior Manager 5000 1800 Middle Manager 1100
ENAME
SAL DECODE(SAL,130
---------- ---------- -------------JAMES FORD MILLER 159.6 3000 Senior Manager 1300 Junior Manager
14 rows selected.
7. Write a SQL Query to convert a name character string into next character string. SQL> select ename, translate (ename,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','BCDEFGHIJKLMNOPQ RSTUVWXYZA')from emp;
22
ENAME
TRANSLATE(
---------- ---------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS TNJUI BMMFO XBSE KPOFT NBSUJO CMBLF DMBSL TDPUU LJOH UVSOFS BEBNT
ENAME
TRANSLATE(
14 rows selected.
23
------------------------- ------------------------- -------------------- -------------------FORBES OUTLOOK TIME CHARLES, JOHN RISHI, SHARMA RAGHURAM, RAJAN NEW YORK NEW DELHI JAIPUR 011-2334567 044-12234566
022-345686946
2. Write SQL Query to Display the name before the comma in the authors name. Ans. SQL> SELECT SUBSTR (ANAME,1,INSTR(ANAME,',',1,1)-1)"SUBSTRING" FROM MAGAZINE; SUBSTRING ------------------------CHARLES RISHI RAGHURAM
3. Write SQL Query to Display the rows which have head office in places that sound like Jaypur Ans. SQL> SELECT* FROM MAGAZINE WHERE SOUNDEX(HO)=SOUNDEX('JAYPUR'); NAME ANAME HO PH
24
4. Write a query to do right trimming on the authors name in the Magazine table. Ans. SQL> SELECT RTRIM(ANAME,' ') FROM MAGAZINE; RTRIM(ANAME,'') ------------------------CHARLES, JOHN RISHI, SHARMA RAGHURAM, RAJAN
5. Write a query to do left trimming on the authors name in the Magazine table. SQL> SELECT LTRIM(ANAME,' ') FROM MAGAZINE; LTRIM(ANAME,'') ------------------------CHARLES, JOHN RISHI, SHARMA RAGHURAM, RAJAN
6. Write SQL Query to to right pad the magazine name with . up to 30 characters. Ans. SQL> SELECT RPAD(NAME,30,'.') FROM MAGAZINE; RPAD(NAME,30,'.') -----------------------------FORBES OUTLOOK ..... .....
25
26
## SET Operators # Creating table LOAN and DEPOSIT. SQL> create table loan ( cname varchar2(30), loanno varchar2(10) ); Table created.
SQL> create table deposit ( cname varchar2(30), accno varchar2(10) ); Table created.
#Inserting Values in table LOAN and DEPOSIT. SQL> insert into loan values ('ABC',123); 1 row created. SQL> insert into loan values ('DEF',456); 1 row created. SQL> insert into loan values ('GHI',789); 1 row created.
SQL> insert into deposit values ('ABC',111); 1 row created. SQL> insert into deposit values ('GHI',333); 1 row created. SQL> insert into deposit values ('JKL',444); 1 row created.
1. Write SQL Query to display the customer name who are having account, loan as well as both in the bank. SQL> select cname from loan union select cname from deposit;
27
2. Write SQL Query to display customer who are having loans and account in the same bank. SQL> select cname from loan intersect select cname from deposit; CNAME -----------------------------ABC GHI
3. Write SQL Query to display cname only of those who have account and no loans. SQL> select cname from deposit minus select cname from loan; CNAME -----------------------------JKL
4. Write SQL Query to display cname of those who only have loan and no account. SQL> select cname from loan minus select cname from deposit; CNAME -----------------------------DEF
28
29
2. Write a PL/SQL code to copy the details of the employee 7764 into new table emp4 which is having same structure as emp. SQL> create table emp4 as select * from emp where 1=2; Table created. SQL> declare 2 3 record emp%rowtype; 4 5 begin 6 7 8 select * into record from emp where empno=7654; 9 10 insert into emp4 values(record.empno,record.ename, record.job,record.mgr,re cord.hiredate,record.sal, 11 12 record.comm,record.deptno); 13 14 end; 15 . SQL> / PL/SQL procedure successfully completed.
SQL> select * from emp4; EMPNO ENAME JOB MGR HIREDATE SAL COMM
30
31
32
7 4 8 32
6 rows selected. SQL> select * from radius_val; RADIUS ---------1 5 7 4 8 32 6 rows selected.
# Consider the table library(userid, name of the book, bookid, date of issue of book, date of return of book). Calculate the fine for each record on the basis of the following condition. 1.) DOR is within 15days from DOI then no fine 2.) 15-30 days , fine of Rs. 5 per day. 3.) After 30days fine of Rs.10 per day. Store the result in another table as Fine Details (userid, fine). SQL> select * from library; USERID BNAME DOI DOR
---------- ---------- --------- --------11001 Aman 11002 Anchal 29-MAR-88 21-APR-88 22-APR-88 28-APR-88
33
11003 Harpreet 02-MAY-88 28-JUL-88 11004 Sajal 11005 Saumya 19-APR-88 30-JUL-88 29-MAR-88 30-JUL-88
SQL> declare 2 fine number(10); 3 temp number(10); 4 temp1 number(10); 5 6 cursor pointer is select * from library; 7 lib_val pointer % ROWTYPE; 8 9 begin 10 11 12 for lib_val in pointer 13 loop 14 temp:= lib_val.dor-lib_val.doi; 15 if(temp<=15) 16 then 17 fine:=0; 18 insert into fine values(lib_val.userid,fine); 19 20 elsif (temp between 16 and 30)
34
21 then 22 temp1:=temp-15; 23 fine:=temp1*5; 24 insert into fine values(lib_val.userid,fine); 25 else 26 temp1:=temp-30; 27 fine:=(temp1*10)+(15*5); 28 insert into fine values(lib_val.userid,fine); 29 end if; 30 31 32 end loop; 33 end; 34 . SQL> SQL> / PL/SQL procedure successfully completed.
35
36
24 end; 25 . SQL> excute incr(6675,4567); SP2-0734: unknown command beginning "excute inc..." - rest of line ignored. SQL> set serveroutput on; SQL> / Procedure created.
SQL> execute incr(6678,7990); 6678is not found PL/SQL procedure successfully completed.
2. The company wants to calculate the annual increment of the employees. Create a function to calculate this increment based on various conditions: 1. If salary is less than 3000, then increment is 20% of net salary i.e. Salary + commission. 2. If salary is greater than 3000, and less than 6000, then increment is 30% of net salary. 3. Else, increment is 40% of net salary. SQL> create or replace function review(empid number) 2 return number 3 is 4 incr emp.sal%type; 5 net emp.sal%type; 6 vempno emp.empno%type; 7 vsal emp.sal%type; 8 vcomm emp.comm%type; 9 begin 10 select empno, sal, nvl(comm,0) into vempno, vsal, vcomm
37
11 from emp where empno=empid; 12 net:=vsal+vcomm; 13 if vsal<=3000 then 14 incr:=0.2*net; 15 elsif vsal>3000 and vsal<=6000 then 16 incr:=0.3*net; 17 else 18 incr:=0.4*net; 19 end if; 20 return(incr); 21 end review; 22 . SQL> / Function created.
38
3. Create a function that will accept employee number as a parameter and return the job of the employee SQL> create or replace function job(empid number) 2 return varchar2 3 is 4 jb emp.job%type; 5 vempno emp.empno%type; 6 begin 7 select empno, job into vempno, jb 8 from emp where empno=empid; 9 return jb; 10 end job; 11 . SQL> / Function created.
SQL> declare 2 job_emp varchar2(20); 3 begin 4 job_emp:=job(7698); 5 dbms_output.put_line(job_emp); 6 end; 7 . SQL> / MANAGER PL/SQL procedure successfully completed.
39
PNO
EOQ
OQ
---------- ---------- ---------100 200 10 11 5000 4000 4000 5000 6000 6000 6000 6000
1. Create a trigger to give a message to the user for each new record entered into the Inventory table. SQL> create or replace trigger sales 2 after insert on sales 3 for each row 4 begin 5 update inventory set oq=oq-:new.oq where pno=:new.pno; 6 end; 7 . SQL> / Trigger created.
SQL> insert into sales values(10, 'asf', 500, 1); 1 row created.
40
---------- ---------- ---------100 200 10 11 5000 4000 4000 5000 6000 6000 5500 6000
2. Create a trigger to check if the reduced quantity in the inventory table goes below the EOQ level, then a message should be given to the user. SQL> create or replace trigger invt_eoq 2 after update on inventory 3 for each row 4 when(new.oq<=new.eoq) 5 begin 6 dbms_output.put_line( 'Order more quantity'); 7 end; 8 . SQL> / Trigger created.
SQL> insert into sales values(10,'efd',2000,10); Order more quantity 1 row created.
41
---------- ---------- ---------100 200 10 11 5000 4000 4000 5000 6000 6000 3500 6000
3. Create a trigger that displays a message to the user every time an amount is withdrawn from the bank account. SQL>create or replace trigger withdraw 2 after update on customer for each row 3 when (new.dam<old.dam) 4 declare 5 a number(7,2); 6 begin 7 a:=:old.dam-:new.dam; 8 dbms_output.put_line('the amount withdrawn: '||a); 9 End; 10 . SQL> / Trigger created.
SQL> update customer set dam=7000 where custid=101; the amount withdrawn: 3000 1 row updated.
42
4. The bank has a condition of maintaining minimum balance of Rs.5000/- in the account. Create a trigger that displays a message to the user as soon as the balance of the account goes below Rs.5000. SQL> create or replace trigger lowbal 2 after update on customer for each row 3 when (new.dam<5000) 4 begin 5 dbms_output.put_line('account balance is low'); 6 end; 7 . SQL> / Trigger created.
SQL> update customer set dam=4500 where custid=102; account balance is low the amount withdrawn: 4500 1 row updated.
5. Maintain a duplicate table of the customers account. If any changes are made in the original table they should be reflected in the duplicate table as well. SQL> create or replace trigger duplicate2 2 after insert on customer for each row 3 begin 4 insert into ncustomer values(:new.custid,:new.cname,:new.dam); 5 end; 6 . SQL> /
43
SQL> select * from ncustomer; CUSTID CNAME ---------- -------------------- ---------100 Saumya 100 Saumya 100 Saumya 103 sim 4500 4500 4500 6000 DAM
SQL> create or replace trigger duplicate3 2 before delete on customer for each row 3 begin 4 delete from ncustomer where custid=:old.custid; 5 end; 6 . SQL> / Trigger created.
44
SQL> select * from ncustomer; CUSTID CNAME ---------- -------------------- ---------100 Saumya 100 Saumya 100 Saumya 4500 4500 4500 DAM