從Oracle的約束到索引

來源:互聯網
上載者:User

Oracle有五種約束。主鍵primary keys 約束,外鍵foreign約束。Unique約束,NOT NULL約束,check約束。記住:約束都是針對於某一列來說的。

這裡說明下check約束:ALTER TABLE temp ADD CONSTRAINT ck_temp_age CHECK

((AGE>0) AND (AGE <= 125)); 這樣age只能屬於0到125區間了。別的資料插不進去。

再解釋一下primary keys 約束和Unique約束的區別:

primary keys 約束要大於Unique約束,就是說Unique約束能有的,primary keys 約束都具有。而且primary keys 約束要求欄位不能是空值。primary keys 約束會自動往欄位添加唯一索引。

唯一索引是索引種類的一種。

Oracle的索引類型:有三種分類方法:

第一種:按類型分 :1.B-樹索引 2.位元影像索引

第二種:1.唯一索引 2.主關鍵字索引 3.一般索引(主要用來提高查詢速度)

第三種:1.單列索引 2.多列索引 3.函數索引

create index i_ename on emp(ename); 這樣便建立了一個一般索引。

CREATE BITMAP INDEX index_name ON normal_index_creation_clause;這樣便建立了一個位元影像索引。

create unique index dept_unique_idx on dept(dept_no) tablespace idx_1; 這樣便建立唯一索引

建立與約束相關的索引 ,可以用using index字句,為與unique和primary key約束相關的索引,例如:

  alter table table_name

  add constraint PK_primary_keyname primary key (field_name)

  using index tablespace tablespace_name;

最後一個建立與約束相關的索引其實用的挺多的。

相關文章

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.