You are on page 1of 66

CE246-DBMS 18CE091

Practical – 1
Aim To study DDL-create and DML-insert commands

Q-1 Find the names of all the students whose total credits are greater than 100

A Query with output

SELECT distinct s.STUDENT_FIRSTNAME,c.CREDIT from stu s,cou1 c where


s.STUDENT_ID=c.student_id and c.CREDIT>100;

Q-2 Find the course id and grades of all courses taken by any student named 'Tanaka'

A Query with output

SELECT s.STUDENT_FIRSTNAME,c.C_ID,C.GRADE FROM Stu s, COU1 c where


s.student_id=c.student_id and s.student_firstname='TANAKA' ;

Q-3 Find the ID and name of instructors who have taught a course in the Comp. Sci. department,
even if they are themselves not from the Comp. Sci. department. To test this query, make

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

sure you add appropriate data, and include the corresponding insert statements along with
your query.

A Query with output

select distinct i.ins_id,i.ins_firstname from ins i,stu s where i.ins_dept='CSE' and i.ins_dept!
=s.student_deptname;

Q-4 Find the courses which are offered in both 'Fall' and 'Spring' semester (not necessarily in the
same year).

A Query with output

select distinct course_title,year,c_id from cre where sem='Spring' or sem='Fall';

Practical – 2

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Aim To study DDL-create and DML-insert commands with constraints

Write the following simple SQL Queries on the University Schema Railway
Schema Bottom Up Approach

Q-1 Find pairs of stations (station codes) that have a track (direct connection) with distance
less than 20Kms between them.

A Query with output

select station_code1,station_code2 from tb_track where distance < 20;

Q-2 Find the IDs of all the trains which have a stop at THANE.

A Query with output

select tb_station.station_name,tb_trainhault.train_id

from tb_station,tb_trainhault

where tb_station.station_code = tb_trainhault.station_code

and tb_station.station_name = ‘THANE’;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 Find the names of all trains that start at MUMBAI.

A Query with output

select train_name from train, tb_trainhault where sequence_no = 1

and station_code = ‘st610’ and train.train_id = tb_trainhault.train_id;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-4 List all the stations in order of visit by the train 'CST-AMR_LOCAL'.

A Query with output

Select station_name from tb_station, tb_trainhault, tb_train where tb_train.train_name =


‘CS-AMR_LOCAL’ and tb_station.station_code = tb_trainhault.station_code and
tb_train.train_id = tb_trainhault.train_id;

Q-5 Find the name of the trains which have stop at Thane, before the 6th station in the route
of the train.

A Query with output

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical-3

Aim To study various options of like predicate and some Built in Functions

Q-1 Retrieve all data from employee, jobs and deposit.

A Query with output

Select * from job,employee,deposit;

Q-2 Give details of account no. And deposited rupees of customers having account opened
between dates 01-01-06 and 25-07-06.

A Query with output

select a_no,cname,amount from deposit where a_date between '01-JAN-2006' and '07-
JUL-2006';

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 Display all jobs with minimum salary is greater than 4000.

A Query with output

select job_id,job_title from job where min_sal>20000;

Q-4 Display name and salary of employee whose department no is 20. Give alias name to

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

name of employee.

A Query with output

select emp_name,emp_sal as employee from employee where dept_no='20';

Q-5 Display employee no,name and department details of those employee whosedepartment
lies in(10,20).

A Query with output

select emp_no,emp_name,dept_no from employee where dept_no between '10' and '20

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-6 Display the non-null values of employee’s commission.

A Query with output

select emp_no,emp_name,emp_sal,dept_no from employee where emp_comm is not


null;

Q-7 Display name of customer along with its account no (both column should be displayed

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

as one) whose amount is not equal to 8000 Rs.

A Query with output

select concat(emp_no,emp_name),emp_sal as emp from employee where emp_sal!


=45000;

Q-8 Display the content of job details with minimum salary either 2000 or 4000.

A Query with output

select job_title from job where min_sal=20000 or max_sal=200000;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical-4

Aim To perform various data manipulation commands, aggregate functions and sorting
concept on all created tables.

Q-1 List total amount from deposit.

A Query with output

select sum(amount) from deposit;

Q-2 List total loan amount from andheri branch.

A Query with output

select sum(amount) from borrow where bname='Andheri';

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 Give maximum amount of loan from branch andheri.

A Query with output

select max(amount) from borrow where bname='Andheri';

Q-4 Count total number of customers.

