You are on page 1of 113

RDBMS

Assignment 1 : Simple Queries

SET A(Person-Area Database)

[NRC@localhost ~]$ su - postgres

Password:

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=# Create database person4;

CREATE DATABASE

postgres=# \c person4

psql (8.4.5)

You are now connected to database "person4".

postgres=# \c person4

psql (8.4.5)

You are now connected to database "person4".

person4=# create type dtype as enum('urban','rural');

CREATE TYPE

person4=# create table area(aname varchar(20) primary key,atype dtype);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "area_pkey" for table "area"

CREATE TABLE

person4=# create table person(pno int primary key,pname varchar(20),bday date,income int,aname
varchar(20) references area);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "person_pkey" for table
"person"

CREATE TABLE

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 1


person4=# insert into area values('Pune','urban');

INSERT 0 1

person4=# insert into area values('Mumbai','urban');

INSERT 0 1

person4=# insert into area values('Pune','urban');

ERROR: duplicate key value violates unique constraint "area_pkey"

person4=# insert into area values('Nashik','urban');

INSERT 0 1

person4=# insert into area values('Nagpur','urban');

INSERT 0 1

person4=# insert into area values('Kolhapur','rural');

INSERT 0 1

person4=# insert into area values('Nagar','rural');

INSERT 0 1

person4=# select * from area;

aname | atype

----------+-------

Pune | urban

Mumbai | urban

Nashik | urban

Nagpur | urban

Kolhapur | rural

Nagar | rural

(6 rows)

person4=# insert into person values(101,'Ashok','2013-07-07',40000,'Pune');

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 2


person4=# insert into person values(102,'Amol','2013-02-09',25500,'Mumbai');

INSERT 0 1

person4=# insert into person values(103,'Shiva','2013-08-02',25500,'Nashik');

INSERT 0 1

person4=# insert into person values(104,'Pratik','2013-01-08',15500,'Nagpur');

INSERT 0 1

person4=# insert into person values(105,'Prashant','2013-11-05',5500,'Kolhapur');

INSERT 0 1

person4=# insert into person values(106,'Prashant','2013-01-02',51500,'Nagar');

INSERT 0 1

person4=# select * from person;

pno | pname | bday | income | aname

-----+----------+------------+--------+----------

101 | Ashok | 2013-07-07 | 40000 | Pune

102 | Amol | 2013-02-09 | 25500 | Mumbai

103 | Shiva | 2013-08-02 | 25500 | Nashik

104 | Pratik | 2013-01-08 | 15500 | Nagpur

105 | Prashant | 2013-11-05 | 5500 | Kolhapur

106 | Prashant | 2013-01-02 | 51500 | Nagar

(6 rows)

person4=# select pname from person where aname='Pune';

pname

-------

Ashok

(1 row)

person4=# select pname from person where pname like 'A%';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 3


pname

-------

Ashok

Amol

(2 rows)

person4=# select count(pname) from person where bday='2013-07-07';

count

-------

(1 row)

person4=# select count(pname) from person where income<=30000;

count

-------

(1 row)

person4=# select count(pname) from person where income between 25000 and 50000;

count

-------

(1 row)

person4=# select * from person order by pname;

pno | pname | bday | income | aname

-----+----------+------------+--------+----------

102 | Amol | 2013-02-09 | 25500 | Mumbai

101 | Ashok | 2013-07-07 | 40000 | Pune

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 4


105 | Prashant | 2013-11-05 | 5500 | Kolhapur

106 | Prashant | 2013-01-02 | 51500 | Nagar

104 | Pratik | 2013-01-08 | 15500 | Nagpur

103 | Shiva | 2013-08-02 | 25500 | Nashik

(6 rows)

person4=# update person set aname='Mumbai' where aname='Pune';

UPDATE 1

person4=# select pname,avg(income) from person group by pname;

pname | avg

----------+------------------------

Prashant | 28500.000000000000

Ashok | 40000.000000000000

Pratik | 15500.0000000000000000

Amol | 25500.000000000000

Shiva | 25500.000000000000

(5 rows)

person4=# delete from person where aname in ( select aname from area where atype='urban');

DELETE 4

person4=# select * from person

person4-# ;

pno | pname | bday | income | aname

-----+----------+------------+--------+----------

105 | Prashant | 2013-11-05 | 5500 | Kolhapur

106 | Prashant | 2013-01-02 | 51500 | Nagar

(2 rows)

person4=# update person set bday='2013-08-08' where pno=105;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 5


UPDATE 1

person4=# select * from person;

pno | pname | bday | income | aname

-----+----------+------------+--------+----------

106 | Prashant | 2013-01-02 | 51500 | Nagar

105 | Prashant | 2013-08-08 | 5500 | Kolhapur

(2 rows)

person4=# select pname,extract(month from bday)=08 from person;

pname | ?column?

----------+----------

Prashant | f

Prashant | t

(2 rows)

SET B(Movie Database)

person4=# \q

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=# Create database movie4;

CREATE DATABASE

postgres=# \c movie4

psql (8.4.5)

You are now connected to database "movie4".

movie4=# create table movie(mname varchar(20) primary key,ryr int,budget int);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "movie_pkey" for table
"movie"

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 6


CREATE TABLE

movie4=# create table actor(aname varchar(20) primary key,role varchar(20),charges int,aadd


varchar(20));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "actor_pkey" for table "actor"

CREATE TABLE

movie4=# create table prod(pid int primary key,pname varchar(20),padd varchar(20));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "prod_pkey" for table "prod"

CREATE TABLE

movie4=# create table ma(mname varchar(20) references movie ,aname varchar(20) references actor);

CREATE TABLE

movie4=# create table mp(mname varchar(20) references movie ,pid int references prod);

CREATE TABLE

movie4=# insert into movie values('DDLJ',2008,2500000);

INSERT 0 1

movie4=# insert into movie values('HPKD',2013,5500000);

INSERT 0 1

movie4=# insert into movie values('Don',2012,5000000);

INSERT 0 1

movie4=# insert into movie values('Kick',2014,6500000);

INSERT 0 1

movie4=# insert into movie values('Lai Bhari',2014,5500000);

INSERT 0 1

movie4=# insert into movie values('Dabang',2012,5000000);

INSERT 0 1

movie4=# insert into actor values('SRK','Hero',510000,'Mumbai');

INSERT 0 1

movie4=# insert into actor values('Varun','Hero',450000,'Mumbai');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 7


INSERT 0 1

movie4=# insert into actor values('SK','Hero',550000,'Mumbai');

INSERT 0 1

movie4=# insert into actor values('Sarang','Villian',350000,'Pune');

INSERT 0 1

movie4=# insert into actor values('Bogambo','Villian',350000,'Pune');

INSERT 0 1

movie4=# insert into prod values(101,'Sanjay','Pune');

INSERT 0 1

movie4=# insert into prod values(102,'Yashraj','Pune');

INSERT 0 1

movie4=# insert into prod values(103,'Karan','Mumbai');

INSERT 0 1

movie4=# insert into prod values(104,'Farah','Mumbai');

INSERT 0 1

movie4=# insert into prod values(105,'Ekta','Mumbai');

INSERT 0 1

movie4=# insert into ma values('DDLJ','SRK');

INSERT 0 1

movie4=# insert into ma values('Don','SRK');

INSERT 0 1

movie4=# insert into ma values('HPKD','Varun');

INSERT 0 1

movie4=# insert into ma values('Kick','SK');

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 8


movie4=# insert into ma values('DDLJ','Sarang');

INSERT 0 1

movie4=# insert into ma values('Dabang','SK');

INSERT 0 1

movie4=# insert into ma values('Lai Bhari','Sarang');

INSERT 0 1

movie4=# insert into ma values('Dabang','Bogambo');

INSERT 0 1

movie4=# insert into mp values('DDLJ',101);

INSERT 0 1

movie4=# insert into mp values('DDLJ',103);

INSERT 0 1

movie4=# insert into mp values('HPKD',102);

INSERT 0 1

movie4=# insert into mp values('Don',102);

INSERT 0 1

movie4=# insert into mp values('Don',104);

INSERT 0 1

movie4=# insert into mp values('Dabang',105);

INSERT 0 1

movie4=# insert into mp values('Kick',104);

INSERT 0 1

movie4=# select * from movie

movie4-# ;

mname | ryr | budget

-----------+------+---------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 9


DDLJ | 2008 | 2500000

HPKD | 2013 | 5500000

Don | 2012 | 5000000

Kick | 2014 | 6500000

Lai Bhari | 2014 | 5500000

Dabang | 2012 | 5000000

(6 rows)

movie4=# select * from actor;

aname | role | charges | aadd

---------+---------+---------+--------

SRK | Hero | 510000 | Mumbai

Varun | Hero | 450000 | Mumbai

SK | Hero | 550000 | Mumbai

Sarang | Villian | 350000 | Pune

Bogambo | Villian | 350000 | Pune

(5 rows)

movie4=# select * from prod;

pid | pname | padd

-----+---------+--------

101 | Sanjay | Pune

102 | Yashraj | Pune

103 | Karan | Mumbai

104 | Farah | Mumbai

105 | Ekta | Mumbai

(5 rows)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 10


movie4=# select * from ma;

mname | aname

-----------+---------

DDLJ | SRK

Don | SRK

HPKD | Varun

Kick | SK

DDLJ | Sarang

Dabang | SK

Lai Bhari | Sarang

Dabang | Bogambo

(8 rows)

movie4=# select * from mp;

mname | pid

--------+-----

DDLJ | 101

DDLJ | 103

HPKD | 102

Don | 102

Don | 104

Dabang | 105

Kick | 104

(7 rows)

movie4=# select aname from ma where mname in(select mname from ma where aname='SRK');

aname

--------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 11


SRK

SRK

Sarang

(3 rows)

movie4=# select mname,budget from movie where budget in(select max(budget) from movie);

mname | budget

-------+---------

Kick | 6500000

(1 row)

movie4=# select aname,count(mname) from ma group by aname having count(mname)>=all(select


count(mname) from ma group by aname);

aname | count

-------+-------

SRK | 3

(1 row)

movie4=# select mname from mp group by mname having count(pid)>1;

mname

-------

Don

DDLJ

(2 rows)

movie4=# select aname from actor where charges in(select max(charges) from actor);

aname

-------

SK

(1 row)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 12


movie4=# select pname from prod where pid in(select pid from mp where mname in(select mname from
mp where pid=101));

pname

--------

Sanjay

Karan

(2 rows)

movie4=# select aname from actor where aadd!='Pune';

aname

-------

SRK

Varun

SK

(3 rows)

movie4=# \q

===========================================================================

Assigment 2 : Nested Queries,using aggregate function

SET A(Bank Database)

postgres=# create database bank4;

postgres=# \c bank4;

psql (8.4.5)

You are now connected to database "bank4".

bank4=# create table branch(bid int primary key,bname varchar(30),bcity varchar(30));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "branch_pkey" for table
"branch"

CREATE TABLE

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 13


bank4=# create table customer(cno int primary key,cname varchar(30),caddr varchar(30),ccity
varchar(20));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "customer_pkey" for table
"customer"

CREATE TABLE

bank4=# create table la(lano int primary key,reqmoney text,appmoney text,ladate date);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "la_pkey" for table "la"

CREATE TABLE

bank4=# select * from branch;

bid | bname | bcity

-----+-------+-------

(0 rows)

bank4=# select * from customer;

cno | cname | caddr | ccity

-----+-------+-------+-------

(0 rows)

bank4=# select * from la;

lano | reqmoney | appmoney | ladate

------+----------+----------+--------

(0 rows)

bank4=# create table BCL(bid int references branch,cno int references customer,lano int references
la);CREATE TABLE

bank4=# select * from BCL;

bid | cno | lano

-----+-----+------

(0 rows)

bank4=# insert into branch values(1001,'Aundh','Pune');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 14


INSERT 0 1

bank4=# insert into branch values(1002,'Deccan','Pune');

INSERT 0 1

bank4=# insert into branch values(1003,'Peder Rd','Mumbai');

INSERT 0 1

bank4=# insert into branch values(1004,'Colaba','Mumbai');

INSERT 0 1

