数据库技术实验六_第1页
数据库技术实验六_第2页
数据库技术实验六_第3页
数据库技术实验六_第4页
数据库技术实验六_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

课 程 名 称 数 据 库 技 术 实 验 名 称 存 储 过 程 和 触 发 器 的 使 用 实 验 成 绩 学 号 姓 名 班 级 日 期 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 (year6) 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 Salaryins0 on Salary for insert,update as begin if(select employeeid from inserted) not in(select EmployeeID from Employees) rollback end create trigger Employeesupdate0 on dbo.Employees for update as begin update Salary set employeeid=(select employeeid from inserted) where employeeid=(select employeeid from deleted) end create trigger Employeesdelete0 on Employees for delete as begin delete from Salary 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(ab) 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 触 发 器 , 实 现 向 不 可 更 新 视 图 插 入 数 据 。 create view a_view as select Employees.EmployeesID,name,workyear,income,outcome from Employees,Salary where Employees.EmployeesID=Salary.enployeeid go create trigger gxst on a_view instead of insert as begin declare Ei char(6),name char(10),wy tinyint,ic float,oc float select Ei=EmployeesID,name=name,wy=workyear,ic=income,oc=outcome from inserted insert into Employees(EmployeesID,name,workyear) values(Ei,name,wy) insert into Salary values(Ei,ic,oc) end insert into a_view values(000011,小芳, 3,2000,1500) select * from a_view where EmployeesID=000011 ( 5) 创 建 DDL 触 发 器 , 当 删 除 数 据 库 时 , 提 示 “无 法 删 除 ”并 回 滚 删 除 操 作 。 create trigger table_delete on database after drop_table as print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论