SQL Server触发器

1.(此题为后续实验题准备基础表和数据)创建一张Total_Hours表,用来保存每个员工所有项目总的工作时间,包含员工SSN和总工作时间(totalHours)两列;然后将employee表的所有员工SSN和初始工作时间(0)插入到表Total_Hours中;最后用从works_on表统计的每个员工所有项目总的实际工作时间更新表Total_Hours。

SQL文本:

use Company
go 

create table Total_Hours
(
SSN varchar(50) primary key,
totalHours decimal(18,1) default(0)
)

insert into dbo.Total_Hours select ssn,0 from Employee

update Total_Hours set totalHours=
(select SUM(hours) from WORKS_ON where Total_Hours.SSN=WORKS_ON.ESSN)

select * from Total_Hours
select * from WORKS_ON

结果截图:

SQL Server触发器_第1张图片

2.使用INSERT触发器实现:①每向employee表插入一个员工时,自动向Total_Hours表插入这个员工的SSN并将其初始totalHours置为0;②每向works_on表插入一行数据时,自动更新Total_Hours表中该员工对应的totalHours

SQL文本:

use Company
go 

select * from Total_Hours
go

create trigger T_employ
on Employee for insert
as
begin
insert Total_Hours select SSN,0 from inserted
end
go

begin try
begin transaction
insert into Employee values('dong','d','he','201631062521','1997-02-24','四川','M',100000,'00123',5)
commit transaction
end try
begin catch
     rollback transaction
end catch
go

select * from Total_Hours
go

create trigger T_works
on Works_on for insert
as
begin
update Total_Hours set totalHours=
(select sum(hours) from Works_on where Total_Hours.SSN=WORKS_ON.ESSN) from inserted
end
go

begin try
begin transaction
insert into WORKS_ON values('201631062521','1','10.0')
commit transaction
end try
begin catch
     rollback transaction
end catch

select * from Total_Hours

结果截图:

SQL Server触发器_第2张图片

3.使用UPDATE触发器实现每当更新works_onHOURS数据时,自动更新Total_Hours表的totalHours。

SQL文本:

use Company
go 

select * from Total_Hours
go
create trigger T_Work
on WORKS_ON
for update
as
begin
declare @ssn varchar(20),@new_hours numeric(18, 1)
select @ssn=ESSN from deleted
select @new_hours=SUM(HOURS) from WORKS_ON where ESSN=@ssn
update Total_Hours
set totalHours=@new_hours
where SSN=@ssn
end
go

begin try
begin tran
update WORKS_ON set HOURS=30.0 where ESSN='201631062521'
commit tran
end try
begin catch
      rollback tran
end catch
select * from Total_Hours

结果截图:

SQL Server触发器_第3张图片

4.使用UPDATE触发器实现:当且仅当修改EMPLOYEE表的SALARY数据时,如果修改后的SALARY小于等于修改以前的SALARY,则不允许修改,并提示“修改后工资小于等于修改前工资,修改失败!”,否则提示“修改成功!”。

SQL文本:

use Company
go 

create trigger T_Salary
on Employee
for update
as
begin
declare @old_Salary int,@new_Salary int,@ssn varchar(20)
select @new_Salary=SALARY,@ssn=SSN from inserted
select @old_Salary=SALARY from deleted
if update(SALARY) begin
   if(@new_Salary>@old_Salary) begin
       print '修改成功!'
   end else begin
       print '修改后工资小于等于修改前工资,修改失败'
       update Employee
       set SALARY=@old_Salary
       where SSN=@ssn
   end
end
end

update Employee set SALARY=20000 where SSN='123456789'
update Employee set SALARY=50000 where SSN='123456789'
select * from Employee

结果截图:

修改之前,John的salary为40000

SQL Server触发器_第4张图片

将salary修改为20000时,提示修改出错。

SQL Server触发器_第5张图片

将salary修改为50000时,修改成功。

SQL Server触发器_第6张图片

SQL Server触发器_第7张图片

5.使用DELETE触发器实现每当删除employee表一个员工时,如果这个员工是普通员工,则同时级联删除Total_Hours表、works_on表、Dependent表中与这个员工相关的所有数据;如果这个员工是部门经理,则不允许删除,并提示“Manager can not be deleted!”。

SQL文本:

use Company
go 

create trigger T_DelEmploy
on Employee
for delete
as
begin
declare @ssn varchar(20),@count int
select @ssn=SSN from deleted
select @count=count(*) from DEPARTMENT where MGRSSN=@ssn

if(@count!=0) begin
   print 'Manager can not be deleted!'
   insert into Employee select * from deleted
end else begin
   delete Total_Hours where SSN=@ssn
   delete works_on where ESSN=@ssn
   delete Dependent where ESSN=@ssn
end
end

delete Employee where SSN='201631062521'
delete Employee where SSN='888665555'
select * from Employee
select * from Total_Hours
select * from Dependent

结果截图:

当删除经理888665555,出现提示。

SQL Server触发器_第8张图片

当删除普通员工201631062521,删除成功。

SQL Server触发器_第9张图片

你可能感兴趣的:(SQL,Server)