You are on page 1of 50

ADVANCES DBMS LAB ASSIGNMENT

PGSE 201(lab)

M.Tech In Software Engineering


INDEX

Sl.no Assignment Experiment name Date of Date of Page


no. Assignment submissio no
n
Create a trigger for the table
“product_price_history” for appending
new row in it whenever the
‘unit_price’ of the “product” tables
1 PGSE201(Lab)/01 24.01.14 31.01.14 4
becomes greater than 800.
Only those product information will be
appended whose ‘unit_price’ is greater
than 800.
Create a trigger for the table
2 PGSE201 (Lab)/02 “student_info” for insert new row in it 31.01.14 7.02.14 8
and implement student grade system in
“grade_info”.
Implement fine calculation in a library
3 PGSE201 (Lab)/03 management system using fine 7.02.14 14.02.14 14
calculation.
Create a procedure to display the sum
of squares of n natural number using
4 PGSE201 (Lab)/04 for loop. 14.02.14 21.02.14 18
S = 12 + 22 + 32 + 42 + …………….
+N2
Create a procedure to display the sum
of squares of n natural number using
5 PGSE201 (Lab)/05 for loop. 21.02.14 28.02.14 21
S = M2 + (M+1)2 +(M+2)2 + (M+3)2 +
……………. +N2 M<N
Create a procedure
update_student_rc_proc to update the
6 PGSE201 (Lab)/06 middle_name of a student for given 28.02.14 7.03.14 24
admission_year and register_num in
student_rc table.
Create an exception handler to display
appropriate message for the error that
7 PGSE201 (Lab)/07 the specified designation_id does not 7.03.14 14.03.14 29
exist in designation_rc table.

Create a table student_info having

2
following attributes
i) first_name
ii) last_name
iii) roll_no
iv) city
v) total_marks
vi) dateofbirth
a) Create the sequence on the table
8 PGSE201 (Lab)/08 student_info to insert data in the table
14.03.14 21.03.14 34
by ascending order of roll_no.

b) Create an index on columns


first_name, roll_no, city and
total_marks. Using the index find the
name of student who scores highest
marks in Kolkata. Then delete the
index we have created.

Populated table using cursor.


9 PGSE201 (Lab)/09 21.03.14 28.03.14 38

10 PGSE201 (Lab)/10 Introduction of POSTGRES using 28.03.14 4.4.14 43


PostgreSQL.

3
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __24.01.14__________

DATE OF SUBMISSION: ___31.01.14___________

TITLE: Create a trigger for the table “product_price_history” for appending new row in it
whenever the ‘unit_price’ of the “product” tables becomes greater than 800.

Only those product information will be appended whose ‘unit_price’ is greater than 800.

OBJECTIVE: To implement concept of trigger in PL SQL

Marks Obtained Signature of the

Sessional in - charge

4
Assignment No.:– PGSE 201(Lab) / 01

Assignment Name:

Create a trigger for the table “product_price_history” for appending new row in it whenever the
‘unit_price’ of the “product” tables becomes greater than 800.

Only those product information will be appended whose ‘unit_price’ is greater than 800.

******************************************************************************

Create “product_price_history” table

create table product_price_history

( product_id varchar2(20),

product_name varchar2(20),

supplier_name varchar2(20),

unit_price number(20));

Insert value in table:

insert into product_price_history values (2001,'monitor', 'acer',5000);

Output “product_price_history” table

select * from product_price_history;

PRODUCT_ID PRODUCT_NAME SUPPLIER_NAME UNIT_PRICE


2001 monitor acer 5000

5
Create “product” table

create table product

( product_id varchar2(20),

product_name varchar2(20),

supplier_name varchar2(20),

unit_price number(20));

Insert value in table:

insert into product values (1002,'radio', 'sony',600);

insert into product values (1003,'tv', 'philips',4000);

Output “product” table

select * from product;