A Query with output

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

select count(cname) from deposit;

Q-5 Count total number of customer’s cities.

A Query with output

select count(distinct bname) as cities from borrow;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-6 Create table supplier from employee with all the columns with data.

A Query with output

create table supplier as (select * from employee);

select * from supplier;

Q-7 Create table sup1 from employee with first two columns.

A Query with output

create table sup1 as(select emp_no,emp_name from employee);

select * from sup1;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-8 Create table sup2 from employee with no data.

A Query with output

create table sup2 as(select * from employee where 1=0);

select * from sup2;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-9 Insert the data into sup2 from employee whose second character should be ‘n’ and string
should be 5 characters long in employee name field.

A Query with output

insert into sup2 select * from employee where length(emp_name) = 4 and emp_name
like '_a__';

select * from sup2;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-10 Delete all the rows from sup1.

A Query with output

delete from sup1;

select * from sup1;

Q-11 Delete the detail of supplier whose sup_no is 103.

A Query with output

delete from supplier where emp_comm=0;

select * from supplier;

Q-12 Rename the table sup2.

A Query with output

alter table sup2 rename to supplier2;

select * from supplier2;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-13 Destroy table sup1 with all the data.

A Query with output

drop table sup1; select * from sup1;

Q-14 Update the value dept_no to 10 where second character of emp. Name is ‘m’.

A Query with output

update employee set dept_no=10 where emp_name like 'Y%';

select * from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-15 Update the value of employee name whose employee number is 103.

A Query with output

update employee set emp_name='Amit' where dept_no = 12;

select * from employee;

Q-16 Add one column phone to employee with size of column is 10.

A Query with output

ALTER TABLE employee ADD phone VARCHAR(15) AFTER dept_no;

select * from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-17 Modify the column emp_name to hold maximum of 30 characters.

A Query with output

alter table employee modify emp_name varchar(30);

desc employee;

Q-18 Count the total no as well as distinct rows in dept_no column with a condition of salary
greater than 1000 of employee.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

A Query with output

SELECT DISTINCT BNAME FROM DEPOSIT;

select count(distinct dept_no) from employee where emp_sal>40000;

Q-19 Display the detail of all employees in ascending order, descending order of their
name and no.

A Query with output

select * from employee order by emp_name asc,emp_no desc;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-20 Display the dept_no in ascending order and accordingly display emp_comm in
descending order.

A Query with output

select dept_no,emp_comm from employee order by dept_no asc, emp_comm desc;

Q-21 Update the value of emp_comm to 500 where dept_no is 20.

A Query with output

update employee set dept_no=11 where emp_name like 'Y%';

select * from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-22 Display the emp_comm in ascending order with null value first and accordingly sort
employee salary in descending order.

A Query with output

select emp_comm,emp_sal from employe order by emp_comm asc NULLS first;

Q-23 Display the emp_comm in ascending order with null value last and accordingly sort
emp_no in descending order.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

A Query with output

select emp_comm,emp_sal from employee order by emp_comm asc nulls last,emp_no


desc;

Practical-5

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Aim To apply the concept of aggregating data using group functions and single raw
functions.

Q-1 List total deposit of customer having account date after 1-jan-96.

A Query with output

select sum(amount) from deposit where a_date>'1996-01-01';

Q-2 List total deposit of customers living in city nagpur.

A Query with output

select sum(amount) from deposit,customer where customer.a_no = deposit.a_no and


city = ‘Nagpur’;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 List maximum deposit of customers living in bombay.

A Query with output

select max(amount) from deposit,customer where customer.a_no= deposit.a_no and city


= ‘Nagpur’;

Q-4 Display the highest, lowest, sum, and average salary of all employees. Label the
columns.

A Query with output


U & P U Patel Department of Computer Engineering
CE246-DBMS 18CE091

SELECT MAX(emp_sal) ‘Maximum’ , MIN(emp_sal) as ‘Minimum’,SUM(emp_sal)


as ‘Sum’, AVG(emp_sal) as ‘Average’ from employee;

Q-5 Maximum, minimum, sum, and average, respectively. Round your results to the nearest
whole number.

SELECT round(MAX(emp_sal)) ‘Maximum’ , round(MIN(emp_sal)) as ‘Minimum’,


round(SUM(emp_sal)) as ‘Sum’, round(AVG(emp_sal)) as ‘Average’ from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-6 Write a query that displays the difference between the highest and lowest salaries. Label
the column difference.

