role之精解:来自puber西瓜

上一篇 / 下一篇  2008-05-20 23:49:24

查看( 262 ) / 评论( 11 )
ROLE(角色)是一个命了名的一组权限的集合。
角色可以grant给任何其他的用户或者角色,但不能grant给它自己,角色不能循环授权。角色不属于任何schema,但它在数据字典中有描述。
role 没有schema(即owener),所以对于SYS建立的role,其他的用户也可以对其进行操作。

创建角色
create role testrole
create role testrole identified by role123  --激活role的时候需要进行口令验证
create role testrole identified externally  --设置为外部认证,对role 激活的用户必须是通过外部认证的用户


修改角色
alter role testrole1 not identified;
alter role testrole1 identified externally;
alter role testrole1 identified by xxx

分配角色
grant role1,role2 to {user|role|public},{user|role|public} [with admin option];
grant testrole to username with admin option ;
参数PUBLIC: Grants the role to all users
参数max_enabled_roles指定了数据库中可以激活的最大的角色数。如果用户没有with admin option参数传递权限,但拥有grant any role的权限,同样可以给其他用户grant和revoke 权限。

设置缺省的role
alter user xxx default role role1,role2,...
alter user xxx default role all
alter user xxx default role all expect role1,...
设置了缺省的role之后,在登陆时将自动激活,而且忽略role的口令。
alter user xxx default role none;取消所有的default role

创建应用程序角色(Application Roles)
create role admin_role identified using hr.employees;
在这里,admin_role是一个应用程序角色,只能被定义在hr.employee PL/SQL package中模块激活。

激活角色
set role [role1 identified by passwd1,role2 identified by passwd2]|
[all except role1]|none

set role testrole 激活角色
set role all
set role all except role1,...
set role none
set role testrole1 identified by xxx

权限回收
REVOKE role [,role] FROM {user|role|PUBLIC} [,{user|role|PUBLIC}]
revoke testrole1 from username ;
revoke testrole1 from public ;
权限回收的时候,with admin option传递出去的权限也全部被回收

系统权限可以传递,但对象权限不能传递
grant select on authours to testrole with grant option ;
这个语句将无法执行。

删除ROLE
drop role testrole; --所有用户或者权限中包含的该角色被回收删除


获取角色信息
dba_roles:列举数据库中存在的所有角色
dba_role_privs:roles granted to the users and roles
role_role_privs:roles that are granted to roles
dba_sys_privs:system privileges granted to users and roles
role_sys_privs:system privileges granted to roles
role_tab_privs:object privileges granted to roles
session_roles:roles enabled for current user


ORACLE预定义的角色

角色        创建者        包含的权限说明
connect        sql.bsq        alter session,create cluster,
create database link,create sequence,
create session,create synonym,
create table,create view
resource        sql.bsq        create cluster,create indextype,
craate operator, create procedure,
create sequence ,create table ,
create trigger ,create type
dba        sql.bsq        所有带有with admin option的系统权限
exp_full_database        catexp.sql        提供执行完整的和增量的数据库输出所需要的权限,包括:select any table,
backup any table,execute any procedure,
execute any type,administer resource manager
和对表sys.incvid,sys.incfil,sys.incexp进行insert,delete和update,也包含下面的角色:
execute_catalog_role
select_catalog_role
imp_full_database        catexp.sql        提供执行完整的数据库输入所需要的权限,包含一个系统全县的扩展表(使用视图权限表dba_sys_privs来查询这些权限)和如下角色:
execute_catalog_role
select_catalog_role
delele_catalog_role        sql.bsq        提供对审计表(aud$)的delete权限
execute_catalog_role        sql.bsq        提供执行在数据字典中的对象的execute权限,还包括角色hs_admin_role
select_catalog_role        sql.bsq        提供选取在数据字典中的对象的select权限,还包括角色hs_admin_role
recovery_catalog_owner        catalog.sql        为恢复目录的拥有者提供权限,包括:
create session,alter session,create synonym,
create view,create database link,
create table,create cluster,
create sequence,create trigger
create procedure
hs_admin_role        caths.sql        用来防止对HS(异构服务)数据字典表(授予select)和包(授予execute)的访问,它被授予select_catalog_role和execute_catalog_role角色,使得有一般数据字典访问权限的用户都可以访问HS数据字典。



4.1 权限分类
权限可分为系统权限和对象权限。

4.2 系统权限
1)权限举例
category        examples
index        Create any index
                Alter any index
                Drop any index
table        Create table
                Create any table
                Alter any table
                Drop any table
               Select any table
                Update any table
               Delete any table
session        Create session
               Alter session
              Restricted session
tablespace        Create tablespace
                                Alter tablespace
                                 Drop tablesapce
                                Unlimited tablespace
create table
create any table
这两个权限是不一样的,后者可以创建其他的用户的table
常规权限授予:
grant connect,resource,dba to user;

2)sysdba 和 sysoper两个权限的区别
Category        examples
sysoper        Startup |Shutdown
                Alter database open|mount
                Alter database backup controlfile to
                Revoke database
                Alter database archivelog|noarchivelog
                Restricted session