PRODUCT_ID PRODUCT_NAME SUPPLIER_NAME UNIT_PRICE


1002 radio sony 600
1003 tv philips 4000

Code for Trigger:

create or replace trigger prod

after insert

on product

for each row

when(new.unit_price > 800)

begin

6
insert into product_price_history

values(:new.product_id,

:new.product_name,

:new.supplier_name,

: new.unit_price);

end;

Run the Trigger:

insert into product values (2004,'ups', 'iball',1500);

insert into product values (2005,'iron', 'bajjaj',800);

insert into product values (2006,'radio', 'LG',1200);

Output:

select * from product;

PRODUCT_ID PRODUCT_NAME SUPPLIER_NAME UNIT_PRICE


1002 radio sony 600
1003 tv philips 4000
2004 ups iball 1500
2005 Iron bajjaj 800
2006 radio LG 1200

select * from product_price_history;

PRODUCT_ID PRODUCT_NAME SUPPLIER_NAME UNIT_PRICE


2001 monitor acer 5000
2004 ups iball 1500
2006 radio LG 1200

7
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __31.01.14__________

DATE OF SUBMISSION: ___7.02.14___________

TITLE : Create a trigger for the table “student_info” for insert new row in it and implement
student grade system in “grade_info”.

OBJECTIVE: To implement concept of trigger and function in pl sql

Marks Obtained Signature of the

Sessional in - charge

8
Assignment No.:– PGSE 201(Lab) / 02
Assignment Name:
Create a trigger for the table “student_info” for insert new row in it and implement student
grade system in “grade_info”.

Create “student_info” table


create table student_info
(student_name varchar2(10),
student_roll number,
stream varchar2(10),
mark1 number,
mark2 number,
mark3 number,
mark4 number);
Insert value in table:
insert into student_info values ('Rahul',1001,'ece',60,80,65,75);

Output “student_info” table

select * from student_info;


STUDENT_NAM STUDENT_ROL STREA MARK MARK MARK MARK
E L M 1 2 3 4
Rahul 1001 ece 60 80 65 75

9
Create “grade_info” table

create table grade_info


(student_name varchar2(10),
student_roll number,
stream varchar2(10),
mark1 number,
mark2 number,
mark3 number,
mark4 number,
grade1 varchar2(10),
grade2 varchar2(10),
grade3 varchar2(10),
grade4 varchar2(10),
total number);
Insert value in table:
insert into grade_info values ('Ramesh',1001,56,67,78,80,'C','B','A','E',281);

Output “grade_info” table


select * from grade_info;

STUDEN STUDEN MA MA MA MA GRA GRA GRA GRA TO


T_NAME T_ROLL RK1 RK2 RK3 RK4 DE1 DE2 DE3 DE4 TA
L
Ramesh 1001 56 67 78 80 C B A E 281

10
Code for Trigger:

CREATE or replace TRIGGER gradee


AFTER INSERT
ON student_info
FOR EACH ROW
DECLARE
g1 VARCHAR2(10);
g2 VARCHAR2(10);
g3 VARCHAR2(10);
g4 VARCHAR2(10);
tot NUMBER (10);
BEGIN
IF (:new.mark1 > 40) AND (:new.mark2 > 40) AND (:new.mark3 > 40) AND (:new.mark4 >
40) THEN
g1 := grade_call(:new.mark1);
g2 := grade_call(:new.mark2);
g3 := grade_call(:new.mark3);
g4 := grade_call(:new.mark4);
tot := :new.mark1 + :new.mark2 + :new.mark3 + :new.mark4;
insert into grade_info values
(:new.student_name,:new.student_roll,:new.stream,:new.mark1,:new.mark2,:new.mark3,:new.m
ark4,g1,g2,g3,g4,tot);
END IF;
END

11
Run the Trigger:
insert into student_info values ('sourav',1002,'IT',90,83,70,64);
insert into student_info values ('suvankar',1003,'BT',60,81,54,34);

