您的位置:首页 > 数据库 > Oracle

oracle 数据库基本知识

2014-06-03 16:47 483 查看
1.安装硬件条件

#grep MemTotal /proc/meminfo

#grep SwapTotal /proc/meminfo

#free

查看共享内存数量的命令

#df -k /dev/shm/

#df -k /tmp

查看操作系统版本

#cat /proc/version

查看内核版本的命令:

#uname -r
#rpm -q package_name package_name

2.表空间,数据文件

SYS>col file_name for a50;

SYS>set linesize 140;

SQL> select file_name,tablespace_name,bytes from dba_data_files;

SQL> select tablespace_name,file_name from dba_temp_files;

3.控制文件

SQL>  select name,status from v$controlfile;

4.日志文件

对于表或者整个表空间设定nologging属性时,基于表或者表空间中所有表的DML操作都不会生成日志信息,当然也就减少了日志信息的产生。

SQL> select group#,status,member from v$logfile;

5.归档日志文件

SQL> select dbid,name,log_mode from v$database;

6.控制文件

SQL>select * from v$controlfile;

SQL>select name,value from v$parameter where name='control_files';

alter system set control_files='/app/oracle/control01.ctl','/app/oracle/control02.ctl',scope=spfile;

alter database open resetlogs;

备份控制文件

a.将控制文件备份到二进制文件,即对控制文件进行复制。

alter database backup controlfile to '/oracle/backup/control.bkp';

b.先查看当前实例的控制文件路径,然后将控制文件备份为SQL脚本。

show parameter user_dump_dest

alter database backup controlfile to trace;

当使用“alter database backup controlfile to trace”语句创建控制文件脚本时,该脚本文件写到user_dump_dest参数所指的路径上,脚本文件名称格式如下:

 unix下文件名为ORA_SID_N(process_id).trc;

 windows操作系统下文件名为SID_ORA_SPID.trc。

 其中,SID表示当前会话的表示号;SPID表示操作系统进程标识号。

恢复控制文件

假定参数CONTROL_FILES所定的控制文件有一个已经损坏,但在数据字典中还能访问到控制文件,则可采用下面的方法进行恢复。

详细步骤如下:

关闭数据库实例;

用操作系统将好的控制文件覆盖掉坏的控制文件;

重新启动数据库。

7.服务器参数文件

service parameter file=spfile,不可修改,pfile可修改。格式为spfilesid.ora,initsid.ora可以修改

位置:$ORACLE_HOME/dbs/

[oracle@standby dbs]$ file -b initkldb.ora 

ASCII text

[oracle@standby dbs]$ file -b spfilekldb.ora 

data

8.密码文件

创建一个密码文件,其sys口令是change_on_install_new

$orapwd file=$ORACLE_HOME/oracle/intra.passwd

password=change_on_install_new entries=30

9.警告日志文件

应注意oracle实例出现过的错误、异常环境及一些永久性操作。

SQL> select value from v$parameter where name='background_dump_dest';

VALUE

--------------------------------------------------------------------------------

/opt/app/oracle/diag/rdbms/kldb/kldb/trace

10.后台跟踪文件和用户跟踪文件

与警告文件一起构成完整的故障信息描述体系。警告日志包括错误事件的说明,而随之产生的跟踪文件记录了该错误的详细信息。

如果系统在安装时遵循oracle的OFA目录结构,那么在Unix环境下,当oracle系统发生错误时,跟踪文件会被记录在$ORACLE_BASE/admin/SID/udump目录下。

SQL> select value from v$parameter where name='user_dump_dest';

11.创建表的sql

create table dept 

(depno number(2),dname varchar2(20),loc varchar2(20),

constraint pk_deptno primary key (deptno)

using index tablespace indx storate (initial 10M next 5M pctincrese 0))

tablespace user storate (initial 10M next 5M pctincrease 0)

alter table t_order disable constraint pk_order_id;

