1. 程式人生 > 實用技巧 >Microsoft SQL第三次實驗

Microsoft SQL第三次實驗

此文轉載自:https://blog.csdn.net/qq_44723233/article/details/109908525

檢視、觸發器、儲存、安全等

實驗要求

(1) 掌握檢視的定義與操作
(2) 掌握對觸發器的定義
(3) 掌握對儲存過程的定義
(4) 掌握如何對使用者進行授權和收回許可權
(5) 掌握使用者定義完整性的方法
(6) 寫出實驗報告

實驗步驟

(1) 建立表的檢視
(2) 利用檢視完成表的查詢
(3) 刪除表的檢視
(4) 建立觸發器
(5) 建立儲存過程
(6) 對使用者進行授權和查詢
(7) 使用者定義完整性

檢視

(1) 建立

建立 CS 系的檢視 CS_View CS 系的檢視 CS_View

遇到問題

’CREATE VIEW’ 必須是查詢批次中的第一個語句
批處理必須以 CREATE 語句開始。也就是說一個查詢分析器裡面只有一個批處理語句才是規範的語法。 CREATE DEFAULT、CREATE FUNCTION、CREATE PROCEDURE、CREATE RULE、CREATE SCHEMA、CREATE TRIGGER 和 CREATE VIEW 語句不能在批處理中與其他語句組合使用。
所有跟在該批處理後的其他語句將被解釋為第一個 CREATE 語句定義的一部分。 之間加GO關鍵字 分批即可。
也可以重新建立一個查詢來寫這個批處理語句

go
create view CS_View
as
select *
from Student
where Sdept='CS'
with check option

(2) 查詢

在檢視 CS_View 上查詢 CS 系選修了 1 號課程的學生

go
select cs_view.sno,sname,ssex,sage,cno
from CS_View,sc
where cs_view.sno=sc.sno and cno=1

(3) 建立 IS 系

建立 IS 系成績大於 80 的學生的檢視 IS_View

go
create view IS_View
as
select Student.
Sno,Sname,cno,grade from Student,sc where Student.sno=sc.Sno and Grade>80

(4) IS_View查詢

在檢視 IS_View 查詢 IS 系成績大於 80 的學生

select *
from IS_View
where grade>80

(5) 刪除

刪除檢視 IS_View

drop view IS_View

(6) 不同使用者操作

利用視覺化視窗建立 2 個不同的使用者 U1 和 U2,利用系統管理員給 U1 授予 Student 表的
查詢和更新的許可權,給 U2 對 SC 表授予插入的許可權。然後



再進入資料庫S_T_的使用者處設定許可權

對U1使用者檢視屬性


但是登陸時出現問題


進行以下操作,成功登入,將sql 2008中的配置也進行相關更改。
解決方案參考來源



用 U1 登入,分別

  • 1)查詢學生表 的資訊;
  • 2)把所有學生的年齡增加 1 歲,然後查詢;
  • 3)刪除 IS 系的學生;
  • 4)查詢 CS 系 的選課資訊。

登入後只能檢視到student表

1)and 2)

select *
from Student
update Student
set Sage+=1
select *
from Student


3)

delete 
from Student
where Sdept='IS'

4)

select sc.Sno,sname,sc.Cno
from Student,sc
where Sdept='CS'and Student.Sno=sc.Sno

用 U2 登入,分別

  • 1)在 SC 表中插入 1 條記錄(‘200215122’,‘1’,75);
  • 2)查詢 SC 表的資訊,
  • 3)查詢檢視 CS_View 的資訊。
insert into sc values('200215122','1',75)
select *
from sc
select *
from CS_view

(7) 收回許可權

用系統管理員登入,收回 U1 的所有許可權

(8) U1再查詢

用 U1 登入,查詢學生表的資訊

(9) 系統管理員登入

用系統管理員登入

觸發器

(10) 建立觸發器

對 SC 表建立一個更新觸發器,當更新了 SC 表的成績時,
  如果更新後的成績大於等於95,則檢查該成績的學生是否有獎學金,如果獎學金是“否”,則修改為“是”。
  如果修改後的成績小於 95,則檢查該學生的其他成績是不是有大於 95 的,如果都沒有,且修改前的成績是大於 95 時,則把其獎學金修改為”否”。
然後進行成績修改,並進行驗證是否觸發器正確執行。

對於update操作,會建立兩個虛表deleted(更新前資料)和inserted(更新後資料),完成後將表student中的deleted資料刪除,插入inserted資料

參考

觸發器我寫得不是很好,希望有不足的地方能指出