Output:
select * from student_info;
STUDENT_NAM STUDENT_ROL STREA MARK MARK MARK MARK
E L M 1 2 3 4
Rahul 1001 ece 60 80 65 75
sourav 1002 IT 90 83 70 64
Suvankar 1003 BT 60 81 54 34

Code for Function:

CREATE OR REPLACE FUNCTION grade_call


(marks IN NUMBER)
RETURN VARCHAR2
IS
grade VARCHAR2(5);
BEGIN
IF marks BETWEEN 90 AND 100 THEN
grade:='O';
ELSIF marks BETWEEN 80 AND 89 THEN
grade:='E';
ELSIF marks BETWEEN 70 AND 79 THEN

12
grade:='A';
ELSIF marks BETWEEN 60 AND 69 THEN
grade:='B';
ELSIF marks BETWEEN 50 AND 59 THEN
grade:='C';
ELSIF marks BETWEEN 40 AND 49 THEN
grade:='D';
ELSE
grade:='F';
END IF;
RETURN grade;
END;

select * from grade_info;

STUDEN STUDEN MA MA MA MA GRA GRA GRA GRA TO


T_NAME T_ROLL RK1 RK2 RK3 RK4 DE1 DE2 DE3 DE4 TA
L
Ramesh 1001 56 67 78 80 C B A E 281
sourav 1002 IT 90 83 70 O E A B 307
Suvankar 1003 BT 60 81 54 O C D F 209

13
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __7.02.14__________

DATE OF SUBMISSION: ___14.02.14___________

TITLE: Implement fine calculation in a library management system using fine calculation

OBJECTIVE: To implement fine calculation in a library management system using calculation.

Marks Obtained Signature of the

Sessional in - charge

14
Assignment No.:– PGSE 201(Lab) / 03
Assignment Name:
Implement fine calculation in a library management system using fine calculation.

Create Tables:
create table issue_book
(student_roll varchar2(10),
book_id varchar2(10),
issue_date date,
due_date date);

create table return_book


(student_roll varchar2(10),
book_id varchar2(10),
due_date date,
return_date date);

create table fine


(student_roll varchar2(10),book_id varchar2(10),amount number);

15
Function
create OR REPLACE FUNCTION find_due_date
(dt IN DATE)
return DATE
IS ndt DATE;
BEGIN
ndt :=dt+30;
return ndt;
END;
Trigger:
CREATE OR REPLACE TRIGGER return_table
AFTER INSERT
ON return_book
FOR EACH ROW
DECLARE
no NUMBER;
d1 DATE;
d2 DATE;
BEGIN
d1 := :new.return_date;
d2 := :new.due_date;
no := to_date(d1) - to_date(d2);
IF no>4 THEN

16
no :=no * 3;
insert into fine values(:new.student_roll,:new.book_id,no);
END IF;
END;
Run trigger:
Step 1:
insert into issue_book values ('h1001','r102','15-jan-2013', '15-feb-2013');
insert into issue_book values ('k1001','ff103','16-jan-2013', '16-feb-2013');

output :
select * from issue_book

STUDENT_ROL BOOK_I ISSUE_DAT DUE_DAT


L D E E
h1001 r102 15-JAN-13 15-feb-2013
k1001 Ff103 16-JAN-13 16-feb-2013