alter table t_order enable primary key;(如果存在引用该主键的外键值,则不能将主键的状态设置为无效,否则,会报ORA-02297错误,此时,必须先删除主外键关系,才能使主键无效。)

alter table t_order drop constraint pk_order_id;

create table emp

(empno number(4),ename varchar2(10),job varchar2(10),

deptno,constarint fk_deptno foreigh key(deptno) references dept(deptno) on

delete cascade);

alter table emp

add constraint fk_deptno foreigh key(deptno) references dept(deptno) on

delete cascade;

oracle 强烈建议,任何应用程序的库表至少需要创建两个表空间,一个用于存储表数据,而另一个用于存储表索引数据。如果将表数据和所以你数据放在一起,那么,表数据的I/O操作和索引的I/O操作将产生影响系统性能的I/O竞争,降低系统的响应效率。而将表数据和索引数据存放在不同的表空间中(如一个为APP_DATA,另一个为APP_IDX),并在物理层面将这两个表空间的数据文件放在不同的物理磁盘上,就可以避免这种竞争。

a.CHECK约束

create table worker(empno number(4) primary key,name varchar2(10),

sex char(2) check(sex='男’ or sex='女'),

age number(2) check (age between 18 and 65));

b.not null 约束

alter table worker modify name not null;

c.unique唯一性约束(允许null)

create table table_name .... column_name data_type constarint constraint_name unique using index tablespace indx

alter table table_name drop constraint unique_indx;

create unique index index_name on table_name(column,....) tablespace indx

12.视图

create or replace view scott.v_emp_dept as

select ename,dname,job,sal

from scott.emp e,scott.dept d

where e.deptno=d.deptno;

drop view view_name;

13.序列

create sequence user.sequece_name

increment by 1

start with 1

maxvalue 100 | nomaxvalue

minvalue 100 | nominvalue;

14.同义词

create public synonym public_emp fro scott.emp;

drop synonym public_emp;

15.创建外部表

create directory test_dir as 'E:temp';

grant read,write on directory test_dir to users;

create table test_table

(ms_no varchar(20),tip varchar(20),descs varchar(20))

organization external

(type oracle_loader

default directory test_dir

access parameters)

records delimited by newline

badfile 'bad_dev.txt'

logfile 'log_dev.txt'

fields terminated by ','

missing field values are null

(ms_no,tip,descs)

)location ('f1.txt','f2.txt'));

16.序列

create sequence emp_sequence

increate by 1

start with 1

nomaxvalue

nocycle

cache 0

alter sequece emp_sequence

increment by 10

maxvalue 10000

cycle

cache 20;

17.分区表

create table niegc_part

(part_id integer primary key,

part_date date,

part_dec varchar2(100))

partition by range(part_date)

(

partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace dw1,

partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace dw2,

partition part_03 values less than(maxvalue) tablespace dw1

);

insert into niegc_part values(1,to_date('2005-12-30','yyyy-mm-dd'),'less 2006-01-01');

create table niegc_part

(

part_id integer primary key,

part_date date,

part_dec varchar2(100)

)

partition by range(part_id,part_date)

(

partition part_01 values less than(1,to_date('2006-01-01','yyyy-mm-dd')) tablespace dw,

partition part_02 values less than(10,to_date('2007-01-01','yyyy-mm-dd')) tablespace dw,

partition part_03 values less than(maxvalue,maxvalue) tablespace dw

);

create table user_dflow(

user_name varchar2(30)

,bj_date date

,...

day_fee_in number(16,6)

,day_fee_out number(16,6)

)

partition by range(bj_date)

(partition y2004 value less than(to_date('20050101','yyyymmdd')) tablespace tabs1,

partition y2005 value less than(to_date('20060101','yyyymmdd')) tablespace tabs2,

partition y2006 value less than(to_date('20070101','yyyymmdd')) tablespace tabs3,

partition y2007 value less than(to_date('20080101','yyyymmdd')) tablespace tabs4,

)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: