0% found this document useful (0 votes)
13 views

PRACADBMS

The first document describes a stored procedure that accepts a student ID and returns the highest percentage and name of that student. The second document shows a stored procedure that accepts a number as input and calculates the sum of its digits. The third document creates a stored procedure that generates the natural number series from 1 to 10.

Uploaded by

wefot57202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PRACADBMS

The first document describes a stored procedure that accepts a student ID and returns the highest percentage and name of that student. The second document shows a stored procedure that accepts a number as input and calculates the sum of its digits. The third document creates a stored procedure that generates the natural number series from 1 to 10.

Uploaded by

wefot57202
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

WAP which accepts the student roll no and returns the highest percent and Write a SQL that

e a SQL that will take a number as input and calculate sum


name of that student to the calling block. of its digits.
STUDENT(Stud_ID,Stud_name,percent); Delimiter //
create table student(studId integer primary key,studName varchar(25), create procedure sumofdigits(in n int(4))
percent decimal); begin declare r integer;
insert into student values(1,'Kumud Mishra',65.25); declare s integer;
insert into student values(2,'Sushant Rajput',72.85); set @s = 0;
insert into student values(3,'Dikvijay Singh',82.74); while @n<>0 do
select * from student; set @r=@n%10;
set @s=@s+@r;
Drop procedure findStudent; Set @n=@n div 10;
Delimiter $$ end while;
create procedure findStudent(in roll int(4), out sname varchar(25),out per decimal) select @s;
begin end // delimiter ;
select studName into sname from student where studId=roll;
select percent into per from student where studId=roll; set @n=12345;
end $$ delimiter ; call sumofdigits(@n);
set @r=2;
set @sn=' '; Write a SQL that will generate the natural series from 1 to 10.
set @p=1.0; Delimiter //
call findStudent(@r,@sn,@p); create procedure Natural_number()
select @sn,@p; begin
Declare n INT;
Write a procedure which accepts the emp no and returns the associated Declare str VARCHAR(255);
emp name. If emp no does not exist then give proper error message. set n=1;
EMP(Empno, Empname). set str='';
Drop table emp; label1: Loop
create table emp(empno int ,empname varchar(35)); IF n>10 THEN
insert into emp values(01,'Peter Parker'),(02,'Tony Stark'),(03,'Steve Rogers'), LEAVE label1;
(04,'Bruce Banner'); END IF;
select * from emp; set str= CONCAT(str,n,','); set
n=n+1;
Drop procedure checkId; END LOOP;
delimiter // SELECT str;
create procedure checkId(in eid int(5),out ename varchar(100)) END // Delimiter ;
begin
declare msg varchar(255); call Natural_number;
set msg='Employee ID does not exist';
select empname into ename from emp where empno=eid; Write a SQL that will generate the Fibonacci series upto7 terms.
if ename is Null then select msg; drop procedure fibo;
end if; Delimiter //
end // delimiter ; create procedure fibo()
set @id=1; begin declare f int;
set @n=''; declare f1 int;
call checkId(@id,@n); declare f2 int;
select @n; declare n INT;
set @id=5; set @n=''; declare str VARCHAR(255);
call checkId(@id,@n); set @f1=0;
select @n; set @f2=1;
Create a cursor for updating the salary of emp working in deptno 10 by 20%. set @n=0;
If any rows are affected than display the no of rows affected. Use implicit cursor. set @str='0,1,';
select* from emp; while @n<5 do
Drop procedure salaryUpdate(); set @f=@f1+@f2;
Delimiter// set @f1=@f2;
create procedure salaryUpdate() set @f2=@f;
begin set @str=CONCAT(@str,@f,',');
declare count int; set @n=@n+1;
declare id int; end while;
declare sal decimal(10,2); select @str;
declare finished int default 0; end // Delimiter;
declare emp_cursor cursor for select eno,salary from emp where deptno=10; call Fibo();
declare continue handler for not found set finished=1; open emp_cursor;
set count =0; Write a SQL that will accept a string and reverse it.
get_record: loop Drop procedure rString;
fetch emp_cursor into id,sal; Delimiter //
if finished=1 then LEAVE create procedure rString (in str varchar(20), in rstr varchar(20))
get_record; begin
end if; select REVERSE(@str) into @rstr;
update emp set salary=salary*1.2 where eno=id; end //delimiter ;
set count=count+1; end loop; set @str='MOCK TEST';
close emp_cursor; set @rstr='';
select count; end // delimiter ; call rString(@str,@rstr);
call salaryUpdate(); select @rstr;
select * from emp;
Create a cursor for the emp table. Produce the output in following
Create a cursor for updating the salary of emp working in deptno 10 by
format: {empname} employee working in department {deptno}
20%.
earns Rs. {salary}. EMP(empno, empname, salary, deptno);
If any rows are affected than display the no of rows affected. Use implicit
Drop table emp;
cursor.
create table emp(eno int primary key,ename varchar(35),
select* from emp;
salary decimal(10,2),deptno int);
Drop procedure salaryUpdate();
insert into emp values(1,'Tony Stark',125000.00,10),
Delimiter//
(2,'Peter Parker',25000.00,11),
create procedure salaryUpdate()
(3,'Steve Rogers',50000.00,10),(4,'Bruce Banner',35000.00,10);
begin
select * from emp;
declare count int;
declare id int;
drop procedure showRecord();
declare sal decimal(10,2);
delimiter //
declare finished int default 0;
create procedure showRecord()
declare emp_cursor cursor for select eno,salary from emp where
begin
deptno=10;
declare name varchar(255);
declare continue handler for not found set finished=1; open emp_cursor;
declare dno int;
set count =0;
declare sal decimal(10,2);
get_record: loop
declare finished int default 0;
fetch emp_cursor into id,sal;
declare msg varchar(255);
if finished=1 then LEAVE
declare emp_cursor cursor for select ename,deptno,salary from emp;
get_record;
declare continue handler for not found set finished=1;
end if;
open emp_cursor; set msg='';
update emp set salary=salary*1.2 where eno=id;
get_information: loop
set count=count+1; end loop;
fetch emp_cursor into name,dno,sal;
close emp_cursor;
if finished=1 then leave get_information;
select count; end // delimiter ;
end if;
call salaryUpdate();
set msg=CONCAT(name," working in department ",dno," earn RS.",sal);
select * from emp;
select msg; end loop;
close emp_cursor ;
end //delimiter ;
call showRecord();

