正在加载...
分页: 10/12 第一页 上页 5 6 7 8 9 10 11 12 下页 最后页 [ 显示模式: 摘要 | 列表 ]
      InnoDB和MyISAM是在使用mysql最常用的两个表类型,各有优缺点,视具体应用而定.下面是已知的两者之间的差别,仅供参考.

1.InnoDB不支持FULLTEXT类型的索引.

2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行,但是MyISAM只要简单的读出保存好的行数即可.注意的是,当count(*)语句包含 where条件时,两种表的操作是一样的.

3.对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引.

4.DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除.

5.LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用.

另外,InnoDB表的行锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表,例如update table set num=1 where name like "%aaa%" 任何一种表都不是万能的,只用恰当的针对业务类型来选择合适的表类型,才能最大的发挥MySQL的性能优势.
Tags: , ,
今天了一个问题用phpmyadmin不能正常登入,提示错误
2002 - 服务器没有响应 (or the local MySQL server's socket is not correctly configured)
但是mysql -u root -p一切正常,我 google 了一下,发现很多人被这个问题困扰,大家解决方法的思路都是围绕 mysql.sock,改mysql.sock的位置.

而实际上,通过修改 mysql.sock 路径解决问题的可能性很小. 大部分这类问题是由于 mysql 做了 ip 绑定(bind-address = 127.0.0.1)造成的.这里只需要将 $cfg['Servers'][$i]['host'] = 'localhost' 改为 '127.0.0.1' 就可以了.或者改成localhost.localdomain,这样改的依据是more /etc/hosts,可以看到localhost和localhost.localdomain被解析为127.0.0.1的,而mysql的my.cnf里面大家都加了bind-address = 127.0.0.1,所以要修改$cfg['Servers'][$i]['host'].

也可以在在config.inc.php配置文件里,添加上:$cfg['Servers'][$i]['socket'] = '/var/mysql/mysql.sock'; (指定mysql的正确路径)
Tags: ,
      TCMalloc(Thread-Caching Malloc)是google开发的开源工具──"google-perftools"中的成员。与标准的glibc库的malloc相比,TCMalloc在内存的分配上效率和速度要高得多,可以在很大程度上提高MySQL服务器在高并发情况下的性能,降低系统负载。
为MySQL添加TCMalloc库的安装步骤(Linux环境):

  1、64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。

wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0

Oracle 常用命令(二)

[ 2009/02/28 19:03 | by selboo ]
//创建一个控制文件命令到跟踪文件
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 <connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <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 <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 <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 <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 <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 <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 <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@

// 在(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 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 <connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <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 <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 <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 <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 <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 <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 <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@

// 在(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 <connect internal as sysdba
shutdown immediate;
exit
EOF1
insert backup commands like the "tar" commands here
svrmgrl <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 <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 <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 <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 <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 <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 <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@

// 在(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 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
一.数据控制语句 (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 单字节

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
分页: 10/12 第一页 上页 5 6 7 8 9 10 11 12 下页 最后页 [ 显示模式: 摘要 | 列表 ]