数据库技术实验六
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数据库技术实验六
from Departments
where DepartmentName=@name)
if (@year>6)
update Employees
set DepartmentID=@DepartmentID
where EmployeeID=@EmployeeID
End
exec dbo.yuangong_infol'000000','经理办公室'
(2)创建存储过程,根据每个员工的学历将收入提高500元。
alter proc SA_IN@enu char(6)
as
begin
update Salary
set InCome=InCome+500
from Salary,Employees
where
Employees.EmployeeID=Salary.EmployeeID and Education=@enu
end
select InCome
from Salary,Employees
where
Salary.EmployeeID=Employees.EmployeeID and Education='本科'
go
exec dbo.sa_in'本科'
go
select InCome
from Salary,Employees
where
Salary.EmployeeID=Employees.EmployeeID and Education='本科'
select InCome
from Salary,Employees
where
Salary.EmployeeID=Employees.EmployeeID and Education='本科'
(3)创建存储过程,使用游标计算本科及以上学历的员工在总员工数中的
比例。
declare
@edu varchar(10),
@part_count int,
@all_count int;
declare mycursor cursor for
select distinct education,
COUNT(education)over(partition
by education)as part_count,
COUNT(education)over()as all_count
from Employees
open mycursor
fetch next from
mycursor into@edu,@part_count,@all_count
while@@FETCH_STATUS=0begin
print@edu+'占总人数比例:
'+convert(varchar(100),convert(numeric(38,2),@part_count/1.0/@all_count *100))+'%'
fetch next from mycursor
into@edu,@part_count,@all_count
end
close mycusor
deallocate mycursor
(4)使用命令方式修改及删除一个存储过程。
if exists(select workyear from Employees where workyear=3)
drop procedure workyear
2. 触发器
(1)对于YGGL数据库,表Employees的Employeeid列与表Salary的Employeeid列应满足参照完整性规则,请用触发器实现两个表间的参照完整性。
create trigger Salaryins0on Salary
for insert,update
as
where employeeid=(select employeeid from
deleted)
end
(2)当修改表Employees时,若将Employees表中员工的工作时间增加1年,则将收入增加500,若增加2年则增加1000,依次增加。若工作时间减
少则无变化。
create trigger em_workyear on Employees
after update
as
begin
declare@a int,@b int
set@a=(select workyear from inserted)
set@b=(select workyear from deleted)
if(@a>@b)
update Salary
set income=income+(@a-@b)*500
where enployeeid in(select EmployeesID from inserted)
end
update Employees
set workyear=12
where EmployeesID='000001'
(3)创建UPDATE触发器,当Salary表中InCome值增加500时,outCome
值则增加50。
create trigger sa_income on Salary
for update as begin
if((select income from inserted)-(select income from deleted)=500) update Salary set outcome=outcome+50
where enployeeid=(select enployeeid from inserted)
end select income,outcome
from Salary where enployeeid='000001'
(5)创建INSTEAD OF触发器,实现向不可更新视图插入数据。