bank4=# insert into branch values(1005,'MG Rd','Pune');

INSERT 0 1

bank4=# insert into branch values(1007,'Laxmi Rd','Pune');

INSERT 0 1

bank4=# insert into branch values(1006,'Bibewadi','Pune');

INSERT 0 1

bank4=# insert into customer values(101,'Akshay','Aundh','Pune');

INSERT 0 1

bank4=# insert into customer values(102,'Patil','Sadhashiv Peth','Pune');

INSERT 0 1

bank4=# insert into customer values(103,'Joshi','Colaba','Mumbai');

INSERT 0 1

bank4=# insert into customer values(104,'Pandey','Colaba','Mumbai');

INSERT 0 1

bank4=# insert into customer values(105,'Deshpande','Deccan','Pune');

INSERT 0 1

bank4=# insert into customer values(106,'Dave','Karve Nagar','Pune');

INSERT 0 1

bank4=# insert into customer values(107,'Sharma','MG Rd','Pune');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 15


INSERT 0 1

bank4=# insert into customer values(108,'Gambhir','MG Rd','Pune');

INSERT 0 1

bank4=# insert into customer values(109,'Kumar','MG Rd','Pune');

INSERT 0 1

bank4=# insert into customer values(110,'Rohan','MG Rd','Pune');

INSERT 0 1

bank4=# insert into la values(2001,'10lakh','8lakh','2013-09-09');

INSERT 0 1

bank4=# insert into la values(2002,'15lakh','15lakh','2012-09-09');

INSERT 0 1

bank4=# insert into la values(2003,'5lakh','4lakh','2013-08-19');

INSERT 0 1

bank4=# insert into la values(2004,'2lakh','2lakh','2013-09-29');

INSERT 0 1

bank4=# insert into la values(2005,'1lakh','1lakh','2013-02-28');

INSERT 0 1

bank4=# insert into la values(2006,'7lakh','6lakh','2013-09-28');

INSERT 0 1

bank4=# insert into la values(2007,'6lakh','6lakh','2013-09-21');

INSERT 0 1

bank4=# insert into la values(2008,'4lakh','3lakh','2014-02-21');

INSERT 0 1

bank4=# insert into la values(2009,'5lakh','3lakh','2014-03-21');

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 16


bank4=# insert into la values(2010,'10lakh','8lakh','2014-07-11');

INSERT 0 1

bank4=# select * from branch;

bid | bname | bcity

------+----------+--------

1001 | Aundh | Pune

1002 | Deccan | Pune

1003 | Peder Rd | Mumbai

1004 | Colaba | Mumbai

1005 | MG Rd | Pune

1007 | Laxmi Rd | Pune

1006 | Bibewadi | Pune

(7 rows)

bank4=# select * from customer;

cno | cname | caddr | ccity

-----+-----------+----------------+--------

101 | Akshay | Aundh | Pune

102 | Patil | Sadhashiv Peth | Pune

103 | Joshi | Colaba | Mumbai

104 | Pandey | Colaba | Mumbai

105 | Deshpande | Deccan | Pune

106 | Dave | Karve Nagar | Pune

107 | Sharma | MG Rd | Pune

108 | Gambhir | MG Rd | Pune

109 | Kumar | MG Rd | Pune

110 | Rohan | MG Rd | Pune

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 17


(10 rows)

bank4=# update la set appmoney='$10000' where lano=2001;

UPDATE 1

bank4=# update la set appmoney='$20000' where lano=2002;

UPDATE 1

bank4=# select max(appmoney) from la;

max

------------

$20,000.00

(1 row)

bank4=# update la set appmoney='$30000' where lano=2002;

UPDATE 1

bank4=# update la set appmoney='$30000' where lano=2003;

UPDATE 1

bank4=# update la set appmoney='$20000' where lano=2004;

UPDATE 1

bank4=# update la set appmoney='$25000' where lano=2005;

UPDATE 1

bank4=# update la set appmoney='$35000' where lano=2006;

UPDATE 1

bank4=# update la set appmoney='$50000' where lano=2007;

UPDATE 1

bank4=# update la set appmoney='$40000' where lano=2008;

UPDATE 1

bank4=# update la set appmoney='$45000' where lano=2009;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 18


UPDATE 1

bank4=# update la set appmoney='$55000' where lano=2010;

UPDATE 1

bank4=# select * from la;

lano | reqmoney | ladate | appmoney

------+----------+------------+------------

2001 | 10lakh | 2013-09-09 | $10,000.00

2002 | 15lakh | 2012-09-09 | $30,000.00

2003 | 5lakh | 2013-08-19 | $30,000.00

2004 | 2lakh | 2013-09-29 | $20,000.00

2005 | 1lakh | 2013-02-28 | $25,000.00

2006 | 7lakh | 2013-09-28 | $35,000.00

2007 | 6lakh | 2013-09-21 | $50,000.00

2008 | 4lakh | 2014-02-21 | $40,000.00

2009 | 5lakh | 2014-03-21 | $45,000.00

2010 | 10lakh | 2014-07-11 | $55,000.00

(10 rows)

bank4=# alter table la drop reqmoney;

ALTER TABLE

bank4=# alter table la add reqmoney money;

ALTER TABLE

bank4=# update la set reqmoney='$12000' where lano=2001;

UPDATE 1

bank4=# update la set reqmoney='$35000' where lano=2002;

UPDATE 1

bank4=# update la set reqmoney='$30000' where lano=2003;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 19


UPDATE 1

bank4=# update la set reqmoney='$25000' where lano=2004;

UPDATE 1

bank4=# update la set reqmoney='$25000' where lano=2005;

UPDATE 1

bank4=# update la set reqmoney='$37000' where lano=2006;

UPDATE 1

bank4=# update la set reqmoney='$65000' where lano=2007;

UPDATE 1

bank4=# update la set reqmoney='$40000' where lano=2008;

UPDATE 1

bank4=# update la set reqmoney='$45000' where lano=2009;

UPDATE 1

bank4=# update la set reqmoney='$65000' where lano=2010;

UPDATE 1

bank4=# select * from la;

lano | ladate | appmoney | reqmoney

------+------------+------------+------------

2001 | 2013-09-09 | $10,000.00 | $12,000.00

2002 | 2012-09-09 | $30,000.00 | $35,000.00

2003 | 2013-08-19 | $30,000.00 | $30,000.00

2004 | 2013-09-29 | $20,000.00 | $25,000.00

2005 | 2013-02-28 | $25,000.00 | $25,000.00

2006 | 2013-09-28 | $35,000.00 | $37,000.00

2007 | 2013-09-21 | $50,000.00 | $65,000.00

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 20


2008 | 2014-02-21 | $40,000.00 | $40,000.00

2009 | 2014-03-21 | $45,000.00 | $45,000.00

2010 | 2014-07-11 | $55,000.00 | $65,000.00

(10 rows)

bank4=# select * from BCL;

bid | cno | lano

-----+-----+------

(0 rows)

bank4=# insert into BCL values(1001,101,2001);

INSERT 0 1

bank4=# insert into BCL values(1002,102,2002);

INSERT 0 1

bank4=# insert into BCL values(1003,103,2003);

INSERT 0 1

bank4=# insert into BCL values(1004,104,2004);

INSERT 0 1

bank4=# insert into BCL values(1005,107,2005);

INSERT 0 1

bank4=# insert into BCL values(1005,108,2006);

INSERT 0 1

bank4=# insert into BCL values(1005,109,2007);

INSERT 0 1

bank4=# insert into BCL values(1005,110,2008);

INSERT 0 1

bank4=# insert into BCL values(1006,105,2009);

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 21


bank4=# insert into BCL values(1007,106,2010);

INSERT 0 1

bank4=# select * from BCL;

bid | cno | lano

------+-----+------

1001 | 101 | 2001

1002 | 102 | 2002

1003 | 103 | 2003

1004 | 104 | 2004

1005 | 107 | 2005

1005 | 108 | 2006

1005 | 109 | 2007

1005 | 110 | 2008

1006 | 105 | 2009

1007 | 106 | 2010

(10 rows)

bank4=# select cname from customer where cno in(select cno from BCL where bid in(select bid from
branch where bid=1001));

cname

--------

Akshay

(1 row)

bank4=# select cname from customer where cno in(select cno from BCL where lano in(select lano from
la where appmoney<reqmoney));

cname

---------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 22


Akshay

Patil

Pandey

Dave

Gambhir

Kumar

(6 rows)

bank4=# select max(appmoney) from la;

max

------------

$55,000.00

(1 row)

bank4=# select sum(appmoney) from la where lano in(select lano from BCL where bid in(select bid from
branch where bid=1002));

sum

------------

$30,000.00

(1 row)

bank4=# select count(cname) from customer where cno in(select cno from BCL where bid in(select bid
from branch where bid=1005));

count

-------

(1 row)

bank4=# select cname,bname from customer,branch,la,BCL where customer.cno=BCL.cno AND


branch.bid=BCL.bid AND la.lano=BCL.lano AND extract(month from ladate)=09;

cname | bname

---------+--------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 23


Akshay | Aundh

Patil | Deccan

Pandey | Colaba

Gambhir | MG Rd

Kumar | MG Rd

(5 rows)

SET B(Student-Teacher Database)

postgres=# create database student4

postgres-# ;

CREATE DATABASE

postgres=#

postgres=#

postgres=# \c

psql (8.4.5)

You are now connected to database "postgres".

postgres=# \c student4

psql (8.4.5)

You are now connected to database "student4".

student4=# create table student(sno int primary key,sname varchar(30),sclass varchar(10),saddrs


varchar(50));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "student_pkey" for table
"student"

CREATE TABLE

student4=# create table teacher(tno int primary key,tname varchar(30),qualification


varchar(10),experience int);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "teacher_pkey" for table
"teacher"

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 24


CREATE TABLE

student4=# create table ST(sno int references student,tno int references teacher,subject varchar(20));

CREATE TABLE

student4=# insert into student values(101,'Pratik','FY','Pune');

INSERT 0 1

student4=# insert into student values(102,'Harshal','FY','Pune');

INSERT 0 1

student4=# insert into student values(103,'Suresh','SY','Pune');

INSERT 0 1

student4=# insert into student values(104,'Ramesh','SY','Mumbai');

INSERT 0 1

student4=# insert into student values(105,'Jayesh','SY','Mumbai');

INSERT 0 1

student4=# insert into student values(106,'Rakesh','SY','Mumbai');

INSERT 0 1

student4=# insert into student values(107,'Vikesh','TY','Pune');

INSERT 0 1

student4=# insert into student values(108,'Mikesh','TY','Pune');

INSERT 0 1

student4=# insert into student values(109,'Mitesh','TY','Pune');

INSERT 0 1

student4=# insert into student values(110,'Priyesh','TY','Pune');

INSERT 0 1

student4=# insert into teacher values(01,'Mr.Joshi','Bsc',3);

INSERT 0 1

student4=# insert into teacher values(02,'Mr.Navle','Msc',5);

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 25


INSERT 0 1

student4=# insert into teacher values(03,'Mrs.Joshi','Bca',2);

INSERT 0 1

student4=# insert into teacher values(04,'Mrs.Patil','Mca',5);

INSERT 0 1

student4=# insert into teacher values(05,'Mr.Patil','Bca',1);

INSERT 0 1

student4=# select * from student

student4-# ;

sno | sname | sclass | saddrs

-----+---------+--------+--------

101 | Pratik | FY | Pune

102 | Harshal | FY | Pune

103 | Suresh | SY | Pune

104 | Ramesh | SY | Mumbai

105 | Jayesh | SY | Mumbai

106 | Rakesh | SY | Mumbai

107 | Vikesh | TY | Pune

108 | Mikesh | TY | Pune

109 | Mitesh | TY | Pune

110 | Priyesh | TY | Pune

(10 rows)

student4=# select * from teacher;

tno | tname | qualification | experience

-----+-----------+---------------+------------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 26


1 | Mr.Joshi | Bsc | 3

2 | Mr.Navle | Msc | 5

3 | Mrs.Joshi | Bca | 2

4 | Mrs.Patil | Mca | 5

5 | Mr.Patil | Bca | 1

(5 rows)