go
create trigger sc_trigger on sc --對sc表建立觸發器
after update--語法:{After|Instead of} {insert|update|delete} 
as	
	begin			--begin後面輸入sql語句
		update Student
		set Scholarship='是'
		where Sno in
			(select sc.sno
			from sc,inserted
			where inserted.Grade>=95 and Student.sno=sc.Sno 
				and sc.sno =inserted.Sno and  scholarship='否' )

		update Student
		set Scholarship='否'
		where sno in
			(select sc.sno from sc,inserted,deleted
			/*修改後小於95*/
			where inserted.Grade<95	and sc.sno =inserted.sno
			/*大於95的數為0*/
				and (select count(grade) from sc where Grade>=95 and sno = inserted.sno)<1			
				and deleted.grade>=95	--該學生修改前分數大於95
			and sc.sno=Student.sno)
	end

  • 1)首先把某個學生成績修改為 98,查詢其獎學金。
  • 2)再把剛才的成績修改為 80, 再查詢其獎學金。
update sc
set Grade=98
where sno='200215122' and cno=2
select scholarship
from Student
where sno='200215122'


update sc
set Grade=80
where sno='200215122' and cno=2

select scholarship
from Student
where sno='200215122'

(11) 刪除觸發器

drop trigger sc_trigger

刪除剛定義的觸發器

儲存過程

引用

  • 儲存過程Procedure是一組為了完成特定功能的SQL語句集合,經編譯後儲存在資料庫中,使用者通過指定儲存過程的名稱並給出引數來執行
  • 儲存過程中可以包含邏輯控制語句資料操縱語句,它可以接受引數、輸出引數、返回單個或多個結果集以及返回值。
  • 由於儲存過程在建立時即在資料庫伺服器上進行了編譯並存儲在資料庫中,所以儲存過程執行要比單個的SQL語句塊要快。同時由於在呼叫時只需用提供儲存過程名必要的引數資訊,所以在一定程度上也可以減少網路流量、簡單網路負擔。

(12) 定義儲存過程

定義一個儲存過程計算 CS 系的課程的平均成績和最高成績,在查詢分析器或查詢編輯器中執行儲存過程,檢視結果。

格式:
go
create proc(全稱procedure可以簡寫為proc) [儲存過程的名字]
	( [@引數1 引數型別1],[@引數2 varhcar(50)],[引數3 varchar(50)='小白'],
	  [引數4 int output] ) /*output加上output後表示該引數是返回的引數*/
as
	SQL語句
	
use S_T_
if (object_id('cs_proc', 'P') is not null)
    drop proc cs_proc	--判斷該儲存過程是否不存在,存在則刪除
go
create  procedure cs_proc
as 
	select avg(grade) ,max(grade)
	from sc,student
	where sc.sno =student.sno and  Sdept='CS'
go

execute cs_proc --呼叫
go

(13)

定義一個帶學號為引數的檢視某個學號的所有課程的成績,查詢結果要包含學生姓名。進行驗證。

if (object_id('grade_proc', 'P') is not null)
    drop proc grade_proc	--判斷該儲存過程是否不存在,存在則刪除
go
create  procedure grade_proc( @sno_s varchar(10))
as 
	select sname,sc.sno,cno,grade
	from Student,sc
	where sc.sno=Student.sno and sc.Sno=@sno_s
go

declare @sno_in varchar(10)='200215122'
exec grade_proc @sno_s=@sno_in
go

(14) 函式

把上一題改成函式。再進行驗證。

if OBJECT_ID(N'dbo.search') is not null 
    drop function dbo.Search
 go
  create function dbo.Search(@Stu_No varchar(10))
  returns  table 
  as
	return
  (select Sname,sc.sno,cno,Grade from Sc,Student where Student.Sno=Sc.Sno and Student.Sno=@Stu_No  )
  
  go
  --呼叫一下函式查詢學號為200215122的學生成績
  select * from dbo.Search('200215122')

安全(約束)

(15) 完整性約束

在 SC 表上定義一個完整性約束,要求成績再 0-100 之間。定義約束前,先把某個學 生的成績修改成 120,進行查詢,再修改回來。定義約束後,再把該學生成績修改為 120,然後進行查詢

update sc 
set grade =120
where sno='200215122' and cno =2;
select sno,cno,grade from sc
where sno='200215122' and cno =2;
update sc 
set grade =80
where sno='200215122' and cno =2

alter table sc 
add constraint c3 check(grade between 0 and 100)

update sc
set grade =120
where sno='200215122' and cno =2;
select sno,cno,grade from sc
where sno='200215122' and cno =2