转:oracle 索引

索引创建策略
1.导入数据后再创建索引
2.不需要为很小的表创建索引
3.对于取值范围很小的字段(比如性别字段)应当建立位图索引
4.限制表中的索引的数目
5.为索引设置合适的PCTFREE值
6.存储索引的表空间最好单独设定

创建不唯一索引
create index emp_ename on employees(ename)
tablespace users
storage(......)
pctfree 0;

创建唯一索引
create unique index emp_email on employees(email)
tablespace users;

创建位图索引
create bitmap index emp_sex on employees(sex)
tablespace users;

创建反序索引
create unique index order_reinx on orders(order_num,order_date)
tablespace users
reverse;

创建函数索引(函数索引即可以是普通的B树索引,也可以是位图索引)
create index emp_substr_empno
on employees(substr(empno,1,2))
tablespace users;

修改索引存储参数(与表类似,INITIAL和MINEXTENTS参数在索引建立以后不能再改变)
alter index emp_ename storage(pctincrease 50); 由于定义约束时由oracle自动建立的索引通常是不知道名称的,对这类索引的修改经常是利用alter table ..using index语句进行的,而不是alter index语句

利用下面的语句将employees表中primary key约束对应的索引的PCTFREE参数修改为5
alter table employees enable primary key using index pctfree 5;

清理索引碎片
1.合并索引(只是简单的将B树叶结点中的存储碎片合并在一起,并不会改变索引的物理组织结构)
alter index emp_pk coalesce;

2.重建索引(不仅能够消除存储碎片,还可以改变索引的全部存储参数设置,并且可以将索引移动到其它的表空间中,重建索引
实际上就是再指定的表空间中重新建立一个新的索引,然后删除原来的索引)
alter index emp_pk rebuild;

删除索引
drop index emp_ename;

如果索引中包含损坏的数据块,或者包含过多的存储碎片,需要首先删除这个索引,然后再重建它.
如果索引是在创建约束时由oracle自动产生的,可以通过禁用约束或删除约束的方法来删除对应的索引.
在删除一个表时,oracle会自动删除所有与该表相关的索引.

索引数据字典
all_indexes/dba_indexes/user_indexes 索引的基本信息
all_ind_columns/dba_ind_columns/user_ind_columns 索引对应的字段信息

create unique index 索引名 on    表名(字段名)
create unique index idx_empname on employee(empname);

create index 索引名 on    表名(字段名)
create index idx_empname on employee(empname);

alter table employees add primary key (empno)
alter table employees add constraint emp_pk primary key (empno)
alter table employees add constraint emp_pk primary key (empno,deptno)
alter table employees modify deptno not null/null

foreign key约束
添加foreign key约束(多字段/表级)
alter table employees
add constraint emp_jobs_fk foreign key (job,deptno)
references jobs (jobid,deptno)
on delete cascade

引用行为(当主表中一条记录被删除时,确定如何处理字表中的外部码字段):
delete cascade : 删除子表中所有的相关记录
delete set null : 将所有相关记录的外部码字段值设置为NULL
delete no action: 不做任何操作

先删除原来的外键约束,再添加约束
ALTER TABLE employees DROP CONSTRAINT emp_deptno_fk;
ALTER TABLE employees ADD CONSTRAINT emp_deptno_fk FOREIGN KEY(deptno) REFERENCES dept(deptno) ON DELETE CASCADE;

删除约束

alter table dept drop unique (dname,loc)   --指定约束的定义内容
alter table dept drop constraint dept_dname_loc_uk --指定约束名

删除约束时,默认将同时删除约束所对应的索引,如果要保留索引,用KEEP INDEX关键字
alter table employees drop primary key keep index

如果要删除的约束正在被其它约束引用,通过ALTER TABLE..DROP语句中指定CASCADE关键字能够同时删除引用它的约束

利用下面的语句在删除DEPT表中的PRIMARY KEY约束时,同时将删除其它表中引用这个约束的FOREIGN KEY约束:
alter table dept drop primary key cascade

禁用/激活约束(禁用/激活约束会引起删除和重建索引的操作)
alter table employees disable/enable unique email
alter table employees disable/enable constraint emp_ename_pk
alter tabel employees modify constraint emp_pk disable/enable
alter tabel employees modify constraint emp_ename_phone_uk disable/enable

如果有FOREIGN KEY约束正在引用UNIQUE或PRIMARY KEY约束,则无法禁用这些UNIQUE或PRIMARY KEY约束,
这时可以先禁用FOREIGN KEY约束,然后再禁用UNIQUE或PRIMARY KEY约束;或者可以在ALTER TABLE...DISABLE
语句中指定CASCADE关键字,这样将在禁用UNIQUE或PRIMARY KEY约束的同时禁用那些引用它们的FOREIGN KEY约束,如:
alter table employees disable primary key cascade

约束数据字典
all_constraints/dba_constraints/user_constraints 约束的基本信息,包括约束的名称,类型,状态
(约束类型:C(CHECK约束),P(主码约束),R(外部码约束),U(唯一码约束))
all_cons_columns/dba/user 约束对应的字段信息


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunliusun/archive/2009/10/30/4747020.aspx

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15149581/viewspace-676995/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15149581/viewspace-676995/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值