0% found this document useful (0 votes)
17 views8 pages

Dbmsexp7

The document describes experiments using SQL queries and subqueries. The first experiment involves creating an employee table with columns for employee number, name, salary, etc. and inserting sample records. A trigger is then created to display the salary difference when a salary is updated. The second experiment involves creating a grades table with columns for student number, marks from 3 tests, and average mark. Triggers are created to automatically calculate the average mark on insert or update and restrict duplicate student numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

Dbmsexp7

The document describes experiments using SQL queries and subqueries. The first experiment involves creating an employee table with columns for employee number, name, salary, etc. and inserting sample records. A trigger is then created to display the salary difference when a salary is updated. The second experiment involves creating a grades table with columns for student number, marks from 3 tests, and average mark. Triggers are created to automatically calculate the average mark on insert or update and restrict duplicate student numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Academic Year 2022-23 SAP ID:60003210190

DEPARTMENT OF INFORMATION TECHNOLOGY

COURSE CODE: DJ19ITL303 DATE: 30/11/2022

COURSE NAME: Database Management System Laboratory CLASS:B/B2

EXPERIMENT NO. 7

LO: Write SQL queries using Subqueries.

AIM / OBJECTIVE: To study and implement Subqueries.

IMPLEMENTATION / COMMANDS:

1. 1). Write a Trigger for employee table which will display the salary difference.

PROCEDURE
(i) Create the employee table having columns empno, fname, lname, salary, address
(ii) Insert values
(iii)Write the after update trigger
(iv)Update the salary in employee table (v)
Display the difference in new and old salary
ANS:
(i) create table Employee
(
empno varchar(15),
fname varchar(15),
lname varchar(15),
salary integer,
Academic Year 2022-23 SAP ID:60003210190

address varchar(15)
);

desc Employee;

ii) insert into Employee(empno, fname, lname, salary, address)


values('21','Sarthak','Mahale',1347798, 'Thane');

insert into Employee(empno, fname, lname, salary, address) values('24','Anil','Prab',100000,


'Kandivali');

insert into Employee(empno, fname, lname, salary, address) values('76','James','Vince',900000,


'Borivali');
Academic Year 2022-23 SAP ID:60003210190

insert into Employee(empno, fname, lname, salary, address) values('26','Meet','Shah',800000,


'Dahisar');

insert into Employee(empno, fname, lname, salary, address) values('54','Rohit','Sharma',700000,


'Dombivali');

select * from Employee;

create or replace trigger display_salary_change


after update of salary on Employee for each
row when(new.empno>0)
Academic Year 2022-23 SAP ID:60003210190

declare
sal_dif integer;
begin sal_dif:= :new.salary -:old.salary;
dbms_output.put_line('old salary:'||:old.salary);
dbms_output.put_line('new salary:'||:new.salary);
dbms_output.put_line('salary Difference:'||sal_dif);
end;

update Employee
set salary = salary+20000
where fname ='Sarthak'
Academic Year 2022-23 SAP ID:60003210190

2. Create table GRADES with 5 columns:


• SENo Number (Student's Enrollment Number)
• M1 Number (Mark from test 1 )
• M2 Number (Mark from test 2)
• M3 Number (Mark from test 3)
• Avg_M Number (average mark from test 1, 2 and 3)
.

create table grades


(SeNo integer,
M1 integer,
M2 integer,
M3 integer,
Avg_marks integer
);

desc grades;
a) Create a trigger GRADES_TRG that calculates the value of the Avg_M column.
a) Create a trigger GRADES_TRG that calculates the value of the Avg_M column.
a) Create a trigger GRADES_TRG that calculates the value of the Avg_M column.
Academic Year 2022-23 SAP ID:60003210190

a) Create a trigger GRADES_TRG that calculates the value of the Avg_M column.

create or replace trigger grades_jpg


before insert or update on grades
for each row begin
:new.Avg_marks:=(:new.M1+:new.M2+:new.M3)/3;
end;

insert into grades(SeNo,M1,M2,M3) values(23,77,88,99);


Academic Year 2022-23 SAP ID:60003210190

select * from grades;

b) Create a trigger on table GRADES such that it restricts the entry of duplicate SENo.

create or replace trigger duplicate_jpg


before insert on grades for each row
declare counter integer;
begin select * into counter from
(select count(rownum)from grades x
where x.SeNo =:new.SeNo);
if counter =1 then
dbms_output.put_line('SeNo Already exist');
end if;
end;
Academic Year 2022-23 SAP ID:60003210190

You might also like