DBA 基础知识

 
ORACLE内部培训资料

Oracle 服务器

实例Instance,数据库Database
Instance:内存结构memory structure(SGA),后台进程background process
-SGA:高速缓存,重做日志缓存区,共享池
-后台进程:SMON,PMON,DBWR,LGWR,CKPT,ARCH
Database-dada file,log file,control file
启动过程
nomount----->mount----->open //oracle启动过程实例-->控制文件-->表
startup force pfile=c:\oracle\admin\bsstest\pfile\init.ora mount; //只启动到mount

文件路径
c:\oracle\ora92\database\spfilesid.ora
c:\oracle\admin\bsstest\pfile\init.ora
c:\oracle\ora92\database\pwdbsstest.ora
C:\oracle\ora92\rdbms\admin\catproc.sql // 重建程序包

在UNIX下安装oracle时设置环境变量
setenv oracle_base=c:\oracle
setenv oracle_home=c:\oracle\ora92
setenv oracle_sid=
setenv oracle_nls33=
setenv path=
setenv ld_library_path=

set arraysize 25 //设置数组提取尺寸为25
set autocommit on //自动提交DML语句
set colsep | //设置分隔符
set heading on //显示标头
set time on // 显示系统时间

数据字典和动态性能视图
数据字典信息从数据文件中取得,动态性能视图信息从sga和控制文件中取得
只读表和视图的集合,所有者为sys用户,数据被存放在system表空间上。
select dbms_metadata.get_ddl(‘table’,’emp’) ddl from dual;
select user form dual;
select * from global_name;
v$controlfile v$database v$datafile v$dbfile v$lock v$tablespace v$session

基本控制语句
sqlplusw scott/tiger@bss //启动sqlplus进程
lsnrctr status / lsnrctl start //查看tnslistener状态,启动
dbca //启动 Database Configuration Assistant,创建数据库
alter system enable restricted session
//则普通用户不能登陆如maple,除非grant restricted session to maple;
set linesize 200
show parameter sga //查看sga_max_size
show parameter log_buffer //查看log_buffer大小
alter session set nls_language=american; //设置提示语言
alter system set db_cache_size=64m;
oracle/admin/fox/pfile/init //database块大小,db_block_size大小
alter system set db_cache_advice=on; //oracle自动分配cache建议
select * from v$version; //查看oracle版本,64-bit/32-bit
select machine,terminal from v$session; //查看某用户是从哪台机器登陆oracle
select username from v$session; //查看当前有哪些用户在使用数据库
select * from v$parameter where name like ‘proc%’;
show parameter processes //查看最大会话数
select * from v$license; //查看曾到达的最大会话数
select name from v$database; //查看数据库sid
alter system kill session ‘sid,serir#’; //解锁
@$path/filename.sql //执行sql脚本文件
spool c:\abcd.txt; select * from table_name; spool off; //把select结果导出为文本

修改口令文件
orapwd file=c:\oracle\ora92\database\pwdbsstest.ora password=admin enties=10;
//修改口令文件,如删除口令文件并将remote_login_passwordfile设置为none,那么只能用os方式登陆数据库
* 在修改口令文件后必须删除c:\oracle\ora92\network\admin\sqlnet.ora中的
SQLNET.AUTHENTICATION_SERVICES= (NTS)或加注释 #
* select * from v$pwfile_users; //查看sysdba和sysoper的用户

* Enterprise Manager Console独立登陆时不能对数据库进行备分
* 用Enterprise Manager Configuration Assistant创建一个资料档案库,然后可以登陆到oracle managerment server,初始用户名sysman密码oem_temp
* 如果无法以sysdba身份远程登陆,修改数据库初始化参数文件中的remote_login_passwordfile设置为exclusive或shared

日志模式
Select log_mode from v$database;
archive log list //查看归档模式,默认为非归档,用以下步骤转换为归档模式
1)    shutdown immediate
2)    startup mount
3)    alter database archivelog/noarchivelog;
4)    alter database open;
5)    full database backup
alter system archive log start;
alter system set log_archive_start=true scope=spfile; //开启自动存档

日志分析(Logmnr)
1)    设置目录c:\oracle\admin\bsstest\pfile\init.ora的参数
utl_file_dir=c:\oracle\admin\bsstest\cdump
2)    重启oracle
3)    建立目录文件
execute dbms_logmnr_d.build('bsstestdict.ora','C:\oracle\admin\bsstest\cdump');
4)    添加日志文件
dbms_logmnr.add_logfile(’ c:\oracle\oradata\bsstest\redo03.log’,dbms_logmnr.new);
5)    启动logmnr
excuted bms_logmnr.start_logmnr
(dictfilename=>’c:\oracle\admin\bsstest\cdump\bsstestdict.ora’);
6)    查询v$logmnr_content,sqlredo/sqlundo这两个很重要

