Vous êtes sur la page 1sur 44

University School of Management Studies Guru Gobind Singh Indraprastha University ( D-Block Sector-16C, Dwarka New-Delhi-110078 )

LAB Practical File


MS-239 Database Management System

Made By: SAUMYA (11216603912)

LAB Practical File

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

MS-239: Database Management System

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

7698 28-SEP-81 7698 08-SEP-81

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

MS-239: Database Management System

LAB Practical File

7698 BLAKE 7782 CLARK 7839 KING 7844 TURNER 7900 JAMES 7902 FORD

MANAGER MANAGER PRESIDENT SALESMAN CLERK ANALYST

7839 01-MAY-81 7839 09-JUN-81 17-NOV-81 7698 08-SEP-81 7698 03-DEC-81 7566 03-DEC-81

2850 2450 5000 1500 950 3000

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

---------- ---------- --------- ---------- --------- ---------- ---------- ---------- -------------------

MS-239: Database Management System

LAB Practical File

7369 SMITH 7566 JONES 7788 SCOTT 7876 ADAMS 7900 JAMES 5 rows selected.

CLERK MANAGER ANALYST CLERK CLERK

7902 17-DEC-80 7839 02-APR-81 7566 19-APR-87 7788 23-MAY-87 7698 03-DEC-81

800 2975 3000 1100 950

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

---------- ---------- ---------- ----

7902 17-DEC-80 7839 02-APR-81 7839 01-MAY-81

5000 2356.2 2257.2

30 20

7839 09-JUN-81 2134.44

MS-239: Database Management System

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 CONTACTNO ADDRESS

MGR HIREDATE

SAL

COMM

DEPTNO

---------- ------------------------------ --------- ---------- -------------- -------------7369 SMITH 7499 ALLEN 30 7521 WARD 30 SALESMAN MANAGER SALESMAN

---------- ---------- ---------- ----

7902 17-DEC-80 7698 20-FEB-81

5000 1584 300

30

7698 22-FEB-81

1237.5

500

MS-239: Database Management System

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 MANAGER 4150 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 SALESMAN PRESIDENT MANAGER ANALYST 4 4 1 3 2

MS-239: Database Management System

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

---------- ---------- ---------BLAKE 30 2850

MS-239: Database Management System

LAB Practical File

SCOTT KING FORD

20 10 20

3000 5000 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 SALESMAN 1037.5 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 20 10 6 5 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 ---------- ---------20 300 500 30 30

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

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------2975 1250 2850 20 1400 30 30

---------- ---------- --------- ---------- --------- ---------- ---------7566 JONES 7654 MARTIN 7698 BLAKE MANAGER SALESMAN MANAGER 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81

EMPNO ENAME

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------2450 3000 5000 10 20 10

---------- ---------- --------- ---------- --------- ---------- ---------7782 CLARK 7788 SCOTT 7839 KING MANAGER ANALYST PRESIDENT 7839 09-JUN-81 7566 19-APR-87 17-NOV-81

EMPNO ENAME

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------1500 0 30

---------- ---------- --------- ---------- --------- ---------- ---------7844 TURNER SALESMAN 7698 08-SEP-81

MS-239: Database Management System

13

LAB Practical File

7876 ADAMS 7900 JAMES

CLERK CLERK

7788 23-MAY-87 7698 03-DEC-81

1100 950 30

20

EMPNO ENAME

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------20 10

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

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 ---------- ---------20 20-FEB-81 1920 300

---------- ---------- --------- ---------- --------- ---------- ---------7369 SMITH 7499 ALLEN 30 7521 WARD 30 CLERK 7902 17-DEC-80 800

MARKETING EXECUTIVE 7698

MARKETING EXECUTIVE

7698 22-FEB-81

1500

500

EMPNO ENAME

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------20 1500 1400

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

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------10 20 10

---------- ---------- --------- ---------- --------- ---------- ---------7782 CLARK 7788 SCOTT 7839 KING MANAGER ANALYST PRESIDENT 7839 7566 09-JUN-81 19-APR-87 17-NOV-81

2327.5 3000 5000

EMPNO ENAME

JOB MGR HIREDATE SAL

COMM DEPTNO CONTACTNO ---------- ---------08-SEP-81 1800

