Upload
saumyass
View
137
Download
11
Embed Size (px)
DESCRIPTION
SQL and PL/SQL queries
Citation preview
LAB Practical FileMS-239 Database Management System
Made By:
SAUMYA (11216603912)
University School of Management Studies
Guru Gobind Singh Indraprastha University
( D-Block Sector-16C, Dwarka New-Delhi-110078 )
2 LAB Practical File
INDEX
Sno. Lab no Date Page No
1. LAB I 8 August 2013 3-6
2. LAB II 12 August 2013
7-9
3. LAB III 26 August 2013
10-11
4. LAB IV 2 September 2013
12-17
5. LAB V 12 September 2013
18-22
6. LAB VI (String Functions) 16 September 2013
23-24
7. LAB VII (SET operators and PL/SQL)
17 September 2013
25-27
8. LAB VIII 18 September 2013
28-30
9. LAB IX 23 September 2013
31-34
10. LAB X (Procedures and Functions)
30 September 2013
35-38
11. LAB XI (Triggers) 14 October 2013
39-44
MS-239: Database Management System
3 LAB Practical File
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 JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- -------------- -------------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 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 MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 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 SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
MS-239: Database Management System
4 LAB Practical File
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 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 CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
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
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- -------------------
MS-239: Database Management System
5 LAB Practical File
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
5 rows selected.
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 MGR HIREDATE SAL COMM DEPTNO CONTACTNO ADDRESS
---------- ------------------------------ --------- ---------- --------- ---------- ---------- ---------- ---------- --------------
7369 SMITH MANAGER 7902 17-DEC-80 5000 30
7566 JONES MANAGER 7839 02-APR-81 2356.2 20
7698 BLAKE MANAGER 7839 01-MAY-81 2257.2 30
7782 CLARK MANAGER 7839 09-JUN-81 2134.44 10
MS-239: Database Management System
6 LAB Practical File
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
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO ADDRESS
---------- ------------------------------ --------- ---------- --------- ---------- ---------- ---------- ---------- --------------
7369 SMITH MANAGER 7902 17-DEC-80 5000 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1584 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1237.5 500 30
MS-239: Database Management System
7 LAB Practical File
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)
--------- ----------
MANAGER 8275
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)
--------- ----------
CLERK 4150
MANAGER 8275
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)
--------- ----------
CLERK 4
SALESMAN 4
PRESIDENT 1
MANAGER 3
ANALYST 2
MS-239: Database Management System
8 LAB Practical File
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 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 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
---------- ---------- ----------
BLAKE 30 2850
MS-239: Database Management System
9 LAB Practical File
SCOTT 20 3000
KING 10 5000
FORD 20 3000
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)
--------- ----------
CLERK 1037.5
SALESMAN 1400
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 6
20 5
10 3
MS-239: Database Management System
10 LAB Practical File
LAB III: 26 August 2013 (Create, Insert and Delete)
# Creating Table CLASS
SQL> create table class (classno number(2) primary key,classname varchar2(25),block varchar2(5));
Table created.
# 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.
MS-239: Database Management System
11 LAB Practical File
# Deleting from table Student
SQL> delete from student where classno=10;
2 rows deleted.
SQL> delete from class where classno=10;
1 row deleted.
MS-239: Database Management System
12 LAB Practical File
LAB IV: 2 September 2013
1. Write SQL Query to Add the attribute ‘contactno’ to the table emp.
SQL> alter table emp add(contactno number(20));
Table altered.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
MS-239: Database Management System
13 LAB Practical File
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
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.
MS-239: Database Management System
14 LAB Practical File
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
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN MARKETING EXECUTIVE 7698 20-FEB-81 1920 300 30
7521 WARD MARKETING EXECUTIVE 7698 22-FEB-81 1500 500 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2826.25 20
7654 MARTIN MARKETING EXECUTIVE 7698 28-SEP-81 1500 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2707.5 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-JUN-81 2327.5 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7844 TURNER MARKETING EXECUTIVE 7698 08-SEP-81 1800 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
MS-239: Database Management System
15 LAB Practical File
14 rows selected.
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 NEW YORK 10
7839 KING NEW YORK 10
7934 MILLER NEW YORK 10
7566 JONES DALLAS 20
7902 FORD DALLAS 20
7876 ADAMS DALLAS 20
7369 SMITH DALLAS 20
7788 SCOTT DALLAS 20
7521 WARD CHICAGO 30
7844 TURNER CHICAGO 30
7499 ALLEN CHICAGO 30
EMPNO ENAME LOC DEPTNO
---------- ---------- ------------- ----------
7900 JAMES CHICAGO 30
7698 BLAKE CHICAGO 30
MS-239: Database Management System
16 LAB Practical File
7654 MARTIN CHICAGO 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
---------- ---------- --------------
7521 1250 SALES
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 7782 2450
RESEARCH 7566 2975
SALES 7844 1500
SALES 7698 2850
SALES 7521 1250
SALES 7499 1600
SALES 7654 1250
7 rows selected.
11. Write SQL Query to Display empno. Deptno and location of employee whose name is either SMITH or JONES.
MS-239: Database Management System
17 LAB Practical File
SQL> select ename,emp.deptno,loc from emp,dept where emp.deptno=dept.deptno and
(ename='SMITH' or ename='JONES');
ENAME DEPTNO LOC
---------- ---------- -------------
SMITH 20 DALLAS
JONES 20 DALLAS
LAB V: 12 September 2013
MS-239: Database Management System
18 LAB Practical File
1. Write SQL Query to Display name, hire date and review date of employees of department 10 where review date is hire date + 90 days.
SQL> select ename,hiredate,hiredate+90 from emp where deptno=10;
ENAME HIREDATE HIREDATE+
---------- --------- ---------
CLARK 09-JUN-81 07-SEP-81
KING 17-NOV-81 15-FEB-82
MILLER 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 17-DEC-80 17-JUN-81
WARD 22-FEB-81 22-AUG-81
JONES 02-APR-81 02-OCT-81
SCOTT 19-APR-87 19-OCT-87
ADAMS 23-MAY-87 23-NOV-87
FORD 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;
ENAME HIREDATE (SYSDATE-HIREDATE)/7
MS-239: Database Management System
19 LAB Practical File
---------- --------- --------------------
ALLEN 20-FEB-81 1698.92869
MARTIN 28-SEP-81 1667.50012
BLAKE 01-MAY-81 1688.92869
TURNER 08-SEP-81 1670.35726
JAMES 03-DEC-81 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 17-DEC-80 17 of DECEMBER ,1980
WARD 22-FEB-81 22 of FEBRUARY ,1981
JONES 02-APR-81 02 of APRIL ,1981
SCOTT 19-APR-87 19 of APRIL ,1987
ADAMS 23-MAY-87 23 of MAY ,1987
FORD 03-DEC-81 03 of DECEMBER ,1981
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;
ENAME HIREDATE TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH
---------- --------- ----------------------------------------------
MS-239: Database Management System
20 LAB Practical File
SMITH 17-DEC-80 Employee hired on December 17TH,1980 at 12:00
ALLEN 20-FEB-81 Employee hired on February 20TH,1981 at 12:00
WARD 22-FEB-81 Employee hired on February 22ND,1981 at 12:00
JONES 02-APR-81 Employee hired on April 02ND,1981 at 12:00
MARTIN 28-SEP-81 Employee hired on September 28TH,1981 at 12:00
BLAKE 01-MAY-81 Employee hired on May 01ST,1981 at 12:00
CLARK 09-JUN-81 Employee hired on June 09TH,1981 at 12:00
SCOTT 19-APR-87 Employee hired on April 19TH,1987 at 12:00
KING 17-NOV-81 Employee hired on November 17TH,1981 at 12:00
TURNER 08-SEP-81 Employee hired on September 08TH,1981 at 12:00
ADAMS 23-MAY-87 Employee hired on May 23RD,1987 at 12:00
ENAME HIREDATE TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH
---------- --------- ----------------------------------------------
JAMES 03-DEC-81 Employee hired on December 03RD,1981 at 12:00
FORD 03-DEC-81 Employee hired on December 03RD,1981 at 12:00
MILLER 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;
ENAME SAL DECODE(SAL,130
---------- ---------- --------------
SMITH 175.76
MS-239: Database Management System
21 LAB Practical File
ALLEN 1920
WARD 3500
JONES 39.57
MARTIN 1500
BLAKE 379.05
CLARK 325.85
SCOTT 3000 Senior Manager
KING 5000
TURNER 1800 Middle Manager
ADAMS 1100
ENAME SAL DECODE(SAL,130
---------- ---------- --------------
JAMES 159.6
FORD 3000 Senior Manager
MILLER 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;
ENAME TRANSLATE(
---------- ----------
SMITH TNJUI
MS-239: Database Management System
22 LAB Practical File
ALLEN BMMFO
WARD XBSE
JONES KPOFT
MARTIN NBSUJO
BLAKE CMBLF
CLARK DMBSL
SCOTT TDPUU
KING LJOH
TURNER UVSOFS
ADAMS BEBNT
ENAME TRANSLATE(
---------- ----------
JAMES KBNFT
FORD GPSE
MILLER NJMMFS
14 rows selected.
LAB VI: 16 September 2013 (String Functions)
1. Write SQL Query to Create a table ‘Magazine’ with attributes name, author’s name, head office and phone number.
MS-239: Database Management System
23 LAB Practical File
Ans. SQL> SELECT * FROM MAGAZINE;
NAME ANAME HO PH
------------------------- ------------------------- -------------------- --------------------
FORBES CHARLES, JOHN NEW YORK 011-2334567
OUTLOOK RISHI, SHARMA NEW DELHI 044-12234566
TIME RAGHURAM, RAJAN JAIPUR 022-345686946
2. Write SQL Query to Display the name before the comma in the author’s 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
------------------------- ------------------------- -------------------- --------------------
TIME RAGHURAM, RAJAN JAIPUR 022-345686946
4. Write a query to do right trimming on the author’s name in the Magazine table.
Ans. SQL> SELECT RTRIM(ANAME,' ') FROM MAGAZINE;
RTRIM(ANAME,'')
MS-239: Database Management System
24 LAB Practical File
-------------------------
CHARLES, JOHN
RISHI, SHARMA
RAGHURAM, RAJAN
5. Write a query to do left trimming on the author’s 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 .....
LAB VII: 17 September 2013 (SET Operators and PL/SQL)
# PL/SQL commands for finding the area of a circle and store the area of the circle in separate table.
MS-239: Database Management System
25 LAB Practical File
SQL> set serveroutput ON;
SQL> declare
2 pi constant number (9,7):=3.1415178;
3 radius number(6);
4 area number(14,2);
5 err number (14,2);
6 a number (6);
7
8 begin
9 radius:=&a;
10 err:=1/(radius-4);
11 area:= pi*power(radius,2);
12
13 insert into areas values (area,radius);
14 dbms_output.put_line('The area is' || area);
15 end;
16 .
SQL> /
Enter value for a: 12
old 9: radius:=&a;
new 9: radius:=12;
The area is452.38
PL/SQL procedure successfully completed.
## SET Operators
# Creating table LOAN and DEPOSIT.
SQL> create table loan ( cname varchar2(30), loanno varchar2(10) );
MS-239: Database Management System
26 LAB Practical File
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;
CNAME
------------------------------
ABC
MS-239: Database Management System
27 LAB Practical File
DEF
GHI
JKL
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
LAB VIII: 18 September 2013
1. Write a PL/SQL code to select all the managers who are hire in 1987. If there is no output or if there are more than one output instead of giving error, appropriate message should be given to the user.
MS-239: Database Management System
28 LAB Practical File
SQL> declare
2 vemp emp.empno%type;
3
4 begin
5
6 select empno into vemp from emp where job='MANAGER' AND hiredate like '%87'
;
7 dbms_output.put_line('Manager who was hire in 1987 is' || vemp);
8
9 exception
10
11 when too_many_rows then
12 dbms_output.put_line(' Too many managers');
13
14 when no_data_found then
15 dbms_output.put_line('No manager found');
16
17 end;
18 .
SQL> /
Manager who was hire in 1987 is 6789
PL/SQL procedure successfully completed.
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.
MS-239: Database Management System
29 LAB Practical File
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
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7654 MARTIN MARKETING 7698 28-SEP-81 1500 1400
30
MS-239: Database Management System
30 LAB Practical File
LAB IX: 23 September 2013 (Loop and Cursor)
# Loop Example
SQL> declare
2 pi constant number(9,7):=3.1415926;
MS-239: Database Management System
31 LAB Practical File
3 a number(14,2);
4 cursor pointer is select * from radius_val;
5 radius pointer%rowtype;
6
7 Begin
8
9 open pointer;
10 loop
11 fetch pointer into radius;
12 exit when pointer%NOTFOUND;
13 a:=pi*power(radius.radius,2);
14 insert into area values(radius.radius,a);
15 end loop;
16 close pointer;
17 end;
18 .
SQL> /
PL/SQL procedure successfully completed.
SQL> select * from area;
RADIUS AREAS
---------- ----------
1 3.14
5 78.54
7 153.94
4 50.27
8 201.06
32 3216.99
MS-239: Database Management System
32 LAB Practical File
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 29-MAR-88 21-APR-88
11002 Anchal 22-APR-88 28-APR-88
11003 Harpreet 02-MAY-88 28-JUL-88
11004 Sajal 19-APR-88 30-JUL-88
11005 Saumya 29-MAR-88 30-JUL-88
MS-239: Database Management System
33 LAB Practical File
SQL> select * from fine;
no rows selected
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)
21 then
22 temp1:=temp-15;
23 fine:=temp1*5;
24 insert into fine values(lib_val.userid,fine);
MS-239: Database Management System
34 LAB Practical File
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.
SQL> select * from fine;
USERID FINE
---------- ----------
11001 40
11002 0
11003 645
11004 965
11005 1005
LAB X: 30 September 2013 (Procedures and Functions)
1. Write a procedure with a name INCR that will increase the salary of the employee by the amount specified. The procedure checks for null salary and missing employee number.
MS-239: Database Management System
35 LAB Practical File
SQL> create or replace procedure incr(e_id number, amt number)
2 is
3 vsalary number;
4 salary_missing exception;
5
6 Begin
7 Select sal into vsalary from emp where empno=e_id;
8
9 if vsalary is NULL then
10 Raise salary_missing;
11
12 else
13 update emp set sal=sal+amt where empno=e_id;
14 end if;
15
16 Exception
17
18 when salary_missing then
19 dbms_output.put_line(e_id ||'has salary as NULL');
20
21 when no_data_found then
22 dbms_output.put_line(e_id|| 'is not found');
23
24 end;
25 .
SQL> excute incr(6675,4567);
SP2-0734: unknown command beginning "excute inc..." - rest of line ignored.
MS-239: Database Management System
36 LAB Practical File
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
11 from emp where empno=empid;
12 net:=vsal+vcomm;
13 if vsal<=3000 then
14 incr:=0.2*net;
MS-239: Database Management System
37 LAB Practical File
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.
declare
incr_sal number(7,2);
begin
incr_sal:=review(7698);
dbms_output.put_line(incr_sal);
end;
.
SQL> /
518.7
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
MS-239: Database Management System
38 LAB Practical File
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.
LAB XI: 14 October 2013 (TRIGGERS)
#Inventory table
SQL> select * from inventory;
PNO EOQ OQ
MS-239: Database Management System
39 LAB Practical File
---------- ---------- ----------
100 5000 6000
200 4000 6000
10 4000 6000
11 5000 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.
SQL> select * from inventory;
PNO EOQ OQ
---------- ---------- ----------
100 5000 6000
200 4000 6000
MS-239: Database Management System
40 LAB Practical File
10 4000 5500
11 5000 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.
SQL> select * from inventory;
PNO EOQ OQ
---------- ---------- ----------
100 5000 6000
200 4000 6000
MS-239: Database Management System
41 LAB Practical File
10 4000 3500
11 5000 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.
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
MS-239: Database Management System
42 LAB Practical File
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 customer’s 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> /
Trigger created.
SQL> insert into customer values(103,'sim',6000);
1 row created.
SQL> select * from ncustomer;
CUSTID CNAME DAM
MS-239: Database Management System
43 LAB Practical File
---------- -------------------- ----------
100 Saumya 4500
100 Saumya 4500
100 Saumya 4500
103 sim 6000
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.
SQL> delete from customer where custid=103;
1 row deleted.
SQL> select * from ncustomer;
CUSTID CNAME DAM
---------- -------------------- ----------
100 Saumya 4500
100 Saumya 4500
100 Saumya 4500
MS-239: Database Management System
44 LAB Practical File
MS-239: Database Management System