sysdba        Sysoper privileges with admin option
                Create database
                Alter tablespace begin/end backup
                Restricted session
                Revoke database until
3)参数o7_DICTIONARY_ACCESSIBILITY
如果该参数设置为TRUE,则用户被分配了select any table的权限后,用户就可以查看属于SYS用户的关于数据字典的相关视图等。
如果该参数设置为FALSE,则用户即使被分配select any table的权限,也不能察看数据字典视图。只有以CONNECT AS SYSDBA的用户的才能够查看。
其他的drop any table权限也类似。实际上,这个就是保护数据字典的一个参数,限制ANY权限在数据字典上的操作。

4)权限的授予
Grant create table to public ; --这就意味着将create table权限赋给所有人。
grant create table to test1 with admin option ;  --test1获得将create table传递给第三个人的权限。

5)系统权限的传递性:
sys ->(grant to) user1 ->(grant to) user2
权限传递之后,sys <-(revoke from) user1  ,user1的权限被回收,但user2的不受影响。
4.3 对象权限
只有该对象的owner(schema)才能将权限传递给其他人。
GRANT { object_privilege [(column_list)]
         [, object_privilege [(column_list)] ]...
         |ALL [PRIVILEGES]}
ON        [schema.]object
TO        {user|role|PUBLIC}[, {user|role|PUBLIC} ]...
        [WITH GRANT OPTION]

REVOKE { object_privilege
                [, object_privilege ]...
                | ALL [PRIVILEGES] }
ON        [schema.]object
FROM {user|role|PUBLIC}
                [, {user|role|PUBLIC} ]...
                [CASCADE CONSTRAINTS]

对象权限的传递性:
original schema ->(grant to) user1 ->(grant to) user2
权限传递之后,original schema <-(revoke from) user1  ,user1的权限被回收,user2的权限也被收回。

TAG:

wisdomone1的个人空间 wisdomone1 发布于2008-05-20 23:50:03
再次谢谢西瓜.
五“宅”一生发布于2008-05-21 00:18:15
谢谢西瓜,谢谢楼主。
luo_813的个人空间 luo_813 发布于2008-05-21 00:37:29
谢谢西瓜
学习了
以后的好好总结
wisdomone1的个人空间 wisdomone1 发布于2008-05-21 02:30:40
转自puber(我的猪窝)
表整理之:move表或索引汇总
缘起一个表空间太大,删除数据后由于文件尾被用,无法resize,打算把所有表空间上的对象move到一个临时存储的表空间做整理。

写procedure用utl_file来自动生成整理脚本,麻烦......

move一个表到另外一个表空间时,索引不会跟着一起move,而且会失效。(LOB类型例外)
表move,我们分为:
*普通表move
*分区表move
*LONG,LOB大字段类型move
来进行测试和说明。

索引的move,我们通过rebuild来实现



SQL> select * from v$version;

Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
PL/SQL Release 9.2.0.6.0 - Production
CORE 9.2.0.6.0 Production
TNS for Solaris: Version 9.2.0.6.0 - Production
NLSRTL Version 9.2.0.6.0 - Production


一:move普通表、索引
基本语法:
alter table tab_name move tablespace tbs_name;

move过的普通表,在不用到失效的索引的操作语句中,语句执行正常,但如果操作的语句用到了索引(主键当做唯一索引),则此时报告用到

的索引失效,语句执行失败,其他如外键,非空约束,缺省值等不会失效。

我们需要重新创建主键或索引,基本语法为:
alter index index_name rebuild;
alter index pk_name rebuild;

如果我们需要move索引,则使用rebuild语法:
alter index index_name rebuild tablespace tbs_name;
alter index pk_name rebuild tablespace tbs_name;


提示:
查询表所具有的索引,可以使用user_indexes视图(索引和主键都在这个视图里可找到)。


二:move分区表及索引
和普通表一样,索引会失效,区别的仅仅是语法而已。

分区基本语法:
特别提醒注意,如果是单级分区,则使用关键字PARTITION,如果是多级分区,则使用SUBPARTITION替代PARTITION。
如果分区或分区索引比较大,可以使用并行move或rebuild,PARALLEL (DEGREE 2);
如:
ALTER TABLE PART_ALARMTEXTDATA move SUBPARTITION ALARMTEXTDATA_050910_ATD01 TABLESPACE users PARALLEL (DEGREE 2);
ALTER INDEX GLOBAL_ALARMTEXTDATA REBUILD tablespace users PARALLEL (DEGREE 2);
ALTER INDEX LOCAL_ALARMTEXTDATA REBUILD SUBPARTITION ALARMTEXTDATA_050910_ATD01 TABLESPACE users PARALLEL (DEGREE 2);


移动表的某个分区:
================
ALTER TABLE tab_name move PARTITION partition_name TABLESPACE tbs_name;

重建全局索引:
================
ALTER INDEX global_index REBUILD;

ALTER INDEX global_index REBUILD tablespace tbs_name;

重建局部索引:
================
ALTER TABLE tab_name MODIFY PARTITION partition_name REBUILD UNUSABLE LOCAL INDEXES;

