部门(部门号,名称,经理名,地址,电话号)
职工(职工号,姓名,年龄,职务,工资,部门号)
一、在Hr用户模式下,创建职工和部门表,各插入2条测试数据。
select *
from v$nls_parameters t
where t.PARAMETER='NLS_CHARACTERSET';
create table Dept
(
Deptno char(9) ,
Deptname varchar2(8),
Deptmanager varchar2(12),
Deptaddress varchar2(20),
Deptphone char(11),
primary key(Deptno)
);
drop table Dept;
select *
from Dept;
create table Emp
(
Empno char(9) ,
Empname varchar2(8),
Empage char(4),
Empjob varchar2(10),
Empsal smallint,
Empdeptno char(9),
primary key(Empno),
foreign key(Empdeptno) references Dept(Deptno)
);
drop table Emp;
select *
from Emp;
insert into Dept values('201400644','学习部','张三','湖北大学','12345678910');
insert into Dept values('201400645','卫生部','李四','湖北大学','0987654321');
insert into Emp values('201400789','王五',25,'销售人员',1200,'201400644');
insert into Emp values('201400790','李明',26,'监督人员',1200,'201400645');
select *
from Emp;
select *
from Dept;
二、定义用户,用SQL的GRANT和REVOKE语句完成以下授权定义或存取控制功能。
1、创建数据库本地用户test,密码为oracle,默认表空间为users,临时表空间为temp,同时具有create session系统权限。
--创建数据库本地用户test
create user test
--设置密码为oracle
identified by oracle
--设置默认表空间为users
default tablespace users
--设置表空间为temp
temporary tablespace temp;
grant create session to test;
revoke create session from test;
2、用户王明对两个表有SELECT权力;
--将两个表的select权限授予给用户王明
--首先创建在管理员登录下,创建一个新用户
create user WangMing
--将用户WangMing用户的密码设置为oracle
identified by oracle;
grant create session to WangMing;
revoke create session from WangMing;
grant select
on Emp
to WangMing;
revoke select
on Emp
from WangMing;
grant select
on Dept
to WangMing;
revoke select
on Dept
from WangMing;
####
select *
from Emp;
select *
from system.Emp;
select *
from system.Dept;
3、用户李勇对两个表有INSERT和DELETE权力;
--创建用户李勇
create user LiYong
identified by oracle;
grant create session
to LiYong;
grant select
on Emp
to LiYong;
revoke select
on Emp
from LiYong;
grant select
on Dept
to LiYong;
revoke select
on Dept
from LiYong;
####
select * from system.Emp;
select * from system.Dept;
4、用户刘星对职工表有SELECT权力,对工资字段具有更新权力;
--创建用户刘星
create user LiuXing
--设置用户刘星的密码
identified by oracle;
grant create session
to LiuXing;
revoke create session
from LiuXing;
grant select,update(Empsal)
on Emp
to LiuXing;
revoke select
on Emp
from LiuXing;
revoke update(Empsal)
on Emp
from LiuXing;
####
select *
from system.Emp;
update system.Emp
set Empsal=3000
where Empno='201400789';
5、用户周平具有对两个表所有权力(读、插、改、删数据),并具有给其他用户授权的权力;
提示:
create user ZhouPing
--将用户周平的密码设置为oracle
identified by oracle
--创建用户时对于同一个用户名的用户只能创建一次
--如果多次重复创建会出现与另外一个用户名或者角色发生冲突的现象
--授予用户周平创建回话的权限
grant create session
to ZhouPing;
revoke create session
from ZhouPing;
grant insert,delete,update,select
on Emp
to ZhouPing
with grant option;
grant all privileges
on Emp
to ZhouPing
with grant option;
grant all
on Emp
to ZhouPing
with grant option;
select *
from system.Emp;
insert into Emp
values('201400792','小赵',27,'监督人员',200,'201400645');
insert into system.Emp
values('201400191','小王',30,'销售人员',3000,'201400644');
insert into system.Emp
values('201400792','小赵',27,'监督人员',200,'201400645');
update system.Emp
set Empsal=2000
where Empno='201400792';
update system.Emp
set Empno='201400791'
where Empname='小王';
delete
from system.Emp
where Empno='201400792';
6、用户杨兰具有从每个部门职工中SELECT最高工资,最低工资,平均工资的权力,他不能查看每个人的工资。
--首先创建用户YangLan
create user YangLan
--设置用户密码为oracle
identified by oracle;
grant create session
to YangLan;
revoke create session
from YangLan
--首先创建视图查询每个部门职工中的最高工资,最低工资和平均工资,
--然后授予用户查询视图的权限。
create view Sallevel
(MaxSal,MinSal,AvgSal)
as
select max(Empsal),min(Empsal),avg(Empsal)
from system.Emp;
grant select
on Sallevel
to YangLan;
revoke select
on Sallevel
from YangLan;
####
select *
from system.Sallevel;
select *
from system.Emp;
三、设计安全机制使得用户“liming”只能查询年龄在40岁以上(包括)职工。
create view Oldemp
as
select *
from Emp
where Empage>=40;
create user LiMing
identified by oracle;
grant create session
to LiMing;
revoke create session
from LiMing;
grant select
on Oldemp
to LiMing;
revoke select
on Oldemp
from LiMing;
####
select *
from system.Oldemp;
四、设计安全机制使得用户“liming”只能访问“职工”的职工号、姓名。
提示:用视图实现
create view Maininfor
as
select Empno,Empname
from Emp;
grant select
on Maininfor
to LiMing;
revoke select
on Maininfor
from LiMing;
####
select *
from system.Maininfor;
五、设计角色“student”,可以查看“职工”的职工号、姓名、年龄。将用户“liming”添加到角色“student”中。
提示:用视图实现
create view Moreinfor
as
select Empno,Empname,Empage
from Emp;
create role student;
grant select
on Moreinfor
to student;
grant student
to LiMing;
select * from Moreinfor;
####
select *
from system.Moreinfor;