




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课 程 名 称 数 据 库 技 术 实 验 名 称 存 储 过 程 和 触 发 器 的 使 用 实 验 成 绩 学 号 姓 名 班 级 日 期 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年【R1快开门式压力容器操作】考试题库及答案
- 2025年公路水运工程试验检测师公共基础试题库及答案(法规与技术标准)
- 2025年监理工程师之交通工程目标控制考试题库含答案a卷
- 桥梁招标课件
- 2025年品牌营销经理招聘面试模拟题与技巧解析
- 2026届安徽省亳州市第三十二中学高三化学第一学期期中联考模拟试题含解析
- 2025年电商运营岗位笔试模拟题及答案解析
- 2025护士资格证考试题库及答案
- 2025年大数据与人工智能课程考试试题及答案
- 2025年旅游管理与规划项目的考试试题及答案
- 小升初测试(试题)-2023-2024学年六年级下册数学苏教版
- 青少年抑郁症的预防和干预策略
- 考公行测言语理解与表达题库之选词填空公考公务员考试省考国考310题(含答案)
- 2021年度计算机审计初级网络培训测试题
- 康复科康复评定表
- 输液反应-完整版
- 【高质量】如何进行有效的校本研修PPT文档
- 水泥生产企业生产安全事故综合应急预案
- 食堂安全培训-课件
- 胆总管结石伴急性胆管炎
- 制度编写书写规范
评论
0/150
提交评论