You are on page 1of 7

Charity Management System

Donor

create table donor(dno int primary key,dname varchar(20),dadd


varchar(150),dmob varchar(10))

CREATE TABLE

select * from donor;

Dno dname dadd dmob

1 hojo kothrud 9824561119

2 rijju kondhwa 9824624819

3 faizan nibm 9028624819

4 armaan kashiwadi 9012589472

5 sadik kondhwa budruk 9012589472

Receiver1

create table receiver1(rno int primary key,rname varchar(20),radd


varchar(150),rmob varchar(10))

CREATE TABLE

Select * from receiver1;

r_no rname radd rmob


kondhwa
5 mehmood budruk 9012301472
4 asad satara 9012301951
3 abdullah katraj 9012348951
2 shoaib hadapsar 9000048951
1 sadique kondhwa 9856471119
Area

create table area(ano int primary key,atype varchar(15),aadd


varchar(200));

CREATE TABLE

Select * from area;

ano atype aadd


1 urban kondhwa
2 rural kashiwadi

Charity_Type

create table charity_type(cno int primary key,charitytype


varchar(15));

CREATE TABLE

Select * from charity_type;

cno charitytype
1 food
2 clothes

Charity

create table charity (dno int references donor,rno int references


receiver1,ano int references area,cno int references charity_type);

CREATE TABLE

Select * from charity;

dno rno ano cno


1 1 1 1
2 2 1 1
3 3 1 2
4 4 2 2
5 5 2 1

_________________________________________________________________
QUERY--->FUNCTION

Write a function that returns the total number of DONOR of a


particular AREA.

( Accept branch name as input parameter.)


create or replace function f1(char(30))

returns int as

$result$

declare

result int;

BEGIN

select count(distinct dno) into result from charity where ano


in(select ano from area where aadd=$1);

return result;

END;

$result$

LANGUAGE plpgsql;

create or replace function f1(char(30))

returns int as

$result$

declare

result int;

BEGIN

select count(distinct dno) into result from charity where ano


in(select ano from area where aadd=$1);

return result;

END;

$result$
LANGUAGE plpgsql;

CREATE FUNCTION

select f1('kondhwa');

f1
----
3
(1 row)

_________________________________________________________________

QUERY--->VIEW

Create a view which detail of all receiver of particular area


create view v2 as select distinct r.r_no,rname,radd,rmob from
charity,receiver1 r,area a where a.ano=charity.ano and r.r_no
=charity.rno;

CREATE VIEW

select * from v1;

r_no | rname | radd |


rmob
-----+----------------------+-------------------------------------+---
-----------------------------

2 "shoaib" "hadapsar " "9000048951"

5 "mehmood" "kondhwa budruk" "9012301472"

3 "abdullah" "katraj " "9012348951"

1 "sadique" "kondhwa " "9856471119"

4 "asad" "satara " "9012301951"

(1 rows)

_________________________________________________________________
QUERY--->CURSOR

Write a stored function using cursor to accept a AREA TYPE from user
and list all DONOR

create or replace function receive(varchar(30)) returns varchar(30) as


$$

declare

d_dname donor.dname%type;

c_doner cursor for select dname from donor where atype='$1';

begin

open c_doner;

loop

fetch c_doner into d_dname;

exit when not done;

raise notice '%',d_dname;

End loop;

return(d_dname);

close c_doner;

end; $$

language plpgsql;

_________________________________________________________________
QUERY---TRIGGER

Write a TRIGGER before update or delete on receiver.If the receiver


name is 5 then raise exception and display “it cannot be deleted
because it’s my friend”

create or replace function del_check() returns trigger as'

Declare

Begin

if old.r_no =''5'' then

raise exception ''it cannot be deleted because it’s my friend '';

end if;

return old;

End;

'language'plpgsql';

CREATE TRIGGER del_receiver BEFORE DELETE ON receiver1 for each row


execute procedure del_check();

DELETE FROM receiver1 where r_no='5';

output--> ERROR: it cannot be deleted because it’s my friend CONTEXT:


PL/pgSQL function del_check() line 5 at RAISE SQL state: P0001

You might also like