A Query with output

select max(amount)-min(amount) as DIFFERENCE from deposit;

Q-7 Create a query that will display the total number of employees and, of that total, the
number of employees hired in 1995, 1996, 1997, and 1998.

A Query with output

select count(cname) from employee where DATE_FORMAT(hire_date,’fmyyyy’) IN

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

(‘2001’,’2003’,’2004’);

Q-8 Find the average salaries for each department without displaying the respective
department numbers.

A Query with output

select avg(emp_sal) from employee group by dept_no;

Q-9 Write a query to display the total salary being paid to each job title, within each
department.

A Query with output

select job_title,max_sal as salary from job;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-10 Find the average salaries > 2000 for each department without displaying the respective
department numbers.

A Query with output

SELECT AVG(emp_sal) FROM employee WHERE emp_sal>5000 group by dept_no;

Q-11 Display the job and total salary for each job with a total salary amount exceeding 3000,
in which excludes president and sorts the list by the total salary.

A Query with output

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-12 List the branches having sum of deposit more than 5000 and located in city
bombay.write a query to display the current date. Label the column date for each
employee, display the employee number, job, salary, and salary increased by 15% and
expressed as a whole number. Label the column new salary.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-13 Modify your query no 5.(2) to add a column that subtracts the old salary from the new
salary. Label the column increase.

A Query with output

Select emp_no as employee_number , emp_sal as Salary,(emp_sal + (0.15*emp_sal))


as New_Salary, ,(emp_sal + (0.15*emp_sal)) – emp_sal as Difference from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-14 Write a query that displays the employee’s names with the first letter capitalized and all
other letters lowercase, and the length of the names, for all employees whose name
starts with j, a, or m. Give each column an appropriate label. Sort the results by the
employees’ last names.

A Query with output

Q-15 Write a query that produces the following for each employee: <employee last name>

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

earns <salary> monthly.

A Query with output

Q-16 Display the name, hire date, number of months employed and day of the week on which
the employee has started. Order the results by the day of the week starting with monday.

A Query with output

Q-17 Display the hiredate of emp in a format that appears as seventh of june 1994 12:00:00

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

am.

A Query with output

Q-18 Write a query to calculate the annual compensation of all employees (sal+comm.).

A Query with output

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical 6
Aim To Study various Data manipulation methods.

Q-1 Give 10% interest to all depositors.

A QUERY WITH OUTPUT:

Q-2 Give 10% interest to all depositors having branch vrce.

A QUERY WITH OUTPUT:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 Give 10% interest to all depositors living in nagpur and having branch city bombay.

A QUERY WITH OUTPUT:

Q-4 Write a query which changes the department number of all employees with empno 7788’s job
to employee 7844’current department number.
A QUERY WITH OUTPUT:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

SELECT * FROM EMPLOYEE;

UPDATE employee SET dept_no=(SELECT dept_no FROM employee WHERE


emp_no=505) WHERE emp_no=509;

Q-5 Transfer 10 rs from account of anil to sunil if both are having same branch.

A QUERY WITH OUTPUT:


UPDATE deposit SET amount = amount + 10 WHERE bname = 'Andheri' AND cname
IN(SELECT cname FROM deposit WHERE bname = 'Dadar');

UPDATE deposit SET amount = amount + 10WHERE bname = 'Dadar' AND cname
IN(SELECT cname FROM deposit WHERE bname = 'Andheri');

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

SELECT * FROM DEPOSIT;

Q-6 Give 100 rs more to all depositors if they are maximum depositors in their respective
branch
A QUERY WITH OUTPUT:
UPDATE DEPOSIT SET AMOUNT=AMOUNT+100 WHERE CNAME IN (SELECT
CNAME FROM DEPOSIT WHERE AMOUNT IN (SELECT MAX(AMOUNT)
FROM DEPOSIT GROUP BY BNAME));

Q-7 Delete depositors of branches having number of customers between 1 to 3.

A QUERY WITH OUTPUT:


Create table copy_deposit as(select * from deposit);

DELETE FROM copy_deposit

WHERE bname IN (SELECT bname FROM deposit GROUP BY bname HAVING


COUNT(cname) <= 3);

Q-8 Delete deposit of Virar.

A QUERY WITH OUTPUT:


DELETE FROM DEPOSIT WHERE BNAME='Virar';

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-9 Delete borrower of branches having average loan less than 2000.

A QUERY WITH OUTPUT:


SELECT * FROM BORROW;

