七、权限管理
用户属性、权限、授权/回收是本单元的主要内容。
数据库中存在预定义用户主要分为三类:管理用户(sys,system),内置用户,示例用户。
12.1 用户属性
12.1.1 用户属性包括:
- 验证方式
- 口令
- 默认表空间:创建的表或者索引放入此,如果用户没有指定就放入到数据库的默认表空间。
- 默认临时表空间:当排序或散列操作数据超过PGA大小,将其数据放入此。
- 表空间配额:这个表空间能够使用的最大空间限制。
- 锁定状态(是否处于锁定状态)
- 口令状态(是否过期)
12.1.2创建用户与修改用户属性,删除用户。代码示例:
创建用户:
//创建用户 首先保证在system用户下
create user law identified by password //创建law用户密码为password
default tablespace users //指定默认表空间为users
temporary tablespace temp //临时表空间为temp
quota 100m on users //users表空间配额100mb
quota unlimited on test; //test表空间的配额:无限制。
注意!用户创建后,因为还没对用户赋予相应的权限,这个用户还不能对数据库进行任何操作。
修改用户属性:
alter user law indentified by newpassword; //修改用户law密码为newpassword
alter user law default tablespace test; //修改用户law默认表空间为test
alter user law temporary tablespace temp1; //修改用户law临时表空间为temp1
alter user law quota 200m on users; //修改users表空间配额为200mb
alter user law account lock/unlock; //锁定/解锁law用户
删除用户:
删除用户时,如果用户还未创建数据库对象时,可以使用"drop user username"来删除,如果用户创建了数据库对象需要使用"drop user username cascade"来删除。代码示例:
drop user law; //删除用户law,law未创建数据库
drop user scott cascade; //删除创建了数据库的用户scott
补充一些操作:
conn system/password;
select username from dba_user
where rownum < 6; //查询数据库中所有的用户名称(where子句含义:名字长度小于6个字符)
12.2权限管理
12.2.1 权限种类
主要分为系统权限与对象权限。
系统权限:
create session权限:可以连接数据库。
create table权限:可以创建表。
any权限:select any table 则用户可以在任意用户中使用表。
unlimited tablespace权限:用户可以使用所有的表空间,空间大小不受限制。
sysdba权限:用户的权限等级与sys相同,可以执行任何操作。
sysoper权限:执行基本的管理任务,但是不能执行创建和删除数据库,不能修改数据库字符,不能查看用户数据,不能执行数据库恢复。
grant create session to law; //赋予law连接数据库的权限
grant select on scott.emp to law; //授予law用户查询scott的emp表的权限
对象权限:alter(修改表结构),delete,execute,index(添加索引),insert(添加行数据),references(引用表),select,update(更新列值)。
12.2.2 授权与撤销权限操作
代码示例:
//系统权限的授权与撤销权限代码
//授予权限
grant create session,create table
to scott,law; //对scott和law授权连接数据库与创建表的权限
grant createsession
to scott with admin option; //授权scott连接数据库同时可以把这个权限再授予给其他用户。
//撤销权限
revoke create session,create table
from scott,law; //回收law和scott的连接数据库与创建表的权限
//赋予对象权限 基本语法 grant 对象权限 on 存在的用户的表 to 用户
grant alter on scott.emp to law; //授予law用户改变emp表结构的权限
grant index on scott.emp to law; //授予law用户在emp表创建索引的权限
grant references on scott.emp to law; //授予law用户引用emp表的权限,即可以把外键指向emp表
grant update(sal) on scott.emp to law with grant option;
//授权law用户可以修改emp的sal列值,同时可以把权限授予其他用户。
注意with admin option 月with grant option一个针对系统权限,一个针对对象权限(近似于传递一级一级给下面用户权限,级联)。同时系统权限不能级联撤销。比如用户A把权限P授权给B时附带了with admin option,用户B又把权限P给了C,那么A从B撤销权限P时,C的权限P不会被撤销。
补充:
//查询当前连接用户权限信息
desc dba_sys_privs
//查询scott用户的系统权限
select grantee,privilege
from dba_sys_privs
where grantee='SCOTT';
Comments NOTHING