📄 demo.sql
字号:
connect dbsa with password "dragon"; //dbsa登录系统,当前用户为dbsa
audit USER(dba); //审计用户dba的所有操作
audit USER(dbsa); //审计用户dbsa的所有操作
connect dba with password "dragon"; //dba登录系统,当前用户为dba
create user user1 with password "dragon"//创建用户user1
with seclevel <3, {}>; //因为只有DBSA才能设置用户安全级,所以设置安全级无效,缺省设为<2,{}>
create user user2 with password "dragon"; //创建用户user2,缺省密级为2
create user user3 with password "dragon"; //创建用户user3,缺省密级为2
connect dbsa with password "dragon"; //dbsa登录系统,当前用户为dbsa
audit USER(user1, user2, user3); //审计用户user1,user2的所有操作
add user user1,user2 to system,dba; //把用户user1,user2加入到角色system,dba中
del user user2 from dba; //把用户user2从角色dba中删除
connect user1 with password "dragon"; //user1登录系统,当前用户为user1
create database db1; //创建数据库db1,user1是数据库db1的创建者
create table table1(a1 int, a2 char(10), a3 int, //创建数据库db1中的表table1
primary key(a1)) with seclevel <2, {"r11","r12","r13"}>;//SecLevel(table2)=<2,{r11,r12,r13}>
create table table2(b1 int, b2 char(10), b3 int, //创建数据库db1中的表table2
primary key(b1)); //SecLevel(table2)=SecLevel(user1)=<3,{}>
/*以下演示自主存取控制*/
/*(1)创建角色*/
create role role1; //创建角色role1
create role role2; //创建角色role2
create role role3 as child of role1; //创建角色role3,并把role3设为role1的子角色
create role role4 as child of role1; //创建角色role4,并把role4设为role1的子角色
create role role5 as child of role2; //创建角色role5,并把role5设为role2的子角色
/*(2)权限授权*/
grant create table,drop table to role1,role2;//把系统权限授予给角色role1,role2
grant select on table1 to role1 with grant option;//把表级对象权限授予给角色role1
grant insert(a1),update(a1,a3) on table1 to role2 with grant option;//把列级对象权限授予给角色role2
grant select(a2) where a3>2 on table1 to role2;//把带有谓词条件的列级对象权限授予给角色role2
grant select on table1 to dba; //不能把权限授给系统预定义角色
/*(3)角色继承,角色树*/
set top role role5; //设置角色role5为顶层角色
set child role role5 of role3; //设置角色role5为角色role3的子角色
set parent role role2 of role5; //设置角色role2为角色role5的父角色
/*(4)权限包容*/
grant delete on table1 to role3;//把表级对象权限授予给角色role3,因为父角色没有权限,所以授权失败
grant select(a1) on table1 to role3;//把列级对象权限授予给角色role3,因为父角色有此权限,所以授权成功
revoke select(a1) on table1 from role1;//把列级对象权限从角色role3中加收,因为子角色有此权限,所以回收失败
set child role role3 of role2;//设置角色role3为role2子角色,则role3及其子孙的所有权限都将被回收
grant insert(a1) on table1 to role3;//把列级对象权限授予给角色role3,因为父角色有此权限,所以授权成功
set parent role role4 of role3;//设置角色role4为role3的父角色,则隐含地将role3的所有权限授权给role4及其祖先角色
//这种隐含授权的情况,授权人只可能以DBA或数据库创建者的身份创建
/*(5)把用户加入角色中*/
add user user1,user2 to role1,role4; //把用户user1,user2加入到角色role1,role4中
add user user2 to role2,role5; //把用户user2加入到角色role5中
add user user3 to role2; //把用户user3加入到角色role2中
connect dbsa with password "dragon"; //dbsa登录系统,当前用户为dbsa
add user dbsa to dba; //不能把DBSA用户加入到其他角色中(控制DBSA权利)
add user user2 to dbsa; //可以把其他用户加入到角色DBSA中
del user user2 from dbsa; //把用户user2从角色DBSA中删除
/*(6)级联回收*/
connect user3 to db1 with password "dragon"; //用户user3登录系统,当前用户为user3
grant update(a1) on table1 to role5 with grant option;//用户user3把对象权限授给角色role5
grant update(a3) on table1 to role5; //用户user3把对象权限授给角色role5
connect user2 to db1 with password "dragon"; //用户user2登录系统,当前用户为user2
grant update(a1) on table1 to role1; //用户user2把对象权限授予给角色role1
grant update(a3) on table1 to role1; //用户user2把对象权限授予给角色role1
connect dba to db1 with password "dragon"; //用户dba登录系统,当前用户为dba
drop user user3; //删除用户user3,要回收他以任何角色身份授出的权限
//connect user1 to db1 with password "dragon"; //用户user1登录系统,当前用户为user1
//drop role role5; //删除角色role5,要加收任何用户以role5的身份授出的权限
//del user user3 from role2; //把用户user3从角色role5中删除,并级联回收相应权限
/*(7)重复授权*/
connect user2 to db1 with password "dragon"; //用户user2登录系统,当前用户为user2
grant select(a1) on table1 to role2; //用户user1把对象权限授给角色role2
grant select(a1) on table1 to role2; //用户user1把对象权限授给角色role2
connect user1 to db1 with password "dragon"; //用户user1登录系统,当前用户为user1
grant select(a1) on table1 to role2; //用户user1把对象权限授给角色role2
revoke select(a1) on table1 from role2; //用户user1从角色role2中回收对象权限
/*(8)授权粒度*/
revoke select(a3) on table1 from role1; //用户user1从角色role1中回收对象权限
revoke select(a2) on table1 from role2; //用户user1从角色role2中回收对象权限,因为没有带谓词条件,所以回收失败
/*审计*/
connect dbsa to db1 with password "dragon"; //用户dbsa登录系统,当前用户为dbsa
noaudit user(dba,dbsa,user1,user2);
audit select on table1 executed by user(user1); //审计用户1在表table1上的select操作
/*以下演示强制存取控制*/
/*(1)设置用户最大安全级*/
connect dbsa to db1 with password "dragon"; //用户dbsa登录系统,当前用户为dbsa
set security level 3 to user1; //设置用户最大安全级为<3,{}>
/*(2)设置用户允许安全级*/
set allow security level <3,{"r11"}> on table1 to user1;//设置用户在表table1上的允许安全级
set allow security level <2,{"r11"}> on table1 to user1;//设置用户在表table1上的允许安全级
/*(3)设置用户当前安全级*/
set current security level <3,{"r11"}> on table1 to user1; //设置用户在表table1上的当前安全级
set current security level <2,{"r11"}> on table1 to user1; //设置用户在表table1上的当前安全级
/*(4)设置表安全级*/
connect user1 to db1 with password "dragon"; //用户user1登录系统,当前用户为user1
set security level <1,{"r21","r22","r23"}> on table2; //用户user1设置表table2的表安全级
/*(5)插入记录*/
insert into table1 //向表table1中插入几条记录
values(1(<1,{"r11"}>), "one"(<1,{"r11"}>), 1(<1,{"r11"}>));
insert into table1
values(2(<2,{}>), "two"(<2,{"r11"}>), 2(<2,{}>));
insert into table1
values(3(<2,{}>), "three"(<2,{}>), 3(<2,{}>));
insert into table1 //主关键字的安全级必须小于非主关键字的安全级
values(4(<2,{}>), "four"(<2,{}>), 4(<1,{}>));
insert into table1 //非主关键字的安全级必须小于表的安全级
values(4(<3,{}>), "four"(<3,{}>), 4(<3,{}>));
insert into table1 //非主关键字的安全级必须小于用户的当前安全级
values(4(<2,{"r11","r12"}>), "four"(<2,{}>), 4(<2,{"r11","r12"}>));
/*演示多实例元组*/
set current security level <1,{"r11"}> on table1 to user1;//降低用户user1在表table1中的当前安全级
insert into table1 //主关键字相同,且存在一个元素的安全级大于用户当前安全级,形成多实例
values(2(<1,{}>), "two"(<1,{"r11"}>), 2(<1,{}>));
insert into table1 //主关键字相同,但不存在一个元素的安全级大于用户当前安全级,拒绝插入
values(1, "one", 1);//不指明安全级的话,缺省等于用户当前安全级
set current security level <2,{"r11"}> on table1 to user1;//恢复user1在table1中的当前安全级
insert into table2 //向表table2中插入几条记录
values(1(<1,{}>), "first"(<1,{"r21"}>), 10(<1,{}>));
insert into table2
values(2(<1,{}>), "second"(<1,{}>), 11(<1,{}>));
insert into table2
values(3(<1,{"r21"}>), "third"(<1,{"r21","r22"}>), 12(<1,{"r21"}>));
connect user2 to db1 with password "dragon"; //用户user2连接到数据库db1上
insert into table1 values(4, "four", 4); //如果有一个列不能插入,则全部失败
insert into table1(a1) values(4); //插入一条记录
insert into table1(a2) values("four"); //主键列不能为空
/*(6)更新记录*/
connect user1 to db1 with password "dragon"; //用户user1连接到数据库db1上
set current security level <1,{"r11"}> on table1 to user1;//降低用户user1在表table1中的当前安全级
update table1 //用户只能看见受他当前安全级支配的属性列
set a2="new"(<1,{}>)
where a1=2;
set current security level <2,{}> on table1 to user1;//设置用户user1在表table1中的当前安全级
update table1 //若存在被更的属性的安全级不受用户当前安全级支配,则保留原记录,并插入新记录
set a2="new"
where a1=2;
/*(7)查询记录*/
set current security level <2,{}> on table1 to user1;//设置user1在table1中的当前安全级
select * from table1; //符合安全视图模型
set current security level <2,{}> on table1 to user2;//设置user2在table1中的当前安全级
del user user2 from role1; //把用户user2从角色role1中删除,以显示带谓词的查询
connect user2 to db1 with password "dragon"; //用户user2连接到数据库db1上
select a2 from table1; //带有谓词条件的查询
connect user1 to db1 with password "dragon"; //用户user1连接到数据库db1上
set current security level <1,{}> on table2 to user1;//设置user1在table2中的当前安全级
select a1,a2,a3,table2.b2,table2.b3 //多个表连接时先求出用户在各表上的视图
from table1, table2
where a1=table2.b1;
/*(8)删除记录*/
delete from table1 //如果不是每个属性列的安全级都受用户当前安全级支配,则保留该元组
where a3>1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -