Sei sulla pagina 1di 5

Department of Computer Science and Engineering

Semester: Summer 2015


Lab 10
Course Title: Database System
Course Code: CSE 301
Section: 01

Submitted To:
Shaila Sharmeen
Assistant Professor
Department of CSE

Submitted by:
Kazi Saymatul Jannat
ID: 2013-2-60-018

Date of submission: 13.12.2015

Introduction:

For Dept:
create table Deptt
(
dept_id int not null,
dept_name varchar(20),
primary key (dept_id),
)
insert into Deptt values (60,'CSE')
insert into Deptt values (10,'BBA')
insert into Deptt values (80,'EEE')
insert into Deptt values (40,'ETE')
insert into Deptt values (50,'ECE')
select *from Deptt

For instructor:
create table Instructor
(
idint not null,
ins_name varchar(20),
salary int,
primary key(id),
)
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert
insert

into
into
into
into
into
into
into
into
into
into
into

Instructor
Instructor
Instructor
Instructor
Instructor
Instructor
Instructor
Instructor
Instructor
Instructor
Instructor

values
values
values
values
values
values
values
values
values
values
values

(101,'ABC',44000)
(102,'XYZ',66000)
(103,'ASD',101871)
(201,'EFG',40000)
(202,'MNO',60000)
(301,'HIJ',88000)
(302,'LMN',44000)
(401,'PQR',60000)
(402,'FGH',50000)
(501,'GHL',40000)
(502,'XCV',60000)

select *from Instructor

Question 1: Update the instructor salary 10% of a specific dept.


Answer:
create procedure update_salary2 @deptname1 varchar(50)
as
begin
update Instructor set salary=salary*1.10
where id=(select dept_id from dept where dept_name=dept_name)
end
Output:

Question 2: Write a function to get the department by its id.


Answer:
create function getName1(@id int)
returnsvarchar(20)
as
begin
declare @dept_namevarchar(20)
select @dept_name = dept_name from Dept where dept_id=@id
return @dept_name
end
select dbo.getName1(60)

Output:

Question 3: Write a function where instructor id is given ,which


return salary.
Answer:
create function getSalary1(@ins_idint)
returnsint
as
begin
declare @salary1 int
select @salary1 =(select salary from Instructor where @ins_id= id)
return @salary1
end
select dbo.getsalary1(103)
Output:

Question 4: If salary>=50000 print His salary is high else print


His salary is low.
Answer:
declare @salary int
select @salary= dbo.getSalary1(103)
if @salary>=50000
print 'His salary is high'
else
print 'His salary is low'
Output:

Question 5: If salary>50000 then 5% increment else 10% increment.

Answer:
declare @salary int
select @salary =dbo.getSalary1(103)
if @salary >=50000
begin
update Instructor set salary= @salary *1.05 where id =103
end
else
begin
update Instructor set salary =@salary*1.1 where id=103
end
Output:

Question 6: Write a function to return the total salary of two


instructor by their id.
Answer:
create function calcSalary(@id1 int, @id2 int)
returns int
as
begin
declare @salary1 int
select @salary1 =(T.salary + S.salary)
from instructor as T, instructor as S where T.id = @id1 and S.id= @id2
return @salary1
end
select dbo.calcSalary(401,402)
Output:

Discussion:

Potrebbero piacerti anche