0% found this document useful (0 votes)
3 views5 pages

DBMS PRG5 Lab

The document outlines the implementation of SQL programming for querying and managing a database, focusing on stored procedures, functions, and triggers. It includes examples of a stored procedure to print serial numbers, a function to calculate factorial values, and triggers for auditing insert and update operations in a table. The results indicate that all SQL components were executed successfully.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

DBMS PRG5 Lab

The document outlines the implementation of SQL programming for querying and managing a database, focusing on stored procedures, functions, and triggers. It includes examples of a stored procedure to print serial numbers, a function to calculate factorial values, and triggers for auditing insert and update operations in a table. The results indicate that all SQL components were executed successfully.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EX-NO : 5

QUERYING/MANAGING THE DATABASE USING SQL


DATE : PROGRAMMING

AIM:
To implement Querying/managing the database using SQL programming – stored procedures/functions
Constraints security using Triggers.

Stored Procedures:-
1. Write a PL/SQL program print serial numbers up to given limit using Stored Procedure

Program
Create database ex5;
Use ex5;
DELIMITER $$
create procedure print_serial_numbers()
BEGIN
DECLARE i INT;
DECLARE str varchar(30);
SET i=1;
SET str='';
krish:loop
if i>10 THEN
leave krish;
end if;
SET str=CONCAT(str,i,' ');
SET i=i+1;
END loop;
select str “Serial Numbers”;
END $$
DELIMITER ;

call print_serial_numbers;

OUTPUT
FUNCTION
2. Write a PL/SQL program to find factorial values for given number using Function

Program

delimiter $$
CREATE FUNCTION factorial(n INT)
RETURNS INT
DETERMINISTIC
BEGIN
declare fact int default 1;
declare cnt int;
set cnt=n;
fact_loop:repeat
set fact=fact*cnt;
set cnt=cnt-1;
until cnt=1
end repeat;
return fact;
END$$
delimiter ;
select factorial(5) "Factorial of Given Number ";

OUTPUT

TRIGGER
1. create table SCM(empid int primary key,empname varchar(30),salary numeric(10,2),gender
varchar(10),dept varchar(30));
2. insert into SCM values
(100,"ABI",15000,"Female","ACC"),

(101,"Babu",5000,"Male","COM"),

(102,"Chinmaie",12000,"Female","SAL"),

(103,"Dinesh",1000,"Male","SAL"),

(104,"Eswari",4000,"Female","COM");
3. select *from SCM;

1. Create another table for transfer the data

create table SCM_Audit_Test(Emp_id int , Operation varchar(100),Inserted_Date date);

select *from SCM_Audit_Test;

2. Create Trigger for AFTER inserted

create trigger SCM_insert

after insert on SCM

for each row

begin

insert into SCM_Audit_Test(Emp_id,Operation,Inserted_Date)


values(new.empid,"INSERT_NEW_RECORD",now());

end $$

delimiter ;

3. Now add new record for checking trigger is working or not

insert into SCM values (109,"joyal",8000,"male","Cbe");

4. Open the second table the output like as following

select *from SCM_Audit_Test;


TRIGGER FOR AFTER UPDATED RECORD INTO A TABLE

BEFORE UPDATE RECORD

select *from SCM;

1. Create another table for storing updated details

create table SCM_Update_Test(id int auto_increment primary key,employee_id int,old_salary


numeric(10,2),new_salary numeric(10,2),Updated_Date timestamp default current_timestamp);

select *from SCM_Update_Test;

2. Create TRIGGER for AFTER UPDATE RECORD IN A TABLE

delimiter $$

create trigger SCM_update

after update on SCM

for each row

begin

insert into SCM_Update_Test(employee_id,old_salary,new_salary)


values(old.empid,old.salary,new.salary);

end $$

delimiter ;

3. Now you can Update record like as following

update SCM set salary=20000 where empid=101;

update SCM set salary=15000 where empid=103;


update SCM set salary=18000 where empid=109;

4. Now you can view the Update_table

select *from SCM_Update_Test;

RESULT:

The Stored Procedure, Function and Trigger has been executed successfully.

You might also like