Create table copy_brw as (SELECT * FROM BORROW);

DELETE FROM copy_brw WHERE AMOUNT<2000;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Sign: Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical 8

CASE-STUDY PART OF JOIN :


Displaying data from multiple tables – Apply inner , outer, natural join must be used

create table class2(id number(5),name varchar2(25));

insert into class2(1,’abhi’);

insert into class2(2,’adam’);

insert into class2(3,’dax);

insert into class2(4,’anami’);

insert into class2(5,’hiteshree’);

 select * from class2;

create table class_info(id number(5) PRIMARY KEY,address varchar2(25));

insert into class_info(1,’delhi’);

insert into class_info(2,’mumbai’);

insert into class_info(3,’chennai’);

insert into class_info(7,’noida’);

insert into class_info(8,’panipat’);

 select * from class_info;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

CROSS JOIN QUERY:


select * from class2 CROSS JOIN class_info;

INNER JOIN OR EQUI JOIN QUERY:


select * from class2 INNER JOIN class_info on class2.id=class_info.id;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

NATURAL JOIN QUERY:


select id,address from class2 NATURAL JOIN class_info;

LEFT OUTER JOIN QUERY:


select * from class2 LEFT OUTER JOIN class_info on(class2.id=class_info.id);

RIGHT OUTER JOIN QUERY:


select * from class2 RIGHT OUTER JOIN class_info on(class2.id=class_info.id);

FULL OUTER JOIN QUERY:


select * from class2 FULL OUTER JOIN class_info on (class2.id=class_info.id);

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Part 2:- Displaying data from Multiple Tables (join)

(1) Give details of customers ANIL.


Query: select * from deposit natural join customer where cname='ANIL';
Output:

(2) Give name of customer who are borrowers and depositors and having living city Nagpur.
Query: select customer.cname from customer join borrow on customer.cname=borrow.cname join
deposit on customer.cname=deposit.cname and city='NAGPUR';
Output:

(3) Give city as their city name of customers having same living branch.
Query: select distinct(a.cname),a.city from customer a,customer b where a.city=b.city and
a.cname!=b.cname;
Output:

(4) Write a query to display the last name, department number, and department name for
all employees.
Query: select emp_name,dept_no,dept_name from employee;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Output:

(4) Create a unique listing of all jobs that are in department 30. Include the location of the
department in the output
Query: select job_id,dept_loc from job natural join employee where dept_no='30';
Output:

(5) Write a query to display the employee name, department number, and department name for
all employees who work in NEW YORK.
Query: select emp_name,dept_no,dept_name from employee where dept_loc='Anand';
Output:

(6) Display the employee last name and employee number along with their manager’s last name
and manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively.
Query: select emp_name employee,emp_no emp# from employee;
Output:

(7) Create a query to display the name and hire date of any employee hired after employee
SCOTT.
Query: select emp_name,hire_date from employee where hire_date>(select hire_date from
employee where emp_name='Aman');
Output:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Sign: Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical – 9
Aim To study various Data Control Languages (DCL),Transfer Control Language (TCL)
commands
Q-1 Develop a query to grant all privileges of employees table into departments table.

Q-2 Develop a query to grant some privileges of employees table into departments table.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-3 Develop a query to revoke all privileges of employees table from departments table..

Q-4 Develop a query to revoke some privileges of employees table from departments table.

Q-5 Write a query to implement the save point.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-6 Insert into employees values(‘c03’,’nishit’,50000,’c01’);

Q-7 Select * from employees;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q-8 Write a query to implement the rollback.

Q-9 Select * from employees;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Q10 Write a query to implement the commit.

Sign: Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical 10
Aim To perform basic pl/sql blocks

Q Write a PL-SQL block for checking weather a given year is a Leap year or not

A QUERY WITH OUTPUT:


SET SERVEROUTPUT ON;

DECLARE
year NUMBER;

BEGIN
year:=&year;
IF MOD(year, 4)=0
AND
MOD(year, 100)!=0
OR
MOD(year, 400)=0 THEN
dbms_output.Put_line(year
|| ' is a leap year ');
ELSE
dbms_output.Put_line(year
|| ' is not a leap year.');
END IF;

END;
SET SERVEROUTPUT ON;

DECLARE
year NUMBER;

BEGIN
year:=&year;
IF MOD(year, 4)=0
AND
MOD(year, 100)!=0
OR
MOD(year, 400)=0 THEN

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