student4=# insert into ST values(101,01,'DS');

INSERT 0 1

student4=# insert into ST values(102,01,'DS');

INSERT 0 1

student4=# insert into ST values(103,01,'RDBMS');

INSERT 0 1

student4=# insert into ST values(103,02,'RDBMS');

INSERT 0 1

student4=# insert into ST values(103,03,'DS');

INSERT 0 1

student4=# insert into ST values(104,04,'DS');

INSERT 0 1

student4=# insert into ST values(105,05,'DS');

INSERT 0 1

student4=# insert into ST values(106,05,'DS');

INSERT 0 1

student4=# insert into ST values(107,05,'DBMS');

INSERT 0 1

student4=# insert into ST values(108,05,'DBMS');

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 27


student4=# insert into ST values(109,04,'DBMS');

INSERT 0 1

student4=# insert into ST values(110,01,'DS');

INSERT 0 1

student4=# update teacher set qualification='Ph.D' where tno=3;

UPDATE 1

student4=# update teacher set qualification='BCA' where tno=3;

UPDATE 1

student4=# update teacher set qualification='Ph.D' where tno=2;

UPDATE 1

student4=# update teacher set qualification='Ph.D' where tno=4;

UPDATE 1

student4=# select * from teacher;

tno | tname | qualification | experience

-----+-----------+---------------+------------

1 | Mr.Joshi | Bsc | 3

5 | Mr.Patil | Bca | 1

3 | Mrs.Joshi | BCA | 2

2 | Mr.Navle | Ph.D | 5

4 | Mrs.Patil | Ph.D | 5

(5 rows)

student4=# select * from ST;

sno | tno | subject

-----+-----+---------

101 | 1 | DS

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 28


102 | 1 | DS

103 | 1 | RDBMS

103 | 2 | RDBMS

103 | 3 | DS

104 | 4 | DS

105 | 5 | DS

106 | 5 | DS

107 | 5 | DBMS

108 | 5 | DBMS

109 | 4 | DBMS

110 | 1 | DS

(12 rows)

student4=# select tname from teacher where experience in(select min(experience) from teacher);

tname

----------

Mr.Patil

(1 row)

student4=# select count(tname) from teacher group by qualification having qualification='Ph.D';

count

-------

(1 row)

student4=# select distinct(tname),subject from teacher,ST where teacher.tno=ST.tno AND teacher.tno


in(select tno from ST group by tno);

tname | subject

-----------+---------

Mrs.Joshi | DS

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 29


Mrs.Patil | DBMS

Mr.Joshi | DS

Mr.Patil | DBMS

Mr.Patil | DS

Mrs.Patil | DS

Mr.Navle | RDBMS

Mr.Joshi | RDBMS

(8 rows)

student4=# select sname,subject from student,ST where student.sno=ST.sno and tno in(select tno from
teacher where tname='Mr.Patil');

sname | subject

--------+---------

Jayesh | DS

Rakesh | DS

Vikesh | DBMS

Mikesh | DBMS

(4 rows)

student4=# select tname from teacher where tno in(select tno from ST Where sno in(select sno from
student where sname='Suresh'));

tname

-----------

Mr.Joshi

Mrs.Joshi

Mr.Navle

(3 rows)

student4=# select tname,count(sno) from teacher,ST where teacher.tno=ST.tno group by tname;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 30


tname | count

-----------+-------

Mrs.Patil | 2

Mr.Patil | 4

Mrs.Joshi | 1

Mr.Navle | 1

Mr.Joshi | 4

(5 rows)

student4=#

SET C(Project-Employee Database)

postgres=# create database Project4;

CREATE DATABASE

postgres=# \c project4;

psql (8.4.5)

You are now connected to database "project4".

project4=# create table project(pno int primary key,pname varchar(20),ptype varchar(20),duration int);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "project_pkey" for table
"project"

CREATE TABLE

project4=# create table employee(eno int primary key,ename varchar(20),qualification


varchar(20),joindate date);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "employee_pkey" for table
"employee"

CREATE TABLE

project4=# create table PE(pno int references project,eno int references employee,startdate date,hours int);

CREATE TABLE

project4=# insert int project values(1000,'Danish','Educational',3);

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 31


project4=# insert into project values(1000,'Danish','Educational',3);

INSERT 0 1

project4=# insert into project values(1500,'Satish','Industrial',5);

INSERT 0 1

project4=# insert into project values(2000,'Alok','Industrial',4);

INSERT 0 1

project4=# insert into project values(2500,'Amol','Industrial',2);

INSERT 0 1

project4=# update project set pname='Electronics' where pno=1000;

UPDATE 1

project4=# update project set pname='System' where pno=1500;

UPDATE 1

project4=# update project set pname='Robotics' where pno=2000;

UPDATE 1

project4=# update project set pname='Networks' where pno=2500;

UPDATE 1

project4=# insert into project values(3000,'Database','Industrial',1);

INSERT 0 1

project4=# select * from project

project4-# ;

pno | pname | ptype | duration

------+-------------+-------------+----------

1000 | Electronics | Educational | 3

1500 | System | Industrial | 5

2000 | Robotics | Industrial | 4

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 32


2500 | Networks | Industrial | 2

3000 | Database | Industrial | 1

(5 rows)

project4=# update project set pno=101 where pno=1000;

UPDATE 1

project4=# update project set pno=102 where pno=1500;

UPDATE 1

project4=# update project set pno=103 where pno=2000;

UPDATE 1

project4=# update project set pno=104 where pno=2500;

UPDATE 1

project4=# update project set pno=105 where pno=3000;

UPDATE 1

project4=# select * from project;

pno | pname | ptype | duration

-----+-------------+-------------+----------

101 | Electronics | Educational | 3

102 | System | Industrial | 5

103 | Robotics | Industrial | 4

104 | Networks | Industrial | 2

105 | Database | Industrial | 1

(5 rows)

project4=# insert into employee values(1000,'Danish','BE.Mech','2011-11-21');

INSERT 0 1

project4=# insert into employee values(1500,'Satish','BE.Mech','2010-01-01');

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 33


project4=# insert into employee values(2000,'Amol','BCS','2012-06-01');

INSERT 0 1

project4=# insert into employee values(2500,'Amok','BCS','2013-02-01');

INSERT 0 1

project4=# insert into employee values(3000,'Nitesh','MCS','2012-08-01');

INSERT 0 1

project4=# insert into employee values(3500,'Mohit','MCA','2011-05-01');

INSERT 0 1

project4=# insert into employee values(4000,'Akshay','MCS','2010-09-05');

INSERT 0 1

project4=# insert into employee values(4500,'Kiran','BCA','2014-01-01');

INSERT 0 1

project4=# select * from employee;

eno | ename | qualification | joindate

------+--------+---------------+------------

1000 | Danish | BE.Mech | 2011-11-21

1500 | Satish | BE.Mech | 2010-01-01

2000 | Amol | BCS | 2012-06-01

2500 | Amok | BCS | 2013-02-01

3000 | Nitesh | MCS | 2012-08-01

3500 | Mohit | MCA | 2011-05-01

4000 | Akshay | MCS | 2010-09-05

4500 | Kiran | BCA | 2014-01-01

(8 rows)

project4=# select * from project;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 34


pno | pname | ptype | duration

-----+-------------+-------------+----------

101 | Electronics | Educational | 3

102 | System | Industrial | 5

103 | Robotics | Industrial | 4

104 | Networks | Industrial | 2

105 | Database | Industrial | 1

(5 rows)

project4=# select * from PE;

pno | eno | startdate | hours

-----+-----+-----------+-------

(0 rows)

project4=# insert into PE values(101,1000,'2014-01-21',10);

INSERT 0 1

project4=# insert into PE values(101,1500,'2014-01-21',10);

INSERT 0 1

project4=# insert into PE values(102,2000,'2013-11-11',15);

INSERT 0 1

project4=# insert into PE values(102,3000,'2013-11-11',15);

INSERT 0 1

project4=# insert into PE values(103,2500,'2014-05-11',35);

INSERT 0 1

project4=# insert into PE values(103,3500,'2014-05-11',35);

INSERT 0 1

project4=# select * from PE;

pno | eno | startdate | hours

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 35


-----+------+------------+-------

101 | 1000 | 2014-01-21 | 10

101 | 1500 | 2014-01-21 | 10

102 | 2000 | 2013-11-11 | 15

102 | 3000 | 2013-11-11 | 15

103 | 2500 | 2014-05-11 | 35

103 | 3500 | 2014-05-11 | 35

104 | 4000 | 2014-11-11 | 11

105 | 4500 | 2014-01-11 | 18

(8 rows)

project4=# select ename from employee where ename like 'A%';

ename

--------

Amol

Amok

Akshay

(3 rows)

project4=# select * from employee where eno in(select eno from PE where PE.pno in(select pno from
project where pname='System'));

eno | ename | qualification | joindate

------+--------+---------------+------------

2000 | Amol | BCS | 2012-06-01

3000 | Nitesh | MCS | 2012-08-01

(2 rows)

project4=# select eno from employee where eno in(select eno from PE where PE.pno in(select pno from
project where pname!='Robotics'));

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 36


eno

------

1000

1500

2000

3000

4000

4500

(6 rows)

project4=# select eno from employee where eno in(select eno from PE where pno in(select pno from PE
where eno=2000));

eno

------

2000

3000

(2 rows)

project4=# select ename from employee order by ename limit 3;

ename

--------

Akshay

Amok

Amol

(3 rows)

project4=# select ename from employee where eno in(select eno from PE where pno in(select pno from
project where duration>3));

ename

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 37


--------

Amol

Amok

Nitesh

Mohit

(4 rows)

===========================================================================

Assignment 3 : Views

SET A (Using the Bank Database)

[NRC@localhost ~]$ su - postgres

Password:

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=# \c bank4;

FATAL: database "bank4" does not exist

Previous connection kept

postgres=# \c bank4;

psql (8.4.5)

You are now connected to database "bank4".

bank4=# select * from Customer;

c_no | c_name | c_add | c_city

------+----------------------+-------------------------------------+----------------------

1 | Raj | Laxmiroad | Pune

2 | Disha | Churchgate | Mumbai

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 38


3 | Mahesh | Marketyard | Pune

4 | Jay | ABC road | Nashik

5 | Preeti | XYZ road | Nagar

6 | Kartik | Thane | Mumbai

(6 rows)

bank4=# select * from Loan_app;

l_no | l_amt_required | l_amt_approved | l_date

------+----------------+----------------+------------

121 | $50,000.00 | $45,000.00 | 2010-09-15

131 | $75,000.00 | $60,000.00 | 2005-09-07

141 | $100,000.00 | $80,000.00 | 1999-11-27

151 | $200,000.00 | $150,000.00 | 2012-04-19

161 | $500,000.00 | $450,000.00 | 2006-12-08

171 | $300,000.00 | $250,000.00 | 2005-12-04

181 | $500,000.00 | $350,000.00 | 2002-12-08

191 | $50,000.00 | $40,000.00 | 2012-06-18

bank4=# select * from B_C_L;

br_id | c_no | l_no

-------+------+------

101 | 3 | 141

101 | 1 | 161

102 | 1 | 171

102 | 6 | 151

103 | 2 | 121

103 | 5 | 161

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 39


103 | 4 | 131

104 | 2 | 131

(8 rows)

bank4=# select Customer.C_no,Customer.C_name from B_C_L,Customer,Loan_app where


Customer.C_no=B_C_L.C_no and Loan_app.L_no=B_C_L.L_no and L_amt_required>'$100000';

c_no | c_name

------+----------------------

1 | Raj

1 | Raj

6 | Kartik

5 | Preeti

(4 rows)

bank4=# create view v1 as select Customer.C_no,Customer.C_name from B_C_L,Customer,Loan_app


where Customer.C_no=B_C_L.C_no and Loan_app.L_no=B_C_L.L_no and L_amt_required>'$100000';

bank4=# select * from v1;

c_no | c_name

------+----------------------

1 | Raj

1 | Raj

6 | Kartik

5 | Preeti

(4 rows)

bank4=# select C_no from v1;

c_no

------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 40


6

(4 rows)

bank4=# select * from Branch;