Write a Trigger that stores the old data table of student table in
student_backup while updating thestudent table. Student_backup
(Stud_ID, Stud_name, Address, Contact_no, Branch, Operation_date)
Student (Stud_ID, Stud_name, Address, Contact_no, Branch)
create table student(studId integer primary key,studName varchar(25));
insert into student values(1,'gk');
insert into student values(2,'rs');
select * from student;

create table studentBackup(studId integer primary key,


studName varchar(25), opDate date);
insert into studentBackup values(1,'gk','2024-01-21');
select * from studentBackup;
delete from studentBackup;
drop trigger backupTrigger;
delimiter //
create trigger backupTrigger before update on student for each row
begin
insert into studentBackup set studId=OLD.studId,
studName=OLD.studName,
opDate=NOW(); end // delimiter ;
update student set studName='ratna' where studId=2;

Write a trigger which checks the age of employee while inserting the record in emp table. If it is negative then generate the error and display proper
message.
create table employee(empId integer primary key,empName varchar(25),empAge integer);
insert into employee values(1,'gk',25);
select * from employee;
drop trigger checkTrigger;
Delimiter //
create trigger checkTrigger before insert on employee for each row
begin
declare msg varchar(50);
if NEW.empAge <0 then set NEW.empAge = 0;
end if;
end//delimiter ;
insert into employee values(3,'cd',-5);
select * from employee;

You might also like