一道題學會SQL Server安全性和完整性控制,包括創建數據庫,創建數據庫用戶,權限賦予及收回等
題目如下:
1、在SQL SERVER 2008上附加teaching數據庫,其中三張表的含義解釋如下:
學生表dbo.student有屬性sno、sname、spec、birthday、email、sex、scholarship,分別代表學號、姓名、專業、生日、電子郵箱、性別、獎學金;
課程表dbo.course有屬性cno、cname、credit、teacher,分別代表課號、課程名、學分、任課教師;
選課表dbo.student_course有屬性sno、cno、grade,分別代表學號、課號、成績。
具體操作過程博主該系列上一文章。
2、SQL Server系統的安全性練習
1)視圖技術可以使無權使用數據的用戶不能接觸他感興趣的數據,請設計一個只能查看每個學生平均成績的視圖student_avg_grade(sno,sname,avg_grade);
2)登錄到SQL Server Management Studio後,先展開數據庫服務器®安全性®登錄名,通過右鍵單擊登錄名,在新建登錄名的對話框中創建登錄名S1;然後展開數據庫teaching®安全性®用戶,通過右鍵單擊用戶,在新建用戶的對話框中創建用戶名U1,並使數據庫用戶名U1與數據庫服務器登錄名S1相關聯。
3)用grant和revoke語句進行授權和收權操作,具體如下:
(1)以S1登錄數據庫服務器,查看用戶U1是否有權力查詢teaching數據庫中的Student表?爲什麼?
(2)以Administration身份登錄數據庫服務器,爲U1用戶授予查詢student表的權力,同時允許U1將該權利授予其他用戶;
(3)以S1登錄數據庫服務器,查看用戶U1是否有權力查詢teaching數據庫中的Student表?
(4)收回用戶U1查詢student表的權力;
(5)再次以S1登錄數據庫服務器,查看用戶U1的權限是否收回?
回收權限執行結果:
3、SQL Server系統的完整性練習。以teaching數據庫爲例,在SSMS中創建數據庫S_C,在查詢分析器中創建S、SC和C三張表時分別設置primary key、foreign key、not null、unique、check完整性約束,然後用實驗數據證實當操作違反了完整性約束條件時,系統是如何處理的。
1)舉例說明如何在S、C表中設置primary key、not null、unique完整性約束;
SQL Sever實現
--2、SQL Server系統的安全性練習
--1)視圖技術可以使無權使用數據的用戶不能接觸他感興趣的數據,
--請設計一個只能查看每個學生平均成績的視圖student_avg_grade(sno,sname,avg_grade);
use teaching
go
create view student_avg_grade(sno,sname,avg_grade)
as select s.sno,s.sname,avg(sc.grade)
from student s,student_course sc
group by s.sno,s.sname
go
--創建登錄名
use teaching
create login S1 with password='mimas1',default_database=teaching
go
--激活登錄名
use teaching
alter login S1 enable
go
--創建用戶
use teaching
create user U1 for login S1 with default_schema=guest
go
--刪除登錄名
use teaching
drop login S1
go
--刪除用戶
use teaching
drop user U1
go
--驗證是否能查詢
use teaching
select * from student
go
--賦予權限
use teaching
grant select on student to U1 with grant option
go
--收回權限
use teaching
revoke select on student from U1 cascade
go
--更新
use teaching
update student
set spec='計算機'
where sno='0012301' and sname='楊海濤'
go
--3
--SQL Server系統的完整性練習。以teaching數據庫爲例,在SSMS中創建數據庫S_C,
--在查詢分析器中創建S、SC和C三張表時分別設置primary key、foreign key、
--not null、unique、check完整性約束,然後用實驗數據證實當操作違反了完整性約
--束條件時,系統是如何處理的。
USE master--使用系統
GO
CREATE DATABASE S_C --創建數據庫
ON PRIMARY --主文件
(
NAME='S_C_data', --文件名
FILENAME='D:\SQLProject\S_C_data.mdf',--路徑
SIZE=5MB,--初始大小
MAXSIZE=100MB,--最大容量
FILEGROWTH=10%--增長速度
)
LOG ON--日誌文件
(
NAME='S_C_log',
FILENAME='D:\SQLProject\S_C_log.ldf',
SIZE=5MB,
FILEGROWTH=0
)
GO
--3.1
--舉例說明如何在S、C表中設置primary key、not null、unique完整性約束;
use S_C
create table S
(
sno char(11) not null primary key,
creadit_ID char(16) not null unique,
age int not null check(age<=0),
sname varchar(20) not null
)
go
create table C
(
cno char(5) not null primary key,
cname varchar(20) not null
)
go
--3.2
--舉例說明如何在SC表中設置foreign key、check完整性約束
create table SC
(
sno char(11) not null foreign key(sno) references S(sno),
cno char(5) not null foreign key(cno) references C(cno),
grade numeric(5,0) not null check(grade<0),
constraint SC_pk primary key(sno,cno)
)
go
use S_C
drop table S,C
go
--非法記錄,check age
use S_C
insert into S(sno,creadit_ID,age,sname)
values('14401010410','3301271996021617',18,'Jhon')
go
--合法記錄
use S_C
insert into S(sno,creadit_ID,age,sname)
values('14401010411','3311271996021617',0,'June')
go
--合法記錄
use S_C
insert into S(sno,creadit_ID,age,sname)
values('14401010412','3311271996021618',-1,'July')
go
--合法記錄
use S_C
insert into C
values('12345','數據庫')
go
--非法記錄
use S_C
insert into C
values('123456','語文')
go
--合法記錄
use S_C
insert into SC(sno,cno,grade)
values('14401010411','12345',-2)
go
--非法記錄,check grade
use S_C
insert into SC
values('14401010412','12345',8)
go
結果截圖: