ORACLE學習筆記 索引和約束

來源:互聯網
上載者:User

標籤:

/*** 約束 ***/

* 如果某個約束只作用於單獨的欄位,即可以在欄位級定義約束,也可以在表級定義約

束,但如果某個約束作用於多個欄位,
  必須在表級定義約束
* 在定義約束時可以通過CONSTRAINT關鍵字為約束命名,如果沒有指定,ORACLE將自動

為約束建立預設的名稱

定義primary key約束(單個欄位)
create table employees (empno number(5) primary key,...)

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

定義primary key約束(多個欄位,在表級定義約束)
create table employees 
(empno number(5),
 deptno number(3) not null,
 constraint emp_pk primary key(empno,deptno)
 using index tablespace indx
 storage (initial 64K
   next 64K
  )
)

ORACLE自動會為具有PRIMARY KEY約束的欄位(主碼欄位)建立一個唯一索引和一個NOT 

NULL約束,定義PRIMARY KEY約束時可以為它的索引
指定儲存位置和儲存參數

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)

not null約束(只能在欄位級定義NOT NULL約束,在同一個表中可以定義多個NOT NULL約

束)
alter table employees modify deptno not null/null

unique約束
create table employees 
( empno number(5),
  ename varchar2(15),
  phone varchar2(15),
  email varchar2(30) unique,
  deptno number(3) not null,
  constraint emp_ename_phone_uk unique (ename,phone)
)

alter table employees 
add constraint emp_uk unique(ename,phone)
using index tablespace indx

定義了UNIQUE約束的欄位中不能包含重複值,可以為一個或多個欄位定義UNIQUE約束,

因此,UNIQUE即可以在欄位級也可以在表級定義,
在UNIQUED約束的欄位上可以包含空值.

foreign key約束

* 定義為FOREIGN KEY約束的欄位中只能包含相應的其它表中的引用碼欄位的值或者

NULL值
* 可以為一個或者多個欄位的組合定義FOREIGN KEY約束
* 定義了FOREIGN KEY約束的外部碼欄位和相應的引用碼欄位可以存在於同一個表中,

這種情況稱為"自引用"
* 對同一個欄位可以同時定義FOREIGN KEY約束和NOT NULL約束

定義了FOREIGN KEY約束的欄位稱為"外部碼欄位",被FORGIEN KEY約束引用的欄位稱為"

引用碼欄位",引用碼必須是主碼或唯一碼,包含外部碼的表稱為子表,
包含引用碼的表稱為父表.

A:
create table employees 
(.....,
 deptno number(3) NOT NULL, 
 constraint emp_deptno_fk foreign key (deptno)
 references dept (deptno)
)

如果子表中的外部碼與主表中的引用碼具有相同的名稱,可以寫成:
B:
create table employees 
(.....,
 deptno number(3) NOT NULL 
 constraint emp_deptno_fk references dept
)

注意:
上面的例子(B)中not null後面沒有加逗號,因為這一句的contraint是跟在那一列

deptno後面的,屬於列定義,所以都無需指明列。而A例中的是表定義,需要指明那一

列,所以要加逗號,不能在列後面定義,還可以寫成:

create table employees
(empno char(4),
 deptno char(2) not null constraint emp_deptno_fk references dept,
 ename varchar2(10)
)
表定義contraint的只能寫在最後,再看兩個例子:

create table employees
(empno number(5),
 ename varchar2(10),
 deptno char(2) not null constraint emp_deptno_fk references dept,
 constraint emp_pk primary key(empno,ename)
)

create table employees
( empno number(5),
  ename varchar2(15),
  phone varchar2(15),
  email varchar2(30) unique,
  deptno number(3) not null,
  constraint emp_pk primary key(empno,ename),
  constraint emp_phone_uk unique (phone)
)

添加foreign key約束(多欄位/表級)
alter table employees
add constraint emp_jobs_fk foreign key (job,deptno) 
references jobs (jobid,deptno)
on delete cascade

更改foreign key約束定義的引用行為(delete cascade/delete set null/delete no 

action),預設是delete on action

引用行為(當主表中一條記錄被刪除時,確定如何處理字表中的外部碼欄位):
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;

check約束
* 在CHECK約束的運算式中必須引用到表中的一個或多個欄位,並且運算式的計算結果

必須是一個布爾值
* 可以在表級或欄位級定義
* 對同一個欄位可以定義多個CHECK約束,同時也可以定義NOT NULL約束

create table employees 
(sal number(7,2)
 constraint emp_sal_ck1 check (sal > 0)
)

alter table employees
add constraint emp_sal_ck2 check (sal < 20000 br style=‘font-size:14px;font-style:normal;font-weight:normal;color:rgb(0, 0, 0);‘ />
刪除約束

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 約束對應的欄位資訊

/*** 索引 ***/

索引和對應的表應該位於不同的資料表空間中,oracle能夠並行讀取位於不同硬碟上的資料

,可以避免產生I/O衝突
B樹索引:在B樹的分葉節點中儲存索引欄位的值與ROWID。
唯一索引和不唯一索引都只是針對B樹索引而言.
Oracle最多允許包含32個欄位的複合索引

索引建立策略
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 索引對應的欄位資訊

ORACLE學習筆記 索引和約束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.