dbms_output.Put_line(year
|| ' is a leap year ');
ELSE
dbms_output.Put_line(year
|| ' is not a leap year.');
END IF;

END;

CONCLUSION :- In this practical, we learned about basics of PL/SQL block.

Sign: Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical 11
Aim To perform the concept of loop

Q Find out whether given string is palindrome or not using For, While and Simple
Loop.
A QUERY WITH OUTPUT:
FOR LOOP :
Code :
SET SERVEROUTPUT ON

DECLARE
N NUMBER;
M NUMBER;
TEMP NUMBER:=0;
R NUMBER;
C NUMBER:=0;
Q NUMBER;

BEGIN
N:=&N;
M:=N;
Q:=N;
WHILE(Q>0)
LOOP
Q:=TRUNC(Q/10);
C:=C+1;
END LOOP;
FOR I IN 1..C
LOOP
R:=MOD(N,10);
TEMP:=(TEMP*10)+R;
N:=TRUNC(N/10);
END LOOP;
IF M=TEMP
THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

END;

WHILE LOOP :
Code :
SET SERVEROUTPUT ON

DECLARE
N NUMBER;
M NUMBER;
TEMP NUMBER:=0;
R NUMBER;

BEGIN
N:=&N;
M:=N;
WHILE N>0
LOOP
R:=MOD(N,10);
TEMP:=(TEMP*10)+R;
N:=TRUNC(N/10);
END LOOP;
IF M=TEMP
THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;

END;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