br_id | br_name | br_city

-------+--------------------------------+------------

101 | Aundh | Nagar

102 | Deccan | Pune

103 | M.G.road | Pune

104 | Thane | Mumbai

105 | Pimpri | Pune

(5 rows)

bank4=# insert into Branch values(106,'Sadashiv Peth','Pune

bank4'# ^C

bank4=# insert into Branch values(106,'Sadashiv Peth','Pune');

INSERT 0 1

bank4=# insert into Loan_app values(110,'$500000','$400000','2014-09-12');

INSERT 0 1

bank4=# insert into B_C_L values(106,2,110);

INSERT 0 1

bank4=# insert into B_C_L values(106,3,109);

ERROR: insert or update on table "b_c_l" violates foreign key constraint "b_c_l_l_no_fkey"

DETAIL: Key (l_no)=(109) is not present in table "loan_app".

bank4=# insert into B_C_L values(106,3,191);

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 41


bank4=# insert into B_C_L values(106,4,181);

INSERT 0 1

bank4=# create view v2 as select


Loan_app.L_no,Loan_app.L_amt_required,Loan_app.L_amt_approved,Loan_app.L_date from
Loan_app,Branch,B_C_L where Loan_app.L_no=B_C_L.L_no and Branch.Br_id=B_C_L.Br_id and
Br_name='Sadashiv Peth';

CREATE VIEW

bank4=# select * from v2;

l_no | l_amt_required | l_amt_approved | l_date

------+----------------+----------------+------------

110 | $500,000.00 | $400,000.00 | 2014-09-12

191 | $50,000.00 | $40,000.00 | 2012-06-18

181 | $500,000.00 | $350,000.00 | 2002-12-08

(3 rows)

bank4=# create view v3 as select Customer.C_no,Customer.C_name,L_amt_required from


B_C_L,Customer,Loan_app where Customer.C_no=B_C_L.C_no and Loan_app.L_no=B_C_L.L_no and
L_amt_required>'$100000';

CREATE VIEW

bank4=# select * from v3;

c_no | c_name | l_amt_required

------+----------------------+----------------

1 | Raj | $500,000.00

1 | Raj | $300,000.00

6 | Kartik | $200,000.00

5 | Preeti | $500,000.00

2 | Disha | $500,000.00

4 | Jay | $500,000.00

(6 rows)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 42


bank4=# select C_no,C_name from v3 where L_amt_required='$500000';

c_no | c_name

------+----------------------

1 | Raj

5 | Preeti

2 | Disha

4 | Jay

(4 rows)

bank4=# select * from v2 where L_amt_required>'$50000';

l_no | l_amt_required | l_amt_approved | l_date

------+----------------+----------------+------------

110 | $500,000.00 | $400,000.00 | 2014-09-12

181 | $500,000.00 | $350,000.00 | 2002-12-08

(2 rows)

bank4=# select L_no,L_amt_required from v2 where L_no in(select L_no from v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select * from v2;

l_no | l_amt_required | l_amt_approved | l_date

------+----------------+----------------+------------

110 | $500,000.00 | $400,000.00 | 2014-09-12

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 43


191 | $50,000.00 | $40,000.00 | 2012-06-18

181 | $500,000.00 | $350,000.00 | 2002-12-08

(3 rows)

bank4=# select distinct L_amt_required from v2 where L_no in(select L_no from v2);

l_amt_required

----------------

$50,000.00

$500,000.00

(2 rows)

bank4=# select L_no,L_amt_required from v2 where L_no =(select L_no from v2);

ERROR: more than one row returned by a subquery used as an expression

bank4=# select L_no,L_amt_required from v2 where L_no =all(select L_no from v2);

l_no | l_amt_required

------+----------------

(0 rows)

bank4=# select L_no,L_amt_required from v2 where L_no =some(select L_no from v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_no =any(select L_no from v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 44


191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_no =(select L_no from v2);

ERROR: more than one row returned by a subquery used as an expression

bank4=# select L_no,L_amt_required from v2 where L_no=(select L_no from v2);

ERROR: more than one row returned by a subquery used as an expression

bank4=# select L_no,L_amt_required from v2 where L_amt_required=(select L_amt_required from v2);

ERROR: more than one row returned by a subquery used as an expression

bank4=# select L_no,L_amt_required from v2 where L_amt_required in(select L_amt_required from v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_amt_required=any(select L_amt_required from


v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_amt_required in(select L_amt_required from v2);

l_no | l_amt_required

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 45


------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_amt_required in(select L_amt_required from v2);

l_no | l_amt_required

------+----------------

110 | $500,000.00

191 | $50,000.00

181 | $500,000.00

(3 rows)

bank4=# select L_amt_required from v2;

l_amt_required

----------------

$500,000.00

$50,000.00

$500,000.00

(3 rows)

bank4=# select L_amt_required from v2 as t where L_amt_required=t.L_amt_required;

l_amt_required

----------------

$500,000.00

$50,000.00

$500,000.00

(3 rows)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 46


bank4=# select L_no from v2 as t where L_amt_required=t.L_amt_required;

l_no

------

110

191

181

(3 rows)

bank4=# select distinct L_no from v2 as t where L_amt_required=t.L_amt_required;

l_no

------

191

110

181

(3 rows)

bank4=# select distinct L_no from v2;;

l_no

------

191

110

181

(3 rows)

bank4=# select L_no,L_amt_required from v2 where L_amt_required='$500000';

l_no | l_amt_required

------+----------------

110 | $500,000.00

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 47


181 | $500,000.00

(2 rows)

bank4=#

Assignment 3 : Views

SET B (Using Project-Employee Database)

project4=# create view v1 as select emp_name,qualification from Employee order by qualification;

CREATE VIEW

project4=# select * from v1;

emp_name | qualification

----------------------+---------------------------------------------------

Ananya | BCA

Ashwini | MCA

Alok | MCA

Preeti | Mcom

Deepali | MCS

Deepak | MCS

Anand | PG

Rajesh | PG

(8 rows)

project4=# create view v2 as select P_name,P_type,start_date from Proj_Emp,Project where


Project.P_no=Proj_Emp.P_no order by start_date;

CREATE VIEW

project4=# select * from v2;

p_name | p_type | start_date

--------------------------------+---------------------+-----------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 48


Admission system | Education | 2009-07-03

Samsung | Commercial | 2010-07-03

Samsung | Commercial | 2010-07-03

Samsung | Commercial | 2010-07-03

Samsung | Commercial | 2010-07-03

Mechanism | Robotics | 2011-07-21

Result system | System | 2012-04-11

Operating system | System | 2012-04-11

Result system | System | 2012-04-11

(9 rows)

project4=# select qualification from v1 group by qualification;

qualification

----------------------------------------------------

BCA

MCA

Mcom

MCS

PG

(5 rows)

project4=# select P_name,P_type from v2 where start_date='2007-07-03';

p_name | p_type

--------+-------

(0 rows)

project4=# select P_name,P_type from v2 where start_date='2010-07-03';

p_name | p_type

--------------------------------+---------------------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 49


Samsung | Commercial

Samsung | Commercial

Samsung | Commercial

Samsung | Commercial

(4 rows)

project4=# select * from v1 where qualification='MCA';

emp_name | qualification

----------------------+---------------------------------------------------

Alok | MCA

Ashwini | MCA

(2 rows)

project4=#

Assignment 3 : Views

SET C (Using the Business trip Database)

project4=# \c buisness4;

psql (8.4.5)

You are now connected to database "buisness4".

buisness4=# select * from salesmen;

s_no | s_name | start_year | dept_no

------+--------------------------------+------------+---------

101 | Mr.Patil | 2001 | c1

102 | Mr.Sharma | 2003 | c2

103 | Mr.Rathod | 2005 | c2

104 | Mr.Malhotra | 2007 | c3

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 50


105 | Mr.Kulkarni | 2010 | c4

106 | Mr.Shah | 2014 | c5

107 | Mr.Das | 2010 | c3

108 | Mr.Chawan | 2009 | c1

(8 rows)

buisness4=# select * from trip;

trip_no | from_city | to_city | departure_date | return_date | s_no

---------+----------------------+----------------------+----------------+-------------+------

111 | Pune | Calcutta | 2014-11-18 | 2014-11-25 | 101

112 | Mumbai | Calcutta | 2012-08-01 | 2012-08-10 | 108

114 | Banglore | Mumbai | 2009-11-03 | 2009-11-15 | 104

115 | Pune | Delhi | 2011-03-26 | 2011-03-30 | 106

116 | Chennai | Mysore | 2010-06-04 | 2010-06-15 | 101

117 | Agra | Mumbai | 2008-08-15 | 2008-08-20 | 107

(6 rows)

buisness4=# select * from department;

dept_no | dept_name

---------+----------------------

c1 | Electronics

c2 | Computer

c3 | Statistics

c4 | Mathematics

c5 | English

(5 rows)

buisness4=# select * from expense;

e_id | amount | trip_no

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 51


------+------------+---------

1 | $21,000.00 | 111

2 | $23,000.00 | 112

4 | $10,000.00 | 114

5 | $25,000.00 | 115

6 | $15,000.00 | 116

7 | $7,000.00 | 117

(6 rows)

buisness4=# create view v1 as select s_no,s_name,start_year from salesmen,department where


salesmen.dept_no=department.dept_no and dept_name='Electronics';

CREATE VIEW

buisness4=# select * from v1;

s_no | s_name | start_year

------+--------------------------------+------------

101 | Mr.Patil | 2001

108 | Mr.Chawan | 2009

(2 rows)

buisness4=# create view v2 as select salesmen.s_name,trip.trip_no,amount from salesmen,expense,trip


where salesmen.s_no=trip.s_no and expense.trip_no=trip.trip_no;

CREATE VIEW

buisness4=# select * from v2;

s_name | trip_no | amount

--------------------------------+---------+------------

Mr.Patil | 111 | $21,000.00

Mr.Chawan | 112 | $23,000.00

Mr.Malhotra | 114 | $10,000.00

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 52


Mr.Shah | 115 | $25,000.00

Mr.Patil | 116 | $15,000.00

Mr.Das | 117 | $7,000.00

(6 rows)

buisness4=# select * from v1 where start_year=2001;

s_no | s_name | start_year

------+--------------------------------+------------

101 | Mr.Patil | 2001

(1 row)

buisness4=# select * from v1;

s_no | s_name | start_year

------+--------------------------------+------------

101 | Mr.Patil | 2001

108 | Mr.Chawan | 2009

(2 rows)

buisness4=# select * from v2;

s_name | trip_no | amount

--------------------------------+---------+------------

Mr.Patil | 111 | $21,000.00

Mr.Chawan | 112 | $23,000.00

Mr.Malhotra | 114 | $10,000.00

Mr.Shah | 115 | $25,000.00

Mr.Patil | 116 | $15,000.00

Mr.Das | 117 | $7,000.00

(6 rows)

buisness4=# select v1.s_name from v1,v2 where v1.s_name=v2.s_name;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 53


s_name

--------------------------------

Mr.Patil

Mr.Chawan

Mr.Patil

(3 rows)

buisness4=# create view v3 as select salesmen.s_no,salesmen.s_name,trip.trip_no,amount from


salesmen,expense,trip where salesmen.s_no=trip.s_no and expense.trip_no=trip.trip_no;

CREATE VIEW

buisness4=# select v1.s_name from v1,v3 where v1.s_no=v3.s_no and amount in(select sum(amount)
from v3 group by trip_no buisness4=# select v1.s_name from v1,v3 where v1.s_no=v3.s_no and
amount>'$10000';

s_name

--------------------------------

Mr.Patil

Mr.Chawan

Mr.Patil

(3 rows)

buisness4=# select v1.s_name from v1,v3 where v1.s_no=v3.s_no and amount>'$21000';

s_name

--------------------------------

Mr.Chawan

(1 row)

buisness4=# select * from v1;

s_no | s_name | start_year

------+--------------------------------+------------

101 | Mr.Patil | 2001

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 54


108 | Mr.Chawan | 2009

(2 rows)

buisness4=# select * from v3;

s_no | s_name | trip_no | amount

------+--------------------------------+---------+------------

