一、用户操作
1、创建用户:createuser xiaoming identified by m123;
注:必须具有dba权限,而且密码必须以字母开头。
2、修改密码:
1、如果给自己修改密码可以直接使用:password用户名
2、如果给别人修改密码则需要具有dba的权限,或是拥有alter user的系统权限:alteruser 用户名identified by 新密码
3、删除用户:drop user用户名【cascade】
注:以dba的身份去删除某个用户,如果用其它用户去删除用户则需要具有drop user的权限。如果要删除的用户,已经创建了表,那么就需要在删除的时候带一个参数cascade。
4、oracle在安装的时候默认会创建3个用户:sys/change_on_install、system/manager、scott/tiger.
5、查看用户的状态:select username, account_status from dba_users where username='CC';
二、权限分配
权限分为2类:
系统权限:用户对数据库访问的相关权限,比如:connect、dba、resource(在表空间建表权限)。
对象权限:用户对其他用户的数据对象操作的权限,比如select、insert、update、delete、all、create index。
数据对象:表、视图、表空间、过程、函数等。
角色:权限的批量授权。
预定义角色:数据库在安装时就已经有的角色。
自定义角色:自己定义的角色。
例如:
1、 让小明能在表空间建表权限:
grant resourceto xiaoming;
2、 (System、sys、scott)让小明能查询scott的emp表权限:
SQL>grant select on scott.empto xiaoming;
Grant succeeded
SQL> conn xiaoming/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaoming
SQL> select * from scott.emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
3、 收回小明的权限
conn scott/123456
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> revoke select on scott.emp from xiaoming;
Revoke succeeded
SQL> conn xiaoming/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaoming
SQL> select * from scott.emp;
select * from scott.emp
ORA-00942:表或视图不存在
4、权限的传递
希望xiaoming用户可以去查询scott的emp表/还希望xiaoming可以把这个权限继续给别人。
如果是对象权限,就加入 with grant option
SQL> show user;
User is "scott"
SQL> grant select on scott.emp to xiaoming with grant option;
Grant succeeded
SQL> conn system/123456;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS
SQL> create user xiaohong identified by m123;
User created
SQL> grant connect to xiaohong;
Grant succeeded
SQL> conn xiaoming/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaoming
SQL> grant select on scott.emp to xiaohong;
Grant succeeded
SQL> conn xiaohong/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaohong
SQL> select * from scott.emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
如果是对象权限,就加入 with admin option
SQL> conn system/123456@ORCL as sysdba;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS
SQL> grant connect to xiaoming with admin option;
Grant succeeded
SQL> create user xiaowang identified by m123;
User created
SQL> conn xiaoming/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaoming
SQL> grant connect to xiaowang;
Grant succeeded
SQL> conn xiaowang/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaowang
5、权限的回收
scott把xiaoming对emp表的查询权限回收,小红也无法访问emp表。
SQL> conn scott/123456;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> revoke select on scott.emp from xiaoming;
Revoke succeeded
SQL> conn xiaohong/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaohong
SQL> select * from scott.emp;
select * from scott.emp
ORA-00942:表或视图不存在
三、profile管理用户口令
profile是口令限制,资源限制的命令集合,当建立数据库的,oracle会自动建立名称为default的profile。当建立用户没有指定profile选项,那么oracle就会将default分配给用户。
1、账户锁定:指定该账户(用户)登陆时最多可以输入密码的次数,也可以指定用户锁定的时间(天)一般用dba的身份去执行该命令。
SQL> conn system/123456@ORCL as sysdba;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS
SQL> create profile lock_account limit failed_login_attempts3 password_lock_time 2;
Profile created
SQL> alter user xiaoming profile lock_account;
User altered
2、账号解锁:
SQL> conn system/123456@ORCL as sysdba;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS
SQL> alter user xiaoming account unlock;
User altered
SQL> conn xiaoming/m123;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as xiaoming
3、终止口令
为了让用户定期修改密码可以使用终止口令的指令来完成,同样这个命令也需要dba的身份来操作。
SQL> conn system/123456@ORCL as sysdba;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS
SQL> create profile myprofile limit password_life_time10 password_grace_time 2;
Profile created
SQL> alter user xiaoming profile myprofile;
User altered
5、 口令历史
如果希望用户在修改密码时,不能使用以前使用过的密码,可使用口令历史,oracle会将口令修改的信息存放到数据字典中,当用户修改密码时,oracle就会对新旧密码进行比较,当发现新旧密码一样时,就提示用户重新输入密码。
SQL> create profile myprofile2 limit password_life_time10 password_grace_time 2 password_reuse_time 10;
Profile created
SQL> alter user xiaoming profile myprofile2;
User altered
6、 删除profile
drop profile password_history【cascade】
注:文件删除后,用这个文件去约束的那些用户通通也都被释放了。加了cascade,就会把级联的相关东西也给删除掉。
SQL> drop profile myprofile2 cascade;
Profile dropped