'Exercise 1'
'The management of adventure works incorporation has decided that no user
should be able to change the prices of products. in addition, the management
wants that all the attempts to change the price should be saved in a
temporary table, Temp.John, the Database Developer, has been asked to make
significant changes in the datebas to implement this policy. what should john
do to achieve the same?'
create table Temp
(
ProductID int,
AttChangeCost money,
AttTime datetime)
PRess F5
create TRIGGER updTrigger ON
Production.ProductionCostHistory instead of UPDATE
as
BEGIN
DECLARE @PID AS int
DECLARE @COST AS money
select @pid = ProductID,@cost = STandarCost From
Deleted INSERT INTO Temp values(@pid, @cost, getdate())
Select 'Sorry you can not change the price of a product'
END
replay
UPDATE Production.ProductCostHistory
SET StandarCost = 55
where ProductID =707
press F5
'Exercise 2'
'The management of adventure works incorporation wants that whenever
the pay rate of and employee is modified, its effect on the monthly salary
of that employee should be displayed John, the database Developer at
Adventure works, has been asked to resolve this problem. Help John
to find an appropriate solution.'
create TRIGGER updTrigger
on HumanResources.EmployeePayHistory For UPDATE
AS
BEGIN
DECLARE @rate AS money
DECLARE @frq AS int
SELECT @rate = Rate,
@frq =PayFRequency From Inserted
Select @rate * @frq * 30 AS 'Monthly Salary'
END
Update HumanResources.EmployeePayHistory
SET Rate = Rate + 5 where EmployeeID =160
'Exercise 3'
'Create a tigger to ensure that the average of the values in the tate columna o f
employeepay hisory table should not be more than 20 when the value of the
rate is increased'
create TRIGGER updatetriggerEPayHistory
ON HumanResources.EmployeePayHistory
For UPDATE
AS
IF UPDATE (Rate)
BEGIN
DECLARE @AvgRate flat
Select @AvgRate = AVG(Rate)
From HumanResource.EmployeePayHistory
IF(@AvgRate > 20)
BEGIN
PRINT 'The averge value of rate cannot be more than 20'
ROLLBACk TRANSACTION
END
END
'Exercise 4'
'Create a tigger on the product table to ensure that if the value for
the sagety stock level column becomes less than or equal to the specified
recorder point column for a product because of an update operation, a
message information that the recorder level has been attained should
be displayed'
Create TRIGGER orderStock
ON Production.Product
For UPDATE
AS
DECLARE @PID varchar
Select @PID =ProductID from inserted
IF((Select safetyStockLeve from inserted)
<=(Select ReorderPoint from inserted))
BEGIN
PRINT ' The stock of '+@PID + 'is underr the reorder point'
END
'Exercise 5'
'Create a tigger named Emp_Update on the EmployeeDetail table.
This trigger should restrict a user from performing any kind of DMl
operating on the table before 9 A.M and after 5P.M.'
create TRIGGER Emp_Update
ON EmployeeDetails after INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @Time INT
SET @Time = DATENAME (HH,GETDATE())
IF @Time not between 9 and 17
Begin
ROLLBACK
Print
('The operation cannot be perrformed before 9 A.M and after 5P.M.')
END
END
'Exercise 6'
'The management of adventure works wnats that whenever the account number
of a user is modified, the old account number, as wll as the new account
number, should be instaneously displayed to the user. how can you accomplish the
desired taks? '
Create trigger vendoraccountnum
on purchasing.vendor
for update AS
Begin
declare @oldvalue nvarchar (15)
declare @newvalue nvarchar (15)
Select @oldvalue = AccountNumber from deleted
Select @newvalue = AccountNumber from inserted
Print 'the old account number is' +@oldvalue
Print 'The old account number is' +@newvalue
END
'Exercise 7'
'Create a trigger named Emp_Delete_Trg on the EmployeeDetails table. this
should restrict the deletion of records from the table if the designation
of an employee is Manager.'
create trigger Emp_Delete_Trg
On EmployeeDetails
After delete
as
begin declare @job varchar(50)
select * @job= designation from deleted
if @job ='Manager'
begin
rollback
Print ('Cannot delete manager from the table')
END
END
'Exercise 8'
'Create a tigger named Emp_Update_Try what will restrict the updating of
salary of an employee if the new salary is less than the previous salary in the employee Details table.'
Create trigger Emp_Upage_Trg
on EmployeeDetails AFTER UPDATE
AS
BEGIN
DECLARE @OldSal Money
DECLARE @NewSal Money
Select @OldSal = Salary from DELETED
Select @NewSal =Salary from INSERTED
if @OLDSAL > @NEWSAL
BEGIN
ROLLBACK
Print('New salary cannot be less than the old salary')
END
END
'Exercise 9'
'Create a triggernamed Emp_Nested_Trg on the EmployeeDetails table for
an insert operation this trigger should ensure that a new record beging
inserted into the EmployeeDetails table has a matching record in the
DepDetails tablee. Otherwise, the insert operation is rolled back.'
create Trigger Emp_Nested_Trg
On EmployeeeDetails AFTER Insert
As
BEGIN
DECLARE @DptNo INT
Select @DptNo = DepNo from INSERTED
IF not exists(Select * from DeptDetails
where DeptNo =@DptNo)
Print 'The specified DeptNo does not exist.'
ROLLBACK
END
'Exercise10'
'Consider the following statement:
Create viwe Emp_Dept
As
Select E.EmployeeID, E.EmpName, E.Designation,E.Salary, D.DeptNo,
D.DeptName from EmployeeDetails E
Inner join DeptDetails D On E.DeptNo = D.DeptNo
A view named Emp_Dept ha been created on the base tables, EmployeeDetails and
DePTDetails, by using the proceding statement. Joint is the databsa developer
with adventure works Incorporation . He want that the users should be able to
insert data in the undelying base tables by using the view. He can implementt
the desired functionality if the inserrt operation affects a single table.
Howevver, the same cannot be done if multiple tables are involved. He
wants to be able to inserrt date in both the tables with the help of
a single insert statement. Help john in rsolving the problem.'
Create Trigger view_Inserrt_Trg
on Emp_Dept
INSTEAD of INSERT
AS
Begin
INSERT INTO EmployeeDetails (EmployeeID, EmpName
Designation, Salary, DeptNo)
Select EmployeeID,EmpName,Degination, Salary
DeptNo from INSERTED
INSERT INTO DeptDEtails(DeptNo, DeptName)
Select DeptNo, DeptName from INSERTED
END
'Exercise 11'
'John is a Databas Developer. He is not able to delted the unwanted records
from the base tables by using the view, Emp_Dept. Help John in
resolving the problem..'
Create Trigger view_Delete_Trg
On Emp_Dept
INSTEAD of DELETE
AS
BEGIN
DECLARE @DeptNo int
DECLARE @Count int
Select @DeptNo = DeptNo from DELETED
SElect @Count = count(*) from EmployeeeDetails where deptNo =@DeptNo
DELETE From EmployeeDetails Where DeptNo =@DeptNo
if @Count = 1
DELETE From DeptDetails where DeptNo =@DeptNo
END
'The management of adventure works incorporation has decided that no user
should be able to change the prices of products. in addition, the management
wants that all the attempts to change the price should be saved in a
temporary table, Temp.John, the Database Developer, has been asked to make
significant changes in the datebas to implement this policy. what should john
do to achieve the same?'
create table Temp
(
ProductID int,
AttChangeCost money,
AttTime datetime)
PRess F5
create TRIGGER updTrigger ON
Production.ProductionCostHistory instead of UPDATE
as
BEGIN
DECLARE @PID AS int
DECLARE @COST AS money
select @pid = ProductID,@cost = STandarCost From
Deleted INSERT INTO Temp values(@pid, @cost, getdate())
Select 'Sorry you can not change the price of a product'
END
replay
UPDATE Production.ProductCostHistory
SET StandarCost = 55
where ProductID =707
press F5
'Exercise 2'
'The management of adventure works incorporation wants that whenever
the pay rate of and employee is modified, its effect on the monthly salary
of that employee should be displayed John, the database Developer at
Adventure works, has been asked to resolve this problem. Help John
to find an appropriate solution.'
create TRIGGER updTrigger
on HumanResources.EmployeePayHistory For UPDATE
AS
BEGIN
DECLARE @rate AS money
DECLARE @frq AS int
SELECT @rate = Rate,
@frq =PayFRequency From Inserted
Select @rate * @frq * 30 AS 'Monthly Salary'
END
Update HumanResources.EmployeePayHistory
SET Rate = Rate + 5 where EmployeeID =160
'Exercise 3'
'Create a tigger to ensure that the average of the values in the tate columna o f
employeepay hisory table should not be more than 20 when the value of the
rate is increased'
create TRIGGER updatetriggerEPayHistory
ON HumanResources.EmployeePayHistory
For UPDATE
AS
IF UPDATE (Rate)
BEGIN
DECLARE @AvgRate flat
Select @AvgRate = AVG(Rate)
From HumanResource.EmployeePayHistory
IF(@AvgRate > 20)
BEGIN
PRINT 'The averge value of rate cannot be more than 20'
ROLLBACk TRANSACTION
END
END
'Exercise 4'
'Create a tigger on the product table to ensure that if the value for
the sagety stock level column becomes less than or equal to the specified
recorder point column for a product because of an update operation, a
message information that the recorder level has been attained should
be displayed'
Create TRIGGER orderStock
ON Production.Product
For UPDATE
AS
DECLARE @PID varchar
Select @PID =ProductID from inserted
IF((Select safetyStockLeve from inserted)
<=(Select ReorderPoint from inserted))
BEGIN
PRINT ' The stock of '+@PID + 'is underr the reorder point'
END
'Exercise 5'
'Create a tigger named Emp_Update on the EmployeeDetail table.
This trigger should restrict a user from performing any kind of DMl
operating on the table before 9 A.M and after 5P.M.'
create TRIGGER Emp_Update
ON EmployeeDetails after INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @Time INT
SET @Time = DATENAME (HH,GETDATE())
IF @Time not between 9 and 17
Begin
ROLLBACK
('The operation cannot be perrformed before 9 A.M and after 5P.M.')
END
END
'Exercise 6'
'The management of adventure works wnats that whenever the account number
of a user is modified, the old account number, as wll as the new account
number, should be instaneously displayed to the user. how can you accomplish the
desired taks? '
Create trigger vendoraccountnum
on purchasing.vendor
for update AS
Begin
declare @oldvalue nvarchar (15)
declare @newvalue nvarchar (15)
Select @oldvalue = AccountNumber from deleted
Select @newvalue = AccountNumber from inserted
Print 'the old account number is' +@oldvalue
Print 'The old account number is' +@newvalue
END
'Exercise 7'
'Create a trigger named Emp_Delete_Trg on the EmployeeDetails table. this
should restrict the deletion of records from the table if the designation
of an employee is Manager.'
create trigger Emp_Delete_Trg
On EmployeeDetails
After delete
as
begin declare @job varchar(50)
select * @job= designation from deleted
if @job ='Manager'
begin
rollback
Print ('Cannot delete manager from the table')
END
END
'Exercise 8'
'Create a tigger named Emp_Update_Try what will restrict the updating of
salary of an employee if the new salary is less than the previous salary in the employee Details table.'
Create trigger Emp_Upage_Trg
on EmployeeDetails AFTER UPDATE
AS
BEGIN
DECLARE @OldSal Money
DECLARE @NewSal Money
Select @OldSal = Salary from DELETED
Select @NewSal =Salary from INSERTED
if @OLDSAL > @NEWSAL
BEGIN
ROLLBACK
Print('New salary cannot be less than the old salary')
END
END
'Exercise 9'
'Create a triggernamed Emp_Nested_Trg on the EmployeeDetails table for
an insert operation this trigger should ensure that a new record beging
inserted into the EmployeeDetails table has a matching record in the
DepDetails tablee. Otherwise, the insert operation is rolled back.'
create Trigger Emp_Nested_Trg
On EmployeeeDetails AFTER Insert
As
BEGIN
DECLARE @DptNo INT
Select @DptNo = DepNo from INSERTED
IF not exists(Select * from DeptDetails
where DeptNo =@DptNo)
Print 'The specified DeptNo does not exist.'
ROLLBACK
END
'Exercise10'
'Consider the following statement:
Create viwe Emp_Dept
As
Select E.EmployeeID, E.EmpName, E.Designation,E.Salary, D.DeptNo,
D.DeptName from EmployeeDetails E
Inner join DeptDetails D On E.DeptNo = D.DeptNo
A view named Emp_Dept ha been created on the base tables, EmployeeDetails and
DePTDetails, by using the proceding statement. Joint is the databsa developer
with adventure works Incorporation . He want that the users should be able to
insert data in the undelying base tables by using the view. He can implementt
the desired functionality if the inserrt operation affects a single table.
Howevver, the same cannot be done if multiple tables are involved. He
wants to be able to inserrt date in both the tables with the help of
a single insert statement. Help john in rsolving the problem.'
Create Trigger view_Inserrt_Trg
on Emp_Dept
INSTEAD of INSERT
AS
Begin
INSERT INTO EmployeeDetails (EmployeeID, EmpName
Designation, Salary, DeptNo)
Select EmployeeID,EmpName,Degination, Salary
DeptNo from INSERTED
INSERT INTO DeptDEtails(DeptNo, DeptName)
Select DeptNo, DeptName from INSERTED
END
'Exercise 11'
'John is a Databas Developer. He is not able to delted the unwanted records
from the base tables by using the view, Emp_Dept. Help John in
resolving the problem..'
Create Trigger view_Delete_Trg
On Emp_Dept
INSTEAD of DELETE
AS
BEGIN
DECLARE @DeptNo int
DECLARE @Count int
Select @DeptNo = DeptNo from DELETED
SElect @Count = count(*) from EmployeeeDetails where deptNo =@DeptNo
DELETE From EmployeeDetails Where DeptNo =@DeptNo
if @Count = 1
DELETE From DeptDetails where DeptNo =@DeptNo
END