101 | Mr.Patil | 111 | $21,000.00

108 | Mr.Chawan | 112 | $23,000.00

104 | Mr.Malhotra | 114 | $10,000.00

106 | Mr.Shah | 115 | $25,000.00

101 | Mr.Patil | 116 | $15,000.00

107 | Mr.Das | 117 | $7,000.00

(6 rows)

buisness4=# create view v4 as select


salesmen.s_no,salesmen.s_name,trip.trip_no,trip.from_city,trip.to_city,amount from
salesmen,expense,trip where salesmen.s_no=trip.s_no and expense.trip_no=trip.trip_no;

CREATE VIEW

buisness4=# select * from v4;

s_no | s_name | trip_no | from_city | to_city | amount

------+--------------------------------+---------+----------------------+----------------------+------------

101 | Mr.Patil | 111 | Pune | Calcutta | $21,000.00

108 | Mr.Chawan | 112 | Mumbai | Calcutta | $23,000.00

104 | Mr.Malhotra | 114 | Banglore | Mumbai | $10,000.00

106 | Mr.Shah | 115 | Pune | Delhi | $25,000.00

101 | Mr.Patil | 116 | Chennai | Mysore | $15,000.00

107 | Mr.Das | 117 | Agra | Mumbai | $7,000.00

(6 rows)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 55


buisness4=# select s_name from v4 where to_city='Mumbai';

s_name

--------------------------------

Mr.Malhotra

Mr.Das

(2 rows)

buisness4=#

Assignment 3 : Views

SET D (Using the Warehouse Database)

warehouse4=# select s_id,store_name,location_city from stores,warehouse where


stores.wid=warehouse.wid and wname='spares';

s_id | store_name | location_city

------+---------------------+---------------------

101 | Pragati | Pune

102 | Deshpande | Pune

103 | Jyoti | Nagpur

(3 rows)

warehouse4=# create view v1 as select s_id,store_name,location_city,stores.wid from stores,warehouse


where stores.wid=warehouse.wid and wname='spares';

CREATE VIEW

warehouse4=# select * from v1;

s_id | store_name | location_city | wid

------+---------------------+---------------------+----

101 | Pragati | Pune | 2

102 | Deshpande | Pune | 2

103 | Jyoti | Nagpur | 2

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 56


(3 rows)

warehouse4=# select * from customer;

c_no | c_name | addr | cu_city

------+---------------------------------------------------+------------+---------------------

1001 | Mr.Patil | Karvenagar | Pune

1002 | Mr.Sharma | MGroad | Rajkot

1003 | Mr.Mehra | Dombivali | Mumbai

1004 | Mr.Dave | KVroad | Jaipur

1005 | Mr.Kulkarni | FCroad | Indore

1006 | Mr.Ghate | Gandhinagar | Ahmedabad

(6 rows)

warehouse4=# select * from orders;

o_no | o_date | c_no

------+-----------+-----

121 | 2014-12-04 | 1001

122 | 2014-10-14 | 1002

123 | 2014-08-23 | 1003

124 | 2014-05-13 | 1004

125 | 2014-05-16 | 1005

126 | 2014-11-19 | 1006

(6 rows)

warehouse4=# update orders set o_date='2013-10-03' where o_no=121 and o_no=122;

UPDATE 0

warehouse4=# update orders set o_date='2013-10-03' where o_no=121 and o_no=122;

UPDATE 0

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 57


warehouse4=# update orders set o_date='2013-10-03' where o_no=121;

UPDATE 1

warehouse4=# update orders set o_date='2013-10-03' where o_no=122;

UPDATE 1

warehouse4=# select * from orders;

o_no | o_date | c_no

------+-----------+-----

123 | 2014-08-23 | 1003

124 | 2014-05-13 | 1004

125 | 2014-05-16 | 1005

126 | 2014-11-19 | 1006

121 | 2013-10-03 | 1001

122 | 2013-10-03 | 1002

(6 rows)

warehouse4=# create view v2 as select customer.c_no,customer.c_name,customer.addr,cu_city from


customer,orders where orders.c_no=customer.c_no and o_date='2013-10-03';

CREATE VIEW

warehouse4=# select * from v2;

c_no | c_name | addr | cu_city

------+---------------------------------------------------+-----------+---------------------

1001 | Mr.Patil | Karvenagar | Pune

1002 | Mr.Sharma | MGroad | Rajkot

(2 rows)

warehouse4=# select c_name from v2 where cu_city='Pune';

c_name

----------------------------------------------------

Mr.Patil

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 58


(1 row)

Assignment 3 : Views

SET E (Using the Student-Teacher Database)

[NRC@localhost ~]$ su - postgres

Password:

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=# \c student4;

psql (8.4.5)

You are now connected to database "student4".

student4=# select * from teacher;

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

2 | Mr.More | Ph.D | 5

3 | Mrs.Sharma | PG | 4

4 | Mr.Kulkarni | MCS | 3

5 | Mrs.Joshi | Ph.D | 10

6 | Miss Deepali | MCS | 2

7 | Miss Varsha | Ph.D | 6

(7 rows)

student4=# select * from stud_teacher;

s_no | t_no | subject

------+------+--------------------------------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 59


102 | 1 | Electronics

101 | 1 | Digital Electronics

106 | 1 | Analog Electronics

101 | 2 | Comp.Sci

104 | 2 | DS

105 | 3 | RDBMS

102 | 3 | RDBMS

107 | 4 | Discrete Maths

107 | 5 | Statistics

105 | 6 | Accounts

107 | 6 | SP

104 | 7 | English

106 | 7 | Marathi

(13 rows)

student4=# select teacher.t_no,t_name,qualification,experience from teacher,stud_teacher where


teacher.t_no=stud_teacher.t_no and subject='Discrete Maths';

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

4 | Mr.Kulkarni | MCS | 3

(1 row)

student4=# select teacher.t_no,t_name,qualification,experience from teacher,stud_teacher where


teacher.t_no=stud_teacher.t_no and subject='Electronics';

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

(1 row)

student4=# update stud_teacher set subject='Mathematics' where t_no=1;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 60


UPDATE 3

student4=# select teacher.t_no,t_name,qualification,experience from teacher,stud_teacher where


teacher.t_no=stud_teacher.t_no and subject='Mathematics';

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

(3 rows)

student4=# update stud_teacher set subject='Mathematics' where t_no=2;

UPDATE 2

student4=# update stud_teacher set subject='Mathematics' where t_no=3;

UPDATE 2

student4=# select teacher.t_no,t_name,qualification,experience from teacher,stud_teacher where


teacher.t_no=stud_teacher.t_no and subject='Mathematics';

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

2 | Mr.More | Ph.D | 5

2 | Mr.More | Ph.D | 5

3 | Mrs.Sharma | PG | 4

3 | Mrs.Sharma | PG | 4

(7 rows)

student4=# select distinct teacher.t_no,t_name,qualification,experience from teacher,stud_teacher where


teacher.t_no=stud_teacher.t_no and subject='Mathematics';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 61


t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

2 | Mr.More | Ph.D | 5

3 | Mrs.Sharma | PG | 4

(3 rows)

student4=# create view v1 as select teacher.t_no,t_name,qualification,experience from


teacher,stud_teacher where teacher.t_no=stud_teacher.t_no and subject='Mathematics';

CREATE VIEW

student4=# select * from v1;

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

1 | Mr.Patil | Ph.D | 7

2 | Mr.More | Ph.D | 5

2 | Mr.More | Ph.D | 5

3 | Mrs.Sharma | PG | 4

3 | Mrs.Sharma | PG | 4

(7 rows)

student4=# create view v2 as select distinct teacher.t_no,t_name,qualification,experience from


teacher,stud_teacher where teacher.t_no=stud_teacher.t_no and subject='Mathematics';

CREATE VIEW

student4=# select * from v2;

t_no | t_name | qualification | experience

------+----------------------+-----------------+------------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 62


1 | Mr.Patil | Ph.D | 7

2 | Mr.More | Ph.D | 5

3 | Mrs.Sharma | PG | 4

(3 rows)

student4=# select student.s_no,s_name,s_class,s_add,stud_teacher.t_no from student,teacher,stud_teacher


where student.s_no=stud_teacher.t_no and teacher.t_no=stud_teacher.t_no and experience>5;

s_no | s_name | s_class | s_add | t_no

------+--------+---------+-------+------

(0 rows)

student4=# select student.s_no,s_name,s_class,s_add,stud_teacher.t_no from student,teacher,stud_teacher


where student.s_no=stud_teacher.s_no and teacher.t_no=stud_teacher.t_no and experience>5;

s_no | s_name | s_class | s_add | t_no

------+--------------------------------+------------+----------------------------------------------------+------

107 | Harshad | M.Com | Pimpri | 5

104 | Rajesh | tybcs | F.C.road | 7

106 | Pooja | sybcs | Sangvi | 7

102 | Suresh | fybcs | Marketyard | 1

101 | Raj | sybcs | Shukrawar Peth | 1

106 | Pooja | sybcs | Sangvi | 1

(6 rows)

student4=# create view v3 as select student.s_no,s_name,s_class,s_add,stud_teacher.t_no from


student,teacher,stud_teacher where student.s_no=stud_teacher.s_no and teacher.t_no=stud_teacher.t_no
and experience>5;

CREATE VIEW

student4=# select * from v3;

s_no | s_name | s_class | s_add | t_no

------+--------------------------------+------------+----------------------------------------------------+------

107 | Harshad | M.Com | Pimpri | 5

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 63


104 | Rajesh | tybcs | F.C.road | 7

106 | Pooja | sybcs | Sangvi | 7

102 | Suresh | fybcs | Marketyard | 1

101 | Raj | sybcs | Shukrawar Peth | 1

106 | Pooja | sybcs | Sangvi

(6 rows)

student4=# select max(experience)from v2;

max

-----

(1 row)

student4=# select max(experience)from v2 group by t_name;

max

-----

(3 rows)

student4=# select t_name from v2 where experience in(select max(experience)from v2);

t_name

----------------------

Mr.Patil

(1 row)

student4=# select s_name from v3 where s_class='sybcs';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 64


s_name

--------------------------------

Prakash

Raj

Pooja

(3 rows)

student4=# select distinct s_name from v3 where s_class='sybcs';

s_name

--------------------------------

Raj

Pooja

(2 rows)

===========================================================================

Assignment 4 : Stored Functions

SET A (Using Bank Database)

bank4=# create function tcust(nm char(30))returns int as '

declare cnt int;

begin

select count(c_no) into cnt from B_C_L where br_id in(select br_id from Branch where br_name=nm);

return cnt;

end; '

language 'plpgsql';

CREATE FUNCTION

bank4=# select tcust('Aundh');

tcust

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 65


-------

(1 row)

...................................................................................

bank4=# create function maximum()returns money as '

declare lmax money;

begin

select max(L_amt_approved) into lmax from Loan_app;

return lmax;

end; '

language 'plpgsql';

CREATE FUNCTION

bank4=# select maximum();

maximum

-------------

$450,000.00

(1 row)

Assignment 4 : Stored Functions

SET B (Using Project-Employee Database)

project5=# create function cntemp(name char(30))returns int as '

declare p int;

begin

select count(Employee.Emp_no) into p from Employee,Project,Proj_Emp where


Project.P_no=Proj_Emp.P_no and Employee.Emp_no=Proj_Emp.Emp_no and P_name=name;

return p;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 66


end; '

language 'plpgsql';

CREATE FUNCTION

project5=# select cntemp('Samsung');

cntemp

--------

(1 row)

..............................................................

project5=# create or replace function num4()returns int as $$

declare n int;

begin

select count(Emp_no) into n from Employee where join_date<'2010-10-03';

return n;

end; $$

language 'plpgsql';

CREATE FUNCTION

project5=# select num4();

num4

------

(1 row)

Assignment 4 : Stored Functions

SET C (Using Business trip Database)

buisness5=# create function maxexp() returns money as'

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 67


declare m money;

begin

select max(amount) into m from expense;

return m;

end; '

language 'plpgsql';

CREATE FUNCTION

buisness5=# select maxexp();

maxexp

------------

$25,000.00

(1 row)

buisness5=# create function tripcnt() returns int as $$

