数据库技术实验六
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
createtriggerem_workyearonEmployees
afterupdate
as
begin
declare@aint,@bint
set@a=(selectworkyearfrominserted)
set@b=(selectworkyearfromdeleted)
if(@a>@b)
updateSalary
End
execdbo.yuangong_infol'000000','经理办公室'
(2)创建存储过程,根据每个员工的学历将收入提高500元。
alterprocSA_IN@enuchar(6)
Biblioteka Baiduas
begin
updateSalary
setInCome=InCome+500
fromSalary,Employees
课程名称
数据库技术
实验
成绩
实验名称
存储过程和触发器的使用
学号
姓名
班级
日期
14.11.25
实验目的:
1.掌握存储过程的使用方法;
2.掌握触发器的实现方法;
实验平台:
利用RDBMS(SQL Server 2008)及其交互查询工具(查询分析器)来操作T-SQL语言;
实验内容:
1.存储过程
(1)创建存储过程,要求当一个员工的工作年份大于6年时将其转到经理办公室工作。
forupdate
as
begin
updateSalary
setemployeeid=(selectemployeeidfrominserted)
whereemployeeid=(selectemployeeidfromdeleted)
end
createtriggerEmployeesdelete0onEmployees
byeducation)aspart_count,
COUNT(education)over()asall_count
fromEmployees
openmycursor
fetchnextfrom
mycursorinto@edu,@part_count,@all_count
while@@FETCH_STATUS=0begin
whereEmployees.EmployeeID=Salary.EmployeeIDandEducation=@enu
end
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
createtriggerSalaryins0onSalary
forinsert,update
as
begin
if(selectemployeeidfrominserted)notin(selectEmployeeIDfromEmployees)
rollback
end
createtriggerEmployeesupdate0ondbo.Employees
(3)创建存储过程,使用游标计算本科及以上学历的员工在总员工数中的比例。
declare
@eduvarchar(10),
@part_countint,
@all_countint;
declaremycursorcursorfor
selectdistincteducation,
COUNT(education)over(partition
set@DepartmentID=(
selectDepartmentID
fromDepartments
whereDepartmentName=@name)
if(@year>6)
updateEmployees
setDepartmentID=@DepartmentID
whereEmployeeID=@EmployeeID
setincome=income+(@a-@b)*500
whereenployeeidin(selectEmployeesIDfrominserted)
end
updateEmployees
setworkyear=12
whereEmployeesID='000001'
fordelete
as
begin
deletefromSalary
whereemployeeid=(selectemployeeidfromdeleted)
end
(2)当修改表Employees时,若将Employees表中员工的工作时间增加1年,则将收入增加500,若增加2年则增加1000,依次增加。若工作时间减少则无变化。
end
closemycusor
deallocatemycursor
(4)使用命令方式修改及删除一个存储过程。
ifexists(selectworkyearfromEmployeeswhereworkyear=3)
dropprocedureworkyear
2. 触发器
(1) 对于YGGL数据库,表Employees的Employeeid列与表Salary的Employeeid列应满足参照完整性规则,请用触发器实现两个表间的参照完整性。
print@edu+'占总人数比例:'+convert(varchar(100),convert(numeric(38,2),@part_count/1.0/@all_count*100))+'%'
fetchnextfrommycursor
into@edu,@part_count,@all_count
go
execdbo.sa_in'本科'
go
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
alterprocedureyuangong_infol@EmployeeIDchar(6),@namenchar(20)
as
begin
declare@yearint
set@year=(
selectWorkYear
fromEmployees
whereEmployeeID=@EmployeeID)
declare@DepartmentIDchar(3)
afterupdate
as
begin
declare@aint,@bint
set@a=(selectworkyearfrominserted)
set@b=(selectworkyearfromdeleted)
if(@a>@b)
updateSalary
End
execdbo.yuangong_infol'000000','经理办公室'
(2)创建存储过程,根据每个员工的学历将收入提高500元。
alterprocSA_IN@enuchar(6)
Biblioteka Baiduas
begin
updateSalary
setInCome=InCome+500
fromSalary,Employees
课程名称
数据库技术
实验
成绩
实验名称
存储过程和触发器的使用
学号
姓名
班级
日期
14.11.25
实验目的:
1.掌握存储过程的使用方法;
2.掌握触发器的实现方法;
实验平台:
利用RDBMS(SQL Server 2008)及其交互查询工具(查询分析器)来操作T-SQL语言;
实验内容:
1.存储过程
(1)创建存储过程,要求当一个员工的工作年份大于6年时将其转到经理办公室工作。
forupdate
as
begin
updateSalary
setemployeeid=(selectemployeeidfrominserted)
whereemployeeid=(selectemployeeidfromdeleted)
end
createtriggerEmployeesdelete0onEmployees
byeducation)aspart_count,
COUNT(education)over()asall_count
fromEmployees
openmycursor
fetchnextfrom
mycursorinto@edu,@part_count,@all_count
while@@FETCH_STATUS=0begin
whereEmployees.EmployeeID=Salary.EmployeeIDandEducation=@enu
end
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
createtriggerSalaryins0onSalary
forinsert,update
as
begin
if(selectemployeeidfrominserted)notin(selectEmployeeIDfromEmployees)
rollback
end
createtriggerEmployeesupdate0ondbo.Employees
(3)创建存储过程,使用游标计算本科及以上学历的员工在总员工数中的比例。
declare
@eduvarchar(10),
@part_countint,
@all_countint;
declaremycursorcursorfor
selectdistincteducation,
COUNT(education)over(partition
set@DepartmentID=(
selectDepartmentID
fromDepartments
whereDepartmentName=@name)
if(@year>6)
updateEmployees
setDepartmentID=@DepartmentID
whereEmployeeID=@EmployeeID
setincome=income+(@a-@b)*500
whereenployeeidin(selectEmployeesIDfrominserted)
end
updateEmployees
setworkyear=12
whereEmployeesID='000001'
fordelete
as
begin
deletefromSalary
whereemployeeid=(selectemployeeidfromdeleted)
end
(2)当修改表Employees时,若将Employees表中员工的工作时间增加1年,则将收入增加500,若增加2年则增加1000,依次增加。若工作时间减少则无变化。
end
closemycusor
deallocatemycursor
(4)使用命令方式修改及删除一个存储过程。
ifexists(selectworkyearfromEmployeeswhereworkyear=3)
dropprocedureworkyear
2. 触发器
(1) 对于YGGL数据库,表Employees的Employeeid列与表Salary的Employeeid列应满足参照完整性规则,请用触发器实现两个表间的参照完整性。
print@edu+'占总人数比例:'+convert(varchar(100),convert(numeric(38,2),@part_count/1.0/@all_count*100))+'%'
fetchnextfrommycursor
into@edu,@part_count,@all_count
go
execdbo.sa_in'本科'
go
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
selectInCome
fromSalary,Employees
whereSalary.EmployeeID=Employees.EmployeeIDandEducation='本科'
alterprocedureyuangong_infol@EmployeeIDchar(6),@namenchar(20)
as
begin
declare@yearint
set@year=(
selectWorkYear
fromEmployees
whereEmployeeID=@EmployeeID)
declare@DepartmentIDchar(3)