Step2:
insert into issue_book values ('h1001','r102',','15-feb-2013',’24-feb-2013’);
insert into issue_book values ('k1001','ff103', '16-feb-2013,’18-feb-2013');

select * from return_book

STUDENT_ROLL BOOK_ID DUE_DATE RETURN_DATE


h1001 r102 15-feb-2013 24-feb-2013
k1001 ff103 16-feb-2013 18-feb-2013

17
select * from fine

STUDENT_ROLL BOOK_ID AMOUNT


h1001 r102 27

NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __14.02.14__________

DATE OF SUBMISSION: ___21.02.14___________

TITLE: Create a procedure to display the sum of squares of n natural number using for loop.

S = 12 + 22 + 32 + 42 + ……………. +N2

OBJECTIVE: To implements create a procedure to display the sum of squares of n natural number
using for loop S = 12 + 22 + 32 + 42 + ……………. +N2

Marks Obtained Signature of the

Sessional in - charge

18
Assignment No.:– PGSE 201(Lab) / 04
Assignment Name:
Create a procedure to display the sum of squares of n natural number using for loop.
S = 12 + 22 + 32 + 42 + ……………. +N2
Procedure:
CREATE OR REPLACE PROCEDURE natural_number(n IN NUMBER)
IS
i NUMBER;
s NUMBER;
BEGIN
s:=0;
FOR i IN 1..n
LOOP
s:=s+POWER(i,2);
DBMS_OUTPUT.PUT_LINE('Step='||s);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Result='||s);
END;

Calling Procedure:
BEGIN
natural_number(5);
END;

19
Output:
Step=1
Step =5
Step =14
Step =30
Step =55
Result=55

Statement processed.
0.00 seconds

20
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __21.02.14__________

DATE OF SUBMISSION: ___28.02.14___________

TITLE: Create a procedure to display the sum of squares of n natural number using for loop.

S = M2 + (M+1)2 +(M+2)2 + (M+3)2 + ……………. +N2 M<N

OBJECTIVE: To implements concept of procedure.

21
Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 05


Assignment Name:
Create a procedure to display the sum of squares of n natural number using for loop.
S = M2 + (M+1)2 +(M+2)2 + (M+3)2 + ……………. +N2 M<N

Procedure:
CREATE PROCEDURE square_mn
(m IN NUMBER, n IN NUMBER)
IS
i NUMBER;
s NUMBER;

BEGIN
s := 0;
i := m;
WHILE i <= n
LOOP
s := s + POWER(i,2);
DBMS_OUTPUT.PUT_LINE('step ' || i || ' = ' || s);

22
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('result=' || s);
END;

Calling Procedure:
BEGIN
square_mn(3,6);
END;
Output:
step 3 = 9
step 4 = 25
step 5 = 50
step 6 = 86
result=86

Statement processed.

0.00 seconds

11141129549203
11143605700225

23
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __28.02.14__________

DATE OF SUBMISSION: ___7.03.14___________

TITLE: Create a procedure update_student_rc_proc to update the middle_name of a student for


given admission_year and register_num in student_rc table.

OBJECTIVE: To Implements a procedure update_student_rc_proc to update the middle_name


of a student for given admission_year and register_num in student_rc table.

24
Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 06


Assignment Name:
Create a procedure update_student_rc_proc to update the middle_name of a student for given
admission_year and register_num in student_rc table.

Create Table1:

create table student_r04(


admission_year varchar2(4),
register_num varchar2(4),
course_id varchar2(2),
first_name varchar2(15),
middle_name varchar2(10),
last_name varchar2(15),
contact_number number(10));

25
Table2:

create table student_rc04(


admission_year varchar2(2),
register_num varchar2(4),
course_id varchar2(2),
first_name varchar2(15),
middle_name varchar2(10),
last_name varchar2(15),
contact_number number(10));

Trigger:

create or replace trigger ss55


after insert on student_r04
for each row
declare
ad varchar2(10);
re varchar2(20);
ad1 varchar2(10);
re1 varchar2(20);
con number;
con1 number;
begin
ad := :new.admission_year;
re := :new.register_num;

26
con := :new.contact_number;
ad1 :=SUBSTR(ad,1,2);
re1 := SUBSTR(re,1,2);
con1 := LENGTH(con);
IF (ad1 = re1) AND (con1=10)THEN
insert into student_rc04 values(:new.admission_year,:new.register_num,:new.course_id
,:new.first_name,:new.middle_name, :new.last_name,:new.contact_number);
END IF;
end;

insert student_r04:
insert into student_r04 values ('13','1301','p1','sourav','kumer','haldar', 9836960175);
insert into student_r04 values ('12','2002','f1',' ramesh','chandra','kumer', 94332562);

select * from student_r04;

ADMISSIO REGISTE COUR FIRST_ MIDDLE LAST_ CONTACT_


N_YEAR R_NUM SE_ID NAME _NAME NAME NUMBER
13 1301 p1 sourav kumer haldar 9836960175
12 2002 c1 ramesh chandra kumer 94332562

Select * from student_rc04;

ADMISSIO REGISTE COURS FIRST_ MIDDLE_ LAST_ CONTACT_


N_YEAR R_NUM E_ID NAME NAME NAME NUMBER
13 1301 p1 sourav kumer haldar 9836960175

Procedure
create or replace procedure student_rc_proc (

27
m_admission_year in student_rc04.admission_year%Type,
m_register_num in student_rc04.register_num%Type,
m_middle_name in student_rc04.middle_name%Type
)
is
begin
update student_rc04 set middle_name=m_middle_name where
admission_year=m_admission_year and register_num=m_register_num;
if(sql%rowcount > 0) then
dbms_output.put_line('updated successfully.');
commit;
else
dbms_output.put_line('no more data to update.');
end if;
end;

Run:
Case 1:
begin
update_student_rc_proc('13','1301','chand');
end;
output:
updated successfully.

Statement processed.

0.00 seconds

Select * from student_rc04

28
ADMISSIO REGISTE COUR FIRST_ MIDDLE LAST_ CONTACT_
N_YEAR R_NUM SE_ID NAME _NAME NAME NUMBER
13 1301 p1 sourav chand haldar 9836960175
Case 2:
begin
update_student_rc_proc('11','1001','kumer');
end;
output:
no more data to update.

Statement processed.

NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __7.03.14__________

DATE OF SUBMISSION: ___14.03.14___________

TITLE: Create an exception handler to display appropriate message for the error that the
specified designation_id does not exist in designation_rc table.

OBJECTIVE: : To implement an exception handler to display appropriate message for the error
that the specified designation_id does not exist in designation_rc table.

29
Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 07


Assignment Name:
Create an exception handler to display appropriate message for the error that the specified
designation_id does not exist in designation_rc table.

Table Creation:

Table1:
create table designation_rc03
(designation_Id varchar2(10),
designation varchar2(10),
doj date);

Table2:
create table designation_rc04
(designation11 varchar2(10),
doj11 date);

30
Table3:
create table bb
(name varchar2(20));insert into designation values('d01','manager');

insert into designation_rc04 values('master','22-jun-12');


insert into designation_rc04 values('manager','22-feb-13');

select * from designation_rc04;

DESIGNATION11 DOJ11
master 22-jun-12
manager 22-feb-13

Trigger:
create or replace trigger assign33
after insert on designation_rc04
for each row
declare
doj date;
doj12 varchar2(20);
desi varchar2(20);
desi1 varchar2(20);
desi12 varchar2(20);
row_count number;
begin
doj := :new.doj11;

31
desi12 := :new.designation11;
desi := SUBSTR(desi12 ,1 ,1);
desi1 := SUBSTR(doj,1,2);
insert into bb values(:new.doj11);
SELECT count(name)
INTO row_count
FROM bb;
doj12 := ( desi || '_' || desi1 || '_' || row_count ) ;
insert into designation_rc03 values(doj12, :new.designation11, :new.doj11);
end;
select * from designation_rc03;

DESIGNATION_ID DESIGNATION DOJ


m_22_1 master 22-jun-12
m_22_2 manager 22-feb-13

Procedure
create or replace procedure query_designation_rc (
d_design_id in designation_rc03.designation_id%Type )
is
p_desig_name designation_rc03.designation%Type;
begin
select designation into p_desig_name from designation_rc03 where
designation_id=d_design_id;
dbms_output.put_line('design_id ' || d_design_id || ' exits.');
exception
when No_Data_Found Then
dbms_output.put_line('design_id ' || d_design_id || ' does not exist.');
when others then
32
Raise_Application_Error(-20100, 'error#' || sqlcode || 'desc:' || sqlerrm);
end;Run:

Case1:
begin
query_designation_rc('m_22_1');
end;
Output:
design_id m_22_1 exits.

Statement processed.

0.02 seconds

Case2:
begin
query_designation_rc('m_04_6');
end;
Output:
design_id m_04_6 does not exist.

Statement processed.

0.00 seconds

33
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE
NAME: ____Bilas Haldar_______________________________________________________

ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __14.03.14__________

DATE OF SUBMISSION: ___21.03.14___________

TITLE: Create a table student_info having following attributes

vii) first_name
viii) last_name
ix) roll_no
x) city
xi) total_marks
xii) dateofbirth

