oracle_命令
/*============================================================================
*Author: MartriWang@gmail.com
*Date: 09/05/2007
*Description:ORACLE Summary
*==========================================================================*/
汇总:
set autotrace on
set autotrace traceonly explain
set timing on
或通过SQL*PLUS trace,然后查看user_dump_dest下的跟踪文件,使用tkprof工具格式化后阅览。
alter session set events '10046 trace name context forever,level 12';
alter session set events '10046 trace name context off';
SELECT p.spid,s.username FROM v$session s,v$process p WHERE s.audsid=USERENV('sessionid') AND s.paddr = p.addr;
使用方法示例:
DBserver% sqlplus perf/perf
SQL*Plus: Release 9.2.0.6.0 - Production on Mon Oct 17 14:32:29 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6.0 - Production
SQL> set timing on
SQL> set autotrace on
SQL> select count(*) from perf_sdcch_nn where start_time = (select max(start_time) from perf_sdcch_nn);
COUNT(*)
----------
638
Elapsed: 00:00:00.80
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=8)
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'IDX02_PERF_SDCCH_NN' (NON-UNIQUE)
(Cost=2 Card=1495 Bytes=11960)
3 2 SORT (AGGREGATE)
4 3 INDEX (FULL SCAN (MIN/MAX)) OF 'IDX02_PERF_SDCCH_NN'
(NON-UNIQUE) (Cost=1 Card=3852090 Bytes=30816720)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
492 bytes sent via SQL*Net to client
656 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
从上面的示例我们可以看到,该SQL语句执行花了0.8秒,select语句和inline view,都使用了名为'IDX02_PERF_SDCCH_NN'的索引
物理读为0,redo log size为0,没有生成REDO日志。
一、ORACLE的启动和关闭
1、在单机环境下
要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下
su - oracle
a、启动ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>startup
SVRMGR>quit
b、关闭ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>shutdown
SVRMGR>quit
启动oracle9i数据库命令:
$ sqlplus /nolog
SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
SQL> connect / as sysdba
Connected to an idle instance.
SQL> startup^C
SQL> startup
ORACLE instance started.
--MartriWang@gmail.com 10/05/2007--
create pfile='/u01/oracle/dbs/init.ora'
from spfile='u01/oracle/dbs/Spfile.ora';
create Spfile='Spfile_name'
FROM PFILE='pfile_name';
orapwd file=<filename> password=<password> entries=<max_users>
2、在双机环境下
要想启动或关闭ORACLE系统必须首先切换到root用户,如下
su - root
a、启动ORACLE系统
hareg -y oracle
b、关闭ORACLE系统
hareg -n oracle
--MartriWang@gmail.com 22/05/2007--
--查找、删除重复记录:
法一: 用Group by语句 此查找很快的
select count(num), max(name) from student --查找表中num列重复的,列出重复的记录数,并列出他的name属性
group by num
having count(num) >;1 --按num分组后找出表中num列重复,即出现次数大于一次
delete from student(上面Select的)
这样的话就把所有重复的都删除了。-----慎重
法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:
---- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录
SELECT * FROM EM5_PIPE_PREFAB
WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D --D相当于First,Second
WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND
EM5_PIPE_PREFAB.DSNO=D.DSNO);
---- 执行下面SQL语句后就可以刪除所有DRAWING和DSNO相同且重复的记录
DELETE FROM EM5_PIPE_PREFAB
WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D
WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND
EM5_PIPE_PREFAB.DSNO=D.DSNO);
Oracle数据库有哪几种启动方式
说明:
有以下几种启动方式:
1、startup nomount
非安装启动,这种方式启动下可执行:
重建控制文件、重建数据库
读取init.ora文件,启动instance,即启动SGA和后台进程,这种启动只需要init.ora文件。
2、startup mount dbname
安装启动,这种方式启动下可执行:
数据库日志归档、
数据库介质恢复、
使数据文件联机或脱机,
重新定位数据文件、重做日志文件。
执行“nomount”,然后打开控制文件,确认数据文件和联机日志文件的位置,
但此时不对数据文件和日志文件进行校验检查。
3、startup open dbname
先执行“nomount”,然后执行“mount”,再打开包括Redo log文件在内的所有数据库文件,
这种方式下可访问数据库中的数据。
4、startup,等于以下三个命令
startup nomount
alter database mount
alter database open
5、startup restrict
约束方式启动
这种方式能够启动数据库,但只允许具有一定特权的用户访问
非特权用户访问时,会出现以下提示:
ERROR:
ORA-01035: ORACLE 只允许具有 RESTRICTED SESSION 权限的用户使用
6、startup force
强制启动方式
当不能关闭数据库时,可以用startup force来完成数据库的关闭
先关闭数据库,再执行正常启动数据库命令
7、startup pfile=参数文件名
带初始化参数文件的启动方式
先读取参数文件,再按参数文件中的设置启动数据库
例:startup pfile=E:Oracleadminoradbpfileinit.ora
8、startup EXCLUSIVE
--
二、用户如何有效地利用数据字典
ORACLE的数据字典是数据库的重要组成部分之一,它随着数据库的产生而产生, 随着数据库的变化而变化,
体现为sys用户下的一些表和视图。数据字典名称是大写的英文字符。
数据字典里存有用户信息、用户的权限信息、所有数据对象信息、表的约束条件、统计分析数据库的视图等。
我们不能手工修改数据字典里的信息。
很多时候,一般的ORACLE用户不知道如何有效地利用它。
dictionary 全部数据字典表的名称和解释,它有一个同义词dict
dict_column 全部数据字典表里字段名称和解释
如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句:
SQL>select * from dictionary where instr(comments,'index')>0;
如果我们想知道user_indexes表各字段名称的详细含义,可以用下面这条SQL语句:
SQL>select column_name,comments from dict_columns where table_name='USER_INDEXES';
依此类推,就可以轻松知道数据字典的详细名称和解释,不用查看ORACLE的其它文档资料了。
下面按类别列出一些ORACLE用户常用数据字典的查询使用方法。
1、用户
查看当前用户的缺省表空间
SQL>select username,default_tablespace from user_users;
查看当前用户的角色
SQL>select * from user_role_privs;
查看当前用户的系统权限和表级权限
SQL>select * from user_sys_privs;
SQL>select * from user_tab_privs;
2、表
查看用户下所有的表
SQL>select * from user_tables;
查看名称包含log字符的表
SQL>select object_name,object_id from user_objects
where instr(object_name,'LOG')>0;
查看某表的创建时间
SQL>select object_name,created from user_objects where object_name=upper('&table_name');
查看某表的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&table_name');
查看放在ORACLE的内存区里的表
SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;
3、索引
查看索引个数和类别
SQL>select index_name,index_type,table_name from user_indexes order by table_name;
查看索引被索引的字段
SQL>select * from user_ind_columns where index_name=upper('&index_name');
查看索引的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&index_name');
4、序列号
查看序列号,last_number是当前值
SQL>select * from user_sequences;
5、视图
查看视图的名称
SQL>select view_name from user_views;
查看创建视图的select语句
SQL>set view_name,text_length from user_views;
SQL>set long 2000; 说明:可以根据视图的text_length值设定set long 的大小
SQL>select text from user_views where view_name=upper('&view_name');
6、同义词
查看同义词的名称
SQL>select * from user_synonyms;
7、约束条件
查看某表的约束条件
SQL>select constraint_name, constraint_type,search_condition, r_constraint_name
from user_constraints where table_name = upper('&table_name');
SQL>select c.constraint_name,c.constraint_type,cc.column_name
from user_constraints c,user_cons_columns cc
where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')
and c.owner = cc.owner and c.constraint_name = cc.constraint_name
order by cc.position;
8、存储函数和过程
查看函数和过程的状态
SQL>select object_name,status from user_objects where object_type='FUNCTION';
SQL>select object_name,status from user_objects where object_type='PROCEDURE';
查看函数和过程的源代码
SQL>select text from all_source where owner=user and name=upper('&plsql_name');
三、查看数据库的SQL
1、查看表空间的名称及大小
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;
2、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
3、查看回滚段名称及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name ;
4、查看控制文件
select name from v$controlfile;
5、查看日志文件
select member from v$logfile;
6、查看表空间的使用情况
select sum(bytes)/(1024*1024) as free_space,tablespace_name
from dba_free_space
group by tablespace_name;
SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;
7、查看数据库库对象
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;
8、查看数据库的版本
Select version FROM Product_component_version
Where SUBSTR(PRODUCT,1,6)='Oracle';
9、查看数据库的创建日期和归档方式
Select Created, Log_Mode, Log_Mode From V$Database;
四、ORACLE用户连接的管理
用系统管理员,查看当前数据库有几个用户连接:
SQL> select username,sid,serial# from v$session;
如果要停某个连接用
SQL> alter system kill session 'sid,serial#';
如果这命令不行,找它UNIX的进程数
SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr;
说明:21是某个连接的sid数
然后用 kill 命令杀此进程号。
五、SQL*PLUS使用
a、近入SQL*Plus
$sqlplus 用户名/密码
退出SQL*Plus
SQL>exit
b、在sqlplus下得到帮助信息
列出全部SQL命令和SQL*Plus命令
SQL>help
列出某个特定的命令的信息
SQL>help 命令名
c、显示表结构命令DESCRIBE
SQL>DESC 表名
d、SQL*Plus中的编辑命令
显示SQL缓冲区命令
SQL>L
修改SQL命令
首先要将待改正行变为当前行
SQL>n
用CHANGE命令修改内容
SQL>c/旧/新
重新确认是否已正确
SQL>L
使用INPUT命令可以在SQL缓冲区中增加一行或多行
SQL>i
SQL>输入内容
e、调用外部系统编辑器
SQL>edit 文件名
可以使用DEFINE命令设置系统变量EDITOR来改变文本编辑器的类型,在login.sql文件中定义如下一行
DEFINE_EDITOR=vi
f、运行命令文件
SQL>START test
SQL>@test
常用SQL*Plus语句
a、表的创建、修改、删除
创建表的命令格式如下:
create table 表名 (列说明列表);
为基表增加新列命令如下:
ALTER TABLE 表名 ADD (列说明列表)
例:为test表增加一列Age,用来存放年龄
sql>alter table test
add (Age number(3));
修改基表列定义命令如下:
ALTER TABLE 表名
MODIFY (列名 数据类型)
例:将test表中的Count列宽度加长为10个字符
sql>alter atble test
modify (County char(10));
b、将一张表删除语句的格式如下:
DORP TABLE 表名;
例:表删除将同时删除表的数据和表的定义
sql>drop table test
c、表空间的创建、删除
--MartriWang@gmail.com 15/06/2007--
d. 删除表列
ALTER TABLE table_name DROP COLUMN 字段名 --del 表中字段
1: create table t2 as select <specific columns> from t1; --以第一个表中的某列建立第二个新表
2: drop table t1; --删除表1
3: rename t2 to t1; --把表2改名为表1
Oracle 8i及以上版本中,可以使用以下语句
alter table 表1 drop column 列1;
六、ORACLE逻辑备份的SH文件
完全备份的SH文件:exp_comp.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=complete file=/oracle/export/db_comp$rq.dmp"
累计备份的SH文件:exp_cumu.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=cumulative file=/oracle/export/db_cumu$rq.dmp"
增量备份的SH文件: exp_incr.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=incremental file=/oracle/export/db_incr$rq.dmp"
root用户crontab文件
/var/spool/cron/crontabs/root增加以下内容
0 2 1 * * /oracle/exp_comp.sh
30 2 * * 0-5 /oracle/exp_incr.sh
45 2 * * 6 /oracle/exp_cumu.sh
当然这个时间表可以根据不同的需求来改变的,这只是一个例子。
七、ORACLE 常用的SQL语法和数据对象
一.数据控制语句 (DML) 部分
1.INSERT (往数据表里插入记录的语句)
INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……);
INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名;
字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’
如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''.
字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验.
日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒
或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’)
TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC.
年-月-日 小时:分钟:秒 的格式YYYY-MM-DD HH24:MI:SS
INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型,
方法借用ORACLE里自带的DBMS_LOB程序包.
INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号
CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1
MAXVALUE 99999 CYCLE NOCACHE;
其中最大的值按字段的长度来定, 如果定义的自动增长的序列号 NUMBER(6) , 最大值为999999
INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL
2.DELETE (删除数据表里记录的语句)
DELETE FROM表名 WHERE 条件;
注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused.
如果确实要删除一个大表里的全部记录, 可以用 TRUNCATE 命令, 它可以释放占用的数据块表空间
TRUNCATE TABLE 表名;
此操作不可回退.
3.UPDATE (修改数据表里记录的语句)
UPDATE表名 SET 字段名1=值1, 字段名2=值2, …… WHERE 条件;
如果修改的值N没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验;
值N超过定义的长度会出错, 最好在插入前进行长度校验..
注意事项:
A. 以上SQL语句对表都加上了行级锁,
确认完成后, 必须加上事物处理结束的命令 COMMIT 才能正式生效,
否则改变不一定写入数据库里.
如果想撤回这些操作, 可以用命令 ROLLBACK 复原.
B. 在运行INSERT, DELETE 和 UPDATE 语句前最好估算一下可能操作的记录范围,
应该把它限定在较小 (一万条记录) 范围内,. 否则ORACLE处理这个事物用到很大的回退段.
程序响应慢甚至失去响应. 如果记录数上十万以上这些操作, 可以把这些SQL语句分段分次完成,
其间加上COMMIT 确认事物处理.
二.数据定义 (DDL) 部分
1.CREATE (创建表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
ORACLE常用的字段类型有
CHAR 固定长度的字符串
VARCHAR2 可变长度的字符串
NUMBER(M,N) 数字型M是位数总长度, N是小数的长度
DATE 日期类型
创建表时要把较小的不为空的字段放在前面, 可能为空的字段放在后面
创建表时可以用中文的字段名, 但最好还是用英文的字段名
创建表时可以给字段加上默认值, 例如 DEFAULT SYSDATE
这样每次插入和修改时, 不用程序操作这个字段都能得到动作的时间
创建表时可以给字段加上约束条件
例如 不允许重复 UNIQUE, 关键字 PRIMARY KEY
2.ALTER (改变表, 索引, 视图等)
改变表的名称
ALTER TABLE 表名1 TO 表名2;
在表的后面增加一个字段
ALTER TABLE表名 ADD 字段名 字段名描述;
修改表里字段的定义描述
ALTER TABLE表名 MODIFY字段名 字段名描述;
给表里的字段加上约束条件
ALTER TABLE 表名 ADD CONSTRAINT 约束名 PRIMARY KEY (字段名);
ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE (字段名);
把表放在或取出数据库的内存区
ALTER TABLE 表名 CACHE;
ALTER TABLE 表名 NOCACHE;
3.DROP (删除表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
删除表和它所有的约束条件
DROP TABLE 表名 CASCADE CONSTRAINTS;
4.TRUNCATE (清空表里的所有记录, 保留表的结构)
TRUNCATE 表名;
三.查询语句 (SELECT) 部分
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 条件;
字段名可以带入函数
例如: COUNT(*), MIN(字段名), MAX(字段名), AVG(字段名), DISTINCT(字段名),
TO_CHAR(DATE字段名,'YYYY-MM-DD HH24:MI:SS')
NVL(EXPR1, EXPR2)函数
解释:
IF EXPR1=NULL
RETURN EXPR2
ELSE
RETURN EXPR1
DECODE(AA﹐V1﹐R1﹐V2﹐R2....)函数
解释:
IF AA=V1 THEN RETURN R1
IF AA=V2 THEN RETURN R2
..…
ELSE
RETURN NULL
LPAD(char1,n,char2)函数
解释:
字符char1按制定的位数n显示,不足的位数用char2字符串替换左边的空位
字段名之间可以进行算术运算
例如: (字段名1*字段名1)/3
查询语句可以嵌套
例如: SELECT …… FROM
(SELECT …… FROM表名1, [表名2, ……] WHERE 条件) WHERE 条件2;
两个查询语句的结果可以做集合操作
例如: 并集UNION(去掉重复记录), 并集UNION ALL(不去掉重复记录), 差集MINUS, 交集INTERSECT
分组查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] GROUP BY字段名1
[HAVING 条件] ;
两个以上表之间的连接查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名 [ AND ……] ;
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名(+) [ AND ……] ;
有(+)号的字段位置自动补空值
查询结果集的排序操作, 默认的排序是升序ASC, 降序是DESC
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……]
ORDER BY字段名1, 字段名2 DESC;
字符串模糊比较的方法
INSTR(字段名, ‘字符串’)>0
字段名 LIKE ‘字符串%’ [‘%字符串%’]
每个表都有一个隐含的字段ROWID, 它标记着记录的唯一性.
四.ORACLE里常用的数据对象 (SCHEMA)
1.索引 (INDEX)
CREATE INDEX 索引名ON 表名 ( 字段1, [字段2, ……] );
ALTER INDEX 索引名 REBUILD;
一个表的索引最好不要超过三个 (特殊的大表除外), 最好用单字段索引, 结合SQL语句的分析执行情况,
也可以建立多字段的组合索引和基于函数的索引
ORACLE8.1.7字符串可以索引的最大长度为1578 单字节
ORACLE8.0.6字符串可以索引的最大长度为758 单字节
--MartriWang@gmail.com 10/05/2007--
(1)*Tree索引。
Create index indexname on tablename(columnname[columnname...])
(2)反向索引。
Create index indexname on tablename(columnname[columnname...]) reverse
(3)降序索引。
Create index indexname on tablename(columnname DESC[columnname...])
(4)位图索引。
Create BITMAP index indexname on tablename(columnname[columnname...])
(5)函数索引。
Create index indexname on tablename(functionname(columnname))
注意:创建索引后分析要索引才能起作用。
analyze index indexname compute statistics;
2.视图 (VIEW)
CREATE VIEW 视图名AS SELECT …. FROM …..;
ALTER VIEW视图名 COMPILE;
视图仅是一个SQL查询语句, 它可以把表之间复杂的关系简洁化.
3.同义词 (SYNONMY)
CREATE SYNONYM同义词名FOR 表名;
CREATE SYNONYM同义词名FOR 表名@数据库链接名;
4.数据库链接 (DATABASE LINK)
CREATE DATABASE LINK数据库链接名CONNECT TO 用户名 IDENTIFIED BY 密码 USING ‘数据库连接字符串’;
--MartriWang@gmail.com 10/05/2007--
create database mynewdb
user sys IDENTIFIED BY sys_id
user system IDENTIFIED BY system_id
LOGFILE GROUP 1 ('/u01/oracle/oradata/mynewdb/redo01.log') SIZE 100M,
GROUP 2 ('/u01/oracle/oradata/mynewdb/redo02.log') SIZE 100M,
GROUP 3 ('/u01/oracle/oradata/mynewdb/redo03.log') SIZE 100M
MAXLOGFILES 5
MAXLOGMEMBERS 5
MAXLOGHISTORY 1
MAXINSTANCES 100
MAXINSTANCES 1
CHARACTER SET US7ASCII
NATIONAL CHARACTER SET AL16UTF16
DATAFILE '/u01/oracle/oradata/mynewdb/system01.dbf' SIZE 325M REUSE
EXTENT MANAGEMENT LOCAL
SYSAUT DATAFILE '/u01/oracle/oradata/mynewdb/sysaux01.dbf' SIZE 325M REUSE
DEFAULT TABLESPACE tbs_1
DEFAULT TEMPORARY TABLESPACE tempts1
TEMPFILE '/u01/oracle/oradata/mynewdb/temp01.dbf' SIZE 20M REUSE
UNDO TABLESPACE undotbs
DATAFILE '/u01/oracle/oradata/mynewdb/undotbs01.dbf'
SIZE 20M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
--MartriWang@gmail.com 10/05/2007--
emctl start dbconsole
emctl stop dbconsole
--http://servername:port/em
--path:$ORACLE_HOME/install/portlist.ini
isqlplusctl start
isqlplusctl stop
--http://servername:port/isqlplus
数据库连接字符串可以用NET8 EASY CONFIG或者直接修改TNSNAMES.ORA里定义.
数据库参数global_name=true时要求数据库链接名称跟远端数据库名称一样
数据库全局名称可以用以下命令查出
SELECT * FROM GLOBAL_NAME;
查询远端数据库里的表
SELECT …… FROM 表名@数据库链接名;
五.权限管理 (DCL) 语句
1.GRANT 赋于权限
常用的系统权限集合有以下三个:
CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理)
常用的数据对象权限有以下五个:
ALL ON 数据对象名, SELECT ON 数据对象名, UPDATE ON 数据对象名,
DELETE ON 数据对象名, INSERT ON 数据对象名, ALTER ON 数据对象名
GRANT CONNECT, RESOURCE TO 用户名;
GRANT SELECT ON 表名 TO 用户名;
GRANT SELECT, INSERT, DELETE ON表名 TO 用户名1, 用户名2;
2.REVOKE 回收权限
REVOKE CONNECT, RESOURCE FROM 用户名;
REVOKE SELECT ON 表名 FROM 用户名;
REVOKE SELECT, INSERT, DELETE ON表名 FROM 用户名1, 用户名2;
查询数据库中第63号错误:
select orgaddr,destaddr from sm_histable0116 where error_code='63';
查询数据库中开户用户最大提交和最大下发数: select MSISDN,TCOS,OCOS from ms_usertable;
查询数据库中各种错误代码的总和:
select error_code,count(*) from sm_histable0513 group by error_code order
by error_code;
查询报表数据库中话单统计种类查询。
select sum(Successcount) from tbl_MiddleMt0411 where ServiceType2=111
select sum(successcount),servicetype from tbl_middlemt0411 group by servicetype
原文地址:http://www.cnoug.org/viewthread.php?tid=60293
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
--MartriWang@gmail.com 14/05/2007--
1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
2 将数据库中system用户与sys用户的表导出
exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
3 将数据库中的表table1 、table2导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
4 将数据库中的表table1中的字段filed1以"00"打头的数据导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"
--MartriWang@gmail.com 14/05/2007--
1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
2 将数据库中system用户与sys用户的表导出
exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
3 将数据库中的表table1 、table2导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
4 将数据库中的表table1中的字段filed1以"00"打头的数据导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
archive log list
alter system set LOG_ARCHIVE_DEST_1='LOCATION=/oracle/oradata/szdb/archive';
alter system set log_archive_format='%t_%s.dbf' scope=spfile;
alter system set log_archive_start=true scope=spfile;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
archive log list
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
// listener.ora 的条目entry
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc.world)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// tnsnames.ora 的条目
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//连接参数的设置(sql*net)
LOC =(DESCRIPTION=
(ADDRESS=
(COMMUNITY=TCP.HQ.COMPANY)
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
//参数文件配置范例
// tnsnames.ora
HQ =(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// Oracle8I tnsnames.ora
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//使用 COPY 实现数据库之间的复制
copy from
remote_username/remote_password@service_name
to
username/password@service_name
[append|create|insert|replace]
TABLE_NAME
using subquery;
REM COPY example
set copycommit 1
set arraysize 1000
copy from HR/PUFFINSTUFF@loc -
create EMPLOYEE -
using -
select * from EMPLOYEE
//监视器的管理
lsnrctl start
lsnrctl start my_lsnr
lsnrctl status
lsnrctl status hq
检查监视器的进程
ps -ef | grep tnslsnr
//在 lsnrctl 内停止监视器
set password lsnr_password
stop
//在lsnrctl 内列出所有的服务
set password lsnr_password
services
//启动或停止一个NT的listener
net start Oracle<version_name>TNSListener
net stop Oracle<version_name>TNSListener
// tnsnames.ora 文件的内容
fld1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)
(HOST = server1.fld.com)(PORT = 1521))
)
(CONNECT_DATA =
(SID = fld1)
)
)
//操作系统网络的管理
telnet host_name
ping host_name
/etc/hosts 文件
130.110.238.109 nmhost
130.110.238.101 txhost
130.110.238.102 azhost arizona
//oratab 表项
loc:/orasw/app/oracle/product/8.1.5.1:Y
cc1:/orasw/app/oracle/product/8.1.5.1:N
old:/orasw/app/oracle/product/8.1.5.0:Y
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
转贴于 学生大读书网 http://
(KEY= loc.world)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// tnsnames.ora 的条目
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//连接参数的设置(sql*net)
LOC =(DESCRIPTION=
(ADDRESS=
(COMMUNITY=TCP.HQ.COMPANY)
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
//参数文件配置范例
// tnsnames.ora
HQ =(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// Oracle8I tnsnames.ora
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//使用 COPY 实现数据库之间的复制
copy from
remote_username/remote_password@service_name
to
username/password@service_name
[append|create|insert|replace]
TABLE_NAME
using subquery;
REM COPY example
set copycommit 1
set arraysize 1000
copy from HR/PUFFINSTUFF@loc -
create EMPLOYEE -
using -
select * from EMPLOYEE
//监视器的管理
lsnrctl start
lsnrctl start my_lsnr
lsnrctl status
lsnrctl status hq
检查监视器的进程
ps -ef | grep tnslsnr
//在 lsnrctl 内停止监视器
set password lsnr_password
stop
//在lsnrctl 内列出所有的服务
set password lsnr_password
services
//启动或停止一个NT的listener
net start Oracle<version_name>TNSListener
net stop Oracle<version_name>TNSListener
// tnsnames.ora 文件的内容
fld1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)
(HOST = server1.fld.com)(PORT = 1521))
)
(CONNECT_DATA =
(SID = fld1)
)
)
//操作系统网络的管理
telnet host_name
ping host_name
/etc/hosts 文件
130.110.238.109 nmhost
130.110.238.101 txhost
130.110.238.102 azhost arizona
//oratab 表项
loc:/orasw/app/oracle/product/8.1.5.1:Y
cc1:/orasw/app/oracle/product/8.1.5.1:N
old:/orasw/app/oracle/product/8.1.5.0:Y
--MartriWang@gmail.com 11/05/2007--
Oracle优化经典文章------索引原理篇
Oracle提供了大量索引选项。知道在给定条件下使用哪个选项对于一个应用程序的性能来说非常重要。一个错误的选择可能会引发死锁,
并导致数据库性能急剧下降或进程终止。而如果做出正确的选择,则可以合理使用资源,使那些已经运行了几个小时甚至几天的进程在几分钟得以完成,
这样会使您立刻成为一位英雄。这篇文章就将简单的讨论每个索引选项。主要有以下内容:
[1] 基本的索引概念
查询DBA_INDEXES视图可得到表中所有索引的列表,注意只能通过USER_INDEXES的方法来检索模式(schema)的索引。访问USER_IND_COLUMNS视图可得到
一个给定表中被索引的特定列。
[2] 组合索引
当某个索引包含有多个已索引的列时,称这个索引为组合(concatented)索引。在 Oracle9i引入跳跃式扫描的索引访问方法之前,查询只能在有限条
件下使用该索引。比如:表emp有一个组合索引键,该索引包含了empno、ename和deptno。在Oracle9i之前除非在where之句中对第一列(empno)指定一
个值,否则就不能使用这个索引键进行一次范围扫描。
特别注意:在Oracle9i之前,只有在使用到索引的前导索引时才可以使用组合索引!
[3] ORACLE ROWID
通过每个行的ROWID,索引Oracle提供了访问单行数据的能力。ROWID其实就是直接指向单独行的线路图。如果想检查重复值或是其他对ROWID本身的引用,
可以在任何表中使用和指定rowid列。
[4] 限制索引
限制索引是一些没有经验的开发人员经常犯的错误之一。在SQL中有很多陷阱会使一些索引无法使用。下面讨论一些常见的问题:
4.1 使用不等于操作符(<>、!=)
下面的查询即使在cust_rating列有一个索引,查询语句仍然执行一次全表扫描。
select cust_Id,cust_name
from customers
where cust_rating <> 'aa';
把上面的语句改成如下的查询语句,这样,在采用基于规则的优化器而不是基于代价的优化器(更智能)时,将会使用索引。
select cust_Id,cust_name
from customers
where cust_rating < 'aa' or cust_rating > 'aa';
特别注意:通过把不等于操作符改成OR条件,就可以使用索引,以避免全表扫描。
4.2 使用IS NULL 或IS NOT NULL
使用IS NULL 或IS NOT NULL同样会限制索引的使用。因为NULL值并没有被定义。在SQL语句中使用NULL会有很多的麻烦。因此建议开发人员在建表时,
把需要索引的列设成NOT NULL。如果被索引的列在某些行中存在NULL值,就不会使用这个索引(除非索引是一个位图索引,关于位图索引在稍后在详细讨论)。
4.3 使用函数
如果不使用基于函数的索引,那么在SQL语句的WHERE子句中对存在索引的列使用函数时,会使优化器忽略掉这些索引。
下面的查询不会使用索引(只要它不是基于函数的索引)
select empno,ename,deptno
from emp
where trunc(hiredate)='01-MAY-81';
把上面的语句改成下面的语句,这样就可以通过索引进行查找。
select empno,ename,deptno
from emp
where hiredate<(to_date('01-MAY-81')+0.9999);
4.4 比较不匹配的数据类型
比较不匹配的数据类型也是比较难于发现的性能问题之一。
注意下面查询的例子,account_number是一个VARCHAR2类型,在account_number字段上有索引。下面的语句将执行全表扫描。
select bank_name,address,city,state,zip
from banks
where account_number = 990354;
Oracle可以自动把where子句变成to_number(account_number)=990354,这样就限制了索引的使用,改成下面的查询就可以使用索引:
select bank_name,address,city,state,zip
from banks
where account_number ='990354';
特别注意:不匹配的数据类型之间比较会让Oracle自动限制索引的使用,即便对这个查询执行Explain Plan也不能让您明白为什么做了一
次“全表扫描”。
[5] 选择性
使用USER_INDEXES视图,该视图中显示了一个distinct_keys列。比较一下唯一键的数量和表中的行数,就可以判断索引的选择性。选择性越高,
索引返回的数据就越少。
[6] 群集因子(Clustering Factor)
Clustering Factor位于USER_INDEXES视图中。该列反映了数据相对于已索引的列是否显得有序。如果Clustering Factor列的值接近于索引中的树叶块
(leaf block)的数目,表中的数据就越有序。如果它的值接近于表中的行数,则表中的数据就不是很有序。
[7] 二元高度(Binary height)
索引的二元高度对把ROWID返回给用户进程时所要求的I/O量起到关键作用。在对一个索引进行分析后,可以通过查询DBA_INDEXES的B-level列查看它的二元高度。
二元高度主要随着表的大小以及被索引的列中值的范围的狭窄程度而变化。索引上如果有大量被删除的行,它的二元高度也会增加。更新索引列也类似于删除操作,
因为它增加了已删除键的数目。重建索引可能会降低二元高度。
[8] 快速全局扫描
在Oracle7.3后就可以使用快速全局扫描(Fast Full Scan)这个选项。这个选项允许Oracle执行一个全局索引扫描操作。快速全局扫描读取B-树索引上所有树叶块。
初始化文件中的DB_FILE_MULTIBLOCK_READ_COUNT参数可以控制同时被读取的块的数目。
[9] 跳跃式扫描
从Oracle9i开始,索引跳跃式扫描特性可以允许优化器使用组合索引,即便索引的前导列没有出现在WHERE子句中。索引跳跃式扫描比全索引扫描要快的多。
下面的程序清单显示出性能的差别:
create index skip1 on emp5(job,empno);
index created.
select count(*)
from emp5
where empno=7900;
Elapsed:00:00:03.13
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE(Cost=4 Card=1 Bytes=5)
1 0 SORT(AGGREGATE)
2 1 INDEX(FAST FULL SCAN) OF 'SKIP1'(NON-UNIQUE)
Statistics
6826 consistent gets
6819 physical reads
select /*+ index(emp5 skip1)*/ count(*)
from emp5
where empno=7900;
Elapsed:00:00:00.56
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE(Cost=6 Card=1 Bytes=5)
1 0 SORT(AGGREGATE)
2 1 INDEX(SKIP SCAN) OF 'SKIP1'(NON-UNIQUE)
Statistics
21 consistent gets
17 physical reads
[10] 索引的类型
B-树索引
位图索引
HASH索引
索引编排表
反转键索引
基于函数的索引
分区索引
本地和全局索引
==================================================================================================================================================
--MartriWang@gmail.com 11/05/2007--
索引(INDEX)
1.索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据;
Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引;
索引由根节点、分支节点和叶子节点组成,上级索引块包含下级索引块的索引数据,叶节点包含索引数据和确定行实际位置的rowid。
使用索引的目的
加快查询速度
减少I/O操作
消除磁盘排序
何时使用索引
查询返回的记录数
排序表<40%
非排序表 <7%
表的碎片较多(频繁增加、删除)
索引的种类
非唯一索引(最常用)
唯一索引
位图索引
局部有前缀分区索引
局部无前缀分区索引
全局有前缀分区索引
散列分区索引
基于函数的索引
2.管理索引的准则
在表中插入数据后创建索引
。在用SQL*Loader或import工具插入或装载数据后,建立索引比较有效;
索引正确的表和列
。经常检索排序大表中40%或非排序表7%的行,建议建索引;
。为了改善多表关联,索引列用于联结;
。列中的值相对比较唯一;
。取值范围(大:B*树索引,小:位图索引);
。Date型列一般适合基于函数的索引;
。列中有许多空值,不适合建立索引
为性能而安排索引列
。经常一起使用多个字段检索记录,组合索引比单索引更有效;
。把最常用的列放在最前面,例:dx_groupid_serv_id(groupid,serv_id),在where条件中使用groupid或groupid,serv_id,查询将使用索引,若仅用到serv_id字段,则索引无效;
。合并/拆分不必要的索引。
限制每个表索引的数量
。一个表可以有几百个索引(你会这样做吗?),但是对于频繁插入和更新表,索引越多系统CPU,I/O负担就越重;
。建议每张表不超过5个索引。
删除不再需要的索引
。索引无效,集中表现在该使用基于函数的索引或位图索引,而使用了B*树索引;
。应用中的查询不使用索引;
。重建索引之前必须先删除索引,若用alter index … rebuild重建索引,则不必删除索引。
索引数据块空间使用
。创建索引时指定表空间,特别是在建立主键时,应明确指定表空间;
。合理设定pctfress,注意:不能给索引指定pctused;
。估计索引的大小和合理地设置存储参数,默认为表空间大小,或initial与next设置成一样大。
考虑并行创建索引
。对大表可以采用并行创建索引,在并行创建索引时,存储参数被每个查询服务器进程分别使用,例如:initial为1M,并行度为8,则创建索引期间至少要消耗8M空间;
考虑用nologging创建索引
。对大表创建索引可以使用nologging来减少重做日志;
。节省重做日志文件的空间;
。缩短创建索引的时间;
。改善了并行创建大索引时的性能。
3.怎样建立最佳索引
明确地创建索引
create index index_name on table_name(field_name)
tablespace tablespace_name
pctfree 5
initrans 2
maxtrans 255
storage
(
minextents 1
maxextents 16382
pctincrease 0
);
创建基于函数的索引
。常用与UPPER、LOWER、TO_CHAR(date)等函数分类上,例:
create index idx_func on emp (UPPER(ename)) tablespace tablespace_name;
创建位图索引
。对基数较小,且基数相对稳定的列建立索引时,首先应该考虑位图索引,例:
create bitmap index idx_bitm on class (classno) tablespace tablespace_name;
明确地创建唯一索引
。可以用create unique index语句来创建唯一索引,例:
create unique index dept_unique_idx on dept(dept_no) tablespace idx_1;
创建与约束相关的索引
。可以用using index字句,为与unique和primary key约束相关的索引,例如:
alter table table_name
add constraint PK_primary_keyname primary key (field_name)
using index tablespace tablespace_name;
如何创建局部分区索引
。基础表必须是分区表;
。分区数量与基础表相同;
。每个索引分区的子分区数量与相应的基础表分区相同;
。基础表的子分区中的行的索引项,被存储在该索引的相应的子分区中,例如:
Create Index TG_CDR04_SERV_ID_IDX On TG_CDR04(SERV_ID)
Pctfree 5
Tablespace TBS_AK01_IDX
Storage (
MaxExtents 32768
PctIncrease 0
FreeLists 1
FreeList Groups 1
)
local
/
如何创建范围分区的全局索引
。基础表可以是全局表和分区表。
create index idx_start_date on tg_cdr01(start_date)
global partition by range(start_date)
(partition p01_idx vlaues less than (‘0106’)
partition p01_idx vlaues less than (‘0111’)
…
partition p01_idx vlaues less than (‘0401’ ))
/
重建现存的索引
重建现存的索引的当前时刻不会影响查询;
重建索引可以删除额外的数据块;
提高索引查询效率;
alter index idx_name rebuild nologging;
对于分区索引:
alter index idx_name rebuild partition partiton_name nologging;
要删除索引的原因
。不再需要的索引;
。索引没有针对其相关的表所发布的查询提供所期望的性能改善;
。应用没有用该索引来查询数据;
。该索引无效,必须在重建之前删除该索引;
。该索引已经变的太碎了,必须在重建之前删除该索引;
。语句:
drop index idx_name;
drop index idx_name drop partition partition_name;
建立索引的代价
基础表维护时,系统要同时维护索引,不合理的索引将严重影响系统资源,主要表现在CPU和I/O上;
插入、更新、删除数据产生大量db file sequential read锁等待;
4.SQL优化器简介
基于规则的优化器
。总是使用索引
。总是从驱动表开始(from子句最右边的表)
。只有在不可避免的情况下,才使用全表扫描
。任何索引都可以
基于成本的优化器
。需要表、索引的统计资料
Analyze table customer compute statistics;
Analyze table customer estimate statistics sample 5000 rows;
。表中设置并行度、表分区
--MartriWang@gmail.com 21/06/2007--
软件环境:
1. Windows 2000+ORACLE 8.1.7
2. ORACLE安装路径为:C:\ORACLE
实现方法:
1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务。
2. 开始->程序->Oracle - OraHome81->Oracle Installation Products-> Universal Installer 卸装所有Oracle产品,
但Universal Installer本身不能被删除
5. 运行regedit,选择HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE,按del键删除这个入口。
6. 运行regedit,选择HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services,滚动 这个列表,删除所有Oracle入口。
7. 运行refedit,选择HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application, 删除所有Oracle入口。
8. 开始->设置->控制面板->系统->高级->环境变量 删除环境变量CLASSPATH和PATH中有关Oracle的设定
9. 从桌面上、STARTUP(启动)组、程序菜单中,删除所有有关Oracle的组和图标
10.删除\Program Files\Oracle目录
11. 重新启动计算机,重起后才能完全删除Oracle所在目录
12、删除与Oracle有关的文件,选择Oracle所在的缺省目录C:\Oracle,删除这个入口目录及所有子目录,并从Windows 2000目录
(一般为C:\WINNT)下删除以下文件ORACLE.INI、oradim73.INI、oradim80.INI、oraodbc.ini等等。
13. WIN.INI文件中若有[ORACLE]的标记段,删除该段
14. 如有必要,删除所有Oracle相关的ODBC的DSN
15. 到事件查看器中,删除Oracle相关的日志 说明:如果有个别DLL文件无法删除的情况,则不用理会,重新启动,开始新的安装,
安装时,选择一个新的目录,则,安装完毕并重新启动后,老的目录及文件就可以删除掉了
--MartriWang@gmail.com 25/06/2007--
--创建数据表空间
CREATE TABLESPACE TS1
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS1.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS2
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS2.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS3
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS3.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS4
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS4.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS5
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS5.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS6
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS6.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS7
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS7.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS8
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS8.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS9
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS9.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS10
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS10.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS11
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS11.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS12
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS12.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
--创建索引表空间
CREATE TABLESPACE TSI1
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI1.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI2
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI2.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI3
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI3.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI4
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI4.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI5
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI5.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI6
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI6.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI7
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI7.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI8
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI8.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI9
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI9.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI10
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI10.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI11
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI11.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI12
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI12.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
--范围分区表
drop table sales1;
--范围必须越来越大
CREATE TABLE sales1(
year int ,
month int,
day int,
fee number)
PARTITION by range (year,month,day)
(
partition sale1 values less than(1999,02,01) tablespace ts1,
partition sale2 values less than(2000,01,01) tablespace ts2,
partition sale3 values less than(2001,01,01) tablespace ts3,
partition sale4 values less than(2002,01,01) tablespace ts4,
partition sale5 values less than(MAXVALUE,MAXVALUE,MAXVALUE) tablespace ts5
);
--第一种索引
drop index idx_sales1;
create index idx_sales1 on sales1(year);
--第二种索引
drop index idx_sales1;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales1 on sales1(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales1;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales1 ON SALES1 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION aSALE1 tablespace tsi1,
PARTITION aSALE2 tablespace tsi2,
PARTITION aSALE3 tablespace tsi3,
PARTITION aSALE4 tablespace tsi4)
;
insert into sales1 values(1999,01,01,1);
--null 插入到maxvalue表空间。
insert into sales1 values(null,null,null,0);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales1 values(2004,01,01,1);
commit;
--列表分区表
drop table sales2;
--范围必须越来越大
CREATE TABLE sales2(
year int ,
month int,
day int,
fee number)
PARTITION by list (year)
(
partition sale1 values (1999) tablespace ts1,
partition sale2 values (2000) tablespace ts2,
partition sale3 values (2001) tablespace ts3,
partition sale4 values (2002) tablespace ts4,
partition sale5 values (null) tablespace ts5,
partition sale6 values (default) tablespace ts6
);
--第一种索引
drop index idx_sales2;
create index idx_sales2 on sales2(year);
--第二种索引
drop index idx_sales2;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales2 on sales2(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales2;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales2 ON sales2 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION iSALE1 tablespace tsi1,
PARTITION iSALE2 tablespace tsi2,
PARTITION iSALE3 tablespace tsi3,
PARTITION iSALE4 tablespace tsi4,
PARTITION iSALE5 tablespace tsi5)
;
insert into sales2 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales2 values(2004,01,01,1);
insert into sales2 values(null,01,01,1);
commit;
/******************************************************************************/
--1 AAAHmyAALAAAAAXAAA 1999 1 1 1 11 11
--2 AAAHm2AAPAAAAAPAAA 1 1 1 15 15
--3 AAAHm3AAQAAAAAPAAA 2004 1 1 1 16 16
/******************************************************************************/
--散列分区表
drop table sales3;
CREATE TABLE sales3(
year int ,
month int,
day int,
fee number)
PARTITION by hash (year)
partitions 5;
drop table sales3;
--ORA-14170: 不能在 CREATE TABLE|INDEX 中指定 <分区 - 说明> 子句
CREATE TABLE sales3(
year int ,
month int,
day int,
fee number)
PARTITION by hash (year)
--partitions 5
(
partition p1 tablespace ts1,
partition p2 tablespace ts2,
partition p3 tablespace ts3,
partition p4 tablespace ts4,
partition p5 tablespace ts5
);
--第一种索引
drop index idx_sales3;
create index idx_sales3 on sales3(year);
--第二种索引
drop index idx_sales3;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales3 on sales3(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
drop index idx_sales3;
CREATE INDEX idx_sales3 ON sales3 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION SALE1 tablespace tsi1,
PARTITION SALE2 tablespace tsi2,
PARTITION SALE3 tablespace tsi3,
PARTITION SALE4 tablespace tsi4,
PARTITION SALE5 tablespace tsi5)
;
insert into sales3 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales3 values(2004,01,01,1);
insert into sales3 values(null,01,01,1);
commit;
/******************************************************************************/
--1 AAAHmyAALAAAAAXAAA 1999 1 1 1 11 11
--2 AAAHm2AAPAAAAAPAAA 1 1 1 15 15
--3 AAAHm3AAQAAAAAPAAA 2004 1 1 1 16 16
/******************************************************************************/
--组合分区表
drop table sales4;
--组合必须越来越大
CREATE TABLE sales4(
year int ,
month int,
day int,
fee number)
PARTITION by range (year) subpartition by hash(month)
subpartitions 8 store in (ts11,ts12)
(
partition sale1 values less than(1999) tablespace ts1,
partition sale2 values less than(2000) tablespace ts2,
partition sale3 values less than(2001) tablespace ts3,
partition sale4 values less than(2002) tablespace ts4
);
--第一种索引
drop index idx_sales4;
create index idx_sales4 on sales4(year);
--第二种索引
drop index idx_sales4;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales4 on sales4(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales4;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales4 ON sales4 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION aSALE1 tablespace tsi1,
PARTITION aSALE2 tablespace tsi2,
PARTITION aSALE3 tablespace tsi3,
PARTITION aSALE4 tablespace tsi4)
;
insert into sales4 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales4 values(2004,01,01,1);
commit;
转载自:http://blog.csdn.net/wonth/archive/2007/06/28/1670355.aspx
*Author: MartriWang@gmail.com
*Date: 09/05/2007
*Description:ORACLE Summary
*==========================================================================*/
汇总:
set autotrace on
set autotrace traceonly explain
set timing on
或通过SQL*PLUS trace,然后查看user_dump_dest下的跟踪文件,使用tkprof工具格式化后阅览。
alter session set events '10046 trace name context forever,level 12';
alter session set events '10046 trace name context off';
SELECT p.spid,s.username FROM v$session s,v$process p WHERE s.audsid=USERENV('sessionid') AND s.paddr = p.addr;
使用方法示例:
DBserver% sqlplus perf/perf
SQL*Plus: Release 9.2.0.6.0 - Production on Mon Oct 17 14:32:29 2005
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.6.0 - Production
SQL> set timing on
SQL> set autotrace on
SQL> select count(*) from perf_sdcch_nn where start_time = (select max(start_time) from perf_sdcch_nn);
COUNT(*)
----------
638
Elapsed: 00:00:00.80
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=8)
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'IDX02_PERF_SDCCH_NN' (NON-UNIQUE)
(Cost=2 Card=1495 Bytes=11960)
3 2 SORT (AGGREGATE)
4 3 INDEX (FULL SCAN (MIN/MAX)) OF 'IDX02_PERF_SDCCH_NN'
(NON-UNIQUE) (Cost=1 Card=3852090 Bytes=30816720)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
492 bytes sent via SQL*Net to client
656 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
从上面的示例我们可以看到,该SQL语句执行花了0.8秒,select语句和inline view,都使用了名为'IDX02_PERF_SDCCH_NN'的索引
物理读为0,redo log size为0,没有生成REDO日志。
一、ORACLE的启动和关闭
1、在单机环境下
要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下
su - oracle
a、启动ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>startup
SVRMGR>quit
b、关闭ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>shutdown
SVRMGR>quit
启动oracle9i数据库命令:
$ sqlplus /nolog
SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
SQL> connect / as sysdba
Connected to an idle instance.
SQL> startup^C
SQL> startup
ORACLE instance started.
--MartriWang@gmail.com 10/05/2007--
create pfile='/u01/oracle/dbs/init.ora'
from spfile='u01/oracle/dbs/Spfile.ora';
create Spfile='Spfile_name'
FROM PFILE='pfile_name';
orapwd file=<filename> password=<password> entries=<max_users>
2、在双机环境下
要想启动或关闭ORACLE系统必须首先切换到root用户,如下
su - root
a、启动ORACLE系统
hareg -y oracle
b、关闭ORACLE系统
hareg -n oracle
--MartriWang@gmail.com 22/05/2007--
--查找、删除重复记录:
法一: 用Group by语句 此查找很快的
select count(num), max(name) from student --查找表中num列重复的,列出重复的记录数,并列出他的name属性
group by num
having count(num) >;1 --按num分组后找出表中num列重复,即出现次数大于一次
delete from student(上面Select的)
这样的话就把所有重复的都删除了。-----慎重
法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:
---- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录
SELECT * FROM EM5_PIPE_PREFAB
WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D --D相当于First,Second
WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND
EM5_PIPE_PREFAB.DSNO=D.DSNO);
---- 执行下面SQL语句后就可以刪除所有DRAWING和DSNO相同且重复的记录
DELETE FROM EM5_PIPE_PREFAB
WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D
WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND
EM5_PIPE_PREFAB.DSNO=D.DSNO);
Oracle数据库有哪几种启动方式
说明:
有以下几种启动方式:
1、startup nomount
非安装启动,这种方式启动下可执行:
重建控制文件、重建数据库
读取init.ora文件,启动instance,即启动SGA和后台进程,这种启动只需要init.ora文件。
2、startup mount dbname
安装启动,这种方式启动下可执行:
数据库日志归档、
数据库介质恢复、
使数据文件联机或脱机,
重新定位数据文件、重做日志文件。
执行“nomount”,然后打开控制文件,确认数据文件和联机日志文件的位置,
但此时不对数据文件和日志文件进行校验检查。
3、startup open dbname
先执行“nomount”,然后执行“mount”,再打开包括Redo log文件在内的所有数据库文件,
这种方式下可访问数据库中的数据。
4、startup,等于以下三个命令
startup nomount
alter database mount
alter database open
5、startup restrict
约束方式启动
这种方式能够启动数据库,但只允许具有一定特权的用户访问
非特权用户访问时,会出现以下提示:
ERROR:
ORA-01035: ORACLE 只允许具有 RESTRICTED SESSION 权限的用户使用
6、startup force
强制启动方式
当不能关闭数据库时,可以用startup force来完成数据库的关闭
先关闭数据库,再执行正常启动数据库命令
7、startup pfile=参数文件名
带初始化参数文件的启动方式
先读取参数文件,再按参数文件中的设置启动数据库
例:startup pfile=E:Oracleadminoradbpfileinit.ora
8、startup EXCLUSIVE
--
二、用户如何有效地利用数据字典
ORACLE的数据字典是数据库的重要组成部分之一,它随着数据库的产生而产生, 随着数据库的变化而变化,
体现为sys用户下的一些表和视图。数据字典名称是大写的英文字符。
数据字典里存有用户信息、用户的权限信息、所有数据对象信息、表的约束条件、统计分析数据库的视图等。
我们不能手工修改数据字典里的信息。
很多时候,一般的ORACLE用户不知道如何有效地利用它。
dictionary 全部数据字典表的名称和解释,它有一个同义词dict
dict_column 全部数据字典表里字段名称和解释
如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句:
SQL>select * from dictionary where instr(comments,'index')>0;
如果我们想知道user_indexes表各字段名称的详细含义,可以用下面这条SQL语句:
SQL>select column_name,comments from dict_columns where table_name='USER_INDEXES';
依此类推,就可以轻松知道数据字典的详细名称和解释,不用查看ORACLE的其它文档资料了。
下面按类别列出一些ORACLE用户常用数据字典的查询使用方法。
1、用户
查看当前用户的缺省表空间
SQL>select username,default_tablespace from user_users;
查看当前用户的角色
SQL>select * from user_role_privs;
查看当前用户的系统权限和表级权限
SQL>select * from user_sys_privs;
SQL>select * from user_tab_privs;
2、表
查看用户下所有的表
SQL>select * from user_tables;
查看名称包含log字符的表
SQL>select object_name,object_id from user_objects
where instr(object_name,'LOG')>0;
查看某表的创建时间
SQL>select object_name,created from user_objects where object_name=upper('&table_name');
查看某表的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&table_name');
查看放在ORACLE的内存区里的表
SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;
3、索引
查看索引个数和类别
SQL>select index_name,index_type,table_name from user_indexes order by table_name;
查看索引被索引的字段
SQL>select * from user_ind_columns where index_name=upper('&index_name');
查看索引的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&index_name');
4、序列号
查看序列号,last_number是当前值
SQL>select * from user_sequences;
5、视图
查看视图的名称
SQL>select view_name from user_views;
查看创建视图的select语句
SQL>set view_name,text_length from user_views;
SQL>set long 2000; 说明:可以根据视图的text_length值设定set long 的大小
SQL>select text from user_views where view_name=upper('&view_name');
6、同义词
查看同义词的名称
SQL>select * from user_synonyms;
7、约束条件
查看某表的约束条件
SQL>select constraint_name, constraint_type,search_condition, r_constraint_name
from user_constraints where table_name = upper('&table_name');
SQL>select c.constraint_name,c.constraint_type,cc.column_name
from user_constraints c,user_cons_columns cc
where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')
and c.owner = cc.owner and c.constraint_name = cc.constraint_name
order by cc.position;
8、存储函数和过程
查看函数和过程的状态
SQL>select object_name,status from user_objects where object_type='FUNCTION';
SQL>select object_name,status from user_objects where object_type='PROCEDURE';
查看函数和过程的源代码
SQL>select text from all_source where owner=user and name=upper('&plsql_name');
三、查看数据库的SQL
1、查看表空间的名称及大小
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;
2、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
3、查看回滚段名称及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name ;
4、查看控制文件
select name from v$controlfile;
5、查看日志文件
select member from v$logfile;
6、查看表空间的使用情况
select sum(bytes)/(1024*1024) as free_space,tablespace_name
from dba_free_space
group by tablespace_name;
SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;
7、查看数据库库对象
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;
8、查看数据库的版本
Select version FROM Product_component_version
Where SUBSTR(PRODUCT,1,6)='Oracle';
9、查看数据库的创建日期和归档方式
Select Created, Log_Mode, Log_Mode From V$Database;
四、ORACLE用户连接的管理
用系统管理员,查看当前数据库有几个用户连接:
SQL> select username,sid,serial# from v$session;
如果要停某个连接用
SQL> alter system kill session 'sid,serial#';
如果这命令不行,找它UNIX的进程数
SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr;
说明:21是某个连接的sid数
然后用 kill 命令杀此进程号。
五、SQL*PLUS使用
a、近入SQL*Plus
$sqlplus 用户名/密码
退出SQL*Plus
SQL>exit
b、在sqlplus下得到帮助信息
列出全部SQL命令和SQL*Plus命令
SQL>help
列出某个特定的命令的信息
SQL>help 命令名
c、显示表结构命令DESCRIBE
SQL>DESC 表名
d、SQL*Plus中的编辑命令
显示SQL缓冲区命令
SQL>L
修改SQL命令
首先要将待改正行变为当前行
SQL>n
用CHANGE命令修改内容
SQL>c/旧/新
重新确认是否已正确
SQL>L
使用INPUT命令可以在SQL缓冲区中增加一行或多行
SQL>i
SQL>输入内容
e、调用外部系统编辑器
SQL>edit 文件名
可以使用DEFINE命令设置系统变量EDITOR来改变文本编辑器的类型,在login.sql文件中定义如下一行
DEFINE_EDITOR=vi
f、运行命令文件
SQL>START test
SQL>@test
常用SQL*Plus语句
a、表的创建、修改、删除
创建表的命令格式如下:
create table 表名 (列说明列表);
为基表增加新列命令如下:
ALTER TABLE 表名 ADD (列说明列表)
例:为test表增加一列Age,用来存放年龄
sql>alter table test
add (Age number(3));
修改基表列定义命令如下:
ALTER TABLE 表名
MODIFY (列名 数据类型)
例:将test表中的Count列宽度加长为10个字符
sql>alter atble test
modify (County char(10));
b、将一张表删除语句的格式如下:
DORP TABLE 表名;
例:表删除将同时删除表的数据和表的定义
sql>drop table test
c、表空间的创建、删除
--MartriWang@gmail.com 15/06/2007--
d. 删除表列
ALTER TABLE table_name DROP COLUMN 字段名 --del 表中字段
1: create table t2 as select <specific columns> from t1; --以第一个表中的某列建立第二个新表
2: drop table t1; --删除表1
3: rename t2 to t1; --把表2改名为表1
Oracle 8i及以上版本中,可以使用以下语句
alter table 表1 drop column 列1;
六、ORACLE逻辑备份的SH文件
完全备份的SH文件:exp_comp.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=complete file=/oracle/export/db_comp$rq.dmp"
累计备份的SH文件:exp_cumu.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=cumulative file=/oracle/export/db_cumu$rq.dmp"
增量备份的SH文件: exp_incr.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=incremental file=/oracle/export/db_incr$rq.dmp"
root用户crontab文件
/var/spool/cron/crontabs/root增加以下内容
0 2 1 * * /oracle/exp_comp.sh
30 2 * * 0-5 /oracle/exp_incr.sh
45 2 * * 6 /oracle/exp_cumu.sh
当然这个时间表可以根据不同的需求来改变的,这只是一个例子。
七、ORACLE 常用的SQL语法和数据对象
一.数据控制语句 (DML) 部分
1.INSERT (往数据表里插入记录的语句)
INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……);
INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名;
字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’
如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''.
字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验.
日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒
或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’)
TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC.
年-月-日 小时:分钟:秒 的格式YYYY-MM-DD HH24:MI:SS
INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型,
方法借用ORACLE里自带的DBMS_LOB程序包.
INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号
CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1
MAXVALUE 99999 CYCLE NOCACHE;
其中最大的值按字段的长度来定, 如果定义的自动增长的序列号 NUMBER(6) , 最大值为999999
INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL
2.DELETE (删除数据表里记录的语句)
DELETE FROM表名 WHERE 条件;
注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused.
如果确实要删除一个大表里的全部记录, 可以用 TRUNCATE 命令, 它可以释放占用的数据块表空间
TRUNCATE TABLE 表名;
此操作不可回退.
3.UPDATE (修改数据表里记录的语句)
UPDATE表名 SET 字段名1=值1, 字段名2=值2, …… WHERE 条件;
如果修改的值N没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验;
值N超过定义的长度会出错, 最好在插入前进行长度校验..
注意事项:
A. 以上SQL语句对表都加上了行级锁,
确认完成后, 必须加上事物处理结束的命令 COMMIT 才能正式生效,
否则改变不一定写入数据库里.
如果想撤回这些操作, 可以用命令 ROLLBACK 复原.
B. 在运行INSERT, DELETE 和 UPDATE 语句前最好估算一下可能操作的记录范围,
应该把它限定在较小 (一万条记录) 范围内,. 否则ORACLE处理这个事物用到很大的回退段.
程序响应慢甚至失去响应. 如果记录数上十万以上这些操作, 可以把这些SQL语句分段分次完成,
其间加上COMMIT 确认事物处理.
二.数据定义 (DDL) 部分
1.CREATE (创建表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
ORACLE常用的字段类型有
CHAR 固定长度的字符串
VARCHAR2 可变长度的字符串
NUMBER(M,N) 数字型M是位数总长度, N是小数的长度
DATE 日期类型
创建表时要把较小的不为空的字段放在前面, 可能为空的字段放在后面
创建表时可以用中文的字段名, 但最好还是用英文的字段名
创建表时可以给字段加上默认值, 例如 DEFAULT SYSDATE
这样每次插入和修改时, 不用程序操作这个字段都能得到动作的时间
创建表时可以给字段加上约束条件
例如 不允许重复 UNIQUE, 关键字 PRIMARY KEY
2.ALTER (改变表, 索引, 视图等)
改变表的名称
ALTER TABLE 表名1 TO 表名2;
在表的后面增加一个字段
ALTER TABLE表名 ADD 字段名 字段名描述;
修改表里字段的定义描述
ALTER TABLE表名 MODIFY字段名 字段名描述;
给表里的字段加上约束条件
ALTER TABLE 表名 ADD CONSTRAINT 约束名 PRIMARY KEY (字段名);
ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE (字段名);
把表放在或取出数据库的内存区
ALTER TABLE 表名 CACHE;
ALTER TABLE 表名 NOCACHE;
3.DROP (删除表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
删除表和它所有的约束条件
DROP TABLE 表名 CASCADE CONSTRAINTS;
4.TRUNCATE (清空表里的所有记录, 保留表的结构)
TRUNCATE 表名;
三.查询语句 (SELECT) 部分
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 条件;
字段名可以带入函数
例如: COUNT(*), MIN(字段名), MAX(字段名), AVG(字段名), DISTINCT(字段名),
TO_CHAR(DATE字段名,'YYYY-MM-DD HH24:MI:SS')
NVL(EXPR1, EXPR2)函数
解释:
IF EXPR1=NULL
RETURN EXPR2
ELSE
RETURN EXPR1
DECODE(AA﹐V1﹐R1﹐V2﹐R2....)函数
解释:
IF AA=V1 THEN RETURN R1
IF AA=V2 THEN RETURN R2
..…
ELSE
RETURN NULL
LPAD(char1,n,char2)函数
解释:
字符char1按制定的位数n显示,不足的位数用char2字符串替换左边的空位
字段名之间可以进行算术运算
例如: (字段名1*字段名1)/3
查询语句可以嵌套
例如: SELECT …… FROM
(SELECT …… FROM表名1, [表名2, ……] WHERE 条件) WHERE 条件2;
两个查询语句的结果可以做集合操作
例如: 并集UNION(去掉重复记录), 并集UNION ALL(不去掉重复记录), 差集MINUS, 交集INTERSECT
分组查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] GROUP BY字段名1
[HAVING 条件] ;
两个以上表之间的连接查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名 [ AND ……] ;
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名(+) [ AND ……] ;
有(+)号的字段位置自动补空值
查询结果集的排序操作, 默认的排序是升序ASC, 降序是DESC
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……]
ORDER BY字段名1, 字段名2 DESC;
字符串模糊比较的方法
INSTR(字段名, ‘字符串’)>0
字段名 LIKE ‘字符串%’ [‘%字符串%’]
每个表都有一个隐含的字段ROWID, 它标记着记录的唯一性.
四.ORACLE里常用的数据对象 (SCHEMA)
1.索引 (INDEX)
CREATE INDEX 索引名ON 表名 ( 字段1, [字段2, ……] );
ALTER INDEX 索引名 REBUILD;
一个表的索引最好不要超过三个 (特殊的大表除外), 最好用单字段索引, 结合SQL语句的分析执行情况,
也可以建立多字段的组合索引和基于函数的索引
ORACLE8.1.7字符串可以索引的最大长度为1578 单字节
ORACLE8.0.6字符串可以索引的最大长度为758 单字节
--MartriWang@gmail.com 10/05/2007--
(1)*Tree索引。
Create index indexname on tablename(columnname[columnname...])
(2)反向索引。
Create index indexname on tablename(columnname[columnname...]) reverse
(3)降序索引。
Create index indexname on tablename(columnname DESC[columnname...])
(4)位图索引。
Create BITMAP index indexname on tablename(columnname[columnname...])
(5)函数索引。
Create index indexname on tablename(functionname(columnname))
注意:创建索引后分析要索引才能起作用。
analyze index indexname compute statistics;
2.视图 (VIEW)
CREATE VIEW 视图名AS SELECT …. FROM …..;
ALTER VIEW视图名 COMPILE;
视图仅是一个SQL查询语句, 它可以把表之间复杂的关系简洁化.
3.同义词 (SYNONMY)
CREATE SYNONYM同义词名FOR 表名;
CREATE SYNONYM同义词名FOR 表名@数据库链接名;
4.数据库链接 (DATABASE LINK)
CREATE DATABASE LINK数据库链接名CONNECT TO 用户名 IDENTIFIED BY 密码 USING ‘数据库连接字符串’;
--MartriWang@gmail.com 10/05/2007--
create database mynewdb
user sys IDENTIFIED BY sys_id
user system IDENTIFIED BY system_id
LOGFILE GROUP 1 ('/u01/oracle/oradata/mynewdb/redo01.log') SIZE 100M,
GROUP 2 ('/u01/oracle/oradata/mynewdb/redo02.log') SIZE 100M,
GROUP 3 ('/u01/oracle/oradata/mynewdb/redo03.log') SIZE 100M
MAXLOGFILES 5
MAXLOGMEMBERS 5
MAXLOGHISTORY 1
MAXINSTANCES 100
MAXINSTANCES 1
CHARACTER SET US7ASCII
NATIONAL CHARACTER SET AL16UTF16
DATAFILE '/u01/oracle/oradata/mynewdb/system01.dbf' SIZE 325M REUSE
EXTENT MANAGEMENT LOCAL
SYSAUT DATAFILE '/u01/oracle/oradata/mynewdb/sysaux01.dbf' SIZE 325M REUSE
DEFAULT TABLESPACE tbs_1
DEFAULT TEMPORARY TABLESPACE tempts1
TEMPFILE '/u01/oracle/oradata/mynewdb/temp01.dbf' SIZE 20M REUSE
UNDO TABLESPACE undotbs
DATAFILE '/u01/oracle/oradata/mynewdb/undotbs01.dbf'
SIZE 20M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
--MartriWang@gmail.com 10/05/2007--
emctl start dbconsole
emctl stop dbconsole
--http://servername:port/em
--path:$ORACLE_HOME/install/portlist.ini
isqlplusctl start
isqlplusctl stop
--http://servername:port/isqlplus
数据库连接字符串可以用NET8 EASY CONFIG或者直接修改TNSNAMES.ORA里定义.
数据库参数global_name=true时要求数据库链接名称跟远端数据库名称一样
数据库全局名称可以用以下命令查出
SELECT * FROM GLOBAL_NAME;
查询远端数据库里的表
SELECT …… FROM 表名@数据库链接名;
五.权限管理 (DCL) 语句
1.GRANT 赋于权限
常用的系统权限集合有以下三个:
CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理)
常用的数据对象权限有以下五个:
ALL ON 数据对象名, SELECT ON 数据对象名, UPDATE ON 数据对象名,
DELETE ON 数据对象名, INSERT ON 数据对象名, ALTER ON 数据对象名
GRANT CONNECT, RESOURCE TO 用户名;
GRANT SELECT ON 表名 TO 用户名;
GRANT SELECT, INSERT, DELETE ON表名 TO 用户名1, 用户名2;
2.REVOKE 回收权限
REVOKE CONNECT, RESOURCE FROM 用户名;
REVOKE SELECT ON 表名 FROM 用户名;
REVOKE SELECT, INSERT, DELETE ON表名 FROM 用户名1, 用户名2;
查询数据库中第63号错误:
select orgaddr,destaddr from sm_histable0116 where error_code='63';
查询数据库中开户用户最大提交和最大下发数: select MSISDN,TCOS,OCOS from ms_usertable;
查询数据库中各种错误代码的总和:
select error_code,count(*) from sm_histable0513 group by error_code order
by error_code;
查询报表数据库中话单统计种类查询。
select sum(Successcount) from tbl_MiddleMt0411 where ServiceType2=111
select sum(successcount),servicetype from tbl_middlemt0411 group by servicetype
原文地址:http://www.cnoug.org/viewthread.php?tid=60293
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
--MartriWang@gmail.com 14/05/2007--
1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
2 将数据库中system用户与sys用户的表导出
exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
3 将数据库中的表table1 、table2导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
4 将数据库中的表table1中的字段filed1以"00"打头的数据导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"
--MartriWang@gmail.com 14/05/2007--
1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
2 将数据库中system用户与sys用户的表导出
exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
3 将数据库中的表table1 、table2导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1,table2)
4 将数据库中的表table1中的字段filed1以"00"打头的数据导出
exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
archive log list
alter system set LOG_ARCHIVE_DEST_1='LOCATION=/oracle/oradata/szdb/archive';
alter system set log_archive_format='%t_%s.dbf' scope=spfile;
alter system set log_archive_start=true scope=spfile;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
archive log list
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
// listener.ora 的条目entry
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc.world)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// tnsnames.ora 的条目
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//连接参数的设置(sql*net)
LOC =(DESCRIPTION=
(ADDRESS=
(COMMUNITY=TCP.HQ.COMPANY)
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
//参数文件配置范例
// tnsnames.ora
HQ =(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// Oracle8I tnsnames.ora
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//使用 COPY 实现数据库之间的复制
copy from
remote_username/remote_password@service_name
to
username/password@service_name
[append|create|insert|replace]
TABLE_NAME
using subquery;
REM COPY example
set copycommit 1
set arraysize 1000
copy from HR/PUFFINSTUFF@loc -
create EMPLOYEE -
using -
select * from EMPLOYEE
//监视器的管理
lsnrctl start
lsnrctl start my_lsnr
lsnrctl status
lsnrctl status hq
检查监视器的进程
ps -ef | grep tnslsnr
//在 lsnrctl 内停止监视器
set password lsnr_password
stop
//在lsnrctl 内列出所有的服务
set password lsnr_password
services
//启动或停止一个NT的listener
net start Oracle<version_name>TNSListener
net stop Oracle<version_name>TNSListener
// tnsnames.ora 文件的内容
fld1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)
(HOST = server1.fld.com)(PORT = 1521))
)
(CONNECT_DATA =
(SID = fld1)
)
)
//操作系统网络的管理
telnet host_name
ping host_name
/etc/hosts 文件
130.110.238.109 nmhost
130.110.238.101 txhost
130.110.238.102 azhost arizona
//oratab 表项
loc:/orasw/app/oracle/product/8.1.5.1:Y
cc1:/orasw/app/oracle/product/8.1.5.1:N
old:/orasw/app/oracle/product/8.1.5.0:Y
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
//创建一个控制文件命令到跟踪文件
alter database backup controlfile to trace;
//增加一个新的日志文件组的语句
connect internal as sysdba
alter database
add logfile group 4
(’/db01/oracle/CC1/log_1c.dbf’,
’/db02/oracle/CC1/log_2c.dbf’) size 5M;
alter database
add logfile member ’/db03/oracle/CC1/log_3c.dbf’
to group 4;
//在Server Manager上MOUNT并打开一个数据库:
connect internal as sysdba
startup mount ORA1 exclusive;
alter database open;
//生成数据字典
@catalog
@catproc
//在init.ora 中备份数据库的位置
log_archive_dest_1 = ’/db00/arch’
log_archive_dest_state_1 = enable
log_archive_dest_2 = "service=stby.world mandatory reopen=60"
log_archive_dest_state_2 = enable
//对用户的表空间的指定和管理相关的语句
create user USERNAME identified by PASSWORD
default tablespace TABLESPACE_NAME;
alter user USERNAME default tablespace TABLESPACE_NAME;
alter user SYSTEM quota 0 on SYSTEM;
alter user SYSTEM quota 50M on TOOLS;
create user USERNAME identified by PASSWORD
default tablespace DATA
temporary tablespace TEMP;
alter user USERNAME temporary tablespace TEMP;
//重新指定一个数据文件的大小 :
alter database
datafile ’/db05/oracle/CC1/data01.dbf’ resize 200M;
//创建一个自动扩展的数据文件:
create tablespace DATA
datafile ’/db05/oracle/CC1/data01.dbf’ size 200M
autoextend ON
next 10M
maxsize 250M;
//在表空间上增加一个自动扩展的数据文件:
alter tablespace DATA
add datafile ’/db05/oracle/CC1/data02.dbf’
size 50M
autoextend ON
maxsize 300M;
//修改参数:
alter database
datafile ’/db05/oracle/CC1/data01.dbf’
autoextend ON
maxsize 300M;
//在数据文件移动期间重新命名:
alter database rename file
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter tablespace DATA rename datafile
’/db01/oracle/CC1/data01.dbf’ to
’/db02/oracle/CC1/data01.dbf’;
alter database rename file
’/db05/oracle/CC1/redo01CC1.dbf’ to
’/db02/oracle/CC1/redo01CC1.dbf’;
alter database datafile ’/db05/oracle/CC1/data01.dbf’
resize 80M;
//创建和使用角色:
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
grant APPLICATION_USER to username;
//回滚段的管理
create rollback segment SEGMENT_NAME
tablespace RBS;
alter rollback segment SEGMENT_NAME offline;
drop rollback segment SEGMENT_NAME;
alter rollback segment SEGMENT_NAME online;
//回滚段上指定事务
commit;
set transaction use rollback segment ROLL_BATCH;
insert into TABLE_NAME
select * from DATA_LOAD_TABLE;
commit;
//查询回滚段的 大小和优化参数
select * from DBA_SEGMENTS
where Segment_Type = ’ROLLBACK’;
select N.Name, /* rollback segment name */
S.OptSize /* rollback segment OPTIMAL size */
from V$ROLLNAME N, V$ROLLSTAT S
where N.USN=S.USN;
//回收回滚段
alter rollback segment R1 shrink to 15M;
alter rollback segment R1 shrink;
//例子
set transaction use rollback segment SEGMENT_NAME
alter tablespace RBS
default storage
(initial 125K next 125K minextents 18 maxextents 249)
create rollback segment R4 tablespace RBS
storage (optimal 2250K);
alter rollback segment R4 online;
select Sessions_Highwater from V$LICENSE;
grant select on EMPLOYEE to PUBLIC;
//用户和角色
create role ACCOUNT_CREATOR;
grant CREATE SESSION, CREATE USER, ALTER USER
to ACCOUNT_CREATOR;
alter user THUMPER default role NONE;
alter user THUMPER default role CONNECT;
alter user THUMPER default role all except ACCOUNT_CREATOR;
alter profile DEFAULT
limit idle_time 60;
create profile LIMITED_PROFILE limit
FAILED_LOGIN_ATTEMPTS 5;
create user JANE identified by EYRE
profile LIMITED_PROFILE;
grant CREATE SESSION to JANE;
alter user JANE account unlock;
alter user JANE account lock;
alter profile LIMITED_PROFILE limit
PASSWORD_LIFE_TIME 30;
alter user jane password expire;
//创建操作系统用户
REM Creating OPS$ accounts
create user OPS$FARMER
identified by SOME_PASSWORD
default tablespace USERS
temporary tablespace TEMP;
REM Using identified externally
create user OPS$FARMER
identified externally
default tablespace USERS
temporary tablespace TEMP;
//执行ORAPWD
ORAPWD FILE=filename PASSWORD=password ENTRIES=max_users
create role APPLICATION_USER;
grant CREATE SESSION to APPLICATION_USER;
create role DATA_ENTRY_CLERK;
grant select, insert on THUMPER.EMPLOYEE to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.TIME_CARDS to DATA_ENTRY_CLERK;
grant select, insert on THUMPER.DEPARTMENT to DATA_ENTRY_CLERK;
grant APPLICATION_USER to DATA_ENTRY_CLERK;
grant DATA_ENTRY_CLERK to MCGREGOR;
grant DATA_ENTRY_CLERK to BPOTTER with admin option;
//设置角色
set role DATA_ENTRY_CLERK;
set role NONE;
//回收权利:
revoke delete on EMPLOYEE from PETER;
revoke all on EMPLOYEE from MCGREGOR;
//回收角色:
revoke ACCOUNT_CREATOR from HELPDESK;
drop user USERNAME cascade;
grant SELECT on EMPLOYEE to MCGREGOR with grant option;
grant SELECT on THUMPER.EMPLOYEE to BPOTTER with grant option;
revoke SELECT on EMPLOYEE from MCGREGOR;
create user MCGREGOR identified by VALUES ’1A2DD3CCEE354DFA’;
alter user OPS$FARMER identified by VALUES ’no way’;
//备份与恢复
使用 export 程序
exp system/manager file=expdat.dmp compress=Y owner=(HR,THUMPER)
exp system/manager file=hr.dmp owner=HR indexes=Y compress=Y
imp system/manager file=hr.dmp full=Y buffer=64000 commit=Y
//备份表
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES)
//备份分区
exp system/manager FILE=expdat.dmp TABLES=(Thumper.SALES:Part1)
//输入例子
imp system/manager file=expdat.dmp
imp system/manager file=expdat.dmp buffer=64000 commit=Y
exp system/manager file=thumper.dat owner=thumper grants=N
indexes=Y compress=Y rows=Y
imp system/manager file=thumper.dat FROMUSER=thumper TOUSER=flower
rows=Y indexes=Y
imp system/manager file=expdat.dmp full=Y commit=Y buffer=64000
imp system/manager file=expdat.dmp ignore=N rows=N commit=Y buffer=64000
//使用操作系统备份命令
REM TAR examples
tar -cvf /dev/rmt/0hc /db0[1-9]/oracle/CC1
tar -rvf /dev/rmt/0hc /orasw/app/oracle/CC1/pfile/initcc1.ora
tar -rvf /dev/rmt/0hc /db0[1-9]/oracle/CC1 /orasw/app/oracle/CC1/pfile/initcc1.ora
//离线备份的shell脚本
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOF1
connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
EOF2
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database archivelog;
archive log start;
alter database open;
//在Server Manager上设置为archivelog mode:
connect internal as sysdba
startup mount cc1;
alter database noarchivelog;
alter database open;
select Name,
Value
from V$PARAMETER
where Name like ’log_archive%’;
//联机备份的脚本
#
# Sample Hot Backup Script for a UNIX File System database
#
# Set up environment variables:
ORACLE_SID=cc1; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
. oraenv
svrmgrl <<EOFarch1
connect internal as sysdba
REM
REM 备份 SYSTEM tablespace
REM
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
REM
REM The SYSTEM tablespace has now been written to a
REM tar saveset on the tape device /dev/rmt/0hc. The
REM rest of the tars must use the "-rvf" clause to append
REM to that saveset.
REM
REM 备份 RBS tablespace
REM
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
REM
REM 备份 DATA tablespace
REM For the purposes of this example, this tablespace
REM will contain two files, data01.dbf and data02.dbf.
REM The * wildcard will be used in the filename.
REM
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM
REM 备份 INDEXES tablespace
REM
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM
REM 备份 TEMP tablespace
REM
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM
REM
REM Step 2. 备份归档日志文件.
archive log stop
REM
REM Exit Server Manager, using the indicator set earlier.
exit
EOFarch1
#
# Record which files are in the destination directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Now go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal
archive log start;
exit
EOFarch2
#
# Now back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
# You may choose to compress them instead.
#
tar -rvf /dev/rmt/0hc $FILES
rm -f $FILES
#
# Step 3. 备份控制文件到磁盘.
#
svrmgrl <<EOFarch3
connect internal
alter database backup controlfile to
’db01/oracle/CC1/CC1controlfile.bck’;
exit
EOFarch3
#
# 备份控制文件到磁带.
#
tar -rvf /dev/rmt/0hc /db01/oracle/CC1/CC1controlfile.bck
#
# End of hot backup script.
//自动生成开始备份的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ begin backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_begin.sql
/
spool off
//自动生成备份结束的脚本
set pagesize 0 feedback off
select
’alter tablespace ’||Tablespace_Name||’ end backup;’
from DBA_TABLESPACES
where Status <> ’INVALID’
spool alter_end.sql
/
spool off
//备份归档日志文件的脚本.
REM See text for alternatives.
# Step 1: Stop the archiving process. This will keep
# additional archived redo log files from being written
# to the destination directory during this process.
#
svrmgrl <<EOFarch1
connect internal as sysdba
archive log stop;
REM
REM Exit Server Manager using the indicator set earlier.
exit
EOFarch1
#
# Step 2: Record which files are in the destination
# directory.
# Do this by setting an environment variable that is
# equal to the directory listing for the destination
# directory.
# For this example, the log_archive_dest is
# /db01/oracle/arch/CC1.
#
FILES=`ls /db01/oracle/arch/CC1/arch*.dbf`; export FILES
#
# Step 3: Go back into Server Manager and restart the
# archiving process. Set an indicator (called EOFarch2
# in this example).
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log start;
exit
EOFarch2
#
# Step 4. Back up the archived redo logs to the tape
# device via the "tar" command, then delete them
# from the destination device via the "rm" command.
#
tar -rvf /dev/rmt/0hc $FILES
#
# Step 5. Delete those files from the destination directory.
#
rm -f $FILES
#
# End of archived redo log file backup script.
REM 磁盘到磁盘的备份
REM
REM Back up the RBS tablespace - to another disk (UNIX)
REM
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
REM
REM 移动归档日志文件的shell脚本
#
# Procedure for moving archived redo logs to another device
#
svrmgrl <<EOFarch2
connect internal as sysdba
archive log stop;
!mv /db01/oracle/arch/CC1 /db10/oracle/arch/CC1
archive log start;
exit
EOFarch2
#
# end of archived redo log directory move.
//生成创建控制文件命令
alter database backup controlfile to trace;
//时间点恢复的例子
connect internal as sysdba
startup mount instance_name;
recover database until time ’1999-08-07:14:40:00’;
//创建恢复目录
rman rcvcat rman/rman@<database_service_name>
// 在(UNIX)下创建恢复目录
RMAN> create catalog tablespace rcvcat;
// 在(NT)下创建恢复目录
RMAN> create catalog tablespace "RCVCAT";
//连接描述符范例
(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora 的条目entry
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
转贴于 学生大读书网 http://
(KEY= loc.world)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// tnsnames.ora 的条目
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//连接参数的设置(sql*net)
LOC =(DESCRIPTION=
(ADDRESS=
(COMMUNITY=TCP.HQ.COMPANY)
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
//参数文件配置范例
// tnsnames.ora
HQ =(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=HQ)
(PORT=1521))
(CONNECT DATA=
(SID=loc)))
// listener.ora
LISTENER =
(ADDRESS_LIST =
(ADDRESS=
(PROTOCOL=IPC)
(KEY= loc)
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = loc)
(ORACLE_HOME = /orasw/app/oracle/product/8.1.5.1)
)
)
// Oracle8I tnsnames.ora
LOC=
(DESCRIPTION=
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HQ)
(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = loc)
(INSTANCE_NAME = loc)
)
)
//使用 COPY 实现数据库之间的复制
copy from
remote_username/remote_password@service_name
to
username/password@service_name
[append|create|insert|replace]
TABLE_NAME
using subquery;
REM COPY example
set copycommit 1
set arraysize 1000
copy from HR/PUFFINSTUFF@loc -
create EMPLOYEE -
using -
select * from EMPLOYEE
//监视器的管理
lsnrctl start
lsnrctl start my_lsnr
lsnrctl status
lsnrctl status hq
检查监视器的进程
ps -ef | grep tnslsnr
//在 lsnrctl 内停止监视器
set password lsnr_password
stop
//在lsnrctl 内列出所有的服务
set password lsnr_password
services
//启动或停止一个NT的listener
net start Oracle<version_name>TNSListener
net stop Oracle<version_name>TNSListener
// tnsnames.ora 文件的内容
fld1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)
(HOST = server1.fld.com)(PORT = 1521))
)
(CONNECT_DATA =
(SID = fld1)
)
)
//操作系统网络的管理
telnet host_name
ping host_name
/etc/hosts 文件
130.110.238.109 nmhost
130.110.238.101 txhost
130.110.238.102 azhost arizona
//oratab 表项
loc:/orasw/app/oracle/product/8.1.5.1:Y
cc1:/orasw/app/oracle/product/8.1.5.1:N
old:/orasw/app/oracle/product/8.1.5.0:Y
--MartriWang@gmail.com 11/05/2007--
Oracle优化经典文章------索引原理篇
Oracle提供了大量索引选项。知道在给定条件下使用哪个选项对于一个应用程序的性能来说非常重要。一个错误的选择可能会引发死锁,
并导致数据库性能急剧下降或进程终止。而如果做出正确的选择,则可以合理使用资源,使那些已经运行了几个小时甚至几天的进程在几分钟得以完成,
这样会使您立刻成为一位英雄。这篇文章就将简单的讨论每个索引选项。主要有以下内容:
[1] 基本的索引概念
查询DBA_INDEXES视图可得到表中所有索引的列表,注意只能通过USER_INDEXES的方法来检索模式(schema)的索引。访问USER_IND_COLUMNS视图可得到
一个给定表中被索引的特定列。
[2] 组合索引
当某个索引包含有多个已索引的列时,称这个索引为组合(concatented)索引。在 Oracle9i引入跳跃式扫描的索引访问方法之前,查询只能在有限条
件下使用该索引。比如:表emp有一个组合索引键,该索引包含了empno、ename和deptno。在Oracle9i之前除非在where之句中对第一列(empno)指定一
个值,否则就不能使用这个索引键进行一次范围扫描。
特别注意:在Oracle9i之前,只有在使用到索引的前导索引时才可以使用组合索引!
[3] ORACLE ROWID
通过每个行的ROWID,索引Oracle提供了访问单行数据的能力。ROWID其实就是直接指向单独行的线路图。如果想检查重复值或是其他对ROWID本身的引用,
可以在任何表中使用和指定rowid列。
[4] 限制索引
限制索引是一些没有经验的开发人员经常犯的错误之一。在SQL中有很多陷阱会使一些索引无法使用。下面讨论一些常见的问题:
4.1 使用不等于操作符(<>、!=)
下面的查询即使在cust_rating列有一个索引,查询语句仍然执行一次全表扫描。
select cust_Id,cust_name
from customers
where cust_rating <> 'aa';
把上面的语句改成如下的查询语句,这样,在采用基于规则的优化器而不是基于代价的优化器(更智能)时,将会使用索引。
select cust_Id,cust_name
from customers
where cust_rating < 'aa' or cust_rating > 'aa';
特别注意:通过把不等于操作符改成OR条件,就可以使用索引,以避免全表扫描。
4.2 使用IS NULL 或IS NOT NULL
使用IS NULL 或IS NOT NULL同样会限制索引的使用。因为NULL值并没有被定义。在SQL语句中使用NULL会有很多的麻烦。因此建议开发人员在建表时,
把需要索引的列设成NOT NULL。如果被索引的列在某些行中存在NULL值,就不会使用这个索引(除非索引是一个位图索引,关于位图索引在稍后在详细讨论)。
4.3 使用函数
如果不使用基于函数的索引,那么在SQL语句的WHERE子句中对存在索引的列使用函数时,会使优化器忽略掉这些索引。
下面的查询不会使用索引(只要它不是基于函数的索引)
select empno,ename,deptno
from emp
where trunc(hiredate)='01-MAY-81';
把上面的语句改成下面的语句,这样就可以通过索引进行查找。
select empno,ename,deptno
from emp
where hiredate<(to_date('01-MAY-81')+0.9999);
4.4 比较不匹配的数据类型
比较不匹配的数据类型也是比较难于发现的性能问题之一。
注意下面查询的例子,account_number是一个VARCHAR2类型,在account_number字段上有索引。下面的语句将执行全表扫描。
select bank_name,address,city,state,zip
from banks
where account_number = 990354;
Oracle可以自动把where子句变成to_number(account_number)=990354,这样就限制了索引的使用,改成下面的查询就可以使用索引:
select bank_name,address,city,state,zip
from banks
where account_number ='990354';
特别注意:不匹配的数据类型之间比较会让Oracle自动限制索引的使用,即便对这个查询执行Explain Plan也不能让您明白为什么做了一
次“全表扫描”。
[5] 选择性
使用USER_INDEXES视图,该视图中显示了一个distinct_keys列。比较一下唯一键的数量和表中的行数,就可以判断索引的选择性。选择性越高,
索引返回的数据就越少。
[6] 群集因子(Clustering Factor)
Clustering Factor位于USER_INDEXES视图中。该列反映了数据相对于已索引的列是否显得有序。如果Clustering Factor列的值接近于索引中的树叶块
(leaf block)的数目,表中的数据就越有序。如果它的值接近于表中的行数,则表中的数据就不是很有序。
[7] 二元高度(Binary height)
索引的二元高度对把ROWID返回给用户进程时所要求的I/O量起到关键作用。在对一个索引进行分析后,可以通过查询DBA_INDEXES的B-level列查看它的二元高度。
二元高度主要随着表的大小以及被索引的列中值的范围的狭窄程度而变化。索引上如果有大量被删除的行,它的二元高度也会增加。更新索引列也类似于删除操作,
因为它增加了已删除键的数目。重建索引可能会降低二元高度。
[8] 快速全局扫描
在Oracle7.3后就可以使用快速全局扫描(Fast Full Scan)这个选项。这个选项允许Oracle执行一个全局索引扫描操作。快速全局扫描读取B-树索引上所有树叶块。
初始化文件中的DB_FILE_MULTIBLOCK_READ_COUNT参数可以控制同时被读取的块的数目。
[9] 跳跃式扫描
从Oracle9i开始,索引跳跃式扫描特性可以允许优化器使用组合索引,即便索引的前导列没有出现在WHERE子句中。索引跳跃式扫描比全索引扫描要快的多。
下面的程序清单显示出性能的差别:
create index skip1 on emp5(job,empno);
index created.
select count(*)
from emp5
where empno=7900;
Elapsed:00:00:03.13
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE(Cost=4 Card=1 Bytes=5)
1 0 SORT(AGGREGATE)
2 1 INDEX(FAST FULL SCAN) OF 'SKIP1'(NON-UNIQUE)
Statistics
6826 consistent gets
6819 physical reads
select /*+ index(emp5 skip1)*/ count(*)
from emp5
where empno=7900;
Elapsed:00:00:00.56
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE(Cost=6 Card=1 Bytes=5)
1 0 SORT(AGGREGATE)
2 1 INDEX(SKIP SCAN) OF 'SKIP1'(NON-UNIQUE)
Statistics
21 consistent gets
17 physical reads
[10] 索引的类型
B-树索引
位图索引
HASH索引
索引编排表
反转键索引
基于函数的索引
分区索引
本地和全局索引
==================================================================================================================================================
--MartriWang@gmail.com 11/05/2007--
索引(INDEX)
1.索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据;
Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引;
索引由根节点、分支节点和叶子节点组成,上级索引块包含下级索引块的索引数据,叶节点包含索引数据和确定行实际位置的rowid。
使用索引的目的
加快查询速度
减少I/O操作
消除磁盘排序
何时使用索引
查询返回的记录数
排序表<40%
非排序表 <7%
表的碎片较多(频繁增加、删除)
索引的种类
非唯一索引(最常用)
唯一索引
位图索引
局部有前缀分区索引
局部无前缀分区索引
全局有前缀分区索引
散列分区索引
基于函数的索引
2.管理索引的准则
在表中插入数据后创建索引
。在用SQL*Loader或import工具插入或装载数据后,建立索引比较有效;
索引正确的表和列
。经常检索排序大表中40%或非排序表7%的行,建议建索引;
。为了改善多表关联,索引列用于联结;
。列中的值相对比较唯一;
。取值范围(大:B*树索引,小:位图索引);
。Date型列一般适合基于函数的索引;
。列中有许多空值,不适合建立索引
为性能而安排索引列
。经常一起使用多个字段检索记录,组合索引比单索引更有效;
。把最常用的列放在最前面,例:dx_groupid_serv_id(groupid,serv_id),在where条件中使用groupid或groupid,serv_id,查询将使用索引,若仅用到serv_id字段,则索引无效;
。合并/拆分不必要的索引。
限制每个表索引的数量
。一个表可以有几百个索引(你会这样做吗?),但是对于频繁插入和更新表,索引越多系统CPU,I/O负担就越重;
。建议每张表不超过5个索引。
删除不再需要的索引
。索引无效,集中表现在该使用基于函数的索引或位图索引,而使用了B*树索引;
。应用中的查询不使用索引;
。重建索引之前必须先删除索引,若用alter index … rebuild重建索引,则不必删除索引。
索引数据块空间使用
。创建索引时指定表空间,特别是在建立主键时,应明确指定表空间;
。合理设定pctfress,注意:不能给索引指定pctused;
。估计索引的大小和合理地设置存储参数,默认为表空间大小,或initial与next设置成一样大。
考虑并行创建索引
。对大表可以采用并行创建索引,在并行创建索引时,存储参数被每个查询服务器进程分别使用,例如:initial为1M,并行度为8,则创建索引期间至少要消耗8M空间;
考虑用nologging创建索引
。对大表创建索引可以使用nologging来减少重做日志;
。节省重做日志文件的空间;
。缩短创建索引的时间;
。改善了并行创建大索引时的性能。
3.怎样建立最佳索引
明确地创建索引
create index index_name on table_name(field_name)
tablespace tablespace_name
pctfree 5
initrans 2
maxtrans 255
storage
(
minextents 1
maxextents 16382
pctincrease 0
);
创建基于函数的索引
。常用与UPPER、LOWER、TO_CHAR(date)等函数分类上,例:
create index idx_func on emp (UPPER(ename)) tablespace tablespace_name;
创建位图索引
。对基数较小,且基数相对稳定的列建立索引时,首先应该考虑位图索引,例:
create bitmap index idx_bitm on class (classno) tablespace tablespace_name;
明确地创建唯一索引
。可以用create unique index语句来创建唯一索引,例:
create unique index dept_unique_idx on dept(dept_no) tablespace idx_1;
创建与约束相关的索引
。可以用using index字句,为与unique和primary key约束相关的索引,例如:
alter table table_name
add constraint PK_primary_keyname primary key (field_name)
using index tablespace tablespace_name;
如何创建局部分区索引
。基础表必须是分区表;
。分区数量与基础表相同;
。每个索引分区的子分区数量与相应的基础表分区相同;
。基础表的子分区中的行的索引项,被存储在该索引的相应的子分区中,例如:
Create Index TG_CDR04_SERV_ID_IDX On TG_CDR04(SERV_ID)
Pctfree 5
Tablespace TBS_AK01_IDX
Storage (
MaxExtents 32768
PctIncrease 0
FreeLists 1
FreeList Groups 1
)
local
/
如何创建范围分区的全局索引
。基础表可以是全局表和分区表。
create index idx_start_date on tg_cdr01(start_date)
global partition by range(start_date)
(partition p01_idx vlaues less than (‘0106’)
partition p01_idx vlaues less than (‘0111’)
…
partition p01_idx vlaues less than (‘0401’ ))
/
重建现存的索引
重建现存的索引的当前时刻不会影响查询;
重建索引可以删除额外的数据块;
提高索引查询效率;
alter index idx_name rebuild nologging;
对于分区索引:
alter index idx_name rebuild partition partiton_name nologging;
要删除索引的原因
。不再需要的索引;
。索引没有针对其相关的表所发布的查询提供所期望的性能改善;
。应用没有用该索引来查询数据;
。该索引无效,必须在重建之前删除该索引;
。该索引已经变的太碎了,必须在重建之前删除该索引;
。语句:
drop index idx_name;
drop index idx_name drop partition partition_name;
建立索引的代价
基础表维护时,系统要同时维护索引,不合理的索引将严重影响系统资源,主要表现在CPU和I/O上;
插入、更新、删除数据产生大量db file sequential read锁等待;
4.SQL优化器简介
基于规则的优化器
。总是使用索引
。总是从驱动表开始(from子句最右边的表)
。只有在不可避免的情况下,才使用全表扫描
。任何索引都可以
基于成本的优化器
。需要表、索引的统计资料
Analyze table customer compute statistics;
Analyze table customer estimate statistics sample 5000 rows;
。表中设置并行度、表分区
--MartriWang@gmail.com 21/06/2007--
软件环境:
1. Windows 2000+ORACLE 8.1.7
2. ORACLE安装路径为:C:\ORACLE
实现方法:
1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务。
2. 开始->程序->Oracle - OraHome81->Oracle Installation Products-> Universal Installer 卸装所有Oracle产品,
但Universal Installer本身不能被删除
5. 运行regedit,选择HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE,按del键删除这个入口。
6. 运行regedit,选择HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services,滚动 这个列表,删除所有Oracle入口。
7. 运行refedit,选择HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application, 删除所有Oracle入口。
8. 开始->设置->控制面板->系统->高级->环境变量 删除环境变量CLASSPATH和PATH中有关Oracle的设定
9. 从桌面上、STARTUP(启动)组、程序菜单中,删除所有有关Oracle的组和图标
10.删除\Program Files\Oracle目录
11. 重新启动计算机,重起后才能完全删除Oracle所在目录
12、删除与Oracle有关的文件,选择Oracle所在的缺省目录C:\Oracle,删除这个入口目录及所有子目录,并从Windows 2000目录
(一般为C:\WINNT)下删除以下文件ORACLE.INI、oradim73.INI、oradim80.INI、oraodbc.ini等等。
13. WIN.INI文件中若有[ORACLE]的标记段,删除该段
14. 如有必要,删除所有Oracle相关的ODBC的DSN
15. 到事件查看器中,删除Oracle相关的日志 说明:如果有个别DLL文件无法删除的情况,则不用理会,重新启动,开始新的安装,
安装时,选择一个新的目录,则,安装完毕并重新启动后,老的目录及文件就可以删除掉了
--MartriWang@gmail.com 25/06/2007--
--创建数据表空间
CREATE TABLESPACE TS1
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS1.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS2
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS2.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS3
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS3.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS4
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS4.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS5
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS5.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS6
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS6.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS7
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS7.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS8
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS8.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS9
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS9.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS10
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS10.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS11
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS11.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TS12
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TS12.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
--创建索引表空间
CREATE TABLESPACE TSI1
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI1.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI2
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI2.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI3
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI3.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI4
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI4.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI5
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI5.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI6
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI6.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI7
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI7.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI8
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI8.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI9
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI9.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI10
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI10.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI11
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI11.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
CREATE TABLESPACE TSI12
LOGGING
DATAFILE 'C:\ORACLE\ORADATA\ORA9\TSI12.ORA' SIZE 1M EXTENT
MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
--范围分区表
drop table sales1;
--范围必须越来越大
CREATE TABLE sales1(
year int ,
month int,
day int,
fee number)
PARTITION by range (year,month,day)
(
partition sale1 values less than(1999,02,01) tablespace ts1,
partition sale2 values less than(2000,01,01) tablespace ts2,
partition sale3 values less than(2001,01,01) tablespace ts3,
partition sale4 values less than(2002,01,01) tablespace ts4,
partition sale5 values less than(MAXVALUE,MAXVALUE,MAXVALUE) tablespace ts5
);
--第一种索引
drop index idx_sales1;
create index idx_sales1 on sales1(year);
--第二种索引
drop index idx_sales1;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales1 on sales1(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales1;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales1 ON SALES1 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION aSALE1 tablespace tsi1,
PARTITION aSALE2 tablespace tsi2,
PARTITION aSALE3 tablespace tsi3,
PARTITION aSALE4 tablespace tsi4)
;
insert into sales1 values(1999,01,01,1);
--null 插入到maxvalue表空间。
insert into sales1 values(null,null,null,0);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales1 values(2004,01,01,1);
commit;
--列表分区表
drop table sales2;
--范围必须越来越大
CREATE TABLE sales2(
year int ,
month int,
day int,
fee number)
PARTITION by list (year)
(
partition sale1 values (1999) tablespace ts1,
partition sale2 values (2000) tablespace ts2,
partition sale3 values (2001) tablespace ts3,
partition sale4 values (2002) tablespace ts4,
partition sale5 values (null) tablespace ts5,
partition sale6 values (default) tablespace ts6
);
--第一种索引
drop index idx_sales2;
create index idx_sales2 on sales2(year);
--第二种索引
drop index idx_sales2;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales2 on sales2(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales2;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales2 ON sales2 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION iSALE1 tablespace tsi1,
PARTITION iSALE2 tablespace tsi2,
PARTITION iSALE3 tablespace tsi3,
PARTITION iSALE4 tablespace tsi4,
PARTITION iSALE5 tablespace tsi5)
;
insert into sales2 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales2 values(2004,01,01,1);
insert into sales2 values(null,01,01,1);
commit;
/******************************************************************************/
--1 AAAHmyAALAAAAAXAAA 1999 1 1 1 11 11
--2 AAAHm2AAPAAAAAPAAA 1 1 1 15 15
--3 AAAHm3AAQAAAAAPAAA 2004 1 1 1 16 16
/******************************************************************************/
--散列分区表
drop table sales3;
CREATE TABLE sales3(
year int ,
month int,
day int,
fee number)
PARTITION by hash (year)
partitions 5;
drop table sales3;
--ORA-14170: 不能在 CREATE TABLE|INDEX 中指定 <分区 - 说明> 子句
CREATE TABLE sales3(
year int ,
month int,
day int,
fee number)
PARTITION by hash (year)
--partitions 5
(
partition p1 tablespace ts1,
partition p2 tablespace ts2,
partition p3 tablespace ts3,
partition p4 tablespace ts4,
partition p5 tablespace ts5
);
--第一种索引
drop index idx_sales3;
create index idx_sales3 on sales3(year);
--第二种索引
drop index idx_sales3;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales3 on sales3(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
drop index idx_sales3;
CREATE INDEX idx_sales3 ON sales3 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION SALE1 tablespace tsi1,
PARTITION SALE2 tablespace tsi2,
PARTITION SALE3 tablespace tsi3,
PARTITION SALE4 tablespace tsi4,
PARTITION SALE5 tablespace tsi5)
;
insert into sales3 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales3 values(2004,01,01,1);
insert into sales3 values(null,01,01,1);
commit;
/******************************************************************************/
--1 AAAHmyAALAAAAAXAAA 1999 1 1 1 11 11
--2 AAAHm2AAPAAAAAPAAA 1 1 1 15 15
--3 AAAHm3AAQAAAAAPAAA 2004 1 1 1 16 16
/******************************************************************************/
--组合分区表
drop table sales4;
--组合必须越来越大
CREATE TABLE sales4(
year int ,
month int,
day int,
fee number)
PARTITION by range (year) subpartition by hash(month)
subpartitions 8 store in (ts11,ts12)
(
partition sale1 values less than(1999) tablespace ts1,
partition sale2 values less than(2000) tablespace ts2,
partition sale3 values less than(2001) tablespace ts3,
partition sale4 values less than(2002) tablespace ts4
);
--第一种索引
drop index idx_sales4;
create index idx_sales4 on sales4(year);
--第二种索引
drop index idx_sales4;
--ORA-14021: 必须指定所有列的 MAXVALUE
--ORA-14010: 不可以指定索引分区的此物理属性
create index idx_sales4 on sales4(year)
global partition by range(year)
(
partition sale1 values less than(1999) tablespace tsi1,
partition sale2 values less than(2000) tablespace tsi2,
partition sale3 values less than(2001) tablespace tsi3,
partition sale4 values less than(2002) tablespace tsi4,
partition sale5 values less than(MAXVALUE) tablespace tsi5
);
--第三种索引
drop index idx_sales4;
--ORA-14024: LOCAL 索引的分区数必须等于基本表的分区数(索引分区名可以与表分区名不一致,不过索引数据怎么存储呢?)
CREATE INDEX idx_sales4 ON sales4 (YEAR)
TABLESPACE EXAMPLE LOCAL
(
PARTITION aSALE1 tablespace tsi1,
PARTITION aSALE2 tablespace tsi2,
PARTITION aSALE3 tablespace tsi3,
PARTITION aSALE4 tablespace tsi4)
;
insert into sales4 values(1999,01,01,1);
--ORA-14400: 插入的分区关键字未映射到任何分区
insert into sales4 values(2004,01,01,1);
commit;
转载自:http://blog.csdn.net/wonth/archive/2007/06/28/1670355.aspx