0% found this document useful (0 votes)
11 views9 pages

Wa0009.

Dbms

Uploaded by

smomin1008
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)
11 views9 pages

Wa0009.

Dbms

Uploaded by

smomin1008
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/ 9

ASSIGNMENET 01

(Set A)
Q.1

fybcsdb262=> select*from students1;


s_id | s_name | age | dep
------+---------+-----+------------------
1 | Saad | 18 | computer science
2 | pradnya | 19 | medical
3 | m.saifi | 17 | IT
fybcsdb262=> select*from course1;
c_id | c_name | credit
------+--------+--------
1 | BCS | 2
2 | MBBS | 3
3 | bsc it | 4
fybcsdb262=> select*from

students1_course1;
s_id | c_id
------+------
1|1
2|2
3|3
1)
create or replace function hhh()
RETURNS TABLE(
s_id int,
s_name varchar,
age int,
dep varchar
)
AS $$
BEGIN
RETURN QUERY
SELECT s.s_id,s.s_name,s.age,s.dep
FROM students1 s;
END;
$$
LANGUAGE plpgsql;
output :
fybcsdb262=> select hhh();
hhh
---------------------------------
(1,Saad,18,"computer science")
(2,pradnya,19,medical) (3,m.saifi,17,IT)
(3 rows)
2)

create or replace function student_list()


RETURNS TABLE(
s_id int,
s_name varchar,
age int,
dep varchar,
c_name varchar
)
AS $$
BEGIN
RETURN QUERY
SELECT s.s_id,s.s_name,s.age,s.dep,c.c_name
FROM students1 s
INNER JOIN students1_course1 sc ON s.s_id = sc.s_id
INNER JOIN course1 c ON sc.c_id = c.c_id;
END;
$$
LANGUAGE plpgsql;

output :
fybcsdb262=> select student_list();
student_list ---------------------------
---------- (1,Saad,18,"computer
science",BCS)
(2,pradnya,19,medical,MBBS)
(3,m.saifi,17,IT,"bsc it")
Q.2
create table employee11(e_id int primary key,e_name varchar,age int,salary

float); CREATE TABLE


fybcsdb262=> select*from employee11;
e_id | e_name | age | salary
------+---------+-----+--------
1 | mubeen | 18 | 50000
2 | pradnya | 17 | 45000
3 | fatima | 19 | 40000
fybcsdb262=> create table department11(d_id int primary key,d_name
varchar,location varchar);
fybcsdb262=> select*from department11;
d_id | d_name | location
------+------------------+-------------
101 | computer science | pune
102 | medical | mumbai
103 | marketing | azam campus
(3 rows)

create table emp11_emp11(e_id int references employee11(e_id),d_id

int references
department11(d_id));
CREATE TABLE

^
fybcsdb262=> select*from emp11_emp11;
e_id | d_id
------+------
1 | 101
1 | 102
3 | 103
(3 rows)
1)

create or replace function Saad2()


RETURNS TABLE(
d_id int
)
AS $$
BEGIN
RETURN QUERY
SELECT d.d_id
FROM department11 d;
END;
$$
LANGUAGE plpgsql;
output :

fybcsdb262=> select Saad2();


Saad2
--------
101
102
103
2)
create or replace function Saad3()
returns void
AS $$
BEGIN
update employee11 set
salary=salary+1.2
where e_id=2;
END;
$$
LANGUAGE plpgsql;
output :
fybcsdb262=> select*from employee11;
e_id | e_name | age | salary
------+---------+-----+--------
3 | fatima | 19 | 40000
1 | mubeen | 18 | 11000
2 | pradnya | 17 | 10800

(Set B)

Q.1

fybcsdb262=> select*from employe10;


e_no | e_name | joining_date
------+----------------+--------------
1 | mubeen | 2005-08-22
2 | pradnya | 2005-10-18
3 | mohammed saifi | 2006-10-18
4 | Aisha | 2010-09-21
5 | azlan | 2020-09-23
fybcsdb262=> select*from project;
p_no | p_name | p_type | duration
------+------------+-------------+----------
1 | calculator | application | 1
2 | celender | application | 5
3 | hotel web | website | 4
4 | clone web | website | 3
5 | camera | application | 1
fybcsdb262=> select*from employe10_project;
e_no | p_no | start_date
------+------+------------
1 | 1 | 2010-07-23
2 | 2 | 2010-08-22
3 | 3 | 2011-09-19
4 | 4 | 2020-12-26
5 | 5 | 2015-05-26
1)

