You are on page 1of 8

COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

INDEX

S.No. Experiment Date Signature Remarks


1. Write a program to create a function
for employee table and find average 16-04-2020
salary.

2. Write a program to call a function with


arguments to calculate:
a). Area of circle 17-04-2020
b). Lateral surface area of cone
c). Area of cube
d). Volume of cylinder

3. Write a program to create or replace a 18-04-2020


function.

4. Write a program to create a function


to calculate factorial of a number 18-04-2020
using recursion.
COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 16-04-2020
PROGRAM No. 1

AIM: Write a program to create a function for employee table and


find average salary.

Code:
Query for creating the table:
CREATE TABLE Employee(ID Varchar(100), FirstName varchar(100),
Lastname varchar(100), startdate date, enddate date, salary integer,
city varchar(100), description varchar(100));

Query for inserting values into the table:


INSERT INTO Employee VALUES('01', 'Aditya', 'Pradhan',
TO_DATE('08/01/2020', 'MM/DD/YYYY'), TO_DATE('07/31/2020',
'MM/DD/YYYY'), 12400, 'Bangalore', 'TBA');

INSERT INTO Employee VALUES('02', 'Shreyas', 'Jain', TO_DATE('08/01/2020',


'MM/DD/YYYY'), TO_DATE('07/24/2019', 'MM/DD/YYYY'), 50000, 'Jaipur', 'AIT');

INSERT INTO Employee VALUES('03', 'Avi', 'Goyal', TO_DATE('08/01/2020',


'MM/DD/YYYY'), TO_DATE('07/24/2019', 'MM/DD/YYYY'), 200000,
'Bangalore', 'Goldman Sachs');

Query for creating function:


CREATE FUNCTION avg_sal() RETURNS integer AS $avgsal$
declare
avgsal integer;
BEGIN
SELECT avg(salary) into avgsal FROM EMPLOYEE;
RETURN avgsal;
END;
$avgsal$ LANGUAGE plpgsql;

Checking the table entries:


COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 16-04-2020

SELECT * FROM EMPLOYEE;


Query for calculating the average salary:
SELECT avg_sal();

Output:
COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 17-04-2020
PROGRAM No. 2

AIM: Write a program to call a function with arguments to calculate:


a). Area of circle
b). Lateral surface area of cone
c). Area of cube
d). Volume of cylinder

Code:
.)a ). Query for calculating the area of circle:
CREATE FUNCTION circle_area(radius INTEGER) RETURNS
INTEGER AS $$
BEGIN
RETURN 3.14*POWER(radius, 2);
END;
$$ LANGUAGE PLPGSQL;

Query for calling circle_area function:


SELECT circle_area(4);

b). ). Query for calculating the lateral surface area of cone:


CREATE FUNCTION cone_area(radius INTEGER, lat INTEGER) RETURNS
INTEGER AS $$
BEGIN
RETURN 3.14*radius*lat;
END;
$$ LANGUAGE PLPGSQL;

Query for calling cone_area function:


SELECT cone_area(5, 3);

.)c ). Query for calculating the area of cube:


COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 17-04-2020

CREATE FUNCTION cube_area(side INTEGER) RETURNS


INTEGER AS $$ BEGIN RETURN 6*POWER(side, 2); END; $$
LANGUAGE PLPGSQL;

Query for calling cube_area function:


SELECT cube_area(3);

d.) ). Query for calculating the volume of cylinder:


CREATE FUNCTION cylinder_vol(rad INTEGER, height
INTEGER) RETURNS INTEGER AS $$ BEGIN RETURN
3.14*POWER(rad, 2)*height; END; $$ LANGUAGE PLPGSQL;

Query for calling cylinder_vol function:


SELECT cylinder_vol(6, 11);

Output:
COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 17-04-2020
COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 18-04-2020
PROGRAM No. 3

AIM: Write a program to create or replace a function.

Code:
CREATE OR REPLACE FUNCTION func()
RETURNS void AS
$$
BEGIN
RAISE NOTICE 'This is function func()';
END;
$$ language plpgsql;

Output:
COLLEGE OF TECHNOLOGY AND ENGINEERING, UDAIPUR

Name: DEEPAK YADAV Class: B.Tech. IV Year [I.T.]


Subject: Parallel and Distributed Database

Date: 18-04-2020
PROGRAM No. 4

AIM: Write a program to create a function to calculate factorial of


a number using recursion.

Code:
CREATE FUNCTION factorial(num INTEGER)
RETURNS INTEGER AS $$
BEGIN
if num = 1
then RETURN 1;
else RETURN (num*factorial(num-1));
end if;
END;
$$ language plpgsql;

Output:

You might also like