用户管理
创建用户
创建的新用户是没有任何权限的,甚至连登陆的数据库的权限都没有,需要为其指定相应的权限。
SQL> create user username
identified by password
default tablespace tablespace
temporary tablespace tablespace
profile profile
quota integer/unlimited on tablespace;
示例:
SQL> create user mydog
identified by mydog // 如果密码是数字,请用双引号括起来
default tablespace account
temporary tablespace temp
profile default
quota 50m on account;
SQL> grant connect, resource to mydog;
相关视图
查询用户缺省表空间、临时表空间
SQL> select username, default_tablespace, temporary_tablespace from dba_users;
查询系统资源文件名
SQL> select * from dba_profiles;
profile使用
SQL> select username, profile, default_tablespace, temporary_tablespace from dba_users;
SQL> create profile my_profile limit
failed_login_attempts 5
idle_time 5;
SQL> alter user mydog profile my_profile;
修改用户
SQL> alter user 用户名
identified 口令
default tablespace tablespace
temporary tablespace tablespace
profile profile
quota integer/unlimited on tablespace;
示例:
1.修改口令字:alter user mydog identified by "123456";
2.修改用户缺省表空间:alter user mydog default tablespace users;
3.修改用户临时表空间:alter user mydog temporary tablespace temp_data;
4.强制用户修改口令字:alter user mydog password expire;
5.将用户加锁:alter user mydog account lock; // 加锁
6.将用户解锁:alter user mydog account unlock; // 解锁
删除用户
SQL>drop user 用户名; //用户没有建任何实体
SQL>drop user 用户名 CASCADE; // 将用户及其所建实体全部删除
// 当前正连接的用户删除不了,需要先把session kill掉
查询用户会话
1、查询用户会话信息:select username, sid, serial#, machine from v$session;
2、删除用户会话信息:alter system kill session 'sid, serial#';
3、查询用户SQL语句: select user_name, sql_text from v$open_cursor;
用户权限
权限分类
- 系统权限:系统规定用户使用数据库的权限。(系统权限是对用户而言)
- 实体权限:某种权限用户对其它用户的表或视图的存取权限。(是针对表或视图而言的)
系统权限管理
系统权限分类:
- DBA: 拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构。
- RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构。
- CONNECT:拥有Connect权限的用户只可以登录Oracle,不可以创建实体,不可以创建数据库结构。
对于普通用户:授予connect, resource权限。 对于DBA管理用户:授予connect,resource, dba权限。
系统权限授权命令
SQL> grant connect, resource, dba to 用户名1 [,用户名2]...;
示例
SQL> connect system/manager
SQL> Create user mydog identified by mydog;
SQL> grant connect, resource to mydog;
查询用户拥有哪里权限
SQL> select * from dba_role_privs; --where grantee='SYS';
SQL> select * from dba_sys_privs;
SQL> select * from role_sys_privs; --where role='DBA';
系统权限传递:
增加WITH ADMIN OPTION选项,则得到的权限可以传递。
SQL> grant connect, resorce to mydog with admin option; //可以传递所获权限。
系统权限回收:系统权限只能由DBA用户回收
SQL> Revoke connect, resource from mydog;
- 系统权限无级联,即A授予B权限,B授予C权限,如果A收回B的权限,C的权限不受影响;
- 系统权限可以跨用户回收,即A可以直接收回C用户的权限。
实体权限管理
实体权限分类:
- select, update, insert, alter, index, delete, all //all包括所有权限
- execute //执行存储过程权限
user01:
SQL> grant select, update, insert on product to user02;
SQL> grant all on product to user02;
user02:
SQL> select * from user01.product; // 此时user02查user_tables,不包括user01.product这个表,但如果查all_tables则可以查到,因为它可以访问。
将表的操作权限授予全体用户:
SQL> grant all on product to public; // public表示是所有的用户,这里的all权限不包括drop。
实体权限数据字典
SQL> select owner, table_name from all_tables; // 用户可以查询的表
SQL> select table_name from user_tables; // 用户创建的表
SQL> select grantor, table_schema, table_name, privilege from all_tab_privs; // 获权可以存取的表(被授权的)
SQL> select grantee, owner, table_name, privilege from user_tab_privs; // 授出权限的表(授出的权限)
实体权限传递(with grant option):
user01:
SQL> grant select, update on product to user02 with grant option; // user02得到权限,并可以传递。
实体权限回收:
user01:
SQL>Revoke select, update on product from user02; //传递的权限将全部丢失。