SIMPLE LOOP :
Code :
SET SERVEROUTPUT ON
DECLARE
INPUT VARCHAR(20):='OPPO';
R VARCHAR(20);
I INTEGER(10);
BEGIN
I:=LENGTH(INPUT);
LOOP
IF(I>=1) THEN
R:=R||SUBSTR(INPUT,I,1);
ELSE
EXIT;
END IF;
I:=I-1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('REVERSE : '||R);
IF INPUT=R THEN
DBMS_OUTPUT.PUT_LINE('THE GIVEN STRING '||INPUT||' IS A PALINDROME');
ELSE
DBMS_OUTPUT.PUT_LINE('THE GIVEN STRING '||INPUT||' IS
NOT A PALINDROME');
END IF;
END;

CONCLUSION :- we learned about different types of loops in PL/SQL block.

Sign : Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Practical 12
Aim To understand the concept of “select into” and “% type” attribute.

Q Create an employees table that is a replica of the emp table. Add a new column, stars, of
varchar2 data type and length of 50 to the employees table for storing asterisk (*).

Create a pl/sql block that rewards an employee by appending an asterisk in the stars column
for every rs1000/- of the employee’s salary. For example, if the employee has a salary
amount
of rs8000/-, the string of asterisks should contain eight asterisks. If the employee has a salary
amount of rs12500/-, the string of asterisks should contain 13 asterisks.
A QUERY WITH OUTPUT:
create table employees as (select * from employee);
declare v_asterisk employees.asterisk%type;
v_empno employees.emp_no%type := to_number(&v_empno);
v_sal employee.emp_sal%type;

begin

select emp_sal into v_sal from employees where emp_no = v_empno;


FOR counter IN 1..(round(v_sal/1000)) LOOP
v_asterisk := v_asterisk || '*';
END LOOP;

UPDATE employees set asterisk=v_asterisk where emp_no = v_empno;


commit;
END;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

Select * from employees;

Conclusion : We understood the concept of select into and %type attribute.

Sign: Grade:

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

PRACTICAL : 13
Aim : To perform the concept of function and procedure

1. Using Function :

create or replace function retrievesal(idno in number,temp out number) return number is


data_not_found EXCEPTION;esal number;
begin
select emp_no into temp from employee where emp_no=idno;
if sql%notfound then
raise data_not_found;
else
select emp_sal into esal from employee where emp_no=idno;
dbms_output.put_line('Salary updated.');
dbms_output.put_line('Old salary : '||esal);
esal:=esal+100;
return esal;
end if;
exception
when data_not_found then dbms_output.put_line('No data found.');
when others then dbms_output.put_line('Error');
end;
declare
idno number(5);
esal number(5);
temp number(5);
begin
idno:=103;
esal:=retrievesal(idno,temp);
dbms_output.put_line('New salary : '||esal);
end;
OUTPUT :

2. Using Procedure :

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

create or replace procedure retrieve(idno in number,temp out number,esal out number) is


data_not_found EXCEPTION;
begin
select emp_no into temp from employee where emp_no=idno;
if sql%notfound then
raise data_not_found;
else
select emp_sal into esal from employee where emp_no=idno;
dbms_output.put_line('Salary updated.');
dbms_output.put_line('Old salary : '||esal);
esal:=esal+100;
dbms_output.put_line('New salary : '||esal);
end if;

exception
when data_not_found then dbms_output.put_line('No data found.');
when others then dbms_output.put_line('Error');
end;

declare
idno number(5);
esal number(5);
temp number(5);
begin
idno:=102;
retrieve(idno,temp,esal);
end;

OUTPUT :

Grade: Sign :

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

PRACTICAL : 14
Aim To perform the concept of exception handler
:
Task Write a PL/SQL block that will accept the employee code, amount and operation. Based
: onspecified operation amount is added or deducted from salary of said employee. Use
user defined exception handler for handling the exception.

set serveroutput on;


declare
cursor c is select * from employee;
greater_exception Exception;
v c%rowtype;
emp_code employee.emp_no%type:=101;
amount number(5):=900;
operation number(2):=1;
newsal number(5);

begin
open c;
loop
fetch c into v;
exit when c%notfound;
if(v.emp_no=emp_code) then
case operation
when 0 then
if amount>v.emp_sal then
raise greater_exception;
else
newsal := v.emp_sal - amount;
dbms_output.put_line('Amount to withdraw is '||amount);
dbms_output.put_line(amount||'Rs. withdrawed.');
dbms_output.put_line('New balance = '||newsal);
end if;
when 1 then
newsal := v.emp_sal + amount;
dbms_output.put_line('Amount to deposit '||amount);
dbms_output.put_line(amount||'Rs. deposited.');
dbms_output.put_line('New balance = '||newsal);
else
dbms_output.put_line('Invalid expression.');
end case;
end if;
end loop;
close c;

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

exception
when greater_exception then
dbms_output.put_line('Amount to withdraw = '||amount||' And balance ='||v.emp_sal);
dbms_output.put_line('Amount is greater.You can not withdraw');
when others then dbms_output.put_line('Error.');
end;

OUTPUT :

Grade : Sign :

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

PRACTICAL: 15
Aim : To perform the concept of trigger
Task: Write a pl/sql block to update the salary where deptno is 10. Generate trigger that
will store the original record in other table before updation take place

create or replace trigger logtri before insert or update or delete on employee for each
row
declare
operation varchar2(5);
begin
if updating then operation:=10;
insert into logt
values(:old.emp_no,:old.emp_name,:old.emp_sal,:new.emp_sal,'update');
end if;
end;

update employee set emp_sal = 1200 where emp_no = 101;


update employee set emp_sal = 2000 where emp_no = 102;
update employee set emp_sal = 3500 where emp_no = 103;

select * from logt;

OUTPUT :-

Grade : Sign :

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

PRACTICAL : 16
Aim : To perform the concept of package
Task : 1 Create a package specification and package body called EMP_PACK that
contains your NEW_EMP procedure as a public construct, and your
VALID_DEPTID function as a private construct. (You can save the specification
and body into separate files.)
CREATE PACKAGE EMP_PACK AS
PROCEDURE NEW_EMP (c_demp.DEPTNO%type);
END EMP_PACK;

Output:

CREATE OR REPLACE PACKAGE BODY EMP_PACK AS


PROCEDURE NEW_EMP(c_demp.DEPTNO%type)
BEGIN
SELECT DEPTNO INTO c_d FROM emp where DEPTNO=c_d;
dbms_output.put_line('dept found ');
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('not found ');
  END NEW_EMP;

Output:

Task : 2 Invoke the NEW_EMP procedure, using 80 as a department number. Because the
dept_no80 does not exist in the DEPT table, you should get an error message as
specified in the exception handler of your procedure.

SET SERVEROUTPUT ON;


DECLARE
Dept_no emp1.deptno%type := 80;
BEGIN
  EMP_PACK.NEW_EMP(Dept_no);
END;

Output:

Task : 3 Invoke the NEW_EMP procedure, using an existing department ID 50.

U & P U Patel Department of Computer Engineering


CE246-DBMS 18CE091

SET SERVEROUTPUT ON;


DECLARE
Dept_no emp1.deptno%type := 50;
BEGIN
EMP_PACK.NEW_EMP(Dept_no);
END;

Output:

Grade : Sign :

U & P U Patel Department of Computer Engineering

You might also like