新SQL——SERVER實驗練習答案.pdf_第1頁
新SQL——SERVER實驗練習答案.pdf_第2頁
新SQL——SERVER實驗練習答案.pdf_第3頁
新SQL——SERVER實驗練習答案.pdf_第4頁
新SQL——SERVER實驗練習答案.pdf_第5頁
已閱讀5頁,還剩40頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

SQL-Server實驗 答案 上海師范大學 計算機系 目 錄 第一部分 企業(yè)管理器的使用 3 試驗一 注冊服務器 3 試驗二 創(chuàng)建數(shù)據(jù)庫 3 試驗三 創(chuàng)建表 4 實驗四 數(shù)據(jù)輸入 5 實驗五 登錄到數(shù)據(jù)庫服務器 6 第二部分 SQL語言 7 第二部分 SQL語言 7 試驗一 數(shù)據(jù)庫創(chuàng)建 7 試驗二 創(chuàng)建表 7 試驗三 創(chuàng)建數(shù)據(jù)完整性 8 試驗四 數(shù)據(jù)完整性試驗 9 試驗五 索引 12 試驗六 更新數(shù)據(jù) 13 試驗七 Sql 查詢語句 13 試驗八 視圖 15 試驗九 安全性控制實驗 15 試驗十 存儲過程 16 試驗十二 觸發(fā)器 17 試驗十二 恢復技術 19 試驗十三 事務 20 試驗十四 鎖 21 第一部分 企業(yè)管理器的使用 第二部分 SQL語言 試驗一 數(shù)據(jù)庫創(chuàng)建 目的:1掌握利用SQL語言進行數(shù)據(jù)庫的創(chuàng)建、維護。 2 sp_helpdb 命令 要求:1 創(chuàng)建數(shù)據(jù)庫 2 修改數(shù)據(jù)庫 3 刪除數(shù)據(jù)庫 一 建立school 數(shù)據(jù)庫 1 使用查詢分析器創(chuàng)建數(shù)據(jù)庫 school Create DataBase school 2 使用 SP_helpdb 查詢數(shù)據(jù)庫 School 的信息 3 使用SQL-Server 的企業(yè)管理器查看數(shù)據(jù)庫 school 的信息。 4 記錄: 1)school 數(shù)據(jù)庫文件所在的文件夾。 2)school 數(shù)據(jù)庫的文件名 二 刪除School數(shù)據(jù)庫 1 使用查詢分析器刪除數(shù)據(jù)庫 school DROP DATABASE school 2 使用SQL-Server 的企業(yè)管理器刪除數(shù)據(jù)庫 school 。 三 create Database 深入研究 1 建立school數(shù)據(jù)庫,要求數(shù)據(jù)庫存儲在c:data文件夾下,初始大小 為5MB ,增量為 1MB。 CREATE DATABASE school ON( Name = school_dat, Filename = c:sqldataschool.mdf, SIZE = 5, FILEGROWTH = 1 ) 2使用SQL-Server 的企業(yè)管理器,將數(shù)據(jù)庫的每次增量改為20%。 試驗二 創(chuàng)建表 目的:1 掌握利用SQL語言創(chuàng)建表的方法。 2 sp_help 命令 要求:1 創(chuàng)建表 2 修改表結構 3 刪除表 一 寫出使用 Create Table 語句創(chuàng)建表 student , sc,course 的SQL語句。 學生表、課程表、選課表屬于數(shù)據(jù)庫 School ,其各自得數(shù)據(jù)結構如 下: 學生 Student (Sno,Sname,Ssex,Sage,Sdept) 序 號 列名含義數(shù)據(jù)類型長度 1Sno學號字符型(char)6 2Sname姓名字符型 (varchar) 8 3Ssex性別字符型(char)2 4Sage年齡整數(shù) (smallint) 5sdept系科字符型 (varchar) 15 課程表 course(Cno,Cname,Cpno,Ccredit) 序 號 列名含義數(shù)據(jù)類型長度 1Cno課程 號 字符型(char)4 2cname課程 名 字符型 (varchar) 20 3Cpno先修 課 字符型(char)4 4Ccredit學分短整數(shù) (tinyint) 學生選課 SC(Sno,Cno,Grade) 序 號 列名含義數(shù)據(jù)類型長度 1Sno學號字符型(char)4 2Cno課程 名 字符型(char)6 3Grade成績小數(shù) (decimal) 12,1 二 把創(chuàng)建表的sql 語句的腳本存儲到文件 school.sql 。 create table Student ( Sno char(6) , Sname char(10) , Ssex char(2) , Sage smallint , Sdept char(10) , ) create table course( Cno char(4) , Cname char(16) , Cpno char(4) , Ccredit int, ) create table SC( Sno char(6), Cno char(4) , Grade int ) 三 使用 SP_HELP 查看表 student 的表結構 利用企業(yè)管理器查看表 sc 的表結構 四 利用 sql 語句表結構修改 1 在student 表中添加列: 家庭地址 address 長度為 60 varchar 型 入學日期 inDate 日期型 ALTER TABLE student ADD address varchar(60) ALTER TABLE student ADD inDate datetime 完成后用sp_help 查看是否成功。 2 將家庭地址 address 長度為 50 ALTER TABLE student ALTER COLUMN varchar(50) 完成后用sp_help 查看是否成功。 3 刪除 student 表的 inDate 列 ALTER TABLE student DROP COLUMN inDate 五 刪除表 1 刪除表 sc 2 刪除表 student 3 刪除表 course 試驗三 創(chuàng)建數(shù)據(jù)完整性 目的:1掌握創(chuàng)建數(shù)據(jù)完整性約束的命令。 2 掌握完整性約束的修改、刪除。 要求:1 能建立完整性約束 2 修改完整性約束 3 刪除完整性約束 一 寫出帶有完整性約束的 Create Table 命令建立表 student、course、sc 。要求: 1 Student表的主碼:sno student 的約束: 姓名不可為空,且唯一 性別 不能為空且取值范圍為男,女 年齡大于16歲 sdept 默認為 JSJ 系 2Course表的主碼:cno course 的約束: Ccredit 取值范圍 0 ,1,2,3,4,5 課程表的每一行的 Cno 與 cpno 不可相同 3 Sc表的主碼:sno,cno 。主碼名為 PK_SC Sc的外碼: 外碼:SC 表的sno 參照表 student 的 sno 外碼:sc 表的Cno 參照表 course 的 cno 4 把上述創(chuàng)建表的sql 語句的腳本存儲到文件 createSchool.sql 。 create table Student ( Sno char(6) , Sname char(10) not null unique , Ssex char(2) check (ssex=男 or ssex=女) , Sage smallint check(sage16) , Sdept char(10) not null default JSJ , primary key (sno) ) create table course( Cno char(4) , Cname char(16) , Cpno char(4) , Ccredit int check (Ccredit =0 and Ccredit80 6 統(tǒng)計選修課程超過 2 門的學生學號 select sno from sc group by sno having count(*)2 7 統(tǒng)計有10位成績大于85分以上的課程號。 Select cno from sc where grade85 group by cno having count(*) =10 8 統(tǒng)計平均分不及格的學生學號 select sno from sc group by sno having avg(grade)80 and cname=數(shù)據(jù)庫原理 b: select sname from student where sno in ( select sno from sc where grade80 and cno in ( select cno from course where cname=數(shù)據(jù)庫原理) ) 5查詢平均分不及格的學生的學號,姓名,平均分。 select sno, max(sname) , avg(grade) as avggrade from sc , student where student.sno=sc.sno group by student.sno having avg(grade) 75) B: Select max(sname ) from sc,student where student.sno=sc.sno and Ssex=女 Group by student.sno having avg(grade)75 7查詢男學生學號、姓名、課程號、成績。(一門課程也沒有選修的男學 生也要列出,不能遺漏) select student.sno,sname,cno,grade from student left join sc ON student.sno=sc.sno and ssex=男 四 嵌套、相關及其他 1 查詢平均分不及格的學生人數(shù) select count(*) from student where sno in ( select sno from sc group by sno having avg(grade)=all ( select avg(grade) from sc group by sno ) *4 查詢沒有選修1001,1002課程的學生姓名。 Select sname from student where not exists ( Select * from course where cno in (1001,1002) and Not exists ( select * from sc where sno=student.sno and cno= o ) ) 5 查詢1002課程第一名的學生學號(2種方法) a: select top 1 sno from sc cno=1002 order by grade desc b: select sno from sc where cno=1002 and grade =all (select grade from sc where cno=1002) 6 查詢平均分前三名的學生學號 select top 3 sno from sc group by sno order by avg(grade) desc 7 查詢 JSJ 系的學生與年齡不大于19歲的學生的差集 a: select * from student where sdept=JSJ and sage19 b: select * from student where sdept=JSJ except select * from student where sage90 union select sno,sname from student where sno in ( select sno from sc group by sno having avg(grade)85 ) 9 查詢每門課程成績都高于該門課程平均分的學生學號 select sno from student where sno not in ( select sno from sc X where grade=60 group by cno Select o , cnt2*1.0 / cnt1 from v1,v2 where o= o 思考: 1 利用 V_JSJ 視圖,可以更新SX 的學生的年齡嗎? 寫出理由 如: update v_jsj set sage=25 where sno= 0004 0004 號學生為 SX 系. 試驗九 安全性控制實驗 目的:掌握Sql-server 的授權機制. 1)建立新用戶 mary , 密碼1234 Sp_addLogin mary, 1234 2) 授予 mary 可以訪問 School 數(shù)據(jù)庫的權力 選擇 school 數(shù)據(jù)庫 Sp_grantDBaccess mary 3) 以mary 登錄 sql-server , 執(zhí)行 select * from student ,記錄執(zhí)行結 果,說明原因。 無法查到數(shù)據(jù),因為mary 沒有查詢 student 的權 限。 4)將 course 的查詢、更改權限授予 mary Grant select , update on course to mary 5)把查詢 student 表和修改學生學號的權限授予用戶 mary,且他能將 此權限轉授他人。 Grant select , update(sno) on student to mary with grant option 6) 把對 course 表的更改權限從mary 收回 Revoke update on course from mary 7) 把第5)小題授予mary的權限收回。 revoke select , update(sno) on student from mary cascade 8)mary 只能查詢 1001 號課程的學生成績,請問如何授權 Create view v_sc1 (sno,cno,grade) as Select sno, cno,grade from sc where cno=0001 Grant select on v_sc1 to mary 思考: 1 sp_addlogin , sp_grantdbaccess 語句的區(qū)別. 2 如有200個人需要授權,SQL-SERVER如何簡化授權機制。 試驗十 存儲過程 目的: 掌握存儲過程的概念、編程及使用 1 編寫一個存儲過程 usp_avgage , 向客戶端返回每個系科的學生平 均年齡。 系科 平均年齡 JSJ 21 SX 20 。 1) 編寫存儲過程的代碼 Create procedure usp_avgage as Select sdept,avg(sage) from student group by sdept 2)調試、運行該存儲過程。 Usp_avgage 2編寫一個存儲過程 usp_sdept, 傳入一個系科代碼,返回該系的平均 年齡,人數(shù) Create procedure usp_sdept dept char(10) as Select avg(sage),count(*) from student where sdept=dept 3 編寫存儲過程 usp_updateGrade , 傳入?yún)?shù)為課程號,處理邏輯: 對傳入的這門課,進行如下處理: 如某學生該門課成績80 , 則加 2 分 如某學生該門課成績60 , 則加 1 分 如某學生該門課成績80 Update sc set grade=grade + 1 where cno=cno and grade between 60 and 80 Update sc set grade=grade -1 where cno=cno and grade age2 print name1 + 學生的年齡大 else print name2 + 學生的年齡大 return 7 編寫存儲過程 usp_comp_age1 , 比較兩個學生的年齡的高低,兩個 學生的學號有參數(shù)輸入,最后輸出: XXXX學生的年齡大。 注意: XXXX為學生的姓名 Create procedure usp_comp_age1 no1 char(6),no2 char(6) as declare age1 int , age2 int declare name1 char(10) , name2 char(10) -臨時存儲兩個人 的姓名 select age1=sage ,name1 = sname from student where sno=no1 select age2=sage, name2 = sname from student where sno=no2 if age1 age2 print name1 + 學生的年齡大 else print name2 + 學生的年齡大 return 10 編寫存儲過程 usp_comp_age2 , 比較兩個學生的年齡的高低,兩 個學生的學號有參數(shù)輸入,最后把年齡大的學生的姓名、性別返回客戶 端。 Create procedure usp_comp_age1 no2 char(6),no2 char(6) as declare age1 int , age2 int declare name1 char(10) , name2 char(10) -臨時存儲兩個人 的姓名 select age1=sage ,name1 = sname from student where sno=no1 select age2=sage, name2 = sname from student where sno=no2 if age1 age2 select sname ,ssex from student where sno=no1 else select sname ,ssex from student where sno=no2 return 12 編寫存儲過程 usp_t1,傳入?yún)?shù)為學號,把該學號的課程1001的成 績減到58分。每次只能減1分,用循環(huán)完成。 create procedure usp_t1 no char(6) as declare age int set age=100 while age58 BEGIN SELECT age = sage from student where sno=no If age58 Update sage=sage -1 where sno=no END RETURN - 以下不需要 4 編寫存儲過程 usp_disp , 傳入?yún)?shù)為課程號,處理邏輯: 返回每 個學生的成績等級。 成績=90 為優(yōu), 成績=80為良,成績=70 為中,成績=60為及格 ,成績 =90 set sLevel = 優(yōu) else if nGrade=80 set sLevel = 良 else if nGrade=70 set sLevel = 中 else if nGrade=80 set sLevel = 及格 else set sLevel = 不及格 -把結果寫入臨時表 insert into #tmp(sno,cno,grade,level) values (sno,cno,nGrade,sLevel) fetch next from cur1 into sno , nGrade -讀出游標下 一行數(shù)據(jù) end close cur1 dealLocate cur1 select * from #tmp -返回結果給客戶端 drop table #tmp -刪除臨時表 return 5 編寫一個存儲過程,傳入?yún)?shù)為學號,執(zhí)行后,把該學號的學生按如 下格式輸出成績: (注意:只有一行) 學號 姓名 1001課程 1002課程 1003 課程 平均分 6 編寫一個存儲過程,傳入?yún)?shù)為 系科,執(zhí)行后,把該系科的學生按 如下格式輸出學生成績: 學號 姓名 1001 課程 1002課程 1003 課程 平均分 create procedure usp_grade dept char(15) as create table #tmp ( sno char(4) , sname char(10) , g1 int null, g2 int null , g3 int null , pj int null ) declare no char(4) , name char(10), nG1 int ,nG2 int ,nG3 int declare cur1 cursor for select sno , sname from student where sdept = dept -游 標 某一個系的學生 open cur1 fetch next from cur1 into no , name while fetch_status=0 begin select nG1=grade from sc where sno=no and cno=1001 select nG2=grade from sc where sno=no and cno=1002 select nG3=grade from sc where sno=no and cno=1003 insert into #tmp(sno,sname,g1,g2,g3,pj) values (no,name,nG1,nG2,nG3,(nG1+nG2+nG3)/3 ) fetch next from cur1 into no , name end close cur1 dealLocate cur1 select * from #tmp drop table #tmp -執(zhí)行 usp_grade JSJ 7 編寫存儲過程,統(tǒng)計男女生1001,1002,1003各自的選修人數(shù),輸出 格式如下: 性別 1001人數(shù) 1002人數(shù) 1003人數(shù) 小計 男 3 5 2 10 女 2 4 1 7 合計 5 9 3 17 (數(shù)據(jù)為示意數(shù)據(jù)) create procedure usp_tj as create table #tmp (ssex char(2), rs1 int,rs2 int ,rs3 int ,xj int ) declare nRs1 int , nRs2 int, nRs3 int select nRs1 = count(*) from student,sc where cno=1001and ssex=男 select nRs2 = count(*) from student,sc where cno=1002and ssex=男 select nRs3 = count(*) from student,sc where cno=1003and ssex=男 insert into #tmp(ssex,rs1,rs2,rs3,xj) values (男,nRs1,nRs2,nRs3, nRs1+nRs2+nRs3) select nRs1 = count(*) from student,sc where cno=1001and ssex=女 select nRs2 = count(*) from student,sc where cno=1002and ssex=女 select nRs3 = count(*) from student,sc where cno=1003and ssex=女 insert into #tmp(ssex,rs1,rs2,rs3,xj) values (女,nRs1,nRs2,nRs3, nRs1+nRs2+nRs3) select * from #tmp drop table #tmp return 8 編寫一個存儲過程,利用存儲過程的參數(shù)返回數(shù)據(jù)庫服務器上的日期 時間。 思考:何時需要存儲過程? 試驗十二 觸發(fā)器 目的: 了解觸發(fā)器的機制及編程設計、使用 一 建立學生表的觸發(fā)器 usp_addstudent,當增加學生時,SX系的學生 不能超過30歲。 1 寫出觸發(fā)器 2 執(zhí)行下列語句塊: begin tran insert into student (sno,sname,ssex,sage,sdept) values (0701,劉歡,男,26,SX) if error=0 commit else rollback end 觀察該學生是否加入到 student 3執(zhí)行下列語句塊: begin tran insert into student (sno,sname,ssex,sage,sdept) values (0702,趙 歡,男,31,SX) if error=0 commit else rollback end 觀察該學生是否加入到 student 二 實現(xiàn)下列觸發(fā)器 1 不能刪除年齡大于25歲的學生記錄。 create trigger utr_student1 on student for delete as declare nCnt int -存儲被刪除的大于25歲的人數(shù) select nCnt = count(*) from deleted where sage25 if nCnt0 begin raiserror(不能刪除大于25歲的學生,16,10) rollback transaction end -測試 insert into student values (8701,aa1,男,27,JSJ) -不能被刪除 insert into student values (8702,bb1,男,24,JSJ) -能刪除 select * from student where sno in (8701,8702) delete from student where sno=8701 select * from student where sno in (8701,8702) delete from student where sno=8702 2 建立觸發(fā)器 usp_delcourse , 使課程表中1001,1002,1003 三門課 不會被刪除。 注意如何調試。 create trigger utr_deleteCourse on course for delete as declare nCnt int select nCnt = count(*) from deleted where cno in (1001,1002,1003) if nCnt0 begin raiserror(不能刪除,16,10) rollback transaction end return 調試: Delete from course where cno=1001 -不會被刪除 Delete from course where cno=1006 -能被刪除 3 對學生表建立一觸發(fā)器,使更改后的年齡只能比原值大 create trigger utr_student_update1 on student for update as if not update(sage) return declare nCnt int select nCnt = count(*) from inserted ,deleted where deleted.sno=inserted.sno and inserted.sage0 begin raiserror(更改后的年齡比原值小了,16,10) rollback transaction end 4對sc表建立觸發(fā)器,使JSJ系的學生不可選擇 1004號課程 create trigger utr_choose on sc for insert as declare nCnt int -存儲被刪除的大于25歲的人數(shù) select nCnt = count(*) from inserted ,student where student.sno=inserted.sno and sdept=JSJ and o=1004 -inserted 存儲insert 命令添加的數(shù)據(jù) 如 0001,1004,90 if nCnt0 begin raiserror(JSJ不可選擇 1004,16,10) rollback transaction end -測試 insert into student values (8701,aa1,男,27,JSJ) insert into sc(sno,cno,grade) values (8701,1001,90) -可以 insert into sc(sno,cno,grade) values (8701,1004,90) -不可 以 select * from sc where sno=8701 5 對表 course 建觸發(fā)器,實現(xiàn)級聯(lián)刪除的功能,但某課選修人數(shù)大于 3則不能刪除。 (先刪除 sc 表對course 的外碼) *三 建立一個觸發(fā)器,使對sc表成績的修改自動記錄修改日志。 日志文件表(tablog)記錄如下: 用戶名 學號 課程號 原成績 修改后成績 更改日期 四 在School數(shù)據(jù)庫中建立一個試驗用的發(fā)票表bill,然后為發(fā)票bill 建立觸發(fā)器 utr_money ,實現(xiàn)當輸入單價和數(shù)量后,自動填寫金額,即 發(fā)票金額不輸入,由單價、數(shù)量相乘后自動填寫到金額中。 Create table bill( billID char(8), -發(fā)票編號 date datetime, -開票日期 product char(10), -產(chǎn)品編號 price int , -單價 qty int , -數(shù)量 charge int , -金額 primary key (billid) ) 思考: 觸發(fā)器中 inserted , deleted 表的作用? 在觸發(fā)器中如沒 有用到此兩個表中的任何一個,你認為觸發(fā)器還有意義嗎? 試驗十二 恢復技術 目的:1 掌握數(shù)據(jù)庫的備份及恢復的方法。 2 了解備份方案的設定 一 完全備份的建立與恢復 1建立完全備份 USE school GO BACKUP DATABASE school TO DISK=C:schooldata.bak 2查看備份文件中的信息 RESTORE FILELISTONLY FROM DISK=c:schooldata.bak RESTORE HEADERONLY FROM DISK=c:schooldata.bak 3恢復完全備份 1) 先刪除數(shù)據(jù)庫 School USE Master GO DROP DATABASE school 2) 然后恢復. RESTORE DATABASE school from DISK=c:schooldata.bak 3): 查看 school 的student 中的數(shù)據(jù) 二 建立差異備份 1 建立備份 1) 制作數(shù)據(jù)文件備份 schoolDiff.bak 2) 把學號 7001, 姓名:王海,性別:男,年齡為23 的學生加入student 3) 制作school 的差異備份 ,存入schoolDiff.bak BACKUP DATABASE school TO DISK=schoolDiff.bak WITH DIFFERENTIAL 4) 把學號 7002, 姓名:趙燕,性別:女,年齡為22 的學生加入student 5) 制作school 的差異備份 ,存入schoolDiff.bak BACKUP DATABASE school TO DISK=schoolDiff.bak WITH DIFFERENTIAL 2查看備份文件 schoolDiff.bak 中的信息 3 刪除 school 數(shù)據(jù)庫 4 恢復數(shù)據(jù)庫 school 到第2步狀態(tài) RESTORE DATABASE school from DISK=c:schoolDiff.bakWITH file=1 NORECOVERY RESTORE DATABASE school from DISK=c:schoolDiff.bakWITH file=2 Select * from student 觀察student 數(shù)據(jù) 5 恢復數(shù)據(jù)庫 school 到最新狀態(tài) RESTORE DATABASE school from DISK=c:schoolDiff.bakWITH file=1 NORECOVERY RESTORE DATABASE school from DISK=c:schoolDiff.bakWITH file=3 Select * from student 觀察student 數(shù)據(jù) 思考: 如果僅執(zhí)行下述恢復語句,能查看 student 的數(shù)據(jù)嗎? RESTORE DATABASE school from DISK=c:schoolDiff.bakWITH file=1 NORECOVERY Select * from student 三 利用日志備份 1 設置故障還原模型為:完全 2 建立備份 1) 制作數(shù)據(jù)文件備份 schooldata1.bak 2) 把學號 7003, 姓名:王江,性別:男,年齡為23 的學生加入student 3) 制作日志備份存入 schoollog.bak 4) 把學號 7004, 姓名:趙蘭,性別:女,年齡為22 的學生加入 student 5) 制作日志備份存入 schoollog.bak 3 觀察schoollog中的信息 4 刪除 school 數(shù)據(jù)庫 5 利用schooldata1.bak 及 schoollog.bak 恢復數(shù)據(jù)庫 school 到最新狀態(tài) 四 使用企業(yè)管理器練習備份調度策略 1 對數(shù)據(jù)庫 school 每天上午8時進行一次數(shù)據(jù)庫完全備份 2 對數(shù)據(jù)庫 school 的每隔 1分鐘備份進行一次差異備份。 3 手工啟動兩個備份作業(yè) 4 刪除 school 數(shù)據(jù)庫 5 利用1,2步的備份進行 school的恢復。 思考:如何把備份文件備份到另外一臺計算機上。 五 使用企業(yè)管理器練習數(shù)據(jù)庫的分離及附加 六 如何清除日志文件。 七 使用企業(yè)管理器練習數(shù)據(jù)庫的壓縮 八 把school 備份到其他計算機上。 試驗十三 事務 目的:1 掌握并理解事務 一 理解 rollback 1在查詢分析器輸入下列語句并執(zhí)行 ,記錄該學生的年齡。 Select * from student where sno=0001 20 2 執(zhí)行下列 語句序列A: BEGIN TRANsaction Update student set sage=sage+1 where sno=0001 Select * from student where sno=0002 此事務結束了嗎? 答:沒有 3 執(zhí)行: Select * from student where sno=0001 記錄該學生的年齡。 思考:student 中的0001的年齡確實被更改了嗎? 為什么? 21 更改了,因為在事務中執(zhí)行了update語句 4 執(zhí)行下列語句。 ROLLBACK TRANsaction 然后再執(zhí)行: Select * from student where sno=0001 , 觀察0001的年齡, 解釋發(fā)生這種現(xiàn)象的原因。 20 Rollback 放棄了事務中所有已執(zhí)行的語句。 二 理解 commit 1在查詢分析器輸入下列語句并執(zhí)行 ,記錄該學生的年齡。 Select * from student where sno=0001 20 2 執(zhí)行下列 語句序列A: BEGIN TRANsaction Update student set sage=sage+1 where sno=0001 Select * from student where sno=0002 3執(zhí)行: commit transaction Select * from student where sno=0001 記錄結果, 此時更改后的數(shù)據(jù)被永久保存了嗎? 21 永久保存 三 執(zhí)行下列 語句序列 BEGIN TRANsaction Update student set sage=sage+1 where sno=0001 Update sc set grade=grade + 1 where sno=0002 and cno=1001 Rollback 上述指令執(zhí)行后,數(shù)據(jù)庫發(fā)生了什么變化? 沒有 試驗十四 鎖 目的:1 理解鎖的概念及鎖的作用 一 利用幫助系統(tǒng)了解Sql-server 的下列語句的含義 1 鎖的隔離級別 SET TRANSACTION ISOLATION LEVEL Serializable 2 設置鎖定超時時間 SET LOCK_TIMEOUT 5000 3 SP_LOCK 二 觀察封鎖 1 執(zhí)行語句序列A BEGIN TRANsaction Update student set sage=sage+1 where sno=0001 Select * from student where sno=0002 2 在查詢分析器中打開第二個連接(連接 school)文件-連接, 輸 入下列語句: 1) select * from student where sno=0002 記錄執(zhí)行結果,說明原因。 能看到結果,因為在未結束事務中對 student的0002學生加了 S鎖, 其他事務還可以對其加 S鎖。 2)select * from student where sno=0001 記錄執(zhí)行結果,說明原因。(如上一步?jīng)]有停止,則強行終止) 不能看到結果,出于等待狀態(tài)。因為在未結束事務中對 student

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論