declare c int;

begin

select count(trip_no) into c from trip where from_city='Pune' and to_city='Mumbai';

return c;

end; $$

language 'plpgsql';

CREATE FUNCTION

buisness5=# select tripcnt();

tripcnt

---------

(1 row)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 68


Assignment 5 : Cursor

SET A (Using Warehouse Database)

ware4=# \d

List of relations

Schema | Name | Type | Owner

--------+-----------+-------+----------

public | cities | table | postgres

public | customer | table | postgres

public | io | table | postgres

public | items | table | postgres

public | orders | table | postgres

public | si | table | postgres

public | stores | table | postgres

public | warehouse | table | postgres

(8 rows)

ware4=# select * from cities;

city | state

-----------+-------------

Pune | Maharashtra

Mumbai | Maharashtra

New.Delhi | Delhi

Kolhapur | Maharashtra

Banglore | Karnataka

(5 rows)

ware4=# select * from customer;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 69


cno | cname | addr | cu_city

-----+-----------+----------+-----------

1 | Mr.Patil | Katraj | Pune

2 | Mr.Nene | Khadki | Pune

3 | Mr.Lele | Khothrud | Pune

4 | Mr.Lale | Thane | Mumbai

5 | Mrs.Patil | XYZ | New.Delhi

6 | Mrs.Nene | UVW | Kolhapur

7 | Mrs.lele | ABC | Banglore

8 | Mrs.lale | DEF | Banglore

(8 rows)

ware4=# select * from io;

ino | ono | oq

-----+-----+----

111 | 11 | 11

222 | 22 | 22

333 | 33 | 33

444 | 44 | 44

555 | 55 | 55

666 | 66 | 66

777 | 77 | 77

888 | 88 | 88

(8 rows)

ware4=# select * from items;

ino | description | weight | cost

-----+-------------+--------+--------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 70


111 | Metal | 100.00 | 500.00

222 | Playwood | 200.00 | 300.00

333 | Ceramic | 100.00 | 800.00

444 | Laminate | 300.00 | 900.00

555 | Channel | 250.00 | 250.00

666 | Plastic | 900.00 | 100.00

777 | POP | 500.00 | 250.00

888 | Cement | 700.00 | 150.00

(8 rows)

ware4=# select * from orders;

ono | odate | cno

-----+------------+-----

11 | 2014-12-12 | 1

22 | 2014-11-11 | 2

33 | 2014-10-10 | 3

44 | 2014-09-09 | 4

55 | 2014-08-08 | 5

66 | 2014-07-07 | 6

77 | 2014-08-08 | 7

88 | 2014-05-05 | 8

(8 rows)

ware4=# select * from si;

sid | ino | qauntity

------+-----+----------

1001 | 111 | 11

1002 | 222 | 22

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 71


1003 | 333 | 33

1004 | 444 | 44

1005 | 555 | 55

1006 | 666 | 66

1007 | 777 | 77

1008 | 888 | 88

1009 | 111 | 99

1010 | 111 | 19

(10 rows)

ware4=# select * from stores;

sid | sname | loc_city | wid

------+------------+-----------+-----

1001 | Ram | Pune | 101

1002 | Laxman | Pune | 102

1003 | Sita | Pune | 103

1004 | Hanuman | Mumbai | 104

1005 | Ravan | New.Delhi | 105

1006 | Kumbakaran | Kolhapur | 106

1007 | Vibhishan | Banglore | 107

1008 | Jatayu | Banglore | 108

1009 | Vanarsena | Pune | 101

1010 | Rakshas | Pune | 101

(10 rows)

ware4=# select * from warehouse;

wid | wname | loc | city

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 72


-----+-------+---------+-----------

101 | ABC | Katraj | Pune

102 | DEF | Khadki | Pune

103 | GHI | Kothrud | Pune

104 | JKL | Thane | Mumbai

105 | MNO | XYZ | New.Delhi

106 | PQR | UVW | Kolhapur

107 | STU | ABC | Banglore

108 | VWX | DEF | Banglore

(8 rows)

ware4=# create language 'plpgsql';

CREATE LANGUAGE

1)

ware4=# create function fun2(nm varchar(20))returns void as '

declare c1 cursor for select wname from warehouse where city=nm;

wn char(30);

Begin

open c1;

loop

fetch c1 into wn;

exit when not found;

raise notice''customer name:-%'',wn;

end loop;

close c1;

end '

language 'plpgsql';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 73


CREATE FUNCTION

ware4=# select fun2('Pune');

NOTICE: customer name:-ABC

NOTICE: customer name:-DEF

NOTICE: customer name:-GHI

fun2

------

(1 row)

2)

ware4=# create function fun4()returns void as '

declare c3 cursor for select ino,description from items where cost between 500 and 1000;

ino int;

d text;

begin

open c3;

loop

fetch c3 into ino,d;

exit when not found;

raise notice''item nos:%'',ino;

raise notice''description:%'',d;

end loop;

close c3;

end '

language 'plpgsql';

CREATE FUNCTION

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 74


ware4=# select fun4();

NOTICE: item nos:111

NOTICE: description:Metal

NOTICE: item nos:333

NOTICE: description:Ceramic

NOTICE: item nos:444

NOTICE: description:Laminate

fun4

------

(1 row)

Assignment 5 : Cursor

SET B (Using Company-Person Database)

postgres=# \c company4

psql (8.4.5)

You are now connected to database "company4".

company4=# create table company(name varchar(20) primary key,add varchar(50),phone


varchar(20),share_value money);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit
index "company_pkey" for table "company"

CREATE TABLE

company4=# create table person(pname varchar(30) primary key,pcity varchar(20));NOTICE: CREATE


TABLE / PRIMARY KEY will create implicit index "person_pkey" for table "person"

CREATE TABLE

company4=# create table cp(name varchar(30) references company(name) on delete cascade,pname


varchar(30) references person(pname) on delete cascade,nos_of_share int);

CREATE TABLE

company4=# \d

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 75


List of relations

Schema | Name | Type | Owner

--------+---------+-------+----------

public | company | table | postgres

public | cp | table | postgres

public | person | table | postgres

(3 rows)

company4=# insert into company values('infosys','abc','020-665544','$5000000');

INSERT 0 1

company4=# insert into company values('infosystem','abc','020-665545','$6000000');

INSERT 0 1

company4=# insert into company values('info','pune','020-665545','$7000000');

INSERT 0 1

company4=# insert into company values('infotec','pune','020-665543','$5500000');

INSERT 0 1

company4=# insert into company values('wipro','pune','020-665345','$6500000');

INSERT 0 1

company4=# insert into person values('Rahul','pune');

INSERT 0 1

company4=# insert into person values('Vidhyadhar','pune');

INSERT 0 1

company4=# insert into person values('sachin','mumbai');

INSERT 0 1

company4=# insert into person values('darshan','mumbai');

INSERT 0 1

company4=# insert into person values('shiva','mumbai');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 76


INSERT 0 1

company4=# insert into cp values('infosys','Rahul',10);

INSERT 0 1

company4=# insert into cp values('infosystem','Vidhyadhar',25);

INSERT 0 1

company4=# insert into cp values('infotec','darshan',5);

INSERT 0 1

company4=# insert into cp values('info','sachin',15);

INSERT 0 1

company4=# insert into cp values('wipro','shiva',12);

INSERT 0 1

company4=# select * from company;

name | add | phone | share_value

------------+------+------------+---------------

infosys | abc | 020-665544 | $5,000,000.00

infosystem | abc | 020-665545 | $6,000,000.00

info | pune | 020-665545 | $7,000,000.00

infotec | pune | 020-665543 | $5,500,000.00

wipro | pune | 020-665345 | $6,500,000.00

(5 rows)

company4=# select * from person;

pname | pcity

------------+--------

Rahul | pune

Vidhyadhar | pune

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 77


sachin | mumbai

darshan | mumbai

shiva | mumbai

(5 rows)

company4=# select * from cp;

name | pname | nos_of_share

------------+------------+--------------

infosys | Rahul | 10

infosystem | Vidhyadhar | 25

infotec | darshan | 5

info | sachin | 15

wipro | shiva | 12

(5 rows)

company4=# select count(pname),sum(share_value) from person,company;

count | sum

-------+-----------------

25 | $150,000,000.00

(1 row)

company4=# select sum(share_value) from person,company;

sum

-----------------

$150,000,000.00

(1 row)

company4=# select count(cp.pname),sum(share_value) from person,company,cp where


person.pname=cp.pname and company.name=cp.name;

count | sum

-------+----------------

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 78


5 | $30,000,000.00

(1 row)

2)

company4=# create function f11()returns void as '

declare c10 cursor for select count(cp.name),sum(share_value) from person,company,cp where


person.pname=cp.pname and company.name=cp.name;

cnt int;

sm money;

begin

open c10;

loop

fetch c10 into cnt,sm;

exit when not found;

raise notice''Count : %'',cnt;

raise notice''Sm : %'',sm;

end loop;

close c10;

end '

language 'plpgsql';

CREATE FUNCTION

company4=# select f11();

NOTICE: Count : 5

NOTICE: Sm : $30,000,000.00

f11

-----

(1 row)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 79


1)

company4=# create function b11()returns void as '

declare cb1 cursor for select pname,nos_of_share from cp where pname=''sachin'';

p varchar(30);

n int;

begin

open cb1;

loop

fetch cb1 into p,n;

exit when not found;

update cp set nos_of_share=nos_of_share+n where pname=''Rahul'';

update cp set nos_of_share=nos_of_share-n where pname=p;

end loop;

close cb1;

end '

language 'plpgsql';

CREATE FUNCTION

company4=# select b11();

b11

-----

(1 row)

company4=# select * from cp;

name | pname | nos_of_share

------------+------------+--------------

infosystem | Vidhyadhar | 25

infotec | darshan | 5

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 80


wipro | shiva | 12

infosys | Rahul | 25

info | sachin | 0

(5 rows)

company4=#

Assignment 5 : Cursor

SET A (Using Student-Marks Database)

marks4=# select * from student;

rno | name | addrs | class

-----+-----+---------------+------

101 | ABC | Sadhashiv Peth | Fy

102 | DEF | Sadhashiv Peth | Fy

103 | GHI | Sadhashiv Peth | Fy

104 | JKL | Nana Peth | Sy

105 | MNO | Nana Peth | Sy

(5 rows)

marks4=# select * from subject;

scode | sname

-------+-------------

1 | Science

2 | Comp.Science

3 | Electronics

(3 rows)

marks4=# select * from ss;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 81


rno | scode | marks

-----+------+------

101 | 1 | 80

102 | 1 | 85

103 | 1 | 81

104 | 2 | 76

105 | 3 | 71

104 | 1 | 69

105 | 1 | 79

101 | 2 | 79

101 | 3 | 89

102 | 2 | 71

102 | 3 | 72

103 | 2 | 76

103 | 3 | 64

104 | 3 | 69

105 | 2 | 89

(15 rows)

marks4=#

1)

marks4=# create function c1(addrs varchar(20))returns void as '

declare c11 cursor for select name,subject,marks from student,subject,ss where student.rno=ss.rno and
subject.scode=ss.scode and addrs=''Sadhashiv Peth'';

n varchar(20);

s varchar(20);

m int;

begin

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 82


open c11;

loop

fetch c11 into n,s,m;

exit when not found;

raise notice''Name:%'',n;

raise notice''Subject Name:%'',s;

raise notice''Marks:%'',m;

end loop;

close c11;

end '

language 'plpgsql';

CREATE FUNCTION

marks4=# select c1('Sadhashib Peth');

c1

----

(1 row)

marks4=# select c1('Sadhashiv Peth');

NOTICE: Name:ABC

NOTICE: Subject Name:(1,Science)

NOTICE: Marks:80

NOTICE: Name:DEF

NOTICE: Subject Name:(1,Science)

NOTICE: Marks:85

NOTICE: Name:GHI

NOTICE: Subject Name:(1,Science)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 83


NOTICE: Marks:81

NOTICE: Name:JKL

NOTICE: Subject Name:(2,Comp.Science)

NOTICE: Marks:76

NOTICE: Name:MNO

