数据库技术实验六
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
课程名称数据库技术实验
成绩
实验名称存储过程和触发器的使用
学号姓名班级日期14.11.25 实验目的:
1.掌握存储过程的使用方法;
2.掌握触发器的实现方法;
实验平台:
利用RDBMS(SQL Server 2008)及其交互查询工具(查询分析器)来操作T-SQL语言;
实验内容:
1.存储过程
(1)创建存储过程,要求当一个员工的工作年份大于6年时将其转到经理办公室工作。
alter procedure yuangong_infol@EmployeeID char(6),@name nchar(20)
as
begin
declare@year int
set@year=(
select WorkYear
from Employees
where EmployeeID=@EmployeeID)
declare@DepartmentID char(3)
set@DepartmentID=(
select DepartmentID
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
begin
if(select employeeid from inserted)not in(select EmployeeID from Employees)
rollback
end
create trigger Employeesupdate0on dbo.Employees
for update
as
begin
update Salary
set employeeid=(select employeeid from inserted)
where employeeid=(select employeeid from deleted)
end
create trigger Employeesdelete0on Employees