首选启动数据库
su - oracle
lsnrctl start
sqlplus /nolog
conn /as sysdba
startup
startup force
sqlplus "/as sysdba"
conn test01/aaaAAA111@orcl
conn system/aaaAAA111@orcl as sysdba
select * from dict; 显示当前用户可访问的所有的数据字典视图
select * from dict_columns; 显示当前用户可访问的所有的数据字典视图每个列的作用
select * from global_name; 显示当前数据库的全名
lsnrctl start
sqlplus '/as sysdba'
sql> startup
emctl start dbconsole
emctl stop dbconsole
isqlplusctl start
select * from dba_ts_quotas ;
grant create session ,create table to test4 with admin option ; 系统权限的授权
grant index on sys.t1 to test4 ; 用户权限的授权
create directory dump_dir2 as '/oradata/db_bak' ;
chmod -R 777 /oradata/db_bak
expdp system/oracle directory=dump_dir2 dumpfile=tab.dmp tables=scott.test1 导出表
expdp system/oracle directory=dump_dir2 dumpfile=schema.dmp schemas=system,test4 导出方案
expdp system/oracle directory=dump_dir2 dumpfile=tablespace.dmp tablespaces=users,test02 导出表空间
expdp system/oracle directory=dump_dir2 dumpfile=dump_dir2 FULL=y 导出整个数据库
select * from dba_users;
select * from dba_sys_privs;
select * from dba_tab_privs;
select * from dba_role_privs;
select * from global_name;
select * from obj;
select * from seq;
select * from tab;
select * from v$fixed_table; 显示所有的动态性能表
select * from v$instance; 显示当前例程的详细信息
select * from v$vga; 显示SGA 的主要组成部分。
select * from v$sgainfo; 显示当前SGA 详细信息。
select * from v$parameter; 显示动态性能视图的详细信息。
select * from v$version;
select * from v$option; 显示安装ORACLE 的选项
select * from v$session; 显示会话信息
select * from V$process; 显示与ORACLE 相关的所有的进程的信息
select * from v$bgprocess; 显示后台进程的信息。
select * from v$database; 显示当前DATABASE 的祥细信息
select * from v$controlfile; 显示数据库的所有的控制文件的信息。
select * from V$datafile; 显示数据库的数据文件的信息
select * from V$lock; 显示锁的信息
select * from V$locked_object; 显示被加锁的数据库的对象
select * from v$tablespace; 显示表空间的信息。
select log_mode from v$database; 显示当前的日志操作模式
select * from dba_users;
select * from dba_sys_privs;
select * from dba_tab_privs;
select * from dba_role_privs;
select * from global_name;
select * from obj;
select * from seq;
select * from tab;
select * from v$fixed_table; 显示所有的动态性能表
select * from v$instance; 显示当前例程的详细信息
select * from v$vga; 显示SGA 的主要组成部分。
select * from v$sgainfo; 显示当前SGA 详细信息。
select * from v$parameter; 显示动态性能视图的详细信息。
select * from v$version;
select * from v$option; 显示安装ORACLE 的选项
select * from v$session; 显示会话信息
select * from V$process; 显示与ORACLE 相关的所有的进程的信息
select * from v$bgprocess; 显示后台进程的信息。
select * from v$database; 显示当前DATABASE 的祥细信息
select * from v$controlfile; 显示数据库的所有的控制文件的信息。
select * from V$datafile; 显示数据库的数据文件的信息
select * from V$lock; 显示锁的信息
select * from V$locked_object; 显示被加锁的数据库的对象
select * from v$tablespace; 显示表空间的信息。
select log_mode from v$database; 显示当前的日志操作模式
select * from v$tablespace; 显示表空间
select * from dba_data_files; 显示表空间所包含的数据文件
select * from v$datafile; 数据文件的信息
select * from dba_segments; 段信息
select * from dba_sys_privs;
select * from dba_tab_privs ;
select * from dba_col_privs;
select * from dba_roles;
select * from dba_role_privs ;
select * from session_roles; 显示当前会话所激活的角色
select * from dba_users;
select * from dba_profiles;
select * from dba_tables;
select * from dba_tab_columns where rownum < 9000 ;
grant sysdba to panxianpei; 授予管理员权限
grant sysdba to scott;
sqlplus "scott/aaaAAA111" as sysdba 现在就可以用管理员权限来连接
create user test identified by aaaAAA111 default tablespace users temporary tablespace temp quota 3G on users;
alter user test identified by aaaAAA111; 改用户口令
alter user test quota 10G on users; 修改用户的配额。
select * from from v$session where username='U1';
alter system kill session '8,25'; 查看用户的会话,并终止会话。
select * from v$pwfile_users; 显示特权用户
select * from dba_users;
show user ;
select * from v$pwfile_users;
select * from dba_users ;
select * from dba_users where account_status='OPEN';
select * from dba_indexes ;
select * from dba_views ;
select * from dba_tablespaces;
select * from dba_directories ;
grant create any directory to system ;
select * from dba_sys_privs where grantee='SYSTEM';
create directory backup_01 as '/u01/app/backup/backup_01' ;
select * from dba_directories ;
drop directory backup02;
drop user scott2 cascade ;
select * from v$parameter ;
select * from dba_tables where tablespace_name='USERS' ;
flashback table EMP to before drop ; 恢复EMP表
select * from V$instance ;
select * from v$database ;
select * from v$log;
select * from V$logfile ; 查看日志成员
select * from v$pwfile_users;
select * from v$bgprocess ;
select * from V$controlfile;
select * from v$logfile ;
select * from v$archived_log ;
select * from v$archive_dest ;
rman target sys/aaaAAA111@orcl nocatalog
shutdown immediate
startup mount ;
backup database format='/u01/app/oracle/%d_%s.dbf';
rman target sys/aaaAAA111@orcl nocatalog
shutdown immediate
startup mount;
startup mount ;
backup database format='/u01/app/oracle/%d_%s.dbf';
select * from dba_data_files;
select * from dba_tablespaces ;
rman target sys/aaaAAA111@orcl nocatalog
shutdown immediate
startup mount
backup tablespace user01 format='/u01/app/oracle/%N_%s.dbf' 备份表空间
backup datafile 7 format='/u01/app/oracle/%N_%f_%s.dbf'; 备份数据文件
backup current controlfile format='/u01/app/oracle/%d_%s.ctl'; 备份控制文
backup datafile 7 format='/u01/app/oracle/%d_%s.dbf'
include current controlfile ; 备份数据文件时,同时备份控制文件
backup spfile format='/u01/app/oracle/%d_%s.par'; 备份参数文件
backup format ='u01/app/oracle/%d_%s.bak' tablespace user01 plus archivelog ;
备份数据文件和归档日志
backup incremental level 0 format='/u01/app/oracle/%d_%s.dbf' tablespace user01 ;
backup incremental level 2 format='/u01/app/oracle/%d_%s.dbf' tablespace user01 ;
增量备份
backup incremental level 2 cumulative format='/u01/app/oracle/%d_%s.dbf' tablespace user01 ;
backup incremental level 1 format='/u01/app/oracle/%d_%s.dbf' tablespace user01 ;
累积备份
select * from v$archived_log ; 显示归档日志的备份信息
select * from v$backup_datafile ; 显示控制文件和数据文件的备份信息
select * from v$backup_redolog ; 显示归档日志备份集的信息
select * from v$backup_set ; 显示备份集的详细的信息
select * from v$backup_corruption ; 显示在执行backup命令时所检测到的损坏数据块信息
select * from v$copy_corruption ; 显示在执行copy命令时所检测到的损坏数据块的信息
select * from v$recover_file ;
set nls_date_format=yyyy-mm-dd hh24:mi:88
rman target sys/aaaAAA111@orcl nocatalog
run {
startup force mount ;
set until time='2012-01-31 05:47:00';
restore database ;
recover database ;
sql ' alter database open resetlogs ';
}
select * from v$database ;
select * from v$recover_file ;
set nls_date_format=yyyy-mm-dd hh24:mi:88
rman target sys/aaaAAA111@orcl nocatalog
run {
startup force mount ;
set until time='2012-01-31 05:47:00';
restore database ;
recover database ;
sql ' alter database open resetlogs ';
}
select * from v$database ;
rman target sys/aaaAAA111@orcl nocatalog
report schema ;
使用FlashBack 查询实现行级恢复
select * from test as of timestamp TO_TIMESTAMP('2012-11-06 14:00:00','YYYY-MM-DD HH24:MI:SS');
insert into test select * from test as of timestamp TO_TIMESTAMP('2012-11-06 14:00:00','YYYY-MM-DD HH24:MI:SS');
commit ;
select * from test
使用FlashBack 恢复到先前状态
flashback table test to TIMESTAMP TO_TIMESTAMP ('2012-11-06 14:02:00','YYYY-MM-DD HH24:MI:SS') ;
select * from dba_users;
select * from v$pwfile_users ;
alter user test2 default tablespace users ;
flashback table sys.test to TIMESTAMP TO_TIMESTAMP ('2012-11-06 14:02:00','YYYY-MM-DD HH24:MI:SS') ;
create table test3 (id number(10), name varchar(20));
create table test3 (id number(10), name varchar(20));
insert into test3 (id,name) values ('239','liu') ;
insert into test3 (id,name) values ('220','zhang') ;
commit ;
select * from test3 ;
alter table test4.test3 enable row movement ;
flashback table test4.TEST3 to TIMESTAMP TO_TIMESTAMP ('2012-11-06 14:38:00','YYYY-MM-DD HH24:MI:SS') ;
flashback table test4.test3 to scn 135343 ;
select * from test4.test3 ;
flashback database to scn 2346442 ;
select log_mode from v$database ;
select current_scn from v$database ;
alter database add supplemental log data ; 激活补充日志
alter system set utl_file_dir='/oradata/oradb/' scope=spfile ;
shutdown immediate ;
startup ;
execute dbms_logmnr_d.build('dict.ora','/oradata/oradb/',dbms_logmnr_d.store_in_flat_file)
select * from V$PWFILE_USERS ;
select * from V$recovery_log ;
select * from tab;
select * from v$fixed_table ;
select * from v$instance ;
select * from v$sga ;
select * from v$sgainfo ;
select * from v$version ;
select * from v$option ;
select * from v$session;
select * from v$process ;
select * from v$bgprocess ;
select * from v$database ;
select * from v$controlfile ;
select name from v$datafile ;
select * from v$dbfile ;
select * from v$logfile ;
select * from v$log ;
select * from v$tablespace ;
select * from V$tempfile ;
select log_mode from v$database ;
select * from session_roles ;
select profile from dba_users ;
select * from DBA_VIEWS ;
create user user01 identified by user01;
grant sysdba to user01 ;
select * from global_name ;
select * from dual ;
select * from V$sgainfo ;
select * from v$datafile ;
select * from v$logfile ;
select * from v$tablespace ;
select * from v$tempfile ;
select * from V$CONTROLFILE;
alter database add logfile size 2G ;
select * from dba_users;
select * from v$pwfile_users;
alter user user01 identified by aaaAAA111 ;
alter user user01 default tablespace users ;
alter database archivelog ;
archive log list ; 显示归档信息
alter user user01 quota 100G on users ;
select * from v$log_history ; 显示重做线程的日志历史记录
archive log list ;
select * from v$archived_log ; 显示归档日志信息
select * from v$archive_dest ; 显示示归档日志位置
select * from v$loghist ; 显示日志历史信息
select * from V$ARCHIVE_PROCESSES ; 显示归档进程信息
alter database begin backup ;
alter database end backup ;
alter system archive log current ;
select * from v$backup ; 确定处于联机备份状态的所有的数据文件
SQL> recover datafile 4 ; 结束联机备份
select * from dba_tablespaces ;
select * from dba_data_files;
select * from V$CONTROLFILE ;
alter database backup CONTROLFILE to '/oradata/oradb/control04.ctl' reuse; 备份控制文件,如果有的话,则复盖
select * from V$ARCHIVED_LOG ; 备份归档日志文件
alter tablespace users begin backup ;
host cp /oradata/oradb/user01.dbf /oradata/backup
alter TABLESPACE users end backup ;
基于时间的恢复
shutdown immediate ;
startup mount ;
select * from v$recover_file ;
recover database until time '2012-10-26 03:00:00 '
alter database open resetlogs ;
alter database begin backup ;
host cp /oradata/oradb/system01.dbf /oradata/oradb/system01_bak.dbf
alter database end backup ;
alter database backup controlfile to '/oradata/oradb/oradb.ctl_bak 'reuse ;
alter system archive log current ;
select * from DBA_TABLES where tablespace_name='users ';
create table test3 as select * from def$_lob ;
select * from DBA_TABLES where table_name ='test6';
create table test6 (id number(10), name varchar(20));
insert into test6 (id,name) values ('239','liu') ;
insert into test6 (id,name) values ('220','zhang') ;
select * from test6 ;
alter database drop logfile group 22 ;
shutdown immediate ;
startup mount ;
alter database noarchivelog ;
alter database archivelog ;
alter database open ;
select * from V$RECOVER_FILE ;
rman target sys/oracle nocatalog
rman target sys/oracle@oradb nocatalog ;
rman nocatalog
RMAN> connect target sys/oracle
rman target sys/oracle@oradb catalog sys/oracle@oradb
rman
connect target sys/oracle
rman target sys/oracle@oradb auxiliary sys/oracle@oradb
rman
connect target sys/oracle
connect auxiliary sys/oracle
alter database log_mode archivelog ;
select log_mode from v$database ;
shutdown immediate ;
startup mount ;
alter database noarchivelog ;
alter database archivelog ;
alter database open ;
rman target sys/oracle nocatalog
backup format '/oradata/oradb/%d_%s.bak' tablespace users ;
RMAN> run {
2> allocate channel d1 type disk;
3> backup format '/oradata/oradb/%d_%s.bak' tablespace users ;
4> release channel d1 ;
5> }
RMAN> sql 'alter system switch logfile ';
select name from v$datafile ;
RMAN> sql " create tablespace user02 datafile ''/oradata/oradb/user02.dbf'' size 2G " ;
在RMAN 中执行命令时如果有引号,要用“” 引起来,且单引号要用双单引号
select * from V$TABLESPACE ;
RMAN> run {execute script s1;} 运行存储脚本s1
RMAN> run { host "ls -al /oradata/oradb/ " ;} 运行系统命令
rman target sys/oracle nocatalog @/oradata/oradb/bak_users.sh log out.log
RMAN> backup copies 2 database format='/oradata/oradb/%d_%s1.dbf ', '/oradata/oradb/%d_%s2.dbf ' ;
backup tablespace users format='/ordata/oradb/%N_%s.dbf ' ; 备份表空间
backup datafile 5 format='/oradata/oradb/%N_%f_%s.dbf ' ;
backup current controlfile format='/oradata/oradb/%d_%s.ctl ' ;
backup datafile 2 format='/oradata/oradb/%d_%s.dbf' include current controlfile ; 备份数据文件时,同时备份控制文件
backup spfile format='/oradata/oradb/%d_%s.par ' ;
backup format='/oradata/oradb/%d_%s.bak ' tablespace users plus archivelog ; 备份数据文件,和归档日志
RMAN> backup as copy format='/oradata/oradb/df_2.dbf ' datafile 2 ; 建立数据文件映像副本
RMAN> copy datafile 2 to '/oradata/oradb/df_2_2.dbf ' ; 建立数据文件映像副本
RMAN> backup as copy format='/oradata/oradb/controlfile_bak.ctl ' current controlfile ;
RMAN> copy current controlfile to '/oradata/oradb/controlfile_bak2.ctl ' ;
增量备份
RMAN> backup incremental level 0 format='/oradata/oradb/%d_%s.dbf ' tablespace users ;
RMAN> backup incremental level 2 format='/oradata/oradb/%d_%s.dbf ' tablespace users ;
RMAN> backup incremental level 1 format='/oradata/oradb/%d_%s.dbf ' tablespace users ;
select * from v$archived_log ; 显示所有归档日志映像副本
所有数据文件被删除
RMAN> startup force mount
RMAN> run {
2> restore database ;
3> recover database ;
4> sql ' alter database open ' ;
5> }
数据文件所在的磁盘故障
MAN> run {
2> startup force mount ;
3> set newname for datafile 2 to '/oradata/oradb/undotbs01.dbf ' ;
4> restore database ;
5> switch datafile all ;
6> recover database ;
7> sql ' alter database open ' ;
8> }
SYSTEM 表空间的数据文件被删除
RMAN> run {
2> startup force mount ;
3> restore datafile 1 ;
4> recover datafile 1 ;
5> sql ' alter database open ' ;
6> }
RMAN> report need backup days 3 tablespace 报告3天没有备份的数据文件
RMAN> list backup of tablespace user01; 列出数据文件 备份集
RMAN> list backup of controlfile ; 列出控制文件 备份集
RMAN> list backup of archivelog all ; 列出归档日志备份集
RMAN> list backup of spfile ; 列出SPFILE备份集
RMAN> list copy of datafile 7; 列出数据文件映像副本
RMAN> list copy of controlfile ; 列出控制文件映像副本
RMAN> list copy of archivelog all ; 列出归档日志映像副本
RMAN> list incarnation of database ; 列出数据库副本
RMAN> crosscheck backup ; 核对所有的备份集
RMAN> crosscheck backup of database ; 核对所有数据文件的备份集
RMAN> crosscheck backup of tablespace system ; 核对特定表空间的备份集
RMAN> crosscheck backup of datafile 4; 核对特定数据文件的备份集
RMAN> crosscheck backup of controlfile ; 核对特定控制文件的备份集
RMAN> crosscheck backup of spfile ; 核对SPFILE的备份集
RMAN> crosscheck copy ; 核对所有映像副本
RMAN> delete obsolete ; 删除陈旧备份
RMAN> delete noprompt obsolete ; 删除陈旧备份,并不提示
RMAN> delete noprompt backup ; 删除所有的备份集
RMAN> delete noprompt copy ; 删除所有的映像副本
1)按用户导
expdp system/aaaAAA111@orcl schemas=oracle directory=BACKUP_01 dumpfile=test.dmp logfile=expdp_test.log
2)并行进程parallel
expdp system/aaaAAA111@orcl schemas=oracle directory=BACKUP_01 dumpfile=test02.dmp parallel=40 job_name=scott3 logfile=expdp_test.log
3)按表名导
expdp system/aaaAAA111@orcl tables=oracle.oracle directory=BACKUP_01 dumpfile=test03.dmp parallel=40 job_name=scott3 logfile=expdp_test.log
4)按查询条件导
expdp scott/tiger@orcl directory=dpdata1 dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
5)按表空间导
expdp system/aaaAAA111@orcl tablespaces=oracle directory=BACKUP_01 dumpfile=test04.dmp parallel=40 job_name=scott3 logfile=expdp_test.log
6)导出整个数据库
expdp system/aaaAAA111@orcl directory=BACKUP_01 dumpfile=test05.dmp parallel=40 job_name=scott3 logfile=expdp_test.log FULL=y;
五、还原数据
1)导到指定用户下
impdp scott/tiger DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=scott;
2)改变表的owner
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
3)导入表空间
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=tablespace.dmp TABLESPACES=example;
4)导入数据库
impdb system/manager DIRECTORY=dump_dir DUMPFILE=full.dmp FULL=y;
5)追加数据
impdp system/manager DIRECTORY=dpdata1 DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION=append;
ORACLE 启动 先以ORACLE 身份登录服务器
Sqlplus /nolog
Connect /as sysdba
startup force
lsnrctl start
sqlplus "/as sysdba" 进去后
CONNECT SYS/sys_password as SYSDBA
startup force
sqlplus sys/aaaAAA111@orcl as sysdba
emctl start dbconsole
sqlplus "/as sysdba"
alter system suspend 暂停DB
alter system resume; 恢复DB
以管理员身份登录
sqlplus sys/aaaAAA111@orcl as sysdba
connect sys/aaaAAA111 as sysdba
conn sys/orcl as sysdba
show parameter
select * from dba_users;
alter database mount;
alter database open;
alter session set sql_trace=true;
lsnrctl start
tnsping orcl
netca
netdb
select * from dba_users;
alter user system identified by aaaAAA111
alter user scott quota 9000m on system;
alter user scott account lock;
alter user scott account unlock;
GRANT ALL PRIVILEGES TO ROLE1;
grant role1 to scott;
select * from dba_ts_quotas
select * from system_privilege_map;
select * from dba_tablespaces;
grant sysdba to system;
alter user system identified by aaaAAA111
alter user system quota 1000000000M on user01;
select * from user_role_privs;
select * from user_sys_privs;
GRANT ALL PRIVILEGES TO ROLE1;
grant role1 to scott;
select * from role_sys_privs;
select * from dba_role_privs;
alter role role1 identified by aaaAAA111;
alter user scott default role role1;
select sid,serial from v$session where username='system';
show user;
select * from v$pwfile_users;
select * from dba_users;
select * from dba_ts_quotas:
select * from system_privilege_map;
select * from dba_sys_privs;
select * from user_sys_privs;
select * from session_privs;
grant alter any table on temp to panxianpei;
select * from dba_tables;
grant execute,all privileges,index,select on scott.emp to public;
select * from dba_tab_privs;
select * from all_tab_privs;
select * from dba_col_privs;
select * from all_col_privs_made;
select * from all_col_privs_recd;
select * from all_tab_privs_made;
select * from all_tab_privs_recd;
create role role3 not identified;
create role role4 identified by aaaAAA111;
grant all privileges on scott.emp to role4 with admin option;
grant role2,role3 to panxianpei with admin option;
alter user panxianpei default role dba;
set role role4 identified by aaaAAA111;
set role none identified by aaaAAA111;
alter role role4 not identified by aaaAAA111;
drop role role4 identified by aaaAAA111;
select * from dba_roles;
select * from dba_role_privs;
select * from session_roles;
show parameter spfile;
select * from v$parameter;
alter session set sql_trace=true;
select * from dba_tab_privs;
select * from dba_users;
select * from dba_col_privs;
select * from dba_sys_privs;
select * from dba_users;
select * from dba_sys_privs;
select * from dba_tab_privs;
select * from dba_col_privs;
select * from dba_role_privs;
select * from dict;
select * from global_name;
select * from ind;
select * from obj;
select * from tab where user='scott';
select * from v$fixed_table;
archive log list;
create tablespace test03 datafile '/u01/app/oracle/oradata/orcl/test03.dbf' size 20m
alter tablespace test03 offline
alter tablespace test03 online;
select * from dba_data_files;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' offline;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' online;
alter tablespace TEST03 read only;
alter tablespace test03 read write
alter tablespace test03 rename to test04;
select * from v$tablespace;
alter database default tablespace users;
alter database default temporary tablespace temp;
select * from dba_data_files;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' resize 540M;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' autoextend on next 10M maxsize 1g;
select * from v$tablespace
select * from v$datafile;
select * from V$tempfile;
show parameter undo_tablespace;
archive log list;
create tablespace test03 datafile '/u01/app/oracle/oradata/orcl/test03.dbf' size 20m
alter tablespace test03 offline
alter tablespace test03 online;
select * from dba_data_files;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' offline;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' online;
alter tablespace TEST03 read only;
alter tablespace test03 read write
alter tablespace test03 rename to test04;
select * from v$tablespace;
alter database default tablespace users;
alter database default temporary tablespace temp;
select * from dba_data_files;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' resize 540M;
alter database datafile '/u01/app/oracle/oradata/orcl/test03.dbf' autoextend on next 10M maxsize 1g;
select * from v$tablespace
select * from v$datafile;
select * from V$tempfile;
show parameter undo_tablespace;
select * from dba_tablespaces;
select * from dba_segments;
select * from dept
select * from dba_tables;
select * from dba_tab_columns;
select * from dba_tab_comments;
select * from dba_col_comments;
select * from dba_unused_col_tabs;
select * from dba_external_tables;
select * from dba_external_locations;
select * from dba_segments;
select * from dba_indexes;
select * from dba_ind_columns;
select * from dba_segments;
select * from dba_constraints;
select * from dba_cons_columns;
select * from user_segments;
select * from dba_segments;
select * from dba_part_tables;
select * from dba_tab_partitions;
select * from dba_tab_subpartitions;
select * from dba_part_key_columns;
select * from dba_subpart_key_columns;
select * from dba_part_indexes;
select * from dba_ind_partitions;
select * from dba_directories;
expdp panxianpei/aaaAAA111 directory=dump_dir3 dumpfile=table.dmp tables=test_table1
expdp panxianpei/aaaAAA111 directory=dump_dir3 dumpfile=table.dmp tables=test_table1
expdp panxianpei/aaaAAA111 directory=dump_dir3 dumpfile=table2.dmp tables=test_table1
expdb system/aaaAAA111 directory=dump_dir3 dumpfile=schema.dmp schemas=system,scott,panxianpei
expdp system/aaaAAA111 directory=dump_dir3 dumpfile=schema.dmp schemas=system,scott,panxianpei
expdp system/aaaAAA111 directory=dump_dir3 dumpfile=tablespace.dmp tablespaces=system
expdp system/aaaAAA111 directory=dump_dir3 dumpfile=full_database.dmp full=y
impdp system/aaaAAA111 directory=dump_dir3 dumpfile=table.dmp tables=test_table1
impdp system/aaaAAA111 directory=dump_dir3 dumpfile=table.dmp tables=panxianpei.test_table1 remap_schema=panxianpei
exp system/aaaAAA111 owner=system file=schemal.dmp
alter user panxianpei quota 10000M on tablespace_test02;
select * from dba_tablespaces;
alter tablespace tablespace_test02 add datafile '/u01/app/oracle/oradata/orcl/test04.dbf' size 200m autoextend on next 20m;
drop table PANXIANPEI.TEST_TABLESPACE2;
flashback table PANXIANPEI.TEST_TABLESPACE2 to before drop;
alter table panxianpei.TEST_TABLESPACE2 enable row movement;
FLASHBACK TABLE PANXIANPEI.TEST_TABLESPACE2 TO TIMESTAMP to_timestamp('2010-09-15 12:14:01', 'YYYY-MM-DD HH24:MI:SS')
flashback table panxianpei.test_tablespace2 to timestamp to_timestamp('2010-09-15 12:13:01', 'YYYY-MM-DD HH24:MI:SS')
select * from dual;
select dbms_flashback.get_system_change_number from dual;
dbms_flashback.enable_at_system_change_number(1036030);
alter database archivelog;
alter database flashback on;
alter database archivelog;
select * from dba_tables;
alter database flashback on;
alter database open;
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
SELECT OLDEST_FLASHBACK_SCN, OLDEST_FLASHBACK_TIME FROM V$FLASHBACK_DATABASE_LOG;
select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;
select sysdate from dual;
select * from V$FLASHBACK_DATABASE_LOG;
select * from v$flashback_database_log;
alter database archivelog;
alter database flashback on;
alter table a enable row movement;
select * from PANXIANPEI.TEST_TABLESPACE2;
alter table PANXIANPEI.TEST_TABLESPACE2 enable row movement;
flashback table PANXIANPEI.TEST_TABLESPACE2 to timestamp to_timestamp('2010-02-14 15:55:20','yyyy-mm-dd hh24:mi:ss');
select ora_rowscn from PANXIANPEI.TEST_TABLESPACE2;
set serveroutput on;
set time on
flashback table panxianpei.test_tablespace2 to before drop;
alter table panxianpei.test_tablespace2 enable row movement
select * from v$database ; 查看SCN值
flashback table panxianpei.test_tablespace2 to scn 1098718; 用以上查的SCN值恢复表。
flashback table panxianpei.test_tablespace2 to timestamp to_timestamp('2010-10-04 11:49:20','yyyy-mm-dd hh24:mi:ss');
select * from v$instance;
select * from v$database;
select * from v$log;
select * from v$logfile;
select * from v$bgprocess;
select * from v$thread;
select * from v$datafile
union
select * from v$controlfile;
select * from v$backup;
alter database backup controlfile to '/u01/app/oracle/oradata/orcl/backup_orcl.ctl';
alter database backup controlfile to '/u01/app/oracle/oradata/orcl/backup_orcl.ctl' reuse;
alter database backup controlfile to trace;
show parameter user_dump_dest;
select * from v$process;
select * from v$archived_log;
alter database rename file '/u01/app/oracle/oradata/orcl/tablespace_test01.dbf' to '/u01/app/oracle/oradata/orcl/tablespace_test02.dbf'
alter datatbase datafile '/u01/app/oracle/oradata/orcl/test03.dbf' offonline
alter tablespace TABLESPACE_TEST03 rename datafile '/u01/app/oracle/oradata/orcl/test02.dbf ' to '/u01/app/oracle/oradata/orcl/test02.dbf '
set autorecovery on;
select * from v$recover_file
select * from v$loghist;
select * from v$recovery_log;
alter database begin backup;
select * from v$recover_file;
select * from v$database;
RECOVER DATAFILE '/u01/app/oracle/oradata/dtberp/system01.dbf'
select * from v$recover_file;
backup format '/u01/app/oracle/oradata/orcl/%d_%s.bak' tablespace system,users;
alter system set control_file='/u01/app/oracle/oradata/orcl' scope=spfile;
select * from v$logfile;
alter system switch logfile;
alter database drop logfile member '/u01/app/oracle/oradata/orcl';
alter database add logfile member '/u01/app/oracle/oradata/orcl' to group 1;
alter database clear unarchived logfile group 1
alter database add logfile ('/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl') size 10m
alter database drop logfile group 1;
alter database open;
recover database until cancel;
alter database open resetlogs;
alter database archivelog;
alter database rename file '/u01/app/oracle/oradata/orcl' to '/u01/app/oracle/oradata/orcl';
recover datafile 1;
recover database 1
alter database 6 offline drop
alter database open;
recover database until cancel;
alter database open resetlogs;
alter system archive log start;
rman target /;
RMAN> run {
2> backup format '/u01/app/oracle/oradata/orcl/%d_%s.bak' tablespace system,users;
3> }
sql 'alter system switch logfile';
RMAN> sql 'alter system switch logfile';
rman target / nocatalog @/u01/app/oracle/oradata/orcl/a.sh log a.log
RMAN> sql "create tablespace user03 datafile ''/u01/app/oracle/oradata/orcl/user03.dbf'' size 5m";
RMAN> run {host ls;}
RMAN> run {host "ls ";}
RMAN> run {host "ls -al ";}
RMAN> configure device type disk parallelism 3;
RMAN> configure device type sbt parallelism 3;
RMAN>backup database format='/u01/app/oracle/oradata/orcl/dump/%s_%d.dbf';
RMAN> sql 'alter system archive log current';
backup tablespace system format='/u01/app/oracle/oradata/orcl/dump/%N_%s.dbf';
backup current controlfile format ='/u01/app/oracle/oradata/orcl/dump/%d_s.ctl';
backup datafile 5 format='/u01/app/oracle/oradata/orcl/dump/%d_%s.dbf' include current controlfile;
backup format='/u01/app/oracle/oradata/orcl/dump/%d_%s.dbf' archivelog from time='sysdate-1' until time='sysdate';
select * from v$archived_log;
select * from v$backup_datafile,v$backup_piece;
report need backup days 3 tablespace users;
report need backup redundancy 2 database;
report need backup recovery windows of 6 days;
report obsolete redundancy 2;
report unrecoverable;
list backup of tablespace users;
alter system archive log current;
show recyclebin;
select * from dba_recyclebin;
select * from dba_recyclebin;
show recyclebin;
flashback table "test" to before drop;
select * from dba_recyclebin;
flashback table "BIN$kEODhKBAhU7gQAB/AQB4Kg==$0" to before drop;
flashback table panxianpei.test_tables2 to before drop;
flashback table "sdlfskdjsad" to before drop rename to new;
select * from v$database;
select * from v$flashback_database_log;
flashback database to scn 1115355;
flashback database to time '2010-10-10 15:00:00 17:00:00';
alter database add supplemental log data;
select * from v$logmnr_logs;
select * from v$logmnr_parameters;
select * from v$datafile;
dbv file=/u01/app/oracle/oradata/orcl/users01.dbf feedback=1000 blocksize=8192
dbv file=/u01/app/oracle/oradata/orcl/users01.dbf feedback=100 blocksize=8192 start=1 end=1000
select * from v$database;
select * from dba_users;
select * from v$system_event
Select * from v$db_object_cache
select * from v$session_wait;
show recyclebin;
select * from dba_recyclebin;
select * from dba_recyclebin;
show recyclebin;
flashback table "test" to before drop;
select * from dba_recyclebin;
flashback table "BIN$kEODhKBAhU7gQAB/AQB4Kg==$0" to before drop;
flashback table panxianpei.test_tables2 to before drop;
flashback table "sdlfskdjsad" to before drop rename to new;
select * from v$database;
select * from v$flashback_database_log;
flashback database to scn 1115355;
flashback database to time '2010-10-10 15:00:00 17:00:00';
alter database add supplemental log data;
select * from v$logmnr_logs;
select * from v$logmnr_parameters;
select * from v$datafile;
dbv file=/u01/app/oracle/oradata/orcl/users01.dbf feedback=1000 blocksize=8192
dbv file=/u01/app/oracle/oradata/orcl/users01.dbf feedback=100 blocksize=8192 start=1 end=1000
select * from v$database;
select * from dba_users;
select * from v$system_event
Select * from v$db_object_cache
select * from v$session_wait;
select * from v$instance;
select * from dba_data_files;
alter system set shared_pool_size=600m;
create user user03 identified by aaaAAA111;
show parameter spfile;
alter session set nls_date_format='yyyy-mm-dd';
alter session set sql_trace=true;
select * from user_tables;
grant all privileges on help to scott;
select * from user_tab_columns;
select * from dba_role_privs;
select * from v$fixed_table;
select * from v$instance;
select * from v$sga;
select * from v$sgainfo;
select * from v$option;
select * from v$session;
select * from v$process;
select * from v$bgprocess;
select * from v$database;
select * from v$controlfile;
select * from v$datafile;
select * from v$logfile;
select * from v$log;
select * from v$logfile;
select * from v$locked_object;
select * from v$tablespace;
select * from v$tempfile;
select * from v$archive_dest;
select * from v$loghist;
select * from v$archive_processes;
alter user panxianpei identified by aaaAAA111;
select * from dba_users;
select * from v$pwfile_users;
select * from dba_users;
Select * from session_privs;
select * from dba_sys_privs;
select * from system_privilege_map;
select * from dba_tab_privs;
select * from dba_audit_trail;
lsnrctl start
sqlplus '/as sysdba'
sql> startup
emctl start dbconsole
emctl stop dbconsole
isqlplusctl start
在新文件系统中创建一个新表空间
新文件系统已可以使用。接下来,在文件系统中创建一个用于存储数据库对象的新表空间。以 SYSTEM 用户身份连接到数据库,然后执行 CREATE TABLESPACE 语句,并在新文件系统中指定数据文件。
例如:
$ sqlplus
SQL*Plus:Release 10.1.0.2.0 - Production on Sun Jun 13 15:01:08 2004
Copyright (c) 1982, 2004, Oracle.All rights reserved.
Enter user-name:system
Enter password:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create tablespace data1
2 datafile '/u03/oradata/demo1/data1_01.dbf' size 100M
3 extent management local
4 segment space management auto;
Tablespace created.
create tablespace test02 datafile '/u01/app/oracle/oradata/orcl/test02.dbf ' size 10M;
现在,可以使用此新表空间存储数据库对象,如表和索引。
例如:
SQL> create table demotab (id number(5) not null primary key,
2 name varchar2(50) not null,
3 amount number(9,2))
4 tablespace data1;
Table created.
启动和停止 iSQL*Plus:
isqlplusctl start
lsnrctl start
查看表空间
select * from dba_tablespaces;
create user test01 identified by aaaAAA111 default tablespace DATA1 temporary tablespace temp quota 3m on DATA1 password expire;
grant create session,create table to test;
conn test01/aaaAAA111@orcl
检查数据库连通
TNSPING
数据库侦听
lsnrctl start
查看SID
SQL> SELECT * FROM V$INSTANCE;
SQL> SELECT * FROM V$DATABASE;
SQL> / 执行上个操作命令
SQL> EDIT读取命令缓存区
查看语言配置
SQL> show parameters nls
SQL> select * from V$NLS_PARAMETERS
查看启动参数文件
SQL> show parameters spfile;
查看数据块配置大小
SQL> show parameters db;
查看ORACLE版本
SQL> SELECT * FROM V$VERSION
显示所有组建版本
SQL> select * from product_component_version;
配置输出日志
SQL> spool c:\testora.log
SQL> spool off
切换归档日志
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
SQL> ALTER SYSTEM SWITCH LOGFILE;
查看数据库对象结构
SQL> desc v$dbfile
查看Datafile
SQL>SELECT * FROM V$DBFILE;
SQL>SELECT * FROM V$DATAFILE;
查看所有的用户
SQL> SELECT USERNAME FROM DBA_USERS
查看控制文件
SQL> select * from v$controlfile;
设置输出的行宽
SQL> set linesize 20000
查看REDOLOG文件
SQL> SELECT * FROM V$LOGFILE;
查看表空间
SQL> SELECT * FROM V$tablespace;
手动删除归档日志后,同步CATALOG
SQL>CROSSCHECK ARCHIVELOG ALL
SQL>CHANGE ARCHIVELOG ALL CROSSCHECK
如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句:
SQL>select * from dictionary where instr(comments,'index')>0;
ORACLE常用命令
一、ORACLE的启动和关闭
1、在单机环境下
要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下
su - oracle
a、启动ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>startup
SVRMGR>quit
b、关闭ORACLE系统
oracle>svrmgrl
SVRMGR>connect internal
SVRMGR>shutdown
SVRMGR>quit
启动oracle9i数据库命令:
$ sqlplus /nolog
SQL*Plus: Release 9.2.0.1.0 - Production on Fri Oct 31 13:53:53 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
SQL> connect / as sysdba
Connected to an idle instance.
SQL> startup^C
SQL> startup
ORACLE instance started.
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用户常用数据字典的查询使用方法。
1、用户
查看当前用户的缺省表空间
SQL>select username,default_tablespace from user_users;
查看当前用户的角色
SQL>select * from user_role_privs;
查看当前用户的系统权限和表级权限
SQL>select * from user_sys_privs;
SQL>select * from user_tab_privs;
2、表
查看用户下所有的表
SQL>select * from user_tables;
查看名称包含log字符的表
SQL>select object_name,object_id from user_objects
where instr(object_name,'LOG')>0;
查看某表的创建时间
SQL>select object_name,created from user_objects where object_name=upper('&table_name');
查看某表的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&table_name');
查看放在ORACLE的内存区里的表
SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;
3、索引
查看索引个数和类别
SQL>select index_name,index_type,table_name from user_indexes order by table_name;
查看索引被索引的字段
SQL>select * from user_ind_columns where index_name=upper('&index_name');
查看索引的大小
SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments
where segment_name=upper('&index_name');
4、序列号
查看序列号,last_number是当前值
SQL>select * from user_sequences;
5、视图
查看视图的名称
SQL>select view_name from user_views;
查看创建视图的select语句
SQL>set view_name,text_length from user_views;
SQL>set long 2000; 说明:可以根据视图的text_length值设定set long 的大小
SQL>select text from user_views where view_name=upper('&view_name');
6、同义词
查看同义词的名称
SQL>select * from user_synonyms;
7、约束条件
查看某表的约束条件
SQL>select constraint_name, constraint_type,search_condition, r_constraint_name
from user_constraints where table_name = upper('&table_name');
SQL>select c.constraint_name,c.constraint_type,cc.column_name
from user_constraints c,user_cons_columns cc
where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')
and c.owner = cc.owner and c.constraint_name = cc.constraint_name
order by cc.position;
8、存储函数和过程
查看函数和过程的状态
SQL>select object_name,status from user_objects where object_type='FUNCTION';
SQL>select object_name,status from user_objects where object_type='PROCEDURE';
查看函数和过程的源代码
SQL>select text from all_source where owner=user and name=upper('&plsql_name');
三、查看数据库的SQL
1、查看表空间的名称及大小
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;
2、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
3、查看回滚段名称及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name ;
4、查看控制文件
select name from v$controlfile;
5、查看日志文件
select member from v$logfile;
6、查看表空间的使用情况
select sum(bytes)/(1024*1024) as free_space,tablespace_name
from dba_free_space
group by tablespace_name;
SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;
7、查看数据库库对象
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;
8、查看数据库的版本
Select version FROM Product_component_version
Where SUBSTR(PRODUCT,1,6)='Oracle';
9、查看数据库的创建日期和归档方式
Select Created, Log_Mode, Log_Mode From V$Database;
四、ORACLE用户连接的管理
用系统管理员,查看当前数据库有几个用户连接:
SQL> select username,sid,serial# from v$session;
如果要停某个连接用
SQL> alter system kill session 'sid,serial#';
如果这命令不行,找它UNIX的进程数
SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr;
说明:21是某个连接的sid数
然后用 kill 命令杀此进程号。
五、SQL*PLUS使用
a、近入SQL*Plus
$sqlplus 用户名/密码
退出SQL*Plus
SQL>exit
b、在sqlplus下得到帮助信息
列出全部SQL命令和SQL*Plus命令
SQL>help
列出某个特定的命令的信息
SQL>help 命令名
c、显示表结构命令DESCRIBE
SQL>DESC 表名
d、SQL*Plus中的编辑命令
显示SQL缓冲区命令
SQL>L
修改SQL命令
首先要将待改正行变为当前行
SQL>n
用CHANGE命令修改内容
SQL>c/旧/新
重新确认是否已正确
SQL>L
使用INPUT命令可以在SQL缓冲区中增加一行或多行
SQL>i
SQL>输入内容
e、调用外部系统编辑器
SQL>edit 文件名
可以使用DEFINE命令设置系统变量EDITOR来改变文本编辑器的类型,在login.sql文件中定义如下一行
DEFINE_EDITOR=vi
f、运行命令文件
SQL>START test
SQL>@test
常用SQL*Plus语句
a、表的创建、修改、删除
创建表的命令格式如下:
create table 表名 (列说明列表);
为基表增加新列命令如下:
ALTER TABLE 表名 ADD (列说明列表)
例:为test表增加一列Age,用来存放年龄
sql>alter table test
add (Age number(3));
修改基表列定义命令如下:
ALTER TABLE 表名
MODIFY (列名 数据类型)
例:将test表中的Count列宽度加长为10个字符
sql>alter atble test
modify (County char(10));
b、将一张表删除语句的格式如下:
DORP TABLE 表名;
例:表删除将同时删除表的数据和表的定义
sql>drop table test
c、表空间的创建、删除
六、ORACLE逻辑备份的SH文件
完全备份的SH文件:exp_comp.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=complete file=/oracle/export/db_comp$rq.dmp"
累计备份的SH文件:exp_cumu.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=cumulative file=/oracle/export/db_cumu$rq.dmp"
增量备份的SH文件: exp_incr.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=incremental file=/oracle/export/db_incr$rq.dmp"
root用户crontab文件
/var/spool/cron/crontabs/root增加以下内容
0 2 1 * * /oracle/exp_comp.sh
30 2 * * 0-5 /oracle/exp_incr.sh
45 2 * * 6 /oracle/exp_cumu.sh
当然这个时间表可以根据不同的需求来改变的,这只是一个例子。
七、ORACLE 常用的SQL语法和数据对象
一.数据控制语句 (DML) 部分
1.INSERT (往数据表里插入记录的语句)
INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……);
INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名;
字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’
如果字段值里包含单引号’ 需要进行字符串转换, 我们把它替换成两个单引号''.
字符串类型的字段值超过定义的长度会出错, 最好在插入前进行长度校验.
日期字段的字段值可以用当前数据库的系统时间SYSDATE, 精确到秒
或者用字符串转换成日期型函数TO_DATE(‘2001-08-01’,’YYYY-MM-DD’)
TO_DATE()还有很多种日期格式, 可以参看ORACLE DOC.
年-月-日 小时:分钟:秒 的格式YYYY-MM-DD HH24:MI:SS
INSERT时最大可操作的字符串长度小于等于4000个单字节, 如果要插入更长的字符串, 请考虑字段用CLOB类型,
方法借用ORACLE里自带的DBMS_LOB程序包.
INSERT时如果要用到从1开始自动增长的序列号, 应该先建立一个序列号
CREATE SEQUENCE 序列号的名称 (最好是表名+序列号标记) INCREMENT BY 1 START WITH 1
MAXVALUE 99999 CYCLE NOCACHE;
其中最大的值按字段的长度来定, 如果定义的自动增长的序列号 NUMBER(6) , 最大值为999999
INSERT 语句插入这个字段值为: 序列号的名称.NEXTVAL
2.DELETE (删除数据表里记录的语句)
DELETE FROM表名 WHERE 条件;
注意:删除记录并不能释放ORACLE里被占用的数据块表空间. 它只把那些被删除的数据块标成unused.
如果确实要删除一个大表里的全部记录, 可以用 TRUNCATE 命令, 它可以释放占用的数据块表空间
TRUNCATE TABLE 表名;
此操作不可回退.
3.UPDATE (修改数据表里记录的语句)
UPDATE表名 SET 字段名1=值1, 字段名2=值2, …… WHERE 条件;
如果修改的值N没有赋值或定义时, 将把原来的记录内容清为NULL, 最好在修改前进行非空校验;
值N超过定义的长度会出错, 最好在插入前进行长度校验..
注意事项:
A. 以上SQL语句对表都加上了行级锁,
确认完成后, 必须加上事物处理结束的命令 COMMIT 才能正式生效,
否则改变不一定写入数据库里.
如果想撤回这些操作, 可以用命令 ROLLBACK 复原.
B. 在运行INSERT, DELETE 和 UPDATE 语句前最好估算一下可能操作的记录范围,
应该把它限定在较小 (一万条记录) 范围内,. 否则ORACLE处理这个事物用到很大的回退段.
程序响应慢甚至失去响应. 如果记录数上十万以上这些操作, 可以把这些SQL语句分段分次完成,
其间加上COMMIT 确认事物处理.
二.数据定义 (DDL) 部分
1.CREATE (创建表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
ORACLE常用的字段类型有
CHAR 固定长度的字符串
VARCHAR2 可变长度的字符串
NUMBER(M,N) 数字型M是位数总长度, N是小数的长度
DATE 日期类型
创建表时要把较小的不为空的字段放在前面, 可能为空的字段放在后面
创建表时可以用中文的字段名, 但最好还是用英文的字段名
创建表时可以给字段加上默认值, 例如 DEFAULT SYSDATE
这样每次插入和修改时, 不用程序操作这个字段都能得到动作的时间
创建表时可以给字段加上约束条件
例如 不允许重复 UNIQUE, 关键字 PRIMARY KEY
2.ALTER (改变表, 索引, 视图等)
改变表的名称
ALTER TABLE 表名1 TO 表名2;
在表的后面增加一个字段
ALTER TABLE表名 ADD 字段名 字段名描述;
修改表里字段的定义描述
ALTER TABLE表名 MODIFY字段名 字段名描述;
给表里的字段加上约束条件
ALTER TABLE 表名 ADD CONSTRAINT 约束名 PRIMARY KEY (字段名);
ALTER TABLE 表名 ADD CONSTRAINT 约束名 UNIQUE (字段名);
把表放在或取出数据库的内存区
ALTER TABLE 表名 CACHE;
ALTER TABLE 表名 NOCACHE;
3.DROP (删除表, 索引, 视图, 同义词, 过程, 函数, 数据库链接等)
删除表和它所有的约束条件
DROP TABLE 表名 CASCADE CONSTRAINTS;
4.TRUNCATE (清空表里的所有记录, 保留表的结构)
TRUNCATE 表名;
三.查询语句 (SELECT) 部分
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE 条件;
字段名可以带入函数
例如: COUNT(*), MIN(字段名), MAX(字段名), AVG(字段名), DISTINCT(字段名),
TO_CHAR(DATE字段名,'YYYY-MM-DD HH24:MI:SS')
NVL(EXPR1, EXPR2)函数
解释:
IF EXPR1=NULL
RETURN EXPR2
ELSE
RETURN EXPR1
DECODE(AA﹐V1﹐R1﹐V2﹐R2....)函数
解释:
IF AA=V1 THEN RETURN R1
IF AA=V2 THEN RETURN R2
..…
ELSE
RETURN NULL
LPAD(char1,n,char2)函数
解释:
字符char1按制定的位数n显示,不足的位数用char2字符串替换左边的空位
字段名之间可以进行算术运算
例如: (字段名1*字段名1)/3
查询语句可以嵌套
例如: SELECT …… FROM
(SELECT …… FROM表名1, [表名2, ……] WHERE 条件) WHERE 条件2;
两个查询语句的结果可以做集合操作
例如: 并集UNION(去掉重复记录), 并集UNION ALL(不去掉重复记录), 差集MINUS, 交集INTERSECT
分组查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] GROUP BY字段名1
[HAVING 条件] ;
两个以上表之间的连接查询
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名 [ AND ……] ;
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……] WHERE
表名1.字段名 = 表名2. 字段名(+) [ AND ……] ;
有(+)号的字段位置自动补空值
查询结果集的排序操作, 默认的排序是升序ASC, 降序是DESC
SELECT字段名1, 字段名2, …… FROM 表名1, [表名2, ……]
ORDER BY字段名1, 字段名2 DESC;
字符串模糊比较的方法
INSTR(字段名, ‘字符串’)>0
字段名 LIKE ‘字符串%’ [‘%字符串%’]
每个表都有一个隐含的字段ROWID, 它标记着记录的唯一性.
四.ORACLE里常用的数据对象 (SCHEMA)
1.索引 (INDEX)
CREATE INDEX 索引名ON 表名 ( 字段1, [字段2, ……] );
ALTER INDEX 索引名 REBUILD;
一个表的索引最好不要超过三个 (特殊的大表除外), 最好用单字段索引, 结合SQL语句的分析执行情况,
也可以建立多字段的组合索引和基于函数的索引
ORACLE8.1.7字符串可以索引的最大长度为1578 单字节
ORACLE8.0.6字符串可以索引的最大长度为758 单字节
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;
一、何为角色?
我在前面的篇幅中说明权限和用户。慢慢的在使用中你会发现一个问题:如果有一组人,他们的所需的权限是一样的,当对他们的权限进行管理的时候会很不方便。因为你要对这组中的每个用户的权限都进行管理。
有一个很好的解决办法就是:角色。角色是一组权限的集合,将角色赋给一个用户,这个用户就拥有了这个角色中的所有权限。那么上述问题就很好处理了,只要第一次将角色赋给这一组用户,接下来就只要针对角色进行管理就可以了。
以上是角色的一个典型用途。其实,只要明白:角色就是一组权限的集合。下面分两个部门来对oracle角色进行说明。
二、系统预定义角色
预定义角色是在数据库安装后,系统自动创建的一些常用的角色。下介简单的介绍一下这些预定角色。角色所包含的权限可以用以下语句查询:
sql>select * from role_sys_privs where role='角色名';
1.CONNECT, RESOURCE, DBA
这些预定义角色主要是为了向后兼容。其主要是用于数据库管理。oracle建议用户自己设计数据库管理和安全的权限规划,而不要简单的使用这些预定角色。将来的版本中这些角色可能不会作为预定义角色。
2.DELETE_CATALOG_ROLE, EXECUTE_CATALOG_ROLE, SELECT_CATALOG_ROLE
这些角色主要用于访问数据字典视图和包。
3.EXP_FULL_DATABASE, IMP_FULL_DATABASE
这两个角色用于数据导入导出工具的使用。
4.AQ_USER_ROLE, AQ_ADMINISTRATOR_ROLE
AQ:Advanced Query。这两个角色用于oracle高级查询功能。
5.SNMPAGENT
用于oracle enterprise manager和Intelligent Agent
6.RECOVERY_CATALOG_OWNER
用于创建拥有恢复库的用户。关于恢复库的信息,参考oracle文档《Oracle9i User-Managed Backup and Recovery Guide》
7.HS_ADMIN_ROLE
A DBA using Oracle's heterogeneous services feature needs this role to access appropriate tables in the data dictionary.
二、管理角色
1.建一个角色
sql>create role role1;
2.授权给角色
sql>grant create any table,create procedure to role1;
3.授予角色给用户
sql>grant role1 to user1;
4.查看角色所包含的权限
sql>select * from role_sys_privs;
5.创建带有口令以角色(在生效带有口令的角色时必须提供口令)
sql>create role role1 identified by password1;
6.修改角色:是否需要口令
sql>alter role role1 not identified;
sql>alter role role1 identified by password1;
7.设置当前用户要生效的角色
(注:角色的生效是一个什么概念呢?假设用户a有b1,b2,b3三个角色,那么如果b1未生效,则b1所包含的权限对于a来讲是不拥有的,只有角色生效了,角色内的权限才作用于用户,最大可生效角色数由参数MAX_ENABLED_ROLES设定;在用户登录后,oracle将所有直接赋给用户的权限和用户默认角色中的权限赋给用户。)
sql>set role role1;//使role1生效
sql>set role role,role2;//使role1,role2生效
sql>set role role1 identified by password1;//使用带有口令的role1生效
sql>set role all;//使用该用户的所有角色生效
sql>set role none;//设置所有角色失效
sql>set role all except role1;//除role1外的该用户的所有其它角色生效。
sql>select * from SESSION_ROLES;//查看当前用户的生效的角色。
8.修改指定用户,设置其默认角色
sql>alter user user1 default role role1;
sql>alter user user1 default role all except role1;
详见oracle参考文档
9.删除角色
sql>drop role role1;
角色删除后,原来拥用该角色的用户就不再拥有该角色了,相应的权限也就没有了。