Oracle创建用户空间

CREATE TEMPORARY TABLESPACE ATS_TEMP
TEMPFILE 'D:\Oracle11g\oradata\orcl\ATS_TEMP.DBF' SIZE 200M REUSE AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED;

CREATE TABLESPACE ATS_PACS
DATAFILE 'D:\Oracle11g\oradata\orcl\xxxx.DBF' 
SIZE 200M REUSE AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
LOGGING
ONLINE
BLOCKSIZE 8K
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT MANUAL;

CREATE TABLESPACE ATS_AIGATE
DATAFILE 'D:\oracle\product\10.2.0\db_1\oradata\orcl\xxxxx.DBF' 
SIZE 200M REUSE AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
LOGGING
ONLINE
BLOCKSIZE 8K
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT MANUAL;


CREATE USER AIGATE IDENTIFIED BY AIGATE         
DEFAULT TABLESPACE ATS_AIGATE
TEMPORARY TABLESPACE ATS_TEMP
PROFILE DEFAULT;


GRANT DBA TO AIGATE WITH ADMIN OPTION;

RAC连接字符串

前几天用OB9导入导出表空间的时候,发现一个问题,
如果RAC连接字符串写的不全,是无论如何都导不进去的,
而OB9大哥的连接字符串却又有长度限制,可怜我一个晚上啊。
最后改用监听,OK了。

(DESCRIPTION=(ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.220)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.221)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.222)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.223)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=on))(CONNECT_DATA=(SERVICE_NAME=ractest)))

OB9导入导出数据库,也是用了oracle自带的命令行工具(imp.exe和exp.exe)

导入时,其调用命令行就是:

D:\oracle\product\10.2.0\db_1\BIN\imp.exe 'user2/pass2@(DESCRIPTION=(ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.220)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.221)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.222)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.223)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=on))(CONNECT_DATA=(SERVICE_NAME=ractest)))' PARFILE="D:\OB9\temp\Imp.PRM"

而Imp.PRM是一个配置文件

FILE="D:\OB9\XXX.DMP"
LOG="D:\OB9\IMP.LOG"
BUFFER=4096
RECORDLENGTH=1024
GRANTS=Y
INDEXES=Y
ROWS=Y
IGNORE=Y
COMMIT=N
DESTROY=N
FROMUSER=(
"user1"
)
TOUSER=(
"user2"
)

导出时,其调用命令行就是:

D:\oracle\product\10.2.0\db_1\BIN\exp.exe 'user1/pass1@(DESCRIPTION=(ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.220)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.221)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.222)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.120.223)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=on))(CONNECT_DATA=(SERVICE_NAME=ractest)))' PARFILE="D:\OB9\temp\Exp.PRM"

而Exp.PRM是一个配置文件

FILE="D:\OB9\xxx.DMP"
LOG="D:\OB9\EXP.LOG"
BUFFER=4096
RECORDLENGTH=1024
COMPRESS=Y
GRANTS=Y
INDEXES=Y
ROWS=Y
CONSTRAINTS=Y
RECORD=Y
CONSISTENT=N
STATISTICS=NONE
OWNER=(
"user1"
)

Oracle10g RAC连接错误

今天和潘潘一起测RAC,结果出现了很神奇的事情
相同配置的几台机器,有的连接RAC正常,有几台确死活连接不上
郁闷了一个下午。

开始怀疑是操作系统版本和环境变量的事情,发现没有任何规律。
后来怀疑是驱动的问题,换了n个驱动,还是不行。
管理工具和测试小程序却都很争气的连接成功。

后来改用OCI连接,结果终于返回了一个错误编码ORA-12545。
原来是Oracle RAC服务端监听的问题。(命名是个大Bug,Oracle还不承认)

解决方法:修改监听参数

--RAC服务器1
ALTER SYSTEM SET LOCAL_LISTENER = '(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.120.81)(PORT = 1521))' SID = 'ractest1';
--RAC服务器2
ALTER SYSTEM SET LOCAL_LISTENER = '(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.120.82)(PORT = 1521))' SID = 'ractest2';

修改监听后,终于好了。

后面查到还有一种修改方式,
在RAC的每个节点上编辑$ORACLE_HOME/network/admin/tnsnames.ora,增加

local_listener_rac=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=<vip>)(PORT=1521))
)

其中对应每个节点各自的虚拟ip地址;

然后,在rac的任意一个节点上修改数据库参数,设置local_listener为local_listener_rac

alter system set local_listener='local_listener_rac' scope=spfile;

重新启动各个节点上的数据库实例,应该就好了,这个没测过,呵呵。

SQL查询100到200行

--SQL Server
--top
select top 100 * from table
where id is not in(select top 100 id from table)
--row_number()
select r.*, row_number() over(order by id desc) as r from table where r>100 and r<=200


--MySQL
--limit
select * from  table limit 100,200;


--Oracle
--rownum
select * from (select rownum r,t.* from table t) where r>100 and r<=200;

SQL获取自增字段的值

数据插入后,如何获取自增字段的数值呢?可以用下面的方法
(建议进行事务控制)

--SQL Server
--当前会话,当前作用域
select SCOPE_IDENTITY() as id
--当前会话,不限定作用域
select @@identity as id
--指定表,不限定会话和作用域
select IDENT_CURRENT ('table_name')


--MySQL
--当前会话
SELECT LAST_INSERT_ID();
select @@IDENTITY as id


--Oracle
--当前会话
select seq_name.currval from dual

CMD常用命令02定期备份Oracle

项目实施后,为了以防万一,决定每天夜里备份数据库
虽然Oracle本身就有自动备份的功能,但我仍然不放心于是用bat和windows的任务计划进行了备份

REM HANSEN 2011-03-31
REM 用于备份Oracle数据库
color 2

set FilePath=S:/Backup/
set UserName=user
set PassWord=pwd
set ServiceName=orcl

if not exist "%FilePath%" (exit)

set FileName=%DATE:~0,10%-%TIME:~0,8%
set FileName=%FileName::=-%

set FileName=%FileName: =0%
set DBFile=%FilePath%%FileName%.dmp
set LogFile=%FilePath%%FileName%.log

echo ORACLE数据库备份开始
echo %DATE:~0,10% %TIME:~0,8%
echo ORACLE数据库备份数据保存到%DBFile%
echo ORACLE数据库备份日志保存到%LogFile%

exp %UserName%/%PassWord%@%ServiceName% file=%DBFile% log=%LogFile% compress=y direct=n rows=y 
owner='%UserName%' consistent=n constraints=y grants=y indexes=y triggers=y

另外又加了个定期删除的bat,但这个有些问题的,就是只考虑了备份文件的创建时间,并没有考虑到要保留几个备份
换句话说,当连续7天备份失败后,就没有备份了,所以请修改一下再用

Rem Delete Files Older Than One Week
set FilePath=E:/Backup/
forfiles /p %FilePath% /s /m *.dmp /d -7 /c "cmd /c del @file
forfiles /p %FilePath% /s /m *.log /d -7 /c "cmd /c del @file

查询Oracle版本

方法1:

SQL> select * from v$version;
 
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
PL/SQL Release 9.2.0.1.0 - Production
CORE	9.2.0.1.0	Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production

方法2:

SQL> select * from product_component_version;
 
PRODUCT                        VERSION                        STATUS
------------------------------ ------------------------------ ------------------------------
NLSRTL                         9.2.0.1.0                      Production
Oracle9i Enterprise Edition    9.2.0.1.0                      Production
PL/SQL                         9.2.0.1.0                      Production
TNS for 32-bit Windows:        9.2.0.1.0                      Production