



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、課程名稱數據庫技術實驗實驗名稱存儲過程和觸發器的使用成績學號姓名班級日期14.11.25實驗目的:1. 掌握存儲過程的使用方法;2. 掌握觸發器的實現方法;實驗平臺:利用RDBMS( SQL Server 2008)及其交互查詢工具(查詢分析器)來操作T-SQL 語言;實驗內容:1. 存儲過程( 1) 創建存儲過程,要求當一個員工的工作年份大于6 年時將其轉到經理辦公室工作。alter procedure yuangong_infol EmployeeID char(6),name nchar(20)asbegindeclare year intset year=(select WorkYea
2、rfrom Employeeswhere EmployeeID =EmployeeID )declare DepartmentID char(3)set DepartmentID =(select DepartmentIDfrom Departmentswhere DepartmentName=name)if (year>6)update Employeesset DepartmentID =DepartmentIDwhere EmployeeID =EmployeeIDEndexec dbo.yuangong_infol '000000','經理辦公室 '
3、;( 2) 創建存儲過程,根據每個員工的學歷將收入提高500 元。alter proc SA_IN enu char( 6)asbeginupdate Salaryset InCome =InCome+500from Salary,Employeeswhere Employees.EmployeeID =Salary.EmployeeID and Education=enu endselect InComefrom Salary,Employeeswhere Salary.EmployeeID =Employees.EmployeeID and Education='本科 '
4、goexec dbo.sa_in '本科 'goselect InComefrom Salary,Employeeswhere Salary.EmployeeID =Employees.EmployeeID and Education='本科 ' select InComefrom Salary,Employeeswhere Salary.EmployeeID =Employees.EmployeeID and Education='本科 '( 3) 創建存儲過程,使用游標計算本科及以上學歷的員工在總員工數中的比例。declareeduvarch
5、ar( 10),part_countint,all_countint;declaremycursorcursorforselectdistincteducation,COUNT( education)over( partitionby education)aspart_count,COUNT( education)over()as all_countfromEmployeesopenmycursorfetchnextfrommycursorintoedu, part_count, all_countwhileFETCH_STATUS= 0 beginprintedu+ ' 占總人數比例
6、:' + convert( varchar( 100 ), convert( numeric ( 38 , 2 ), part_count/ 1.0 / all_count* 100 )+ '%'fetchnextfrommycursorintoedu, part_count, all_countendclosemycusordeallocatemycursor( 4) 使用命令方式修改及刪除一個存儲過程。ifexists(selectworkyearfromEmployeeswhereworkyear= 3 )dropprocedureworkyear2. 觸發器(
7、1) 對于 YGGL數據庫,表 Employees 的 Employeeid 列與表 Salary 的Employeeid列應滿足參照完整性規則,請用觸發器實現兩個表間的參照完整性。create trigger Salaryins0 on Salaryfor insert,updateasbeginif (select employeeid from inserted) not in( select EmployeeID from Employees)rollbackendcreate trigger Employeesupdate0 on dbo.Employeesfor updateasb
8、eginupdate Salaryset employeeid=( select employeeid from inserted)where employeeid =(select employeeid from deleted)endcreate trigger Employeesdelete0 on Employeesfor deleteasbegindelete from Salarywhere employeeid =(select employeeid from deleted)end( 2)當修改表Employees 時,若將Employees 表中員工的工作時間增加1年,則將收
9、入增加500 ,若增加2 年則增加1000 ,依次增加。若工作時間減少則無變化。createtriggerem_workyearon Employeesafterupdateasbegindeclarea int , b intseta=( selectworkyearfrominserted)setb=( selectworkyearfromdeleted)if ( a>b)updateSalarysetincome= income+( a- b)* 500whereenployeeidin(selectEmployeesIDfrom inserted)endupdateEmploye
10、essetworkyear= 12whereEmployeesID= '000001'( 3)創建 UPDATE觸發器,當Salary表中 InCome 值增加 500時, outCome值則增加 50 。createtriggersa_incomeonSalaryforupdate as beginif (selectincomefrominserted)-(selectincomefromdeleted)= 500 )updateSalarysetoutcome = outcome+ 50whereenployeeid=( selectenployeeidfrominser
11、ted)end selectincome, outcomefromSalarywhereenployeeid= '000001'( 5) 創建 INSTEAD OF 觸發器,實現向不可更新視圖插入數據。createviewa_viewasselectEmployees. EmployeesID , name , workyear , income, outcomefromEmployees , SalarywhereEmployees. EmployeesID= Salary. enployeeidgocreatetriggergxston a_viewinsteadof in
12、sertasbegindeclareEichar( 6),name char ( 10 ),wy tinyint, ic float, oc floatselectEi = EmployeesID, name=name , wy=workyear, ic = income, oc= outcomefrominsertedinsertintoEmployees( EmployeesID , name , workyear)values ( Ei , name, wy)insertintoSalaryvalues( Ei , ic , oc)endinsertintoa_viewvalues( '000011','小芳',3,2000 ,1500)select* froma_viewwhereEmployeesID= '000011'( 5)創建DDL 觸發器,當刪除數據庫時,提示“無法刪除”并回滾刪除操作。crea
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030中國環境美化及園藝服務行業產業運行態勢及投資規劃深度研究報告
- 服務貿易統計培訓課件
- 學生學習風格與個性化教學策略
- 教師職業發展與教學質量的提升策略研究
- 退換貨培訓課件
- 教育數字化轉型中的技術革新與挑戰
- 智慧城市公共服務平臺的數據管理與分析
- 鋼結構焊接培訓課件
- 教育大數據在教學管理中的創新應用
- 抖音商戶短視頻播放量分析反饋制度
- 【汽車銷售服務有限公司銷售量問題探究10000字(論文)】
- 散熱器安裝施工方案與技術措施
- 義務教育語文課程標準(2022)測試題帶答案(20套)
- DB14-T 2611-2022雁門關農牧交錯帶 肉羊顆粒飼料調制加工技術規程
- GB/T 27818-2011化學品皮膚吸收體外試驗方法
- 官員任期、財政資源與數字時代地方政府組織聲譽建構
- 無塵室管理規范(ppt)
- 2021年云南技師學院教師招聘試題及答案解析
- 電氣工程CAD教程PPT課件
- 暑假初二升初三數學銜接班精品教材
- 風力發電機組主傳動鏈滾動軸承運行狀態評估結果和措施、定期維護項目及要求、基于評估結果備件計劃
評論
0/150
提交評論