NOTICE: Subject Name:(3,Electronics)

NOTICE: Marks:71

c1

----

(1 row)

2)

marks4=# create function c22()returns void as '

declare c21 cursor for select rno,count(scode),sum(marks) from ss group by rno;

r int;

s int;

m int;

p float;

begin

open c21;

loop

fetch c21 into r,s,m;

exit when not found;

p=(m*100)/(s*100);

raise notice''Roll Nos :% '',r;

raise notice''Total : %'',m;

raise notice''Percentage:%'',p;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 84


end loop;

close c21;

end '

language 'plpgsql';

CREATE FUNCTION

marks4=# select c22();

NOTICE: Roll Nos :103

NOTICE: Total : 221

NOTICE: Percentage:73

NOTICE: Roll Nos :101

NOTICE: Total : 248

NOTICE: Percentage:82

NOTICE: Roll Nos :105

NOTICE: Total : 239

NOTICE: Percentage:79

NOTICE: Roll Nos :104

NOTICE: Total : 214

NOTICE: Percentage:71

NOTICE: Roll Nos :102

NOTICE: Total : 228

NOTICE: Percentage:76

c22

-----

(1 row)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 85


Assignment 5 : Cursor

SET D (Using Railway reservation Database)

railway4=# \d

List of relations

Schema | Name | Type | Owner

--------+-----------+-------+----------

public | passenger | table | postgres

public | ticket | table | postgres

public | train | table | postgres

(3 rows)

railway4=# select * from passenger;

pid | pname | addr | age | gender

-----+------------+------+-----+--------

11 | mr jadhav | abc | 20 | male

22 | mr patil | xyz | 30 | male

33 | mrs patil | xyz | 40 | female

44 | mrs jadhav | sty | 50 | female

(4 rows)

railway4=# select * from ticket;

tno | pid | ticket_no | b_no | no_berths | date | ticket_amt | status | da

-----+-----+-----------+------+-----------+------------+------------+--------+------------

101 | 11 | 1 | 12 | 5 | 2009-05-03 | | |

102 | 22 | 2 | 15 | 3 | 2009-04-02 | | |

104 | 44 | 5 | 12 | 5 | 2010-05-03 | 4000.00 | w |

102 | 22 | 6 | 12 | 5 | 2010-03-03 | 4000.00 | w |

103 | 33 | 4 | 12 | 5 | 2010-05-03 | 2000.00 | c | 2010-05-03

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 86


101 | 11 | 3 | 12 | 5 | 2010-05-03 | 2500.00 | w |

(6 rows)

railway4=# select * from train;

tno | tname | d_time | a_time | s_stin | dest_stin | no_of_bogies | bogies_capacity

-----+-----------------+----------+----------+--------+-----------+--------------+-----------------

101 | chennai express | 11:00:00 | 21:30:00 | abc | xyz | 12 | 45

102 | mumbai express | 12:00:00 | 21:30:00 | def | pqr | 20 | 60

103 | pune express | 12:00:00 | 22:30:00 | jkl | stu | 30 | 60

104 | express | 12:00:00 | 22:30:00 | mno | ghi | 40 | 80

(4 rows)

railway4=#

1)

railway4=# create function fu1()returns void as '

declare cf1 cursor for select pname from passenger where pid in(select pid from ticket where da=''2010-
05-03'' and status=''c'');

paname char(20);

begin

open cf1;

loop

fetch cf1 into paname;

exit when not found;

raise notice ''Passenger Name: %'',paname;

end loop;

close cf1;

end '

language 'plpgsql';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 87


CREATE FUNCTION

railway4=# select fu1();

NOTICE: Passenger Name: mrs patil

fu1

-----

(1 row)

2)

railway4=# create function fu4()returns void as '

declare cf2 cursor for select count(pname) from passenger where pid in(select pid from ticket where
date=''2010-05-03'' and status=''w'');

cnt int;

begin

open cf2;

loop

fetch cf2 into cnt;

exit when not found;

raise notice ''Total: %'',cnt;

end loop;

close cf2;

end '

language 'plpgsql';

CREATE FUNCTION

railway4=# select fu4();

NOTICE: Total: 2

fu4

-----

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 88


(1 row)

Assignment 5 : Cursor

SET E (Using Bus Driver Database)

buss4=# \d

List of relations

Schema | Name | Type | Owner

--------+--------+-------+----------

public | bd | table | postgres

public | bus | table | postgres

public | driver | table | postgres

public | route | table | postgres

(4 rows)

buss4=# select * from bd

buss4-# ;

bno | dno | duty_date | shift

-----+-----+------------+-------

11 | 1 | 2009-05-03 | m

22 | 2 | 2009-04-03 | e

33 | 3 | 2009-03-03 | m

(3 rows)

buss4=# select * from bus;

bno | capacity | dname | rno

-----+----------+------------+-----

11 | 40 | shivam | 101

22 | 30 | shivshakti | 102

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 89


33 | 50 | shiv | 103

(3 rows)

buss4=# select * from driver;

dno | dname | licenes_no | addr | d_age | salary

-----+-----------+------------+----------------------+-------+--------

1 | darshan | 100 | aba | 30 | 2000

2 | vidya | 200 | abb | 20 | 4000

3 | vidyadhar | 300 | bbb | 40 | 5000

(3 rows)

buss4=# select * from route;

rno | source | destination | no_of_stations

-----+----------------------+----------------------+----------------

101 | xyz | abc | 2

102 | pqr | efg | 3

103 | uvw | hij | 5

(3 rows)

buss4=# select dname,licenes_no,salary from driver;

dname | licenes_no | salary

-----------+------------+--------

darshan | 100 | 2000

vidya | 200 | 4000

vidyadhar | 300 | 5000

(3 rows)

buss4=# \d driver;

Table "public.driver"

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 90


Column | Type | Modifiers

------------+-----------------------+-----------

dno | integer | not null

dname | character varying(20) |

licenes_no | integer |

addr | character(20) |

d_age | integer |

salary | double precision |

1)

buss4=# create function e1(dname varchar(20))returns void as '

declare e11 cursor for select dname,licenes_no,salary from driver;

n varchar(20);

l int;

s float;

begin

open e11;

loop

fetch e11 into n,l,s;

exit when not found;

raise notice''Driver Name:%'',n;

raise notice''Licenes Nos:%'',l;

raise notice''Salary:%'',s;

end loop;

close e11;

end '

language 'plpgsql';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 91


CREATE FUNCTION

buss4=# select e1('darshan');

NOTICE: Driver Name:darshan

NOTICE: Licenes Nos:100

NOTICE: Salary:2000

NOTICE: Driver Name:darshan

NOTICE: Licenes Nos:200

NOTICE: Salary:4000

NOTICE: Driver Name:darshan

NOTICE: Licenes Nos:300

NOTICE: Salary:5000

e1

----

(1 row)

2)

buss4=# create function e2()returns void as '

declare e21 cursor for select source,destination from route where rno=101;

declare e22 cursor for select source,destination from route where rno=102;

s char(20);

d char(20);

sa char(20);

da char(20);

begin

open e21;

loop

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 92


fetch e21 into s,d;

exit when not found;

raise notice''Source:%'',s;

raise notice''Destination:%'',d;

end loop;

close e21;

open e22;

loop

fetch e22 into sa,da;

exit when not found;

raise notice''Source:%'',sa;

raise notice''Destinatio:%'',da;

end loop;

close e22;

end '

language 'plpgsql';

CREATE FUNCTION

buss4=# select e2();

NOTICE: Source:xyz

NOTICE: Destination:abc

NOTICE: Source:pqr

NOTICE: Destinatio:efg

e2

----

(1 row)

buss4=#

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 93


Assignment 6 : Handling Errors & Exception

SET A (Using Bank Database)

1)

create or replace function f1(nm varchar(30))returns text as '

declare rec record;

cnt int;

begin

for rec in select * from branch

loop

if(rec.bname<>nm)then

raise notice''Invalid'';

else

select count(cno) into cnt from bcl where bid in(select bid from branch where rec.bname=nm);

end if;

end loop;

raise notice''customer count is : %'',cnt;

return'' '';

end '

language 'plpgsql';

CREATE FUNCTION

bank4=# select f1('Deccan');

NOTICE: Invalid

NOTICE: Invalid

NOTICE: Invalid

NOTICE: Invalid

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 94


NOTICE: Invalid

NOTICE: Invalid

NOTICE: customer count is : 10

f1

----

(1 row)

2)

create or replace function f2()returns text as'

declare rec record;

begin

for rec in select * from la

loop

if(rec.appmoney<''$10000'')then

raise notice ''amount is less than 10000 '';

else

update la set appmoney=appmoney+(appmoney*0.2)where lano=rec.lano;

end if;

end loop;

