语法:
GRANT USER user_name IDENTIFIED BY password
[DEFAULT TABLESPACE default_tablespace]
[TEMPORARY TABLESPACE temporary_tablespace]
user_name指定要创建的数据库的名称
password 指定数据库用户的密码
default_tablespace指定存储数据库对象时使用的默认表空间
temporary_tablespace指定保存临时对象时使用的默认表空间
例子:
如果要运行,需要以特权身份连接到数据库
create user temp identified by temp
default tablespace temp001
temporary tablespace temp002;
注意:表中必须有temp001和temp002才可以。
查看数据库中所有表空间
select tablespace_name from dba_tablespaces
授权:
GRANT CREATE SESSION TO temp;
使用下面这条语句
ALTER USER temp IDENTIFIED BY green;
CONNECT temp/green
PASSWORD
Changing password for TEMP
Old password: *******
New password: *******
Retype new password: *******
Password changed;
CONNECT temp/green
DROP USER temp;
注意:如果要删除的用户模式中包含任何表或其他项,就必须在drop user语句中,再要删除的用户名后面加上关键字cascade。但是这样做的时候,要保证其他用户不需要访问这些对象。
常用特权:
create session: | 连接到数据库 |
---|---|
create sequence | 创建序列。 |
create synonym | 创建同义词 |
create table | 在用户模式中创建表 |
create any table | 在任何模式中创建表 |
drop table | 删除用户模式中的表 |
drop any table | 删除任何模式中的表 |
create procedure | 创建存储过程 |
execute any procedure | 执行在任何模式下的存储过程 |
create user | 创建用户 |
drop user | 删除用户 |
create view | 创建视图 |
GRANT CREATE SESSION, CREATE USER, CREATE TABLE TO temp;
加上WITH ADMIN OPTION,就可以把这种特权再授权给其他用户
GRANT EXECUTE ANY PROCEDURE TO temp WITH ADMIN OPTION
通过授权给PUBLIC,就是所有用户有了某种特权
CONNECT temp/green
GRANT EXECUTE ANY PROCEDURE TO PUBLIC;
CONNECT temp/green
select username,provilege,admin_option
from user_sys_privs
order by username, privilege;
USER PRIVILEGE ADM
-------------------------------------------
PUBLIC EXECUTE ANY PROCEDURE NO
TEMP CREATE SESSION NO
TEMP CREATE USER NO
......
CONNECT temp/green
REVOKE EXECUTE ANY PROCEDURE FROM temp;
常用的对象特权:
SELECT | 执行查询操作 |
---|---|
INSERT | 执行插入操作 |
UPDATE | 执行更新操作 |
DELETE | 执行删除操作 |
EXECUTE | 执行存储过程 |
create user test001 identified by test001;
conncet test001/test001
grant select, insert, update on test001.test to temp;
查询某个用户对那些表向其他用户开放了哪些对象特权
select grantee, table_name, grantor, privilege, grantable, hierarchy
from user_tab_privs_made
where table_name='test';
查询某个用户对哪些列对象开放了哪些特权
select grantee, table_name, column_name, grantor, privilege, grantable
from user_col_privs_made
order by column_name;
查询某个用户被授予了哪些表上的哪些对象特权
select owner, table_name, grantor, privilege, grantable, hierarchy
from user_tab_privs_recd
order by table_name, privilege;
查询某个用户被授予了哪些列的对象特权
select owner, table_name, column_name, grantor, privilege, grantable
from user_col_privs_recd;
同义词:同义词用于引用其他模式中的表。
创建同义词
connect system/system
grant create synonym to test001;
connect temp/green
create synonym testTemp for temp.test;
如果其他用户使用temp模式下的test表,直接使用同义词testTemp就可以
例如:
select * from testTemp;
创建公共同义词
所有的用户都可以看到这个同义词
connect system/system
grant create public synonym to temp;
connect temp/green
create public synonym testTemp for temp.test;
注意:虽然所有用户都可以看到这个同义词,但是只有当用户具有test表的select特权,才可以使用同义词,否则会报错
撤销用户的对象特权
revoke insert on test from temp;
角色就是一组特权
connect system/system
grant create role to vivi;
grant create user to vivi with admin option;
connect vivi/vivivi
create role type_manager;
create role overall_manger identified by manager_password;
grant select, insert, update, delete on test to type_manager;
grant create user to type_manager;
grant type_manager to overall_manger;
connect system/system
create user Zoey identified by zoey;
grant create session to zoey;
grant type_manager to zoey;
connect Zoey/zoey
select username, granted_role, admin_option, default_role
from user_role_privs;
select role, privilege, admin_optionfrom role_sys_privsorder by role, privilege;
select role, owner, table_name, column_name, privilege, grantablefrom role_tab_privswhere role=type_manager;
set role type_manager;
修改角色不再为默认角色
alter user zoey default role all except type_manager;
revoke type_manager from zoey;
revoke all on test from type_manager;
drop role type_manager;
connect system/system
grant audit system to zoey;
grant audit any to zoey;
audit create table;
audit select table by zoey;
audit update table by zoey whenever not successful;
noaudit update on zoey.test;