数据库
首页 > 数据库> > 数据库【第四章作业题】

数据库【第四章作业题】

作者:互联网

6.对下列两个关系模式:

  学生(学号、班级、年龄、性别、家庭住址、班级号)
  班级(班级号,班级名,班主任,班长)。

使用GRANT语句完成以下授权功能

首先建立以下实验需要的table。

create table Class
(ClassNum int primary key,
ClassName char(5) unique not null,
HeadTeacher char(5) not null,
Monitor char(5) 
);
create table Students
(Id char(11)primary key,
Name char(5) not null,
Age smallint check(age>0 and age<100) not null,
Sex char(2) check(sex='男' or sex='女'),
Address char(20),
ClassNum int foreign key
references Class(ClassNum) on delete cascade);

用户U1,U2都是之前就建立好的,我这里就直接使用了。

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

grant all privileges
on Class
to U1
with grant option;
grant all privileges
on Students
to U1
with grant option;

还是会有以下提示,不需要管。

ALL 权限已不再推荐使用,并且只保留用于兼容性目的。它并不表示对实体定义了 ALL 权限。
ALL 权限已不再推荐使用,并且只保留用于兼容性目的。它并不表示对实体定义了 ALL 权限。

 

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

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

运行成功。

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

grant select
on Class
to public;

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

grant select,update
on Students
to R1;

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

alter role R1
add member U1

7.今有以下两个关系模式:

  职工(职工号,姓名,年龄,职务,工资,部门号)
  部门(部门号,名称,经理名,地址,电话号)

用SQL的GRANT和REVOKE语句(加上视图机制)完成以下授权定义或存取控制功能

建立以下实验需要的table

create table Department(
Id char(11) primary key,
Name char(5) unique not null,
Manager char(5) not null,
PhoneNumber char(11)
);
create table Employee
(Id char(11) primary key,
Name char(5) not null,
Age smallint check(Age>0 and Age<100),
Salary Money,
DepartmentId char(11) references Department(Id)
on delete cascade
);

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

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

(2)用户李勇对两个表有INSERT和DELETE权限。

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

(3)每个职工只对自己的记录有SELECT权限。

先创建相应的视图,并且把员工ID作为员工的用户名

create view Users
as
select *
from Employee
where Employee.Id=USER

创建好试图后,员工使用自己的用户名登录,直接从视图中查询,就只能看到自己的信息。

grant select
on Users
to public;

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

grant select,update(Salary)
on Employee
to 刘星;

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

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

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

grant select,update,delete,insert
on Employee
to 周平;
grant select,update,delete,insert
on Department
to 周平;

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

grant select
on SalaryLevel
to 杨兰;

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

(1)

revoke select
on Department
from 王明
revoke select
on Employee
from 王明

(2)

revoke insert,delete
on Department
from 李勇
revoke insert,delete
on Employee
from 李勇

(3)

同时删除视图。

revoke select
on Users
from public;
drop view Users;

(4)

revoke select,update(Salary)
on Employee
from 刘星;

(5)

revoke alter
on Employee
from 张新;
revoke alter
on Department
from 张新;

(6)

revoke select,update,delete,insert
on Employee
from 周平;
revoke select,update,delete,insert
on Department
from 周平;

(7)

revoke select
on SalaryLevel
from 杨兰;
drop view SalaryLevel;

 

心得:还是不太会使用怎么用其他用户登录SQLserver,只会使用第一节课使用的那个sa用户登录,其他的不知道什么原因,一直就进不去。

哎,有时间在研究研究,完结,撒花!

 

标签:revoke,grant,数据库,char,作业题,第四章,Employee,权限,select
来源: https://blog.csdn.net/qq_45745322/article/details/115815846