34
a) Create the sequence on the table student_info to insert data in the table by ascending order of
roll_no.

b) Create an index on columns first_name, roll_no, city and total_marks. Using the index find the
name of student who scores highest marks in Kolkata. Then delete the index we have created.

OBJECTIVE: To implement the concept of sequence and index.

Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 08


Assignment Name:
Create a table student_info having following attributes
xiii) first_name
xiv) last_name
xv) roll_no
xvi) city
xvii) total_marks
xviii) dateofbirth
a) Create the sequence on the table student_info to insert data in the table by ascending order of
roll_no.

b) Create an index on columns first_name, roll_no, city and total_marks. Using the index find the
name of student who scores highest marks in Kolkata. Then delete the index we have created.

Create table (student_info):


CREATE TABLE student_info (
first_name varchar2(25),
last_name varchar2(25),

35
roll_no number(10),
city varchar2(25),
total_marks number(5,2),
dateofbirth date);

Output:
Table created.

0.01 seconds

11141129549203
11143605700225

Answer A)

Create Sequence (student_se):


CREATE SEQUENCE student_se
MINVALUE 1
MAXVALUE 4
START WITH 1
INCREMENT BY 1
CYCLE
CACHE 3;
Output:
Sequence created.

0.00 seconds

11141129549203
11143605700225

