Professional Documents
Culture Documents
PGSE 201(lab)
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.
3
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE
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.
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.
******************************************************************************
( product_id varchar2(20),
product_name varchar2(20),
supplier_name varchar2(20),
unit_price number(20));
5
Create “product” table
( product_id varchar2(20),
product_name varchar2(20),
supplier_name varchar2(20),
unit_price number(20));
after insert
on product
begin
6
insert into product_price_history
values(:new.product_id,
:new.product_name,
:new.supplier_name,
: new.unit_price);
end;
Output:
7
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE
TITLE : Create a trigger for the table “student_info” for insert new row in it and implement
student grade system in “grade_info”.
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”.
9
Create “grade_info” table
10
Code for Trigger:
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
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;
13
NEOTIA INSTITUTE OF TECHNOLOGY MANAGEMENT & SCIENCE
TITLE: Implement fine calculation in a library management system using fine calculation
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);
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
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');
17
select * from fine
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
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
TITLE: Create a procedure to display the sum of squares of n natural number using for loop.
21
Marks Obtained Signature of the
Sessional in - charge
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
24
Marks Obtained Signature of the
Sessional in - charge
Create Table1:
25
Table2:
Trigger:
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);
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
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.
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
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');
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;
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_______________________________________________________
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.
Sessional in - charge
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.
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)
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;
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
Output:
Index created.
0.02 seconds
FIRST_NAME
soumitra
38
ROLL NO : 14411313002 DEPARTMENT: __________CSE___________
Sessional in - charge
39
create table student008(
roll_no number ,
name varchar2(20),
stream varchar2(20),
dgpa number,
insert:
employee08:
emp_id number,
employe_id number,
name varchar2(10),
doj date,
employer_name varchar2(10),
dob date,
dol date,
employee_designation varchar2(10),
40
insert employee08:
values (1,12,'rahul','10-feb-08','rakesh','20-mar-1990','11-jan-10','manager');
values (2,13,'ram','10-jan-06','radha','20-mar-1988','12-jan-09',’master');
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),
cursor:
DECLARE
CURSOR insert_cursor IS
v_employee_rec insert_cursor%ROWTYPE;
BEGIN
OPEN insert_cursor;
END LOOP;
CLOSE insert_cursor;
END;
43
ROLL NO: 14411313002 DEPARTMENT: __________CSE___________
Sessional in - charge
44
Create table:
CREATE TABLE weather (
city varchar(80),
temp_lo int,
temp_hi int,
prcp real,
date date
);
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)
46
(1 row)
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)
Aggregation Function:
48
postgres=# SELECT max(temp_lo) FROM weather;
max
-----
46
(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