数据库原理 实验五 安全性及事务操作
- 格式:doc
- 大小:151.50 KB
- 文档页数:5
《数据库原理》实验报告
题目:实验五 安全性及事务操作
学号: 成 绩
班级: 计算12
日期:2014.05 姓名:
指导老师:林颖贤 一、实验目的:
1、掌握SQL Server 的安全机制;
2、掌握服务器的安全性的管理;
3、掌握数据库用户的管理;
4、掌握权限的管理。
二、实验使用环境:
Windows 7 x64、SQL Server 2005
三、实验内容与完成情况:
1、安全性综合实验
1)设计安全机制使得用户“test”只能查询采购部门的职工。
sp_addlogin 'test','123'
execute sp_grantdbaccess 'test','wangming'
create view V_emp
as
select * from Employees
where Emp_depart ='采购部'
grant select on V_emp to wangming
select*from V_emp
2)设计角色“Employees”,可以查看“职工”的职工号与姓名。
并将用户“test”作为成员加入角色“Employees”,这样用户“test”只能查看“职工”的职工号与姓名。
exec sp_addrole'employee'
sp_addrolemember'employee','wangming'
create view V_Role_Emp
as
select Emp_no,Emp_name from Employees
grant select on V_Role_Emp to employee
select*from V_Role_Emp
3)请进行安全设置,用户李建国师拥有以下权力:他要能查进货表中的信息,并拥有对自已进货的信息修改的权限,其它表的信息无权查看。
sp_addlogin'test1','123'
exec sp_grantdbaccess'test1','李建国'
create view V_Li
as
select*from Purchase
where Emp_no in(select Emp_no from employees
where Emp_name='李建国')
grant select,update on V_Li to李建国
grant select on Purchase to李建国
4)如何使得采购部门的员工都具有这样的权限:能查看进货表的信息,并拥有对自已采购信息的修改,其它的信息无权查看。
(要求:编写存储过程proc_stu_grant,其作用:
输入参数为员工姓名,从进货表中查找该员工所进货的产品,如果没有则返回,有的话则相应的在login表中添加账号和密码。
并且,创建相应的登录账号和数据库用户)create proc proc_emp_grant @Emp_name nvarchar(10)
as
begin
declare @sqlstr varchar(255)
declare @sqlview varchar(14)
if exists(select Pur_no from Purchase
where Emp_no in(select Emp_no from Employees
where Emp_name=@Emp_name))
begin
set @sqlview=@Emp_name+'_view'
set @sqlstr='create view '+@sqlview+'
as
select * from Purchase
where Emp_no in(select Emp_no from employees
where Emp_name='''+@Emp_name+''')'
exec(@sqlstr)
exec sp_addlogin @Emp_name,'123456'
exec sp_grantdbaccess @Emp_name,@Emp_name
set @sqlstr='grant select on Purchase to '+@Emp_name
exec(@sqlstr)
set @sqlstr='grant select,update on '+@sqlview+' to '+@Emp_name
exec(@sqlstr)
end
else print'没有该员工的进货信息!'
end
exec proc_emp_grant '赵明'
exec proc_emp_grant '赵哈哈'
5)银行转账问题
CREATE TABLE bank --创建账户表,存放用户的账户信息
(
customerName CHAR(10), --顾客姓名
currentMoney MONEY --当前余额
)
GO
--添加约束:根据银行规定,账户余额不能少于1元,否则视为销户
ALTER TABLE bank ADD CONSTRAINT CK_currentMoney CHECK(currentMoney>=1) GO
--张三开户,开户金额为1000元;李四开户,开户金额1元
INSERT INTO bank(customerName,currentMoney) VALUES('张三',1000) INSERT INTO bank(customerName,currentMoney) VALUES('李四',1) 写出用事务解决银行转账的存储过程:
create table bank
( customerName char(10),
currentMoney money)
alter table bank add constraint ck_currentMoney check(currentMoney>=1) insert into bank(customerName,currentMoney)values('张三',1000)
insert into bank(customerName,currentMoney)values('李四',1)
select*from bank
create proc proc_bank
@send char(10),@receive char(10),@money money
as
begin
declare @xianyou money
select @xianyou=currentMoney from bank where customerName=@send
if((@xianyou-@money)<0)
begin
begin tran fail
update bank set currentMoney=currentMoney-@money
where customerName=@send
update bank set currentMoney=currentMoney+@money
where customerName=@receive
print'转账失败!'
rollback tran fail
end
else
begin
update bank set currentMoney=currentMoney-@money
where customerName=@send
update bank set currentMoney=currentMoney+@money
where customerName=@receive
print'转账成功!'
end
end
exec proc_bank '张三','李四',500
四、出现的的问题及解决方案
1、问题:删除用户时提示异常。
解决方案:在删除用户时,先删除对应的构架,再删除对应的用户。
2、问题:操作不在对应数据库。
解决方案:执行代码前,养成良好习惯,先使用use 命令,切换到指定数据库。
3、问题:begin与end经常没有一一对应。
解决方案:在代码排版上缩进一致,便于检查。
五、实验小结
1、通过本次实验,对SQL Server的安全机制有了进一步的了解,掌握了服务器的安全性的管理、数据库用户的管理以及用户权限的管理。
2、在做任务5时,对存储过程用到字符串连接的思想掌握的还不是非常好。
3、对存储过程,还有触发器掌握的不是十分熟练
4、良好的数据库安全性,对于防止非法使用数据库有及其重要的意义。