REPLACE( string1, string_to_replace [, replacement_string] )
参数说明:
string1
需要替换的原字符串。
string_to_replace
string1
中需要替换的字符。
replacement_string
string1
中出现的所有
string_to_replace
都将被
replacement_string
替换。如果省略了
replacement_string
参数,则
REPLACE
函数只删除所有出现的
string_to_replace
,并返回结果字符串。
REPLACE
函数会返回一个替换后的结果字符串。
REPLACE
函数可用于以下版本的 Oracle / PLSQL:
- Oracle 12c
- Oracle 11g
- Oracle 10g
- Oracle 9i
- Oracle 8i
只修改查询结果:
SELECT REPLACE(T.FULL_CODE, 'TREE', 'ROOT')
FROM TABLE_NAME T
批量更新表中的某个列中的所有值:
UPDATE TB_MENU T
SET T.FULL_MENU_CODE = REPLACE(T.FULL_MENU_CODE, 'TREE', 'ROOT')
Oracle:一次性替换某列中的所有值场景引入Oracle / PLSQL: REPLACE 函数适用版本示例查询更新场景引入假设,我们需要将原 FULL_CODE 列中的值:FULL_CODE----------TREETREE.ANLYSTREE.LOG_MGTTREE.LOG_MGT.ERR_LOGTREE.LOG_MGT.OPS_LOGTREE.SYS_MGTTREE...
alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN);
指定表空间
alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN) using index tablespace TABLE_SPACE_NAME;
2.增加外键
alter table TABLE_NAME add constraint FK_NAME foreign key (TABLE_COLUMN) references KEY_TABLE_NAME;
3.使主键或外键失效、生效
alter table TABLE_NAME disable(enable) constraint KEY_NAME;
4、查看各种约束
select constraint_name,table_name,constraint_type,status from user_constraints;
select constraint_name, constraint_type,search_condition, r_constraint_name from user_constraints where table_name = upper(\'&table_name\')
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;
5、删除主键或外键
alter table TABLE_NAME drop constraint KEY_NAME;
6、建外键
单字段时:create table 表名 (col1 char(8),
cno char(4) REFERENCE course);
多个字段时,在最后加上 Foreign Key (字段名) REFERENCE 表名(字段)
连带删除选项 (on delete cascade
当指定时,如果父表中的记录被删除,则依赖于父表的记录也被删除
REFERENCE 表名() on delete cascade;
7、删除带约束的表
Drop table 表名 cascade constraints;
8:索引管理
<1>.creating function-based indexes
sql> create index summit.item_quantity on summit.item(quantity-quantity_shipped);
<2>.create a B-tree index
sql> create [unique] index index_name on table_name(column,.. asc/desc) tablespace
sql> tablespace_name [pctfree integer] [initrans integer] [maxtrans integer]
sql> [logging | nologging] [nosort] storage(initial 200k next 200k pctincrease 0
sql> maxextents 50);
<3>.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows
<4>.creating reverse key indexes
sql> create unique index xay_id on xay(a) reverse pctfree 30 storage(initial 200k
sql> next 200k pctincrease 0 maxextents 50) tablespace indx;
<5>.create bitmap index
sql> create bitmap index xay_id on xay(a) pctfree 30 storage( initial 200k next 200k
sql> pctincrease 0 maxextents 50) tablespace indx;
<6>.change storage parameter of index
sql> alter index xay_id storage (next 400k maxextents 100);
7.allocating index space
sql> alter index xay_id allocate extent(size 200k datafile \'c:/oracle/index.dbf\');
<8>.alter index xay_id deallocate unused;
<9>、查看索引
SQL>select index_name,index_type,table_name from user_indexes order by table_name;
<10>、查看索引被索引的字段
SQL>select * from user_ind_columns where index_name=upper(\'&index_name\');
11、创建序列
select * from user_sequences;
create sequence SEQ_NAME start with 1000
maxvalue 1000 increment by 1;
alter sequence SEQ_NAME minvalue 50 maxvalue 100;
12、删除重复行
update a set aa=null where aa is not null;
delete from a where rowid!=
(select max(rowid) from a b where a.aa=b.aa);
13、删除同其他表相同的行
delete from a where exits
(select \'X\' from b where b.no=a.no);
delete from a where no in (select no from b);
14、查询从多少行到多少行的记录(可以用在web开发中的分页显示)
select * from ( select rownum row_id,b.* from (select a.* from sys_oper a) b )
where row_id between 15 and 20
15、对公共授予访问权
grant select on 表名 to public;
create public synonym 同义词名 for 表名;
16、填加注释
comment on table 表名 is \'注释\';
comment on column 表名.列名 is \'注释\';
17、分布式数据库,创建数据库链路
create [public] database link LINKNAME
[connect to USERNAME identified by PASSWORD]
[using \'CONNECT_STRING\']
可以在服务器端,也可以在客户端建立,但必须注意,两台服务器之间
数据库必须可以互访,必须各有各自的别名数据库
18、查看数据库链路
select * from all_db_links;
select * from user_db_links;
查询 select * from TABLENAME@DBLNKNAME;
创建远程数据库同义词
create synonym for TABLENAME@DBLNKNAME;
操纵远程数据库记录
insert into TABLENAME@DBLNKNAME (a,b) values (va,vb);
update TABLENAME@DBLNKNAME set a=\'this\';
delete from TABLENAME@DBLNKNAME;
怎样执行远程的内嵌过程
begin
otherdbpro@to_html(参数);
19、数据库链路用户密码有特殊字符的时候,可以用双引号把密码引起来
create public database link dblink1 connect to db1 identified by \"123*456\" using \'db11\'
20.oracle8中扩充了group by rollup和cube的操作。有时候省了你好多功夫的。
<1>下面的语句可以进行总计
select region_code,count(*) from aicbs.acc_woff_notify
group by rollup(region_code);
<2> 对第1个字段小计,最后合计
select region_code,write_status,count(*) from aicbs.acc_woff_notify
group by rollup(region_code,write_status);
----------------------
570 0 3
570 1 2
570 5 --此处小计了570的记录
571 0 10
571 1 2
571 12 --此处小计了571的记录
.....
100 --此处有总计
<3> 复合rollup表达式,只做总计
select region_code,write_status,count(*) from aicbs.acc_woff_notify
group by rollup(region_code,write_status);
<4> 对第1个字段小计,再对第2个字段小计,最后合计
select region_code,write_status,count(*) from aicbs.acc_woff_notify
group by cube(region_code,write_status);
----------------------
100 --此处有总计
0 60 --对write_status=0的小计
1 39 --对write_status=1的小计
3 1 --对write_status=3的小计
570 5 --此处小计了570的记录
570 0 3
570 1 2
571 12 --此处小计了571的记录
571 0 10
571 1 2
<3> 复合cube表达式,只做总计
select region_code,write_status,count(*) from aicbs.acc_woff_notify
group by cube(region_code,write_status);
<4>下面的语句可以按照rollup不同的字段进行小计
select region_code,write_status,count(*) from aicbs.acc_woff_notify
group by region_code,rollup(write_status);
21.查询view的创建语句
sql>set long 1000
sql>select * from user_views where view_name=\'MY_VIEW_NAME\';
sql>select * from all_views where view_name=\'MY_VIEW_NAME\';
22、去除数据库中特殊字符
<1>.字符串字段中含有\"\'\",如果用来组合sql语句,会造成语句不准确。
比如:replace(f1,\'\'\'\',\'\')
<2>.字符串字段中含有\"\\t \\n\",如果用来在c或者c++程序中输出到文件,格式无法保证。
比如:replace(f2,\'\\t\',\'\')
<3>.清除换行和回车
比如: replace(f2,chr(13)||chr(10),\'\')
23、如何在字符串里加回车或者tab键
在sqlplus中执行
sql>select \'UserId=1233111\'||chr(10)||\'AccId=13431\'||chr(9)||\'AccId2=11111\' from dual;
24、树形查询
create table zj(
bm number(8),
bmmc varchar2(20),
sjbm number(8)
insert into zj values(1,\'aaa\',0)
insert into zj values(11,\'aaa1\',1)
insert into zj values(12,\'aaa2\',1)
insert into zj values(111,\'aaa11\',11)
insert into zj values(112,\'aaa12\',11)
insert into zj values(113,\'aaa13\',11)
insert into zj values(121,\'aaa21\',12)
insert into zj values(122,\'aaa22\',12)
insert into zj values(123,\'aaa23\',12)
select bm,bmmc,sjbm,level
from zj
start with sjbm=0
connect by prior bm = sjbm
select bm,bmmc,sjbm,level
from zj
start with sjbm=0
connect by sjbm = prior bm
25、快照
create snapshot SNAPSHOT_NAME
[storage (storage parameter)]
[tablespace TABLESPACE_NAME]
[refresh [fast\\complete\\force]
[start with START_DATE next NEXT_DATE]
as QUERY;
create snapshot snapshot_to_study as select * from TABLE_NAME@to_study;
create role aa identified by aaa;
授权 grant create snapshot,alter snapshot to aaa;
grant aaa to emp;
create snapshot SNAPSHOT_TO_HTML refresh complete start with sysdate next
sysdate+5/(24*60*60) as select * from a@to_html;
删除 drop snapshot snap_to_html
手工刷新快照,(调用DBMS_SNAPSHOT包中的refresh过程)DBMS_SNAPSHOT.refresh(snapshot_name,refresh_type);
begin
DBMS_SNAPSHOT.REFRESH(\'snap_to_html\',\'c\');
对所有快照进行刷新
begin
DBMS_SNAPSHOT.REFRESH_ALL;
怎样执行远程的内嵌过程
begin
otherdbpro@to_html(参数);
26、用户管理
create a user: database authentication
sql> create user juncky identified by oracle default tablespace users
sql> temporary tablespace temp quota 10m on data password expire
sql> [account lock|unlock] [profile profilename|default];
<1>.查看当前用户的缺省表空间
SQL>select username,default_tablespace from user_users;
<2>生成用户时指定缺省表空间
create user 用户名 identified by 口令 default tablespace 表空间名;
<3>重新指定用户的缺省表空间
alter user 用户名 default tablespace 表空间名
<4>查看当前用户的角色
SQL>select * from user_role_privs;
<5>查看当前用户的系统权限和表级权限
SQL>select * from user_sys_privs;
SQL>select * from user_tab_privs;
<6>查看用户下所有的表
SQL>select * from user_tables;
<7> alter user语句的quota子句限制用户的磁盘空间
如:alter user jf quota 10M on system;
27、查看放在ORACLE的内存区里的表
SQL>select table_name,cache from user_tables where instr(cache,\'Y\')>0;
28、约束条件
create table employee
(empno number(10) primary key,
name varchar2(40) not null,
deptno number(2) default 10,
salary number(7,2) check salary<10000,
birth_date date,
soc_see_num char(9) unique,
foreign key(deptno) references dept.deptno)
tablespace users;
关键字(primary key)必须是非空,表中记录的唯一性
not null 非空约束
default 缺省值约束
check 检查约束,使列的值符合一定的标准范围
unqiue 唯一性约束
foreign key 外部键约束
29、查看创建视图的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\');
30、查看同义词的名称
SQL>select * from user_synonyms;
31、用Sql语句实现查找一列中第N大值
select * from
(select t.*,dense_rank() over (order by sal) rank from employee)
where rank = N;
32 虚拟自段
<1>. CURRVAL 和 nextval
为表创建序列
CREATE SEQUENCE EMPSEQ ... ;
SELECT empseq.currval FROM DUAL ;
自动插入序列的数值
INSERT INTO emp
VALUES (empseq.nextval, \'LEWIS\', \'CLERK\',
7902, SYSDATE, 1200, NULL, 20) ;
<2>. ROWNUM
按设定排序的行的序号
SELECT * FROM emp WHERE ROWNUM < 10 ;
<3>. ROWID
返回行的物理地址
SELECT ROWID, ename FROM emp WHERE deptno = 20 ;
33、对CLOB字段进行全文检索
SELECT * FROM A WHERE dbms_lob.instr(a.a,\'K\',1,1)>0;
34. 特殊字符的插入,比如\"&\"
insert into a values (translate (\'at{&}t\',\'at{}\',\'at\'));
35.表管理
<1>.create a table
sql> create table table_name (column datatype,column datatype]....)
sql> tablespace tablespace_name [pctfree integer] [pctused integer]
sql> [initrans integer] [maxtrans integer]
sql> storage(initial 200k next 200k pctincrease 0 maxextents 50)
sql> [logging|nologging] [cache|nocache]
<2>.copy an existing table
sql> create table table_name [logging|nologging] as subquery
<3> create table ... as 方式建表的时候,指定表参数
create table a
storage(
initial 1M /*第一次创建时分配空间*/
next 1M /*第一次分配的存储空间用完时在分配*/
as select * from b;
<4>.创建临时表
sql> create global temporary table xay_temp as select * from xay;
on commit preserve rows/on commit delete rows
在Oracle中,可以创建以下两种临时表:
a 会话特有的临时表:
create global temporary table () on commit preserve rows;
会话指定,当中断会话时ORACLE将截断表
b 事务特有的临时表:
create global temporary table () on commit delete rows;
事务指定,每次提交后ORACLE将截断表(删除全部行)
临时表只在当前连接内有效
临时表不建立索引,所以如果数据量比较大或进行多次查询时,不推荐使用
数据处理比较复杂的时候时表快,反之视图快点
在仅仅查询数据的时候建议用游标: open cursor for \'sql clause\';
pctfree = (average row size - initial row size) *100 /average row size
pctused = 100-pctfree- (average row size*100/available data space)
<6>.change storage and block utilization parameter
sql> alter table table_name pctfree=30 pctused=50 storage(next 500k
sql> minextents 2 maxextents 100);
<7>.manually allocating extents
sql> alter table table_name allocate extent(size 500k datafile \'c:/oracle/data.dbf\');
<8>.move tablespace
sql> alter table employee move tablespace users;
<9>.deallocate of unused space
sql> alter table table_name deallocate unused [keep integer]
<10>.drop a column
sql> alter table table_name drop column comments cascade constraints checkpoint 1000;
alter table table_name drop columns continue;
<11>.mark a column as unused
sql> alter table table_name set unused column comments cascade constraints;
alter table table_name drop unused columns checkpoint 1000;
alter table orders drop columns continue checkpoint 1000
data_dictionary : dba_unused_col_tabs
37. 中文是如何排序的?
Oracle9i之前,中文是按照二进制编码进行排序的。
在oracle9i中新增了按照拼音、部首、笔画排序功能。设置NLS_SORT值
SCHINESE_RADICAL_M 按照部首(第一顺序)、笔划(第二顺序)排序
SCHINESE_STROKE_M 按照笔划(第一顺序)、部首(第二顺序)排序
SCHINESE_PINYIN_M 按照拼音排序
38. 数据表中的字段最大数:
表或视图中的最大列数为 1000
39. oracle中的裸设备:
裸设备就是绕过文件系统直接访问的储存空间
40. 在Oracle服务器上通过SQLPLUS查看本机IP地址 ?
select sys_context(\'userenv\',\'ip_address\') from dual;
如果是登陆本机数据库,只能返回127.0.0.1
41. 在ORACLE中取毫秒?
9i之前不支持,9i开始有timestamp.
9i可以用select systimestamp from dual;
42. 将N秒转换为时分秒格式?
set serverout on
declare
N number := 1000000;
ret varchar2(100);
begin
ret := trunc(n/3600) || \'小时\' || to_char(to_date(mod(n,3600),\'sssss\'),\'fmmi\"分 \"ss\"秒\"\') ;
dbms_output.put_line(ret);
43、在某个用户下找所有的索引
select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name
from user_ind_columns, user_indexes
where user_ind_columns.index_name = user_indexes.index_name
and user_ind_columns.table_name = user_indexes.table_name
order by user_indexes.table_type, user_indexes.table_name,
user_indexes.index_name, column_position;
44. not in的替代。
一般not in的效率比较低。特别是数据量大的时候,几乎不能执行。
用下面几种方式可以替换写法
比如要查询在fee_rev_info表中已经销户的用户(不在cm_user中的)(不过下面的例子不是很好,因为bill_id是cm_user的唯一索引)
select * from fee_rev_info where bill_id not in (select bill_id from cm_user)
<1> 用not exists
select * from fee_rev_info a where not exists (select \'p\' from cm_user b where b.bill_id = a.bill_id)
<2> 用外连接(+)
select a.* from fee_rev_info a,cm_user b
where a.bill_id = b.bill_id (+)
and b.bill_id is null
<3> 用hash_aj
select /*+HASH_AJ*/* from fee_rev_info where bill_id not in (select bill_id from cm_user)
45.怎么样查询特殊字符,如通配符%与_
假如数据库中有表 STATIONTYPE,STATION_571 STATION_572 ...
select * from tab where tname like \'STATION_%\'
会显示 STATIONTYPE,STATION_571 ... 可以用下面的语句
select * from tab where tname like \'STATION\\_%\' escape\'\\\'
46.如果存在就更新,不存在就插入可以用一个语句实现吗
9i已经支持了,是Merge,但是只支持select子查询,
如果是单条数据记录,可以写作select .... from dual的子查询。
MERGE INTO table
USING data_source
ON (condition)
WHEN MATCHED THEN update_clause
WHEN NOT MATCHED THEN insert_clause;
MERGE INTO cm_user_credit
USING (select * from dual) ON (user_id =1302514690 )
when MATCHED then update set credit_value = 1000
when NOT MATCHED then insert (user_id,acc_id,bill_id,plan_id,region_code,credit_value) values(1302514690,1305032158,\'13857141218\',10070247,\'571\',1000);
47.怎么实现一条记录根据条件多表插入
9i以上可以通过Insert all语句完成,仅仅是一个语句,如:
INSERT ALL
WHEN (id=1) THEN
INTO table_1 (id, name)
values(id,name)
WHEN (id=2) THEN
INTO table_2 (id, name)
values(id,name)
INTO table_other (id, name)
values(id, name)
SELECT id,name
FROM a;
如果没有条件的话,则完成每个表的插入,如
INSERT ALL
INTO table_1 (id, name)
values(id,name)
INTO table_2 (id, name)
values(id,name)
INTO table_other (id, name)
values(id, name)
SELECT id,name
FROM a;
48.如何实现行列转换
<1>、固定列数的行列转换
student subject grade
---------------------------
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
语文 数学 英语
student1 80 70 60
student2 90 80 100
语句如下:
select student,sum(decode(subject,\'语文\', grade,null)) \"语文\",
sum(decode(subject,\'数学\', grade,null)) \"数学\",
sum(decode(subject,\'英语\', grade,null)) \"英语\"
from table
group by student
<2>、不定列行列转换
c1 c2
--------------
1 我是谁
这一类型的转换必须借助于PL/SQL来完成,这里给一个例子
CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)
RETURN VARCHAR2
Col_c2 VARCHAR2(4000);
BEGIN
FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP
Col_c2 := Col_c2||cur.c2;
END LOOP;
Col_c2 := rtrim(Col_c2,1);
RETURN Col_c2;
SQL> select distinct c1 ,get_c2(c1) cc2 from table;即可
--例子:
create table okcai_1
user_id varchar2(10),
user_number varchar2(10),
user_num number(8)
user_id user_number user_num
---------------------
1 123 2
1 456 5
1 789 6
2 11 2
2 22 3
2 33 4
2 44 5
2 55 6
2 66 7
2 77 8
3 1234 1
3 5678 2
create or replace function get_col(
p_userId number,
p_col number
) return varchar
v_tmp varchar2(255);
begin
select user_number||chr(9)||user_num into v_tmp
(select user_number,user_num,rownum row_id
from okcai_1
where user_id = p_userId) a
where row_id = p_col;
return ltrim(v_tmp);
--return v_tmp;
select distinct user_id,get_col(user_id,1),get_col(user_id,2),get_col(user_id,3) .... from okcai_1
create or replace function get_col(
p_userId number,
p_col number
) return varchar
v_tmp varchar2(255);
begin
select user_number||chr(9)||user_num into v_tmp
(select user_number,user_num,rownum row_id
from okcai_1
where user_id = p_userId) a
where row_id = p_col;
return ltrim(v_tmp);
--return v_tmp;
select distinct user_id,get_col_new(user_id) from okcai_1;
49.怎么设置存储过程的调用者权限
普通存储过程都是所有者权限,如果想设置调用者权限,请参考如下语句
create or replace
procedure ...()
AUTHID CURRENT_USER
begin
50.Oracle有哪些常见关键字
详细信息可以查看v$reserved_words视图
51.怎么查看数据库参数
<1> show parameter 参数名
如通过show parameter spfile可以查看9i是否使用spfile文件
其中参数名是可以匹配的。
比如show parameter cursor ,则会显示跟cursor相关的参数
select * from v$parameter
除了这部分参数,Oracle还有大量隐含参数,可以通过如下语句查看:
SELECT NAME
,VALUE
,decode(isdefault, \'TRUE\',\'Y\',\'N\') as \"Default\"
,decode(ISEM,\'TRUE\',\'Y\',\'N\') as SesMod
,decode(ISYM,\'IMMEDIATE\', \'I\',
\'DEFERRED\', \'D\',
\'FALSE\', \'N\') as SysMod
,decode(IMOD,\'MODIFIED\',\'U\',
\'SYS_MODIFIED\',\'S\',\'N\') as Modified
,decode(IADJ,\'TRUE\',\'Y\',\'N\') as Adjusted
,description
FROM ( --GV$SYSTEM_PARAMETER
SELECT x.inst_id as instance
,x.indx+1
,ksppinm as NAME
,ksppity
,ksppstvl as VALUE
,ksppstdf as isdefault
,decode(bitand(ksppiflg/256,1),1,\'TRUE\',\'FALSE\') as ISEM
,decode(bitand(ksppiflg/65536,3),
1,\'IMMEDIATE\',2,\'DEFERRED\',\'FALSE\') as ISYM
,decode(bitand(ksppstvf,7),1,\'MODIFIED\',\'FALSE\') as IMOD
,decode(bitand(ksppstvf,2),2,\'TRUE\',\'FALSE\') as IADJ
,ksppdesc as DESCRIPTION
FROM x$ksppi x
,x$ksppsv y
WHERE x.indx = y.indx
AND substr(ksppinm,1,1) = \'_\'
AND x.inst_id = USERENV(\'Instance\')
ORDER BY NAME
52.怎样建立基于函数索引
8i以上版本,确保
Query_rewrite_enabled=true
Query_rewrite_integrity=trusted
Compatible=8.1.0以上
Create index indexname on table (function(field));
53.怎么样移动表或表分区
[A]移动表的语法
Alter table tablename move
[Tablespace new_name
Storage(initial 50M next 50M
pctincrease 0 pctfree 10 pctused 50 initrans 2) nologging]
移动分区的语法
alter table tablename move (partition partname)
[update global indexes]
之后之后必须重建索引
Alter index indexname rebuild
如果表有Lob段,那么正常的Alter不能移动Lob段到别的表空间,而仅仅是移动了表段,可以采用如下的方法移动Lob段
alter table tablename move
lob(lobsegname) store as (tablespace newts);
54.怎么样修改表的列名
[A]9i以上版本可以采用rname命令
ALTER TABLE UserName.TabName
RENAME COLUMN SourceColumn TO DestColumn
9i以下版本可以采用create table …… as select * from SourceTable的方式。
另外,8i以上可以支持删除列了
ALTER TABLE UserName.TabName
SET UNUSED (ColumnName) CASCADE CONSTRAINTS
ALTER TABLE UserName.TabName
DROP (ColumnName) CASCADE CONSTRAINTS
55.case的用法
在sql语句中
CASE test_value
WHEN expression1 THEN value1
[[WHEN expression2 THEN value2] [...]]
[ELSE default_value]
SELECT last_name, job_id, salary
CASE job_id
WHEN \'IT_PROG\' THEN 1.10*salary
WHEN \'ST_CLERK\' THEN 1.15*salary
WHEN \'SA_REP\' THEN 1.20*salary
ELSE salary END \"REVISED_SALARY\"
FROM employees
select
when real_charge>=20000 and real_charge<30000 then 5000
when real_charge>=30000 and real_charge<40000 then 9000
when real_charge>=40000 and real_charge<50000 then 10000
when real_charge>=50000 and real_charge<60000 then 14000
when real_charge>=60000 and real_charge<70000 then 18000
when real_charge>=70000 and real_charge<80000 then 19000
when real_charge>=80000 and real_charge<90000 then 24000
when real_charge>=90000 and real_charge<100000 then 27000
when real_charge>=100000 and real_charge<110000 then 27000
when real_charge>=110000 and real_charge<120000 then 29000
when real_charge>=120000 then 36000
end ,acc_id,user_id,real_charge from okcai_jh_charge_200505
在存储过程中
case v_strGroupClassCode
when \'1\' then
v_nAttrNum := v_nAttrNum + 300;
v_strAttrFlag := \'1\'||substr(v_strAttrFlag,2,7);
when \'2\' then
v_nAttrNum := v_nAttrNum + 200;
v_strAttrFlag := \'2\'||substr(v_strAttrFlag,2,7);
NULL;
end case;
注意的是存储过程和sql语句有的细微差别是用end case,而不是end。语句后面跟\";\"
先讲下我遇到的情况:
有一张表a,已经存在一个字段该字段是date类型,需求将该字段改为varchar2()类型,我们都知道,
Oracle在该字段有值情况是不可以更新数据的,如果你不在乎该字段在表中位置变化的话,可以方法一操作:
方法一:新增一个字段,然后把原列数据复制到这个列下,然后把原字段删除,把新增的字段重命名。
如果不想改变列位置但需要改变类型,请看下面:
oracle支持快
SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003
SQL> connect / as sysdba
Connected to an idle instance.
SQL> startup^C
SQL> startup
ORACLE instance started.
2、在双机环境下
要想启动或关闭ORACLE系统必须首先切换到root用户,如下
su - root
a、启动ORACLE系统
hareg -y oracle
b、关闭ORACLE系统
hareg -n oracle
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用户常用数据字典的查询使用方法。
查看当前用户的缺省表空间
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;
查看用户下所有的表
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;
查看索引个数和类别
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;
查看视图的名称
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、表空间的创建、删除
六、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
RETURN EXPR1
DECODE(AA﹐V1﹐R1﹐V2﹐R2....)函数
IF AA=V1 THEN RETURN R1
IF AA=V2 THEN RETURN R2
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 单字节
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 ‘数据库连接字符串’;
数据库连接字符串可以用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
//备份表
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;
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
//在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 备份 SYSTEM tablespace
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
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 备份 RBS tablespace
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
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.
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM 备份 INDEXES tablespace
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM 备份 TEMP tablespace
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM Step 2. 备份归档日志文件.
archive log stop
REM Exit Server Manager, using the indicator set earlier.
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;
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’;
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 Exit Server Manager using the indicator set earlier.
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;
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 Back up the RBS tablespace - to another disk (UNIX)
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
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;
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@
// 在(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 的条目
(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
(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
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
//在lsnrctl 内列出所有的服务
set password lsnr_password
services
//启动或停止一个NT的listener
net start OracleTNSListener
net stop OracleTNSListener
// 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;
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
//在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 备份 SYSTEM tablespace
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
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 备份 RBS tablespace
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
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.
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM 备份 INDEXES tablespace
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM 备份 TEMP tablespace
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM Step 2. 备份归档日志文件.
archive log stop
REM Exit Server Manager, using the indicator set earlier.
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;
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’;
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 Exit Server Manager using the indicator set earlier.
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;
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 Back up the RBS tablespace - to another disk (UNIX)
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
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;
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@
// 在(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
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;
insert backup commands like the "tar" commands here
svrmgrl <<EOF2
connect internal as sysdba
startup
//在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 备份 SYSTEM tablespace
alter tablespace SYSTEM begin backup;
!tar -cvf /dev/rmt/0hc /db01/oracle/CC1/sys01.dbf
alter tablespace SYSTEM end backup;
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 备份 RBS tablespace
alter tablespace RBS begin backup;
!tar -rvf /dev/rmt/0hc /db02/oracle/CC1/rbs01.dbf
alter tablespace RBS end backup;
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.
alter tablespace DATA begin backup;
!tar -rvf /dev/rmt/0hc /db03/oracle/CC1/data0*.dbf
alter tablespace DATA end backup;
REM 备份 INDEXES tablespace
alter tablespace INDEXES begin backup;
!tar -rvf /dev/rmt/0hc /db04/oracle/CC1/indexes01.dbf
alter tablespace INDEXES end backup;
REM 备份 TEMP tablespace
alter tablespace TEMP begin backup;
!tar -rvf /dev/rmt/0hc /db05/oracle/CC1/temp01.dbf
alter tablespace TEMP end backup;
REM Follow the same pattern to back up the rest
REM of the tablespaces.
REM Step 2. 备份归档日志文件.
archive log stop
REM Exit Server Manager, using the indicator set earlier.
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;
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’;
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 Exit Server Manager using the indicator set earlier.
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;
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 Back up the RBS tablespace - to another disk (UNIX)
alter tablespace RBS begin backup;
!cp /db02/oracle/CC1/rbs01.dbf /db10/oracle/CC1/backups
alter tablespace RBS end backup;
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;
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@
// 在(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
……………………………………………………………………………………
Mybaitis foreach批量insert 以及配合 oracle merge into 函数,批量update和insert
oracle中相当于mysql的replace into函数:merge into
因为作者使用国产的神通数据库 写法与oracle相同 没办法使用mysql的replace into实现插入
使用merge into来代替 不太推荐使用这个 能不用尽量不用吧
xml中
<update id="mergeStudents" parameterTy
当您需要在 Oracle 表的某列中进行文字截取时,可以使用 SUBSTR 函数来实现。SUBSTR 函数用于从字符串中截取指定长度的子字符串。
以下是一个示例,假设您有一个名为 "employees" 的表,其中包含一个名为 "full_name" 的列,存储了员工的全名。您想要从该列中截取出姓氏。
```sql
SELECT SUBSTR(full_name, 1, INSTR(full_name, ' ') - 1) AS last_name
FROM employees;
在上述示例中,SUBSTR 函数的第一个参数是要截取的字符串(即 full_name 列),第二个参数是截取的起始位置,第三个参数是截取的长度。INSTR 函数用于查找字符串中的空格,并返回其位置。
通过执行以上 SQL 语句,您将获得一个结果集,其中包含了从 "full_name" 列中截取出的姓氏。
请注意,这只是一个简单的示例,实际情况可能会有所不同,具体的截取方式可能需要根据您的数据结构和需求进行调整。
2、声明不用final,但是变量的可访问范围内无修改
3、对2补充,既不能在lambda外边修改该变量也不能再lambda内部修改
4、final List<Object> 这种数组,里边内容可修改,但变量引用不能被修改
这样lambda外边的代释执行完,变量释放了,也不影响lambda中的变量引用。
简单记忆lambda变量引用:就是final+值传递,
[/code]