刚开始使用Oracle,做一些笔记以积累。
2009年11月6日 项目DSSC,服务器上创建数据库并导入数据。
创建表空间:
create tablespace ts_stone
logging
datafile 'xxx.dbf'
size 100m
autoextend on
next 100m maxsize 2048m
extent management local;
临时表空间:
create temporary tablespace temp_ts_stone
tempfile 'xxxxx.dbf'
size 50m
autoextend on
next 50m maxsize 2048m
extent management local;
创建用户并指定表空间:
create user stone identified by pwd_stone
default tablespace ts_stone
temporary tablespace temp_ts_stone;
用户授权:
grant connect, resource to stone
导出数据:
exp stone/pwd_stone@ts_stone file='D\oracle\data\ts_stone.dmp' full=y
exp stone/pwd_stone@ts_stone file='D\oracle\data\ts_stone.dmp' owner=stone
exp stone/pwd_stone@ts_stone file='D\oracle\data\ts_stone.dmp' tables=(tableA, tableB, tableC)
导入数据:
imp stone/pwd_stone@ts_stone file=xxx.dmp tables=(tableA, tableB, tableC)
imp stone/pwd_stone@ts_stone file=xxx.dmp full=y ignore=y ......
要显示1到2行则可以通过
select * from dangan where rownum between 1 and 2
在Oracle
中,要按特定条件查询前N条记录,用个rownum
就搞定了。
select *
from emp where rownum
<= 5
而且书上也告诫,不能对rownum
用">",这也就意味着,如果你想用
select * from emp
where rownum
> 5
则是失败的。要知道为什么会失败,则需要了解rownum
背后的机制:
1 Oracle
executes your query.
2 Oracle
fetches the first row and calls it row number 1.
3 Have we gotten past row number meets the criteria? If no, then
Oracle
discards the row, If yes, then Oracle
return
the row.
4 Oracle
fetches the next row and advances the row number (to 2,
and then to 3, and then to 4, and so forth).
5 Go to step 3.
了解了原理,就知道rownum
>不会成功,因为在第三步的时候查询出的行已经被丢弃,第四步查出来的rownum
仍然是1,这样永远也不会成功。
同样道理,rownum
如果单独用=,也只有在rownum
=1时才有用。
对于rownum
来说它是oracle
系统顺序分配为从查询返回的行的编号, ......
之前对ORACLE中的变量一直没个太清楚的认识,比如说使用:、&、&&、DEIFINE、VARIABLE……等等。今天正好闲下来,上网搜了搜相关的文章,汇总了一下,贴在这里,方便学习。
==================================================================================
在oracle 中,对于一个提交的sql语句,存在两种可选的解析过程, 一种叫做硬解析,一种叫做软解析.
一个硬解析需要经解析,制定执行路径,优化访问计划等许多的步骤.硬解释不仅仅耗费大量的cpu,更重要的是会占据重要的们闩(latch)资源,严重的影响系统的规模的扩大(即限制了系统的并发行),而且引起的问题不能通过增加内存条和cpu的数量来解决。之所以这样是因为门闩是为了顺序访问以及修改一些内存区域而设置的,这些内存区域是不能被同时修改。当一个sql语句提交后,oracle会首先检查一下共享缓冲池(shared pool)里有没有与之完全相同的语句,如果有的话只须执行软分析即可,否则就得进行硬分析。
而唯一使得oracle 能够重复利用执行计划的方法就是采用绑定变量。绑定变量的实质就是用于替代sql语句中的常量的替代变量。绑定变量能够使得每次提交的sql语句都完全一样 ......
--行列转换 行转列
DROP TABLE t_change_lc;
CREATE TABLE t_change_lc (card_code VARCHAR2(3), q NUMBER, bal NUMBER);
INSERT INTO t_change_lc
SELECT '001' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4
UNION
SELECT '002' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4;
SELECT * from t_change_lc;
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
--行列转换 列转行
DROP TABLE t_change_cl;
CREATE TABLE t_change_cl AS
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
SELECT * from t_change_cl;
SELECT t.card_code,
t.rn q,
decode(t.rn, 1, t.q1, 2, t.q2, 3, t.q3, 4, t ......
--行列转换 行转列
DROP TABLE t_change_lc;
CREATE TABLE t_change_lc (card_code VARCHAR2(3), q NUMBER, bal NUMBER);
INSERT INTO t_change_lc
SELECT '001' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4
UNION
SELECT '002' card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal from dual CONNECT BY ROWNUM <= 4;
SELECT * from t_change_lc;
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
--行列转换 列转行
DROP TABLE t_change_cl;
CREATE TABLE t_change_cl AS
SELECT a.card_code,
SUM(decode(a.q, 1, a.bal, 0)) q1,
SUM(decode(a.q, 2, a.bal, 0)) q2,
SUM(decode(a.q, 3, a.bal, 0)) q3,
SUM(decode(a.q, 4, a.bal, 0)) q4
from t_change_lc a
GROUP BY a.card_code
ORDER BY 1;
SELECT * from t_change_cl;
SELECT t.card_code,
t.rn q,
decode(t.rn, 1, t.q1, 2, t.q2, 3, t.q3, 4, t ......
oracle 存储过程的基本语法 及注意事项
oracle 存储过程的基本语法
1.基本结构
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER,
参数2 IN NUMBER
) IS
变量1 INTEGER :=0;
变量2 DATE;
BEGIN
END 存储过程名字
2.SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 变量1,变量2 from typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
3.IF 判断
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
4.while 循环
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
5.变量赋值
V_TEST := 123;
6.用for in 使用cursor
...
IS
CURSOR cur IS SELEC ......
1: 加大回滚段(可以给500M甚至1G)
2:分段commit
iCount :=1;
for rec in cur_name loop
insert into table_name (.....);//DML Lanaguage
if iCount =2000 then
commit;
iCount:=0;
else
iCount:= iCount +1;
end if;
end loop; ......