ALTER INDEX local_index_name REBUILD PARTITION partition_name TABLESPACE tbs_name;


提示:
USER_PART_TABLES
USER_IND_PARTITIONS
USER_IND_SUBPARTITIONS
USER_LOB_PARTITIONS
USER_LOB_SUBPARTITIONS
USER_PART_INDEXES
USER_PART_LOBS
可查询分区相关内容,同时,分区对象,也是segment,所以也可在dba_segments里查的到。


三:move LONG,LOB类型
据说DBMS_REDEFINITION包可以提供一些方便,没用过。

I:LONG类型
long类型不能通过MOVE来传输
特别提示,尽量不要用LONG类型,特难管理。
参考:http://www.anysql.net/2005/12/long_vs_lob.html
1,LONG不能使用insert into ... select ...等带select的模式。

create table t123 (id int,en long);

insert into t123(id,en) select * from t123;
报告错误,可以用pl/sql来帮助解决,如:
declare
cursor cur_t123 is select * from t123;
use_t123 cur_t123%rowtype;
begin

open cur_t123;
loop
fetch cur_t123 into use_t123;
exit when cur_t123%notfound;

insert into t123(id,en) values (use_t123.id,use_t123.en);

end loop;
close cur_t123;

end;
/


对有LONG类型字段的表的转移,可以使用:
create新表的方法。
* create一个新的表,存储在需要转移的表空间。
* 创建新的索引(使用tablespace 子句指定新的表空间)。
* 把数据转移过来
方法一:用COPY的方法:
copy from bigboar/bigboar@bigboar_sid insert t123(id,en) using select id,en from t123;
方法二:PL/SQL(如上)
方法三:直接就把LONG转换成CLOB类型
create table t321(id int,en clob) tablespace users;
insert into t321(id,en) select id,to_lob(en) from t123;
方法四:exp/imp
exp bigboar/bigboar file=a.dat tables=t123
imp bigboar/bigboar file=a.dat full=y IGNORE =y
* drop掉旧表。
* rename 新表为旧表表名。


II:LOB类型
在建立含有lob字段的表时,oracle会自动为lob字段建立两个单独的segment,一个用来存放数据(segment_type=LOBSEGMENT),另一个用来存

放索引(segment_type=LOBINDEX)。默认它们会存储在和表一起的表空间。
我们对表MOVE时,LOG类型字段和该字段的索引不会跟着MOVE,必须要单独来进行MOVE,语法如下如:
alter table t321 move tablespace users;
alter table t321 move lob(en) store as (tablespace users);
xiaodong_1567的个人空间 xiaodong_1567 发布于2008-05-21 08:06:27
thanks
Alienovo的个人空间 Alienovo 发布于2008-05-21 09:07:09
谢谢分享!
carcase发布于2008-05-21 09:56:48
总结的不错
阿日的个人空间 阿日 发布于2008-05-21 10:31:23
不错,我想问一下
我按move的方式进行操作步骤如下
在表空间fttest中建立了一个表 test1,并建立了相关索引
SQL> connect fttest/fttest
已连接。
SQL> create table test1(id int,name varchar2(10));
SQL> alter table test1 add constraint pk_test1 primary key(id);
表已更改。
SQL> create index idx1 on test1(id,name);
索引已创建。

利用move命令
SQL> connect /as sysdba
已连接。
SQL> alter table fttest.test1 move tablespace ftitem10g;
表已更改。
SQL> connect ftitem/ftitem;
已连接。
SQL> select * from test1;
select * from test1
              *
第 1 行出现错误:
ORA-00942: 表或视图不存在

SQL> connect fttest/fttest
已连接。
SQL> alter table test1 move tablespace ftitem10g;
表已更改。
SQL> select table_name,tablespace_name from user_tables where table_name='TEST1';

TABLE_NAME                     TABLESPACE_NAME
------------------------------ ------------------------------
TEST1                          FTITEM10G
SQL> select index_name,table_name from user_indexes where table_name='TEST1';
INDEX_NAME                     TABLE_NAME
------------------------------ ------------------------------
PK_TEST1                       TEST1
IDX1                           TEST1

SQL> alter index PK_TEST1 rebuild;
索引已更改。
SQL> alter index IDX1 rebuild;

索引已更改。

但为什么我查询其表所属的表空间已经变成FTITEM10G了,可我在pl/SQL来查看表仍在FTEST表空间中呢而没有在FTITEM10G表空间中,望大家指点,我初学
likaiabc的个人空间 likaiabc 发布于2008-05-21 11:10:16
谢谢
Oracle的琐碎生活 yuxuan 发布于2008-05-21 12:32:14
谢谢分享
蚊子窝 foxmile 发布于2008-05-21 14:02:04
咋没见西瓜自己整这么个玩意。
我来说两句

(可选)

日历

« 2008-10-11  
   1234
567891011
12131415161718
19202122232425
262728293031 

我的存档

数据统计

  • 访问量: 368
  • 日志数: 30
  • 建立时间: 2008-04-04
  • 更新时间: 2008-04-04

RSS订阅

Open Toolbar