36
Insert 5 Values:
INSERT INTO student_info VALUES (:first_name, :last_name,
student_seq.nextval, :city, :total_marks, :dateoobirth);

Output:
SELECT * FROM student_info;

FIRST_NAM LAST_NAM ROLL_N CITY TOTAL_MA DATEOFBIR


E E O RKS TH
sonali das 1 dumdum 70 20-SEP-92
bikas mondal 2 diamond 70.5 20-SEP-92
soumitra haldar 3 kolkata 80.5 20-SEP-92
sovan kayal 4 falta 73.5 20-SEP-92
sumit das 1 sarisha 60.5 20-SEP-92

Answer B)
Run Query without using index:
SELECT first_name FROM student_info
WHERE city='kolkata' AND total_marks IN (SELECT MAX(total_marks) FROM
student_info);
44170288286817

44183034646933

SELECT first_nam

21572909937200
14106352553967
14144690751258

21606912080381

:FIRST_NAME,:LA

21736625668766

a,a,k,25,12-jan-20

37
Output:

FIRST_NAME
soumitra

Create index (student_idx):


CREATE INDEX student_idx
ON student_info (first_name, roll_no, total_marks, city);

Output:
Index created.

0.02 seconds

Run Query using index:


SELECT first_name FROM student_info
WHERE city='kolkata' AND total_marks IN (SELECT MAX(total_marks) FROM
student_info);
Output:

FIRST_NAME
soumitra

NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

38
ROLL NO : 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __21.03.14__________

DATE OF SUBMISSION: ___28.03.14___________

TITLE: Populated table using cursor.


.

