Lab 8 Solutions
Lab 8 Solutions
Lab 8 Solutions
Q.1 ) Create a trigger on the table employees, which after an update or insert, converts all the
values of first and last names of the updated or inserted rows to upper case. (Hint: Use cursors
to retrieve the values of each row and modify them.)
Solution:
OPEN employee_cursor;
fetch next from employee_cursor into @fname,@lname;
go
2) Create a trigger that restores the values before an update operation on the employees table
if the salary exceeds 100000. (Hint: Use the inserted and deleted tables to look at the old and
new values in the table respectively.)
Solution :
3) Create a trigger to insert into the view alex such that the underlying base tables are populated
correctly. Handle cases where the input is incorrect and rollback if a wrong input is asked to be
inserted. Assume that you cannot create a new department by an insert query.
4) Create a trigger to delete from the view alex such that the corresponding rows in the
underlying base tables are removed correctly. Handle cases where the rows requested to be
deleted are not possible and rollback accordingly. Assume that a department cannot be left
without a manager.
-- fetch next
fetch next from delete_cursor into
@id,@first_name,@last_name,@salary,@code;
end
go
5) Create a trigger to modify from the view alex such that the corresponding rows in the
underlying base tables are changed correctly. Handle cases where the modifications asked for are
not possible and rollback accordingly. Assume that a department cannot be left without a
manager.
-- check if the primary keys are same for any two rows
-- check if primary key does not already exist
if( (@int_1 != @int_2) or (@int_3 > 0) ) begin
rollback transaction check_1;
end
else begin
declare @insert_id numeric(9);
declare @insert_code char(5);
declare @insert_first_name varchar(10), @insert_last_name
varchar(20);
declare @insert_salary numeric(9,2);
end
end
Exercise 2
1. Create a unique composite non-clustered index on the first_name and order in the
descending order.
2. Create a composite clustered index on the first_name and the last_name and order both
in the increasing order.
3. Create a unique Clustered index on the age and order in the increasing order
>> drop index student_info.IX_student_info2 create UNIQUE
clustered index IX_student_info3 on student_info(age asc);
4. Create composite non-clustered index on the id,age both in the increasing order
5. Create a non-clustered unique function based index on the upper(first_name + ' ' +
lastname)