2021-04-25

** ## 第四章习题 **

对下列两个关系模式:
学生(学号,姓名,年龄,性别,家庭住址,班级号)
班级(班级号,班级名,班主任,班长)
使用grant语句进行授权。

(1) 授权用户U1对两个表的所有权限,并可以向其他用户授权。

grant all privileges
on Class
to U1
with grant option;

grant all privileges
on Student
to U1
with grant option;

2021-04-25_第1张图片

(2)授予用户U2对学生表具有查看权限,对家庭地址具有更新权限

grant select, update(Address)
on Students
to U2;

2021-04-25_第2张图片

(3)将对班级查看权限授予所有用户

grant select
on Class
to public;

(4)将对学生表的查询、更新权限授予角色R1

grant select,update
on Student
to R1;

(5)将角色R1授予用户U1,并且U1可继续授权给其他角色。

grant R1
to U1
with grant option;

今有以下两个关系模式
职工(职工号,姓名,年龄,职务,工资,部门号)
部门(部门号,名称,经理名,地址,电话号)
请用SQL的GRANT 和REVOKE 语句(加上视图机制)完成以下授权定义或存取控制功能:

(1)用户王明对两个表有SELECT权限。

grant select 
on Employee
to 王明;
grant select 
on Department
to 王明;

2021-04-25_第3张图片
(2)用户李勇对两个表有INSERT和 DELETE权限。 ⠀

grant insert,delete
on Employee
to 李勇;
grant insert,delete
on Department
to李勇;

2021-04-25_第4张图片
在这里插入图片描述
在这里插入图片描述
(3)每个职工只对自己的记录有SELECT权限。 ⠀

grant select
on Staff
when user()=Sname
to all;

(4)用户刘星对职工表有SELECT 权限,对工资字段具有更新权限。 ⠀ ⠀

grant select
on Employee
to 刘星;
grant select
on Department
to 刘星;

(5)用户张新具有修改这两个表的结构的权限。

grant update
on Employee
to 张新;
grant update
on Department
to 张新;

(6)用户周平具有对两个表的所有权限(读、插、改、删数据),并具有给其他用户授权的权限。

grant all 
on Employee
to 周平
with grant option;
 
grant all 
on Department
to 周平
with grant option;

(7)用户杨兰具有从每个部门职工中SELECT最高工资、最低工资、平均工资的权限,他不能查看每个人的工资。

create view staff(Dname,max_salary,min_salary,avg_salary)
as
select Department.Dname,max(salary),min(salary),avg(salary)
from Staff,Deparment
where Staff.Depa_num=Department.Depa_num 
group by Staff.Depa_num;

grant select
on staff
to 杨兰;

针对习题7中(1)~(7)的每一种情况,撤销各用户所授予的权限。

revoke select
on Employee
from 王明;
revoke select
on Department
from 王明;
revoke insert,delete
on Department
from 李勇;
revoke insert,delete
on Employee
from 李勇;
revoke select,update
on Employee
from 刘星;
revoke update
on Employee
from 张新;
revoke update
on Department
from 张新;
revoke all 
on Employee
from 周平 cascade;
revoke all 
on Department
from 周平 cascade;
revoke select
on SL
from 杨兰;

————————————————————————————————————————
完成。

你可能感兴趣的:(sql)