OBJECTIVE: To implements concept of cursor

Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 09


Assignment Name:
Populated table using cursor.

39
create table student008(

roll_no number ,

name varchar2(20),

stream varchar2(20),

dgpa number,

primary key (roll_no));

insert:

insert into student008 values (2,'ramesh','IT',7);

insert into student008 values (3,'montu','cse',8);

select * from student008

ROLL_NO NAME STREAM DGPA


2 ramesh IT 7
3 montu cse 8

employee08:

create table employee08(

emp_id number,

employe_id number,

name varchar2(10),

doj date,

employer_name varchar2(10),

dob date,

dol date,

employee_designation varchar2(10),

primary key (emp_id));

40
insert employee08:

insert into employee08

values (1,12,'rahul','10-feb-08','rakesh','20-mar-1990','11-jan-10','manager');

insert into employee08

values (2,13,'ram','10-jan-06','radha','20-mar-1988','12-jan-09',’master');

EM EMPLO NAME DOJ EMPLOYER_ DOB DOL EMPLOYEE_D


P_I YE_ID NAME ESIGNATION
D
1 12 Rahul 10- Rakesh 20- 11-jan-10 Manager
feb- mar-
08 1990
2 13 ram 10- Radha 20- 12-jan-09 master
jan-06 mar-
1988

Student008_employee08:

create table student008_employee08(

roll_no number ,

sname varchar2(20),

stream varchar2(20),

dgpa number,

emp_id number,

employe_id number,

name varchar2(10),

doj date,

employer_name varchar2(10),

dob date,

dol date,

41
employee_designation varchar2(10),

primary key (emp_id,roll_no));

cursor:

DECLARE

CURSOR insert_cursor IS

SELECT s.roll_no as rno, s.name as sname,s.stream as str , s.dgpa as sdgp,e.emp_id as


eid,e.employe_id as emid,e.name as ena,e.doj as edoj, e.employer_name as emna,e.dob as edo,
e.dol as edol , e.employee_designation as emd

FROM student008 s, employee08 e

WHERE s.roll_no = e.emp_id;

v_employee_rec insert_cursor%ROWTYPE;

BEGIN

OPEN insert_cursor;

FETCH insert_cursor INTO v_employee_rec;

WHILE (insert_cursor%FOUND) LOOP

insert into student008_employee08 values(v_employee_rec.rno,v_employee_rec.sname,


v_employee_rec.str,v_employee_rec.sdgp,v_employee_rec.eid,v_employee_rec.emid,v_employe
e_rec.ena,v_employee_rec.edoj,v_employee_rec.emna,v_employee_rec.edo,v_employee_rec.ed
ol,v_employee_rec.emd);

FETCH insert_cursor INTO v_employee_rec;

END LOOP;

dbms_output.put_line('cursor run successfully');

CLOSE insert_cursor;

END;

RO NA STRE D EM EMPL NA D EMPLOY D DOL EMPLOY


LL ME AM GP P_I OYE_I M O ER_NAM O EE_DESI
42
_N A D D E J E B GNATION
O
2 ram IT 7 1 12 Ra 10 Rakesh 20 11-jan- Manager
esh hul - - 10
fe m
b- ar
08 -
19
90
3 mo cse 8 2 13 ra 10 Radha 20 12-jan- master
ntu m - - 09
ja m
n- ar
06 -
19
88
Select * from student008_employee08;

NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE

NAME: ____Bilas Haldar_______________________________________________________

43
ROLL NO: 14411313002 DEPARTMENT: __________CSE___________

DATE OF ASSIGNMENT: __28.03.14__________

DATE OF SUBMISSION: ___4.04.14___________

TITLE: Introduction of POSTGRES using PostgreSQL.

OBJECTIVE: Implementation of POSTGRES using PostgreSQL

Marks Obtained Signature of the

Sessional in - charge

Assignment No.:– PGSE 201(Lab) / 10


Assignment Name:
Introduction of POSTGRES using PostgreSQL.

44
Create table:
CREATE TABLE weather (
city varchar(80),
temp_lo int,
temp_hi int,
prcp real,
date date
);

CREATE TABLE cities (


name varchar(80),
location point
);

Insert value into tables:


INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date) VALUES ('San Francisco', 43, 57,
0.0, '1994-11-29');