return '''';

end'

language'plpgsql';

CREATE FUNCTION

bank4=# select f2();

NOTICE: amount is less than 10000

NOTICE: amount is less than 10000

NOTICE: amount is less than 10000

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 95


NOTICE: amount is less than 10000

NOTICE: amount is less than 10000

f2

----

(1 row)

Assignment 6 : Handling Errors & Exception

SET B (Using Project-Employee Database)

1)

project4=# create or replace function f3() returns text as'

declare rec record;

n int;

begin

select hours into n from pe where pno in(select pno from project where pname=''Electronics'');

update pe set hours=hours-2 where pno in(select pno from project where pname=''Electronics'');

if(n-2=0)then

raise notice''hours are 0'';

end if;

raise notice''number : %'',n;

return'' '';

end'

language 'plpgsql';

CREATE FUNCTIO

project4=# select f3();

NOTICE: number : 8

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 96


f3

----

(1 row)

project4=#

Assignment 6 : Handling Errors & Exception

SET C (Using Bus Transport Database)

1)

buss4=# create or replace function f4(d date) returns text as'

declare

rec record;

nm driver.dname%type;

begin

for rec in select * from bd

loop

if(rec.duty_date<>d)then

raise notice''Date Is Invalid'';

else

select dname into nm from driver where dno in(select dno from bd where shift=''m'' and shift=''e'' and
duty_date=d);

end if;

end loop;

raise notice''dname=%'',nm;

return'' '';

end'

language 'plpgsql';

CREATE FUNCTION

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 97


buss4=# select f4('2009-04-03');

NOTICE: Date Is Invalid

NOTICE: dname=<NULL>

f4

----

(1 row)

buss4=#

2)

create or replace function f5(b int,dt date)returns text as'

declare rec record;

name driver.dname%type;

begin

for rec in select * from bd

loop

if(rec.bno<>b)then

raise notice''invalid'';

else select dname into name from driver where dno in(select dno from bd where bno=b and
duty_date=dt);

end if;

end loop;

raise notice ''driver name=%'',name;

return '' '';

end'

language 'plpgsql';

CREATE FUNCTION

buss4=# select * from bd;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 98


bno | dno | duty_date | shift

-----+----+-----------+------

22 | 2 | 2009-04-03 | e

33 | 3 | 2009-03-03 | m

(2 rows)

buss4=# select f5(22,'2009-04-03');

NOTICE: invalid

NOTICE: driver name=vidya

f5

----

(1 row)

==========================================================================

Assignment 7 : Triggers

SET A (Using Item-Supplier Database)

supplier4=# select * from item;

ino | iname | quantity

-----+------+---------

101 | ABC | 100

102 | DEF | 150

103 | GHI | 50

104 | JKL | 250

105 | MNO | 200

(5 rows)

supplier4=# select * from supplier;

sno | sname | address | city

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 99


-----+-----------+---------+-----

1 | Shiva | Kothrud | Pune

2 | Darshan | Bibewadi | Pune

3 | Vidhyadhar | Katraj | Pune

(3 rows)

supplier4=# select * from itemsupplier

supplier4-# ;

ino | sno | rate

-----+----+----------

101 | 1 | $1,000.00

101 | 1 | $2,000.00

102 | 1 | $1,500.00

103 | 2 | $2,500.00

104 | 2 | $500.00

105 | 3 | $1,500.00

(6 rows)

--------------------------------------------------------------------------

1)

create or replace function a1() returns trigger as '

begin

if(new.rate-old.rate)>''$2000'' then

raise exception''Difference Should B Less Then $2000 % '',new;

end if;return new;

end '

language 'plpgsql';

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 100


CREATE FUNCTION

supplier4=# create trigger t3 before insert or update on itemsupplier for each row execute procedure a1();

CREATE TRIGGER

supplier4=# update itemsupplier set rate='$6000' where ino=104;

ERROR: Difference Should B Less Then $2000 (104,2,"$6,000.00")

supplier4=# update itemsupplier set rate='$2000' where ino=105;

UPDATE 1

2)

supplier4=# create or replace function a2() returns trigger as '

begin

if(new.rate =''$0'') then

raise exception''Rate Should Not Be Zero %'',new;

end if;return new;

end '

language 'plpgsql';

CREATE FUNCTION

supplier4=# create trigger T11 before insert or update on itemsupplier for each row execute procedure
a2();

CREATE TRIGGER

supplier4=# insert into itemsupplier values(101,1,'$0');

ERROR: Rate Should Not Be Zero (101,1,$0.00)

Assignment 7 : Triggers

SET B (Using Student-Marks Database)

student4=# \d

List of relations

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 101


Schema | Name | Type | Owner

--------+---------+-------+----------

public | s1 | view | postgres

public | s2 | view | postgres

public | s2a | view | postgres

public | st | table | postgres

public | student | table | postgres

public | teacher | table | postgres

(6 rows)

1)

student4=# create or replace function f5() returns trigger as '

student4'# begin

student4'# raise exception''Student Record Is Deleted %'',old;

student4'# return old;

student4'# end '

student4-# language 'plpgsql';

CREATE FUNCTION

student4=# create trigger t1 before delete on student for each row execute procedure f5();

CREATE TRIGGER

student4=# select * from student;

sno | sname | sclass | saddrs

-----+---------+--------+--------

101 | Pratik | FY | Pune

102 | Harshal | FY | Pune

103 | Suresh | SY | Pune

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 102


104 | Ramesh | SY | Mumbai

105 | Jayesh | SY | Mumbai

106 | Rakesh | SY | Mumbai

107 | Vikesh | TY | Pune

108 | Mikesh | TY | Pune

109 | Mitesh | TY | Pune

110 | Priyesh | TY | Pune

(10 rows)

student4=# delete from student;

ERROR: Student Record Is Deleted (101,Pratik,FY,Pune)

student4=#

2)

student4=# create or replace function f6() returns trigger as '

begin

if new.marks<10 or new.marks>100 then

raise exception'' Invalid %'',new;

end if;

return new;

end '

language 'plpgsql';

CREATE FUNCTION

student4=# create trigger t4 before insert or update on ss for each row execute procedure f6();

CREATE TRIGGER

student4=# insert into ss values(105,4,91);

INSERT 0 1

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 103


student4=# insert into ss values(105,4,09);

ERROR: Invalid (105,4,9)

student4=#

Assignment 7 : Triggers

SET C (Using News Paper Database)

[NRC@localhost ~]$ su - postgres;

Password:

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=# create database paper4

postgres-# ;

CREATE DATABASE

postgres=# create table newspaper(name varchar(20) primary key,language varchar(20),publisher


varchar(20),cost money);

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "newspaper_pkey" for table
"newspaper"

CREATE TABLE

postgres=# create table cities(pincode varchar(6) primary key,city varchar(20),state varchar(20));

NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "cities_pkey" for table "cities"

CREATE TABLE

postgres=# create table n_c(name varchar(20) references newspaper(name) on delete cascade,pincode


varchar(6) references cities(pincode) on delete cascade);

CREATE TABLE

postgres=# select * from newspaper;

name | language | publisher | cost

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 104


------+----------+-----------+------

(0 rows)

postgres=# select * from cities;

pincode | city | state

---------+------+-------

(0 rows)

postgres=# select * from n_c;

name | pincode

------+---------

(0 rows)

postgres=# drop table n_c;

DROP TABLE

postgres=# create table n_c(name varchar(20) references newspaper(name) on delete cascade,pincode


varchar(6) references cities(pincode) on delete cascade,daily_req int);

CREATE TABLE

postgres=# select * from n_c;

name | pincode | daily_req

------+---------+-----------

(0 rows)

postgres=# insert into newspaper values('Sakal','Marathi','Sakal','$2');

INSERT 0 1

postgres=# insert into newspaper values('Today','Marathi','Sakal','$2');

INSERT 0 1

postgres=# insert into newspaper values('XYZ','English','Abc','$5');

INSERT 0 1

postgres=# insert into cities values('411002','Pune','Maharashtra');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 105


INSERT 0 1

postgres=# insert into cities values('422020','Nashik','Maharashtra');

INSERT 0 1

postgres=# insert into cities values('455020','XYZ','Kolkata');

INSERT 0 1

postgres=# insert into n_c values('Sakal','411002',5000);

INSERT 0 1

postgres=# insert into n_c values('Today','422020',500);

INSERT 0 1

postgres=# insert into n_c values('XYZ','455020',500);

INSERT 0 1

postgres=# select * from newspaper;

name | language | publisher | cost

-------+----------+-----------+-------

Sakal | Marathi | Sakal | $2.00

Today | Marathi | Sakal | $2.00

XYZ | English | Abc | $5.00

(3 rows)

postgres=# select * from cities;

pincode | city | state

---------+--------+-------------

411002 | Pune | Maharashtra

422020 | Nashik | Maharashtra

455020 | XYZ | Kolkata

(3 rows)

postgres=# select * from n_c;

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 106


name | pincode | daily_req

-------+---------+-----------

Sakal | 411002 | 5000

Today | 422020 | 500

XYZ | 455020 | 500

(3 rows)

postgres=#

postgres=# \q

-bash-4.1$ psql

psql (8.4.5)

Type "help" for help.

postgres=#

1)

create or replace function t11()returns trigger as '

begin

if length(new.pincode)<6 or length(new.pincode)>6 then

raise exception ''Incorrect Pincode %'',new;

end if;

return new;

end '

language 'plpgsql';

CREATE FUNCTION

create trigger t1 before insert on cities for each row execute procedure t11();

CREATE TRIGGER

paper4=# insert into cities values('41101','Solapur','Maharashtra');

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 107


ERROR: Incorrect Pincode (41101,Solapur,Maharashtra)

paper4=# insert into cities values('411012','Solapur','Maharashtra');

INSERT 0 1

paper4=# insert into cities values('4110121','Solapur','Maharashtra');

ERROR: value too long for type character varying(6)

paper4=#

2)

paper4=# create or replace function t12()returns trigger as '

begin

if old.state=''Maharashtra'' then

raise exception ''Cant Delete %'',old;

end if;

return old;

end '

language 'plpgsql';

CREATE FUNCTION

paper4=# create trigger t2 before delete on cities for each row execute procedure t12();

CREATE TRIGGER

paper4=# select * from cities;

pincode | city | state

---------+---------+-------------

411002 | Pune | Maharashtra

422020 | Nashik | Maharashtra

455020 | XYZ | Kolkata

411012 | Solapur | Maharashtra

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 108


(4 rows)

paper4=# delete from cities where state='Maharashtra';

ERROR: Cant Delete (411002,Pune,Maharashtra)

paper4=# delete from cities where state='Kolkata';

DELETE 1

Assignment 7 : Triggers

SET D (Using Railway reservation Database)

1)

railway4=# create or replace function t1()returns trigger as '

begin

if new.a_time<new.d_time then

raise exception ''correct time %'',new;

end if;

return new;

end '

language 'plpgsql';

CREATE FUNCTION

create trigger t2 before insert or update on train for each row execute procedure t1();

CREATE TRIGGER

railway4=# insert into train values(105,'Kanyakumari express','20:00:00','18:00:00','aaa','xxx',12,60);

INSERT 0 1

railway4=# insert into train values(106,'Kanyakumari express','12:00:00','18:00:00','aaa','xxx',12,60);

ERROR: arrival time 18:00:00 should be less than departure time

2)

railway4=# create trigger t8 before insert or update on ticket for each row execute procedure f1();

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 109


CREATE TRIGGER

railway4=# select * from ticket;

tno | pid | ticket_no | b_no | no_berths | date | ticket_amt | status | da

-----+-----+-----------+------+-----------+------------+------------+--------+------------

101 | 11 | 1 | 12 | 5 | 2009-05-03 | | |

102 | 22 | 2 | 15 | 3 | 2009-04-02 | | |

104 | 44 | 5 | 12 | 5 | 2010-05-03 | 4000.00 | w |

102 | 22 | 6 | 12 | 5 | 2010-03-03 | 4000.00 | w |

101 | 11 | 3 | 12 | 5 | 2010-05-03 | 2500.00 | w |

103 | 33 | 4 | 12 | 5 | 2010-05-03 | 2000.00 | c | 2010-05-03

(6 rows)

railway4=# create or replace function f1() returns trigger as'

begin

if old.status!=new.status then

raise exception ''Cannot change status %'',old;

end if;

return old;

end

'

language 'plpgsql';

CREATE FUNCTION

railway4=# create trigger t before insert or update on ticket for each row execute procedure f1();

CREATE TRIGGER

railway4=# update ticket set status='c' where tno=104;

ERROR: Cannot change status (104,44,5,12,5,2010-05-03,4000.00,w,)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 110


railway4=# update ticket set status='w' where tno=104;

UPDATE 1

Assignment 7 : Triggers

SET E (Using Bus Transportation Database)

1)

buss4=# select * from driver;

dno | dname | licenes_no | addr | d_age | salary

-----+----------+-----------+---------------------+------+-------

1 | darshan | 100 | aba | 30 | 2000

2 | vidya | 200 | abb | 20 | 4000

3 | vidyadhar | 300 | bbb | 40 | 5000

(3 rows)

1)

buss4=# create or replace function agep() returns trigger as '

begin

if new.d_age<18 or new.d_age>50 then

raise exception''Invalid Age % '',new;

end if; return new;

end '

language 'plpgsql';

CREATE FUNCTION

buss4=# create trigger t1 after insert or update on driver for each row execute procedure agep();

CREATE TRIGGER

buss4=# insert into driver values(4,'ABC',400,'xyz',15,15000);

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 111


ERROR: Invalid Age (4,ABC,400,"xyz ",15,15000)

buss4=# insert into driver values(4,'ABC',400,'xyz',25,15000);

INSERT 0 1

buss4=# select * from driver;

dno | dname | licenes_no | addr | d_age | salary

-----+----------+-----------+---------------------+------+-------

1 | darshan | 100 | aba | 30 | 2000

2 | vidya | 200 | abb | 20 | 4000

3 | vidyadhar | 300 | bbb | 40 | 5000

4 | ABC | 400 | xyz | 25 | 15000

(4 rows)

buss4=# update driver set d_age=10 where dno=1;

ERROR: Invalid Age (1,darshan,100,"aba ",10,2000)

buss4=# update driver set d_age=20 where dno=1;

UPDATE 1

buss4=# select * from driver;

dno | dname | licenes_no | addr | d_age | salary

-----+----------+-----------+---------------------+------+-------

2 | vidya | 200 | abb | 20 | 4000

3 | vidyadhar | 300 | bbb | 40 | 5000

4 | ABC | 400 | xyz | 25 | 15000

1 | darshan | 100 | aba | 20 | 2000

(4 rows)

2)

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 112


uss4=# create or replace function c1()returns trigger as '

begin

if old.capacity>10 then

raise exception ''invalid%'',old;

end if;

return old;

end '

language 'plpgsql';

CREATE FUNCTION

buss4=# create trigger t22 after delete on bus for each row execute procedure c1();

CREATE TRIGGER

buss4=# insert into bus values(55,8,'abc',101);

INSERT 0 1

buss4=# select * from bus;

bno | capacity | dname | rno

-----+---------+-----------+----

22 | 30 | shivshakti | 102

33 | 50 | shiv | 103

44 | 9 | abc | 101

55 | 8 | abc | 101

(4 rows)

buss4=# delete from bus where bno=44;

DELETE 1

buss4=# delete from bus where bno=33;

ERROR: invalid(33,50,shiv,103)

===========================================================================

AHIRE CLASSES,BARAMATI (9960703408/8788100181) Page 113

You might also like