Dbms Answers All - Haam's Community
Dbms Answers All - Haam's Community
Dbms Answers All - Haam's Community
HAAM’S COMMUNITY
WORKSHEET -1.1
Q1. Create a table called Employee with the following structure.
Name Type
Empno Number
Ename Varchar2(10)
Job Varchar2(10)
Mgr Number
Sal Number
a. Add a column commission with domain to the Employee table.
b. Insert any five records into the table.
c. Update the column details of job
d. Rename the column of Employ table using alter command.
e. Delete the employee whose Empno is 105.
CODE -
empno number,
ename varchar2(10),
job varchar2(10),
mgr number,
sal number
);
set job='salesman'
select * from employee; alter
table employee rename
column job to post;select *
from employee; delete from
employee where
empno=105;
Code :
create table employee
(
empno number,
ename varchar2(10),
job varchar2(10),
mgr number,
sal number
);
alter table employee
add commission varchar(10);
insert into employee
values(110,'yana','clerk',5402,3000,100);
insert into employee
values(105,'avi','manager',5112,5000,200);
insert into employee
values(169,'nitin','salesman',5407,10000,300);
insert into employee
values(180,'sahil','designer',5445,8000,150);
insert into employee
values(147,'kartik','analyst',5123,3000,200);
update employee
set job='salesman'
select * from employee;
alter table employee
rename column job to post;
select * from employee;
delete from employee
where empno=105;
select * from employee;
in(10,20);
d. List all employee names and their manager whose manager is 7902
or 7566 0r 7789.
e. List all employees who belongs to the department 10 or 20.
WORKSHEET -1.2
CODE :
CODE :
Output :
Q2- Write a PL/SQL program to find area and
perimeter of rectangle.
Code :
DECLARE
l NUMBER(4, 2) := 3;
b NUMBER(4, 2) := 3;
a NUMBER(4, 2);
p NUMBER(4, 2);
BEGIN
a := l * b;
p := 2 * (l + b);
dbms_output.Put_line('Area of the rectangle is '
|| a);
dbms_output.Put_line('Perimeter of the rectangle is '
|| p);
END;
Output :
WORKSHEET -2.1
END IF;
i := i + 1;
exit WHEN i = 100;
END LOOP;
dbms_output.new_line;
END;
/
OUTPUT :
OUTPUT :
WORKSHEET -2.3
Q1- Write a PL/SQL block to display the last name of manager, and their
departments for a particular city, using parameters with a default value in explicit cursor.
CODE))
OUTPUT)
Q2–
Write a block in PL/SQL to print the specific number
of
rows from a table. CODE :
DECLARE
CURSOR emp_cur IS
SELECT first_name,last_name, salary FROM
employees
WHERE ROWNUM < 8
ORDER BY first_name;
emp_fname employees.first_name%TYPE;
emp_lname employees.last_name%TYPE;
emp_sal employees.salary%TYPE;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO emp_fname,emp_lname,
emp_sal;
IF emp_cur%NOTFOUND THEN
EXIT;
ELSE
DBMS_OUTPUT.PUT_LINE
(rpad('Name: ' || emp_fname||' '|| emp_lname ,30)||
'salary: ' ||
emp_sal);
END IF;
END LOOP;
END;
WORKSHEET – 2.4
Q1–
Aim/Overview of the practical:
Write a query to create a view that finds the
salesman who has the customer with the
highest
order at least 3 times on a day.
Relation1: Customer
customer_id | cust_name | city | grade |
salesman_id
-------------+----------------+------------+-------+-------
------
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
Relation2: elitsalesman
salesman_id | name | city | commission
-------------+------------+----------+------------ 5001
| James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
CODE:-
create table Customer (customer_id
int,cust_name varchar(100),city
varchar(50),grade int,salesman_id int); insert
into Customer
values(3002,'Nick Rimando','New
York',100,5001); insert into Customer
values(3007,'Brad Davis','New York',200,5001);
insert into Customer
values(3005,'Graham
Zusi','California',200,5002); insert into Customer
values(3008,'Julian Green','london',300,5002);
insert into Customer
values(3004,'Fabian Johnson','Paris',300,5006);
insert into Customer
values(3009,'Geoff Cameron','Berlin',100,5003);
insert into Customer
values(3003,'Jozy Altidor','Moscow',200,5007);
insert into Customer
values(3001,'Brad guzan','London',' ',5005);
select * from Customer;
create table elitSalesman (salesman_id int,name
varchar(100),city
varchar(50),commission float); insert into
elitSalesman
values(5001,'James Hoog','New York',0.15);
insert into elitSalesman
values(5002,'Nail Knite','Paris',0.13); insert into
elitSalesman
values(5005,'Pit Alex','London',0.11); insert into
elitSalesman
values(5006,'Mc Lyon','Paris',0.14); insert into
elitSalesman
values(5007,'Paul Adam','Rome',0.13); insert into
elitSalesman
values(5003,'Lauson Hen','San Jose',0.12); select
* from elitSalesman;
create table Orders (order_no int,purch_amt
float,ord_date
varchar(10),customer_id int,salesman_id int);
insert into Orders
values(70001,150.5,'2012-10-05',3005,5007);
insert into Orders
values(70009,270.65,'2012-09-10',3001,5005);
insert into Orders
values(70002,65.26,'2012-10-05',3002,5001);
insert into Orders
values(70004,110.5,'2012-08-17',3009,5003);
insert into Orders
values(70007,948.5,'2012-09-10',3005,5002);
insert into Orders
values(70005,2400.6,'2012-07-27',3007,5001);
insert into Orders
values(70008,5760,'2012-09-10',3002,5001);
insert into Orders
values(70010,1983.43,'2012-10-10',3004,5006);
insert into Orders
values(70003,2480.4,'2012-10-10',3009,5003);
insert into Orders
values(70012,250.45,'2012-06-27',3008,5002);
insert into Orders
values(70011,75.29,'2012-08-17',3003,5007);
insert into Orders
values(70013,3045.6,'2012-04-25',3002,5001);
select * from orders;
CREATE VIEW sHighestOrders3
AS SELECT a.name
FROM elitsalesman a, orders b
WHERE a.salesman_id = b.salesman_id
AND b.purch_amt =
(SELECT MAX (purch_amt)
FROM orders c
WHERE c.ord_date = b.ord_date)
GROUP BY a.name
HAVING count(*) >= 3; select
* from sHighestOrders3;
Q2–
Consider the following relations:
Salesman
salesman_id | name | city | commission
-------------+------------+----------+------------ 5001
| James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12
Customer
customer_id | cust_name | city | grade |
salesman_id
-------------+----------------+------------+-------+---------
----
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005
CODE:
create table salesman(
salesman_id number(20),
name varchar(225), city
varchar(225),
commission float(20))
;
CODE:
CREATE OR REPLACE PROCEDURE E_bill(unit in
number,net out number) is
BEGIN
--drop procedure E_bill
--for first 100 unit :RS 5 per unit
if (unit between 0 and 100) then
net:= unit*5;
END;
/
Declare
---INPUT UNIT
unit1 number:=200;
total number;
BEGIN
E_bill(unit1,total);
dbms_output.put_line('FOR FIRST ' || unit1 || ' UNITS ' || '
ELECTRICITY BILL IS :' || total);
END;
OUTPUT:
Q2–
Write a procedure to calculate factorial of a number entered by
user.
CODE -
CREATE or replace PROCEDURE fact(n number) as
f number:=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('factorial = ' || f);
end;
/
exec fact(5)
OUTPUT -
WORKSHEET – 3.2
Q1–
Create a package circle having the following functions:
1. Area of circle.
2. Circumference of circle.
Radius is to be passed as argument to both the functions to
calculate values.
CODE:
create or replace package circle AS
end;
begin
ar:=3.14*r*r;
return (ar);
end area;
begin
cir:=2*3.14*r;
end circum;
end;
declare
r number:=10;
a number;
c number;
begin
a:=circle.area(r,a);
circle.circum(r,c);
end;
OUTPUT –
Q2–
Create a package named as INFO which contains procedure that is
passed a student's identification number and return student's full
name and phone number from the student table to the calling
program and function, pass a department number to it. If the
DEPT table does not contain the department number, return a
FALSE value otherwise return a TRUE value. Print the
appropriate message to the calling program.
CODE:
CREATE TABLE student (ID INTEGER,FNAME
VARCHAR(20),LNAME VARCHAR(20),GENDER CHAR(1),dept
number(20),p_no number(10));
INSERT INTO student
VALUES(101,'ROHIT','SINGH','M',20,9867543210);
INSERT INTO student
VALUES(102,'VISHAL','PANDEY','M',21,8998899809);
INSERT INTO student
VALUES(103,'PIHU','KUMARI','M',22,9080706050);
INSERT INTO student
VALUES(104,'RAHUL','SINHA','F',23,9192939495);
INSERT INTO student
VALUES(105,'HARRY','POTTER','M',24,8765432198);
-----------------------------------------------------------
BEGIN
END stu_info;
END stu;
-----------------------------------package body----------------------------------
-
s_fname student.fname%TYPE;
BEGIN
if s_dept=10 then
dbms_output.put_line('TRUE ');
else
dbms_output.put_line(' FALSE ');
end if;
END stu_dept;
END stu1;
----------------------------------student info------------------------------------
declare
sid number;
begin
sid:=102;
stu.stu_info(sid);
end;
------------------------------------department-----------------------------------
declare
sdept number;
begin
sdept:=20;
stu1.stu_dept(sdept);
end;
OUTPUT :
SS1 –
SS2 –
WORKSHEET – 3.3
Q1–
Create a row trigger which ensures that whenever salary is updated in the
emp table, then some message is displayed to the user and if new salary is
lesser than old salary display the difference between both the salaries.
CODE:
create table emp(sno number(10), fname varchar(20), eid number(10),salary
number(20));
insert into emp values(001,'JAMES',101,20000);
select * from emp
Declare
sal_diff number;
Begin
dbms_output.put_line('Salary Updated');
if(:new.salary<:old.salary) then
sal_diff:= :old.salary-:new.salary;
dbms_output.put_line('DIFFERENCE : ' || sal_diff);
end if;
end;
/
----------------------------
update emp set salary=salary-500 where sno=001;
OUTPUT:
Q2 –
Apply a trigger on relation client (sno, fname, lname, eid, and password) on
attributes fname and password after/before delete or update these columns.
The trigger should insert the old or deleted values from both these columns
into a new table client1.
CODE:
create table client(sno number(10), fname varchar(20), eid number(10),passwrd
varchar(20));
insert into client values(001,'JAMES',101,'james101');
select * from client
----------------
create table client1(sno1 number(10), fname1 varchar(20), eid1
number(10),passwrd1 varchar(20));
--drop table client
select * from client1
---------------------
create or replace trigger client_trigg
before delete or update on client
for each row
Begin
insert into client1 values(:old.sno,:old.fname,:old.eid,:old.passwrd) ;
end;