INSERT INTO weather (date, city, temp_hi, temp_lo) VALUES ('1994-11-29', 'Hayward', 54,
37);

Query a table:

45
postgres=# SELECT * FROM weather;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 43 | 57 | 0 | 1994-11-29
Hayward | 37 | 54 | | 1994-11-29
San Francisco | 46 | 50 | 0.25 | 1994-11-27
(3 rows)

postgres=# SELECT * FROM cities;


name | location
---------------+-----------
San Francisco | (-194,53)
(1 row)

postgres=# SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;


city | temp_avg | date
---------------+----------+------------
San Francisco | 50 | 1994-11-29
Hayward | 45 | 1994-11-29
San Francisco | 48 | 1994-11-27
(3 rows)

postgres=# SELECT * FROM weather


postgres-# WHERE city = 'San Francisco' AND prcp > 0.0;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27

46
(1 row)

postgres=# SELECT * FROM weather


postgres-# ORDER BY city;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
Hayward | 37 | 54 | | 1994-11-29
San Francisco | 43 | 57 | 0 | 1994-11-29
San Francisco | 46 | 50 | 0.25 | 1994-11-27
(3 rows)

postgres=# SELECT DISTINCT city


postgres-# FROM weather
postgres-# ORDER BY city;
city
---------------
Hayward
San Francisco
(2 rows)

Join between tables:


postgres=# SELECT *
postgres-# FROM weather, cities
postgres-# WHERE city = name;
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------

47
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisc | (-194,53)
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
(2 rows)

postgres=# SELECT *
postgres-# FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
city | temp_lo | temp_hi | prcp | date | name | location
---------------+---------+---------+------+------------+---------------+-----------
San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
Hayward | 37 | 54 | | 1994-11-29 | |
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
(3 rows)

postgres=# SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,


postgres-# W2.city, W2.temp_lo AS low, W2.temp_hi AS high
postgres-# FROM weather W1, weather W2
postgres-# WHERE W1.temp_lo < W2.temp_lo
postgres-# AND W1.temp_hi > W2.temp_hi;
city | low | high | city | low | high
---------------+-----+------+---------------+-----+------
San Francisco | 43 | 57 | San Francisco | 46 | 50
Hayward | 37 | 54 | San Francisco | 46 | 50
(2 rows)

Aggregation Function:

48
postgres=# SELECT max(temp_lo) FROM weather;
max
-----
46
(1 row)

postgres=# SELECT city FROM weather


postgres-# WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
city
---------------
San Francisco
(1 row)

Update:
postgres=# UPDATE weather
postgres-# SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2
postgres-# WHERE date > '1994-11-28';
UPDATE 2
postgres=# SELECT * FROM weather;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 41 | 55 | 0 | 1994-11-29
Hayward | 35 | 52 | | 1994-11-29
(3 rows)

Deletions:

49
postgres=# DELETE FROM weather WHERE city = 'Hayward';
DELETE 1
postgres=# SELECT * FROM weather;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 41 | 55 | 0 | 1994-11-29
(2 rows)

Views
postgres=# CREATE VIEW myview AS
postgres-# SELECT city, temp_lo, temp_hi, prcp, date, location
postgres-# FROM weather, cities
postgres-# WHERE city = name;
CREATE VIEW
postgres=# SELECT * FROM myview;
city | temp_lo | temp_hi | prcp | date | location
---------------+---------+---------+------+------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | (-194,53)
San Francisco | 41 | 55 | 0 | 1994-11-29 | (-194,53)
(2 rows)

50

You might also like