CREATE FUNCTION ASS5(pname varchar)


RETURNS INT
AS $$
DECLARE
ecount int;
BEGIN
SELECT COUNT(ep.e_no)INTO ecount from emloye10_project join project p
on
ep.p_no=p.p_no where p.pname = p_name;
return ecount;
END;$$
language plpgsql;

2)

CREATE FUNCTION ASS5(pname varchar)


RETURNS INT
AS $$
DECLARE
ecount int;
BEGIN
SELECT COUNT(ep.e_no)INTO ecount from emloye10_project join project p
on
ep.p_no=p.p_no where p.pname = p_name;
return ecount;
END;$$
language plpgsql;
Q.2

fybcsdb262=> select*from agents;


a_id | a_name | a_type
------+---------------+-----------
101 | Saad shaikh | universal
102 | fatima shaikh | general
103 | pradnya pawar | universal
fybcsdb262=> select*from estate;
estates_id | estates_name | estate_type | location | price | a_id
------------+--------------------+-------------------+----------+---------+-----
-
1 | luxe lixing realty | ownership | pune | 1000000 | 101
2 | grandeur homes | possessory estate | mumbai | 250000 | 102
3 | summit estates | owenship | raigad | 3000000 | 103
1)

CREATE or replace FUNCTION Q1(loc varchar)


RETURNS table(
estates_type varchar(50),
agents_name varchar(30)
)
AS $$
BEGIN
return query
select estate.estate_type,agents.a_name from estate
inner join agents on estate.a_id=agents.a_id
where estate.location=loc;
END; $$
language plpgsql;

2)

CREATE or replace FUNCTION Q4_1(agent_name varchar(50))


RETURNS float
AS $$
declare
total_Price float;
BEGIN
select sum(price) into total_price from estate
inner join agents on estate.a_id=agents.a_id
where agents.a_name=agent_name;
return total_price;
end; $$
language plpgsql;

ouput :

fybcsdb262=> select Q4_1('fatima shaikh');


q4_1
--------
250000

(Set C)
Q.1
create table customer(c_id int primary key,c_name varchar,c_add
varchar,moblie_no int);
CREATE TABLE
create table plans(p_id int primary key,p_name varchar,monthly_fee

int);
CREATE TABLE
create table bills(b_id int primary key,b_amount int,date date,c_id int
references customer(c_id));
CREATE TABLE
create table cus_plan(c_id int references customer(c_id),p_id int

references
plans(p_id));
CREATE TABLE
create table cus_plan(c_id int references customer(c_id),p_id int

references
plans(p_id));
CREATE TABLE
fybcsdb262=> select*from customer;
c_id | c_name | c_add | moblie_no
------+---------+--------+-----------
1 | Saad | pune | 7775
2 | fatima | mumbai | 8854
3 | pradnya | panvel | 4586
fybcsdb262=> select*from plans;
p_id | p_name | monthly_fee
102 | bbb | 800
103 | ccc | 1200
fybcsdb262=> select*from bills;
b_id | b_amount | date | c_id
------+----------+------------+------
1001 | 1500 | 2022-08-22 | 1
1002 | 2400 | 2020-09-21 | 2
1003 | 4390 | 2019-02-22 | 3
fybcsdb262=> select*from cus_plan;
c_id | p_id
------+------
1 | 101
2 | 102
3 | 103
1)
CREATE or replace FUNCTION setc1(plan_name
varchar(40))
RETURNS table
(c_name varchar)
AS $$
BEGIN
return query
select customer.c_name from customer
join cus_plan cp on customer.c_id=cp.c_id
join plans p on cp.p_id=p.p_id
where p.p_name=plan_name;
end;
$$ language plpgsql;
output :
fybcsdb262=> select setc1('aa');
setc1
-------
Saad
(1 row)
2)
create or replace function bills(bill_id int)
returns table(c_id int,c_name varchar,c_add varchar,moblie_no int)
as $$
begin
return query
select c.c_id,c.c_name,c.c_add,c.moblie_no from customer c join bills
b on
b.c_id=c.c_id where bill_id=b.b_id;
end;
$$ language plpgsql;
output :
fybcsdb262=> select bills(1001);
bills
---------------------
(1,Saad,pune,7775)

You might also like