---------- ---------- --------- ---------- --------- ---------- ---------7844 TURNER 0 30 7876 ADAMS MARKETING EXECUTIVE 7698

CLERK 7788

23-MAY-87

1100

20

MS-239: Database Management System

15

LAB Practical File

7900 JAMES 14 rows selected.

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

---------- ---------- ------------- ---------7900 JAMES CHICAGO 30

MS-239: Database Management System

16

LAB Practical File

7698 BLAKE 7654 MARTIN

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

---------- ---------- -------------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 RESEARCH SALES SALES SALES SALES SALES 7 rows selected. 7782 7566 7844 7698 7521 7499 7654 2450 2975 1500 2850 1250 1600 1250

MS-239: Database Management System

17

LAB Practical File

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

---------- ---------- ------------SMITH JONES 20 DALLAS 20 DALLAS

MS-239: Database Management System

18

LAB Practical File

LAB V: 12 September 2013


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

MS-239: Database Management System

19

LAB Practical File

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

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;

MS-239: Database Management System

20

LAB Practical File

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;

MS-239: Database Management System

21

LAB Practical File

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;

MS-239: Database Management System

22

LAB Practical File

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(

---------- ---------JAMES FORD MILLER KBNFT GPSE NJMMFS

14 rows selected.

MS-239: Database Management System

23

LAB Practical File

LAB VI: 16 September 2013 (String Functions)


1. Write SQL Query to Create a table Magazine with attributes name, authors name, head office and phone number. Ans. SQL> SELECT * FROM MAGAZINE; NAME ANAME HO PH

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

------------------------- ------------------------- -------------------- -------------------TIME RAGHURAM, RAJAN JAIPUR 022-345686946

MS-239: Database Management System

24

LAB Practical File

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

MS-239: Database Management System

25

LAB Practical File

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

MS-239: Database Management System

26

LAB Practical File

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

MS-239: Database Management System

27

LAB Practical File

CNAME -----------------------------ABC 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

MS-239: Database Management System

28

LAB Practical File

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

MS-239: Database Management System

29

LAB Practical File

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

---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO

MS-239: Database Management System

30

LAB Practical File

---------7654 MARTIN 30 MARKETING 7698 28-SEP-81 1500 1400

MS-239: Database Management System

31

LAB Practical File

LAB IX: 23 September 2013 (Loop and Cursor)


# Loop Example SQL> declare 2 pi constant number(9,7):=3.1415926; 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 5 3.14 78.54

MS-239: Database Management System

32

LAB Practical File

7 4 8 32

153.94 50.27 201.06 3216.99

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

MS-239: Database Management System

33

LAB Practical File

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> 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)

MS-239: Database Management System

34

LAB Practical File

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.

SQL> select * from fine; USERID FINE

---------- ---------11001 11002 11003 11004 11005 40 0 645 965 1005

MS-239: Database Management System

35

LAB Practical File

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

MS-239: Database Management System

36

LAB Practical File

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

MS-239: Database Management System

37

LAB Practical File

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.

declare incr_sal number(7,2); begin incr_sal:=review(7698); dbms_output.put_line(incr_sal); end; . SQL> / 518.7

MS-239: Database Management System

38

LAB Practical File

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.

MS-239: Database Management System

39

LAB Practical File

LAB XI: 14 October 2013 (TRIGGERS)


#Inventory table SQL> select * from inventory;

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.

MS-239: Database Management System

40

LAB Practical File

SQL> select * from inventory; PNO EOQ OQ

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

MS-239: Database Management System

41

LAB Practical File

SQL> select * from inventory; PNO EOQ OQ

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

MS-239: Database Management System

42

LAB Practical File

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

MS-239: Database Management System

43

LAB Practical File

Trigger created. SQL> insert into customer values(103,'sim',6000); 1 row created.

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.

SQL> delete from customer where custid=103; 1 row deleted.

MS-239: Database Management System

44

LAB Practical File

SQL> select * from ncustomer; CUSTID CNAME ---------- -------------------- ---------100 Saumya 100 Saumya 100 Saumya 4500 4500 4500 DAM

MS-239: Database Management System

Vous aimerez peut-être aussi