ORACLE循序渐进讲解

版权所有,禁止匿名转载;禁止商业使用。

由于工作原因很长一段时间没有使用oracle,最近花了点时间学习下,把自己所学的记录下来以便日后巩固(以10g为例)。


一、ORACLE 安装和卸载


(1)ORACLE安装可以去百度上搜下只要按着步骤基本上都能安装成功。卸载稍微麻烦点需要删注册表方法网上也有很多。


(2) ORACLE安装成功后会自动创建两个用户sys和system。


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> <strong>conn</strong> system/orcl
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 
Connected as system

SQL> <strong>create user</strong> test <strong>identified by</strong> test;
User created

SQL>

2、给用户修改密码

SQL> <strong>alter user</strong> system<strong> identified by</strong> scott1;
User altered

3、删除用户

SQL><strong> drop user </strong>test;
User dropped

注意如果想删除用户对应的数据对象时(如表)需要用加cascade 如drop user test cascade

4、创建用户并登录数据库(授权test登录角色connect)。

SQL> <strong>create user</strong> test<strong> identified by</strong> test;
User created

SQL> <strong>grant</strong> connect <strong>to</strong> test;
Grant succeeded

SQL> conn test/test;
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 
Connected as test

SQL> <strong>show user</strong>

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> <strong>grant</strong> select <strong>on </strong>scott.emp <strong>to </strong>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> <strong>create view </strong>v_test <strong>as</strong>
  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> <strong>grant </strong>create view <strong>to </strong>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> <strong>grant </strong>v_test <strong>to </strong>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> <strong>grant </strong>update(sal) <strong>on </strong>emp <strong>to </strong>test;

Grant succeeded

授权test用户只能插入empno和empname字段。

SQL> <strong>grant </strong>insert(empno,ename) <strong>on </strong>emp <strong>to </strong>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> <strong>REVOKE </strong>SELECT <strong>ON </strong>V_TEST <strong>FROM </strong>TEST;
Revoke succeeded

SQL> <strong>REVOKE </strong>UPDATE(SAL) <strong>ON </strong>EMP <strong>FROM </strong>TEST;
REVOKE UPDATE(SAL) ON EMP FROM TEST
<span style="color:#ff0000;">ORA-01750: UPDATE/REFERENCES 只能从整个表而不能按列 REVOKE</span>
SQL> <strong>REVOKE </strong>UPDATE <strong>ON </strong>EMP <strong>FROM </strong>TEST;
Revoke succeeded
SQL> <strong>REVOKE </strong>INSERT <strong>ON </strong>EMP <strong>FROM </strong>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> <strong>GRANT </strong>SELECT <strong>ON </strong>V_TEST <strong>TO </strong>TEST <strong>WITH GRANT OPTIONS</strong>;
GRANT SELECT ON V_TEST TO TEST WITH GRANT OPTIONS
<span style="color:#ff0000;">ORA-00994: 缺失 OPTION 关键字</span>

SQL> <strong>GRANT </strong>SELECT <strong>ON </strong>V_TEST <strong>TO </strong>TES<span style="background-color: rgb(255, 255, 255);">T <strong>WITH GRANT OPTION</strong></span>;
Grant succeeded

SQL> CONN TEST/TEST
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 
Connected as test

SQL> <strong>GRANT </strong>SELECT <strong>ON </strong>V_TEST <strong>TO </strong>TEST1;
GRANT SELECT ON V_TEST TO TEST1
<span style="color:#ff0000;">ORA-00942: 表或视图不存在</span>

SQL> <strong>GRANT </strong>SELECT <strong>ON </strong>SCOTT.V_TEST <strong>TO </strong>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> <strong>create profile</strong> lock_account <strong>limit failed_login_attempts</strong> 2 <strong>password_lock_time </strong>3;
Profile created

SQL> <strong>alter user</strong> test <strong>profile </strong>lock_account;
User altered
<span style="color:#ff0000;">通过下面对用户强制解锁</span>
SQL> <strong>alter user </strong>test <strong>account </strong>unlock;
User altered
(设定用户定期修改密码,每隔10天修改自己的密码宽限期为3天)

SQL> <strong>create profile</strong> remind_account <strong>limit password_life_time</strong> 10 <strong>password_grace_time </strong>3;
Profile created

SQL> <strong>alter user</strong> test <strong>profile </strong>remind_account;
User altered

删除直接drop profile remind_account;


0 0