由于工作原因很长一段时间没有使用oracle,最近花了点时间学习下,把自己所学的记录下来以便日后巩固(以10g为例)。
一、ORACLE 安装和卸载
(1)ORACLE安装可以去百度上搜下只要按着步骤基本上都能安装成功。卸载稍微麻烦点需要删注册表方法网上也有很多。
(2)ORACLE安装成功后会自动创建两个用户sys和system。
1、sys用户是超级用户,具有最高权限,具有dba的角色和sysdba、sysoper数据库管理权限,所有 oracle 的数据字典的基表和视图都存放在sys 用户中,这些基表和视图对于 oracle 的运行是至关重要的,由数据库自己维护,任何用户都不能手动更改,sys有创建create database的权限。
2、system用户是管理操作员,权限一样很大具有dba角色,没有创建create database的权限。(对于用户和权限的详细区别后面内容有提到)
二、登录ORACLE的方式
(1)通过ORALCE自带的sql plus方式登录(用户名、密码、主机字符串(数据库实例))。
(2)通过window的cmd登录(sqlplus 用户名/密码)。
(3)通过isqlplus登录http://localhost:5560/isqlplus/
(4)通过oracle的企业管理器(oem oracle enterprise manager)。
(5)通过第三方工具PL/SQL DEVELOPER。
三、ORACLE用户管理
1、创建用户
SQL> conn system/orcl
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as system
SQL> create user test identified by test;
User created
SQL>
2、给用户修改密码
SQL> alter user system identified by scott1;
User altered
3、删除用户
SQL> drop user test;
User dropped
注意如果想删除用户对应的数据对象时(如表)需要用加cascade 如drop user test cascade
4、创建用户并登录数据库(授权test登录角色connect)。
SQL> create user test identified by test;
User created
SQL> grant connect to test;
Grant succeeded
SQL> conn test/test;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as test
SQL> show user
User is "test"
5、授权用户对象权限(用system用户为test授权可select或update或delete或all操作scott用户的emp表)
SQL> conn system/orcl;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as system
SQL> grant select on scott.emp to test;
Grant succeeded
SQL> conn test/test;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as test
SQL> select * from scott.emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980-12-17 1800.00 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981-02-22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981-04-02 2975.00 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981-05-01 2850.00 30
7782 CLARK MANAGER 7839 1981-06-09 2450.00 10
7788 SCOTT ANALYST 7566 1987-04-19 3000.00 20
7839 KING PRESIDENT 1981-11-17 5000.00 10
7844 TURNER SALESMAN 7698 1981-09-08 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987-05-23 1100.00 20
7900 JAMES CLERK 7698 1981-12-03 950.00 30
7902 FORD ANALYST 7566 1981-12-03 3000.00 20
7934 MILLER CLERK 7782 1982-01-23 1300.00 34.56 10
14 rows selected
注:(1)grant select可换insert、update、delete、all。
(2)同样可以授权数据对象(表或视图)的某列值给指定的用户,如果是select授权某列值给指定的用记时使用创建视图然后把视图授权给指定的用户
(注:网上有好多使用grant select on emp(ename,sal) to test;授权,这在9i下可以成功,但在9i以后不允许授予select某列的权限,但可以授insert ,update某列的权限)
SQL> conn scott/scott
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> create view v_test as
2 select ename,sal from emp;
create view v_test as
select ename,sal from emp
ORA-01031: 权限不足
SQL> conn system/orcl;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as system
SQL> grant create view to scott;
Grant succeeded
SQL> conn scott/scott
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> create view v_test as
2 select ename,sal from emp;
View created
SQL> grant v_test to test;
grant v_test to test
ORA-01919: 角色 'V_TEST' 不存在
SQL> grant select on v_test to test;
Grant succeeded
SQL> select * from v_test;
ENAME SAL
---------- ---------
SMITH 1800.00
ALLEN 1600.00
WARD 1250.00
JONES 2975.00
MARTIN 1250.00
BLAKE 2850.00
CLARK 2450.00
SCOTT 3000.00
KING 5000.00
TURNER 1500.00
ADAMS 1100.00
JAMES 950.00
FORD 3000.00
MILLER 1300.00
14 rows selected
授权test用户只能修改sal字段。
SQL> grant update(sal) on emp to test;
Grant succeeded
授权test用户只能插入empno和empname字段。
SQL> grant insert(empno,ename) on emp to test;
Grant succeeded
6、收回用户对应权限。(用scott用户登录收回上面的v_test查询的权限和修改sal字段权限和插入empno,ename的权限)
SQL> CONN SCOTT/SCOTT
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> REVOKE SELECT ON V_TEST FROM TEST;
Revoke succeeded
SQL> REVOKE UPDATE(SAL) ON EMP FROM TEST;
REVOKE UPDATE(SAL) ON EMP FROM TEST
ORA-01750: UPDATE/REFERENCES 只能从整个表而不能按列 REVOKE
SQL> REVOKE UPDATE ON EMP FROM TEST;
Revoke succeeded
SQL> REVOKE INSERT ON EMP FROM TEST;
Revoke succeeded
注:注意上面的错误提示不能对表的某列revoke只能对整个表revoke。
7、权限的传递。(用SCOTT用户登录授权test用户select查询v_test的权限并允许test用户拥有把此权限传递授予test1用户)
SQL> CONN SCOTT/SCOTT
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as scott
SQL> GRANT SELECT ON V_TEST TO TEST WITH GRANT OPTIONS;
GRANT SELECT ON V_TEST TO TEST WITH GRANT OPTIONS
ORA-00994: 缺失 OPTION 关键字
SQL> GRANT SELECT ON V_TEST TO TEST WITH GRANT OPTION;
Grant succeeded
SQL> CONN TEST/TEST
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as test
SQL> GRANT SELECT ON V_TEST TO TEST1;
GRANT SELECT ON V_TEST TO TEST1
ORA-00942: 表或视图不存在
SQL> GRANT SELECT ON SCOTT.V_TEST TO TEST1;
Grant succeeded
注:(1)权限的传递只需要在授权时加上with grant option就能让被授权的用户将些权限授予其他用户。
(2)如果授权的是系统权限(上面操作的都是对象权限)传递给其他人时只需要加上with admin option。
(3)此时如果scott用户把test的查询v_test权限收回将一同把test1的查询v_test权限收回。
8、使用profile管理用户口令。
profile 是口令限制,资料限制的命令集合,当建立数据库时,ORACLE会自动建立名称为default的profile,当建立用记没有指定profile选项,寻oracle会将default分配给用户。
(设定test用户最多只能尝试2次登录,锁定时间为3天)
SQL> conn system/orcl
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as system
SQL> create profile lock_account limit failed_login_attempts 2 password_lock_time 3;
Profile created
SQL> alter user test profile lock_account;
User altered
通过下面对用户强制解锁
SQL> alter user test account unlock;
User altered
(设定用户定期修改密码,每隔10天修改自己的密码宽限期为3天)
SQL> create profile remind_account limit password_life_time 10 password_grace_time 3;
Profile created
SQL> alter user test profile remind_account;
User altered
删除直接drop profile remind_account;