表空间和索引
在mount状态下移动数据文件:
shutdown immediate-startup mount-host move c:\test\sys01.dbf d:\test\sys01.dbf
-alter database rename file ‘c:\test\sys01.dbf’ to ‘d:\test\sys01.dbf’-alter database open
在open状态下移动数据文件:
select tablespace_name from dba_data_files where file_name=’c:\test\data01.dbf’;
alter tablespace data01 offline;
host move c:\test\sys01.dbf d:\test\sys01.dbf
alter tablespace rename file ‘c:\test\data01.dbf’ to ‘d:\test\data01.dbf
alter tablespace data01 online;

扩展表空间
alter tablespace tbs01 add datafile ‘c:\test\data02.dbf’ size 100m; //增加书记文件
alter database datafile ‘c:\test\data01.dbf’ resize 200m; //增加尺寸
alter database datafule ‘c:\test\data01.dbf’ autoextend on next 10m maxsize 200m

删除表空间
drop tablespace data01 including contents and datafiles;

建立和指定临时表空间
create temporary tablespace temp_data tempfile ‘c:\test\temp_data.dbf’ size 5m
alter user scott temporary tablespace temp_data;

建立范围分区表
create table sales...partition by range (sales_date)
( patition p1 values less than (’01-apr-2003’0),
patition p2 values less than (’01-jul-2003’0),
patition p3 values less than (’01-oct-2003’0),
patition p4 values less than (’01-jan-2003’0)
);

建立散列分区表
create table product (product_id number(6),description varchar2(30))
partition by hash(product_id)
(partition p1 tablespace data01,partition p2 tablespace data02);

alter table scott.emp move tablespace users; //重新组织表,需重新建立索引
show parameter db_create
alter system set db_create_file_dest='c:\oracle\oradb';
create tablespace test; //在指定位置创建表空间

alter table emp add phone varchar2(10); //增加列
alter table emp drop (phone,manager); //删除列
alter table emp rename column ename to name //修改列名

drop table emp cascade constraints; //指定级联删除
truncate table table_name; //快速清空一个大表,不可以回退
alter user scott quota 10m on users; //限制scott在users表空间只有10m
select name from v$datafile;
select * from v$tablespace;
select * from dba_data_files;
alter tablespace users offline; //设置为脱机,则不能访问users表空间的表
alter database datafile ‘c:\oracle\oradata\bsstest\maple.dbf’ autoextend on //自动扩展
alter table maple deallocate unused(keep 8k); //释放表空间
truncate table maple; //清空表数据并释放空间,delete只删除数据
alter table maple drop column name; //删除表的一个字段
alter index testindex coalesce; //碎片重整

用户和口令管理
create user test indentified by 123456; //创建一个用户
alter user chen identified by admin //修改自己的口令
alter user chen quota 10m on users; //修改空间配额
grant connect,create table to testuser; //授权连接,创建表权限给testuser
alter user testuser quota 10m on users; //给testuser分配users表空间权限10m
drop user testuser; // 删除用户test
(create user mapleppp
inentified by 198141
default tablespace users
temporary tablespace temp
quota 10m on users
password expire;) //举例
alter user test account lock / unlock; //锁定用户帐号
create profile profile1 limit failed_login_attempts 3 password_lock_time 1/1440;
alter user test profile profile1 //3次登陆不成功锁定帐户1分钟,1小时是1/24
alter profile profile1 limit password_life_time 2 password_grace_time 3;//有效期2天
alter user chen profile profile1; //应用到用户
alter user chen account unlock; //撤消应用
drop profile profile1 cascade; // 删除profile1
alter system set resource_limit=true; //设置并行登陆会话数
grant sysdba to maple; //授权sysdba
select * from v$pwfile_users; //查看sysdba权限用户
revoke sysdba from maple; //撤消sysdba权限
删除正在连接的用户
Select sid,serial# from v$session where username=’U1’;
Sid serial#
------ ---------
8 25
Alter system kill session ‘8,25’;
Drop user u1 cascade;

使用profile管理资源
Alter system set resource_limit=true scope=both;
显示特权用户信息
Select * from v$pwfile_users;
Select default_tablespace,temporary_tablespace from dba_users where username=’chen’;
Select profile from dba_users where username=’chen’;

角色和权限
要将sysdba特权授予其他用户,需将remote_login_passwordfile设置为exclusive
select * from dba_roles; //查看系统角色
select * from session_privs; //查看当前会话所具有的系统权限
grant create session to testuser;
grant create (any) table to testuser (with admin option);
grant create any index to testuser;
grant drop any table to testuser;
select * from user_tab_privs; //查看授予了哪些权限给哪些用户
audit create table / select,update,insert on testtable
by session / access whenever successful / not successful

