Oracle 基礎--索引

今天,我在完成 數據庫恢復后,溫習了一下索引基礎,感覺真是曾經滄海難爲水,單身依舊笑春風。

1: 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段, 必须在表级定义约束 
2:  在定义约束时可以通过CONSTRAINT关键字为约束命名,如果没有指定,ORACLE将自动为约束建立默认的名称


定义primary key约束(单个字段)
create table employees (empno number(5) primary key,...)

指定约束名
create table employees (empno number(5) constraint emp_pk primary key,...)

SQL> edit
Wrote file afiedt.buf

  1  create table employees(
  2  empno number(5),
  3  ename varchar2(15),
  4  phone varchar2(15),
  5  email varchar2(30) unique,
  6  deptno number(3) not null
  7* )
  8  /

Table created.

再次定義同一個字段not null時:

SQL> alter table employees modify deptno not null;
alter table employees modify deptno not null
                             *
ERROR at line 1:
ORA-01442: column to be modified to NOT NULL is already NOT NULL


SQL> edit
Wrote file afiedt.buf

  1* alter table employees add constraint emp_uk unique(ename,phone)
SQL> /

Table altered.

定义了UNIQUE约束的字段中不能包含重复值,可以为一个或多个字段定义UNIQUE约束,因此,UNIQUE即可以在字段级也可以在表级定义, 在UNIQUED约束的字段上可以包含空值.

foreign key约束

* 定义为FOREIGN KEY约束的字段中只能包含相应的其它表中的引用码字段的值或者NULL值
* 可以为一个或者多个字段的组合定义FOREIGN KEY约束
* 定义了FOREIGN KEY约束的外部码字段和相应的引用码字段可以存在于同一个表中,这种情况称为"自引用"
* 对同一个字段可以同时定义FOREIGN KEY约束和NOT NULL约束
定义了FOREIGN KEY约束的字段称为"外部码字段",被FORGIEN KEY约束引用的字段称为"引用码字段",引用码必须是主码或唯一码,包含外部码的表称为子表,
包含引用码的表称为父表.

SQL> alter table dept add constraint dpt_pk primary key(deptno);

Table altered.

SQL>  alter table employees add constraint emp_deptno_fk foreign key(deptno) references dept(d
eptno);

Table altered.
SQL> edit
Wrote file afiedt.buf

  1* create table jobs(deptno number(3) not null,jobid number(3) not null, constraint job_pk p
rimary key(jobid,deptno))
SQL> /

SQL> edit
Wrote file afiedt.buf

  1* alter table employees add jobid number(3) not null
SQL> /

SQL> alter table employees add constraint emp_job_fk foreign key(jobid,deptno) references
  2  jobs(jobid,deptno) on delete cascade;

Table altered.

更改foreign key约束定义的引用行为(delete cascade/delete set null/delete no action),默认是delete on action


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


先删除原来的约束,再添加约束

SQL> alter table employees drop constraint emp_uk ;

Table altered.

SQL> alter table employees add constraint emp_uk unique(ename,phone);

Table altered.

check约束
* 在CHECK约束的表达式中必须引用到表中的一个或多个字段,并且表达式的计算结果必须是一个布尔值
* 可以在表级或字段级定义
* 对同一个字段可以定义多个CHECK约束,同时也可以定义NOT NULL约束
SQL> alter table employees add sal number(7,2) constraint emp_sal_ch1 check(sal>0);

Table altered.
删除约束时,默认将同时删除约束所对应的索引,如果要保留索引,用KEEP INDEX关键字

SQL>  alter table dept drop primary key  keep index;
 alter table dept drop primary key  keep index
*
ERROR at line 1:
ORA-02273: this unique/primary key is referenced by some foreign keys

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

SQL> alter table dept drop primary key cascade;

Table altered.

删除约束,除了指定約束名,也可以指定约束的定义内容

SQL> alter table employees drop unique(ename,phone);
Table altered.

禁用/激活约束(禁用/激活约束会引起删除和重建索引的操作)
SQL> edit
Wrote file afiedt.buf

  1* alter table employees disable constraint emp_sal_ch1
SQL> /

Table altered.

SQL> alter table employees modify constraint emp_sal_ch1 enable;

Table altered.

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

SQL> alter table employees disable primary key cascade;

Table altered.

all_constraints/dba_constraints/user_constraints 约束的基本信息,包括约束的名称,类型,状态
(约束类型:C(CHECK约束),P(主码约束),R(外部码约束),U(唯一码约束))

Oracle 的索引
    索引和对应的表应该位于不同的表空间中,oracle能够并行读取位于不同硬盘上的数据,可以避免产生I/O冲突
B树索引:在B树的叶节点中存储索引字段的值与ROWID。
唯一索引和不唯一索引都只是针对B树索引而言.
Oracle最多允许包含32个字段的复合索引

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

创建唯一索引,去掉unique 就建立的是不唯一索引。

SQL> alter table employees drop unique(email);

Table altered.

SQL>  create unique index emp_email on employees(email) tablespace rman;

Index created.

创建位图索引

SQL> alter table employees add sex varchar2(2);

Table altered.

SQL> create bitmap index emp_sex on employees(sex) tablespace rman;

Index created.

创建反序索引

SQL> create unique index emp_reinx on employees(empno,jobid) tablespace rman reverse;

Index created.

创建函数索引(函数索引即可以是普通的B树索引,也可以是位图索引)

SQL> create index emp_substr_empno on employees(substr(empno,1,2)) tablespace rman;

Index created.

由于定义约束时由oracle自动建立的索引通常是不知道名称的,对这类索引的修改经常是利用alter table ..using index语句进行的,而不是alter index语句,利用employees表中primary key约束对应的索引的PCTFREE参数修改为5
SQL> alter table employees enable primary key using index pctfree 5;

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

Index altered.

删除索引
SQL> drop index emp_ename;

Index dropped.

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

 

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

转载于:http://blog.itpub.net/701141/viewspace-886/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值