create role testrole1 (identified by testrole1); //创建角色并授权
grant select on maple to testrole1;
grant testrole1 to testuser1;
set role testrole1; //激活角色
execute dbms_output.put_line (‘test string1’);

字符集和SQL语句
select parameter,value from nls_database_parameters; //查看字符集
create table tt2 as select * from uesr1.table1;
alter table maple add (name varchar2(20) default 'unknown'); //增加column
alter table maple modify (name varchar2(30)); //修改
comment on table employee is ‘employee info’; //创建表提示信息
create table test1 (id int not null,lname varchar(20),fname varchar(20),constraint uk_test1_1 unique(lname,fname)) //创建表基本语法
create table test2 (rid int,name varchar(20),constraint fk_test2_1 foreign key(rid) references test1(id)); //创建和表1的约束关系
alter table test1 add constraint ck_test2_1 check(name like ‘k%’) //约束条件
alter table test2 drop constraint ck_test2_1;
alter table test2 drop constraint pk_test2 cascade; //连带删除

视图和索引
create or replace view testview1 as select id,name from maple where ...with read only; //创建视图
create index testindex1 on maple(id); //创建索引
约束关系
使用SQL*Loader或Insert完成数据装载之前,为加快数据装载速度,应先禁止约束。
Alter table emp disable novalidate constraint check_job;
激活约束
Alter table emp enable novalidate/validate constraint check_job;
增加约束
Alter table department modify name not null;
Alter table department add constraint u_deptname unique(name);
Alter table department add constraint check_job check (job in (‘manager’,’clerk’));
Alter table department add constraint check_salary check (salary between 100 and 500);
修改约束名
Alter table department rename constraint pk_department to pk_depno;

备份和恢复
物理备份:备份数据库的物理文件(数据文件,控制文件)
逻辑备份:export导出数据库部分或全部结构和数据
只读表空间只需备份一次。不要备份联机重做日志,要多元化重做日志。当数据库物理结构变化时应立即备份控制文件。
当用户执行了误操作drop table后,可以用import工具导入表的结构或数据,也可以用logminer工具确定误操作时间,然后执行基于时间点的不完全恢复,当执行了DML误操作后,可以用logminer工具还原DML操作,确定错误的DML,然后执行相反的操作。
多元化控制文件
Alter system set control_files=”c:\test\test01.ctl”,”d:\test\test02.ctl” scope=spfile;
Shutdown immediate
Host copy c:\test\test01.ctl d:\test\test02.ctl
Startup
Select * from v$controlfiles;
多元化重做日志
Alter database add logfile member ‘d:\test\redo1b.log’ to group 1;
多元化归档日志
Alter system set log_archive_dest=””;
Alter system set log_archive_duplex_dest=””;
Alter system set log_archive_dest_1=”location= c:\test\archive”;
Alter system set log_archive_dest_1=”location= d:\test\archive”;
Alter system set log_archive_dest_1=”service=standby”;
在线备份控制文件副本
Alter database backup controlfile to ‘e:\bck\test01.bak’ reuse;
备份归档日志
Select name from v$archived_log;
在mount状态下修改数据文件位置
Alter database rename file ‘d:\test\system01.dbf’ to ‘e:\test\system01.dbf’
在open状态下修改数据文件位置,但不能修改system表空间和在用undo表空间的数据文件位置,在修改前必须让表空间或数据文件脱机
Alter tablespace users rename datafile ‘d:\test\system01.dbf’ to ‘e:\test\system01.dbf’;
修改重做日志也是使用alter database命令,可以在mount也可以在open状态下。
修改控制文件位置只需修改control_files,需要重启数据库
Alter system set control_files=‘d:\test\test01.ctl’, ‘e:\test\test01.ctl’ scope=spfile;
恢复命令
—recover database,只能在mount状态下
—recover tablespace,不能恢复system表空间和在用undo表空间,只能在open状态下
—recover datafile,可以在mount也可以在open状态下
不完全恢复要求在mount状态
—基于时间恢复
Startup mount
Host copy e:\bck\system01.dbf c:\test
Recover database until time ‘2003-05-07 20:00:55’
Alter database open resetlogs;
Archive log list
—基于终止恢复
Recover database until cancel
—基本控制文件副本恢复
Recover database until time ‘2003-05-07 20:00:55’ using backup controlfile
—基于表空间的时间点恢复

系统问题
Select machine,terminal from v$session; //检查某个用户从哪台机子登陆
Select username from v$session;
Select * from dba_sys_privs; //查看每个用户的权限
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值