天天看點

檢視Oracle的表中有哪些索引

用user_indexes和user_ind_columns系統表檢視已經存在的索引

對于系統中已經存在的索引我們可以通過以下的兩個系統視圖(user_indexes和user_ind_columns)來檢視其具體内容,例如是屬于那個表,哪個列和,具體有些什麼參數等等。

user_indexes:     系統視圖存放是索引的名稱以及該索引是否是唯一索引等資訊。

user_ind_column:  系統視圖存放的是索引名稱,對應的表和列等。

檢視索引個數和類别:

SQL> select * from user_indexes where table='表名' ;

檢視索引被索引的字段:

SQL> select * from user_ind_columns where index_name=upper('&index_name');

我們可以通過類似下面的語句來檢視一個表的索引的基本情況:

select user_ind_columns.index_name,user_ind_columns.column_name,

user_ind_columns.column_position,user_indexes.uniqueness

from user_ind_columns,user_indexes

where user_ind_columns.index_name = user_indexes.index_name

and user_ind_columns.table_name = ‘你想要查詢的表名字’;      

通過這條SQL語句我們能檢視到一個表的具體的索引的情況,如果你想對這表的索引進行進一步的探究你應該到user_indexes中去具體的看以下這個索引的基本情況。

完整性限制

  DBA_CONSTRAINTS、ALL_CONSTRAINTS和USER_CONSTRAINST  顯示有關限制的一般資訊。

  DBA_CONS_COLUMNS、ALL_CONS_COLUMNS和USER_CONS_COLUMNS 顯示有關列的相關限制的一般資訊。

ALL_CONS_COLUMNS 視圖和DBA_CONS_COLUMNS 視圖與USER_CONS_COLUMNS有相同的列定義。

ALL_CONS_COLUMNS 視圖能夠顯示使用者可以通路的所有表上限制的列資訊,而不管所有者是誰。

DBA_CONS_COLUMNS 視圖列出了整個資料庫的列級限制資訊。

USER_CONS_COLUMNS

user_constraints 和 user_cons_columns表得作用及其聯系

user_constraints:  是表限制的視圖,描述的是限制類型(constraint_type)是什麼,屬于哪些表(table_name),如果限制的類型為R(外鍵)的話,那麼r_constraint_name字段存放的就是被引用主表中的主鍵限制名。  

user_cons_columns: 是表限制字段的視圖,說明表中的和限制相關的列參與了哪些限制。這些限制有主鍵限制,外鍵限制,索引限制.

兩者可以通過(owner,constraint_name,table_name)關聯:

select 
a.owner 外鍵擁有者, 
a.table_name 外鍵表, 
substr(c.column_name,1,127) 外鍵列, 
b.owner 主鍵擁有者, 
b.table_name 主鍵表, 
substr(d.column_name,1,127) 主鍵列 
from 
user_constraints a, 
user_constraints b, 
user_cons_columns c, 
user_cons_columns d 
where 
    a.r_constraint_name=b.constraint_name 
and a.constraint_type='R' 
and b.constraint_type='P' 
and a.r_owner=b.owner 
and a.constraint_name=c.constraint_name 
and b.constraint_name=d.constraint_name 
and a.owner=c.owner 
and a.table_name=c.table_name 
and b.owner=d.owner 
and b.table_name=d.table_name      

資料字典表列說明:

desc user_constraints

Name                                                                                   Comments                                                                    

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

OWNER                                                                   Owner of the table                                                          

CONSTRAINT_NAME                                             Name associated with constraint definition                                  

CONSTRAINT_TYPE                                              Type of constraint definition                                               

TABLE_NAME                                                          Name associated with table with constraint definition                       

SEARCH_CONDITION                                             Text of search condition for table check                                    

R_OWNER                                                                 Owner of table used in referential constraint                               

R_CONSTRAINT_NAME                                          Name of unique constraint definition for referenced table                   

DELETE_RULE                                                          The delete rule for a referential constraint                                

STATUS                                                                      Enforcement status of constraint -  ENABLED or DISABLED                     

DEFERRABLE                                                           Is the constraint deferrable - DEFERRABLE or NOT DEFERRABLE                 

DEFERRED                                                                 Is the constraint deferred by default -  DEFERRED or IMMEDIATE              

VALIDATED                                                       Was this constraint system validated? -  VALIDATED or NOT VALIDATED         

GENERATED                                         Was the constraint name system generated? -  GENERATED NAME or USER NAME    

BAD                                                                        Creating this constraint should give ORA-02436.  Rewrite it before 2000 AD. 

RELY                                                                                       If set, this flag will be used in optimizer                                 

LAST_CHANGE                                                               The date when this column was last enabled or disabled                      

INDEX_OWNER                                                                The owner of the index used by the constraint                               

INDEX_NAME                                                                    The index used by the constraint                                            

INVALID                                                                                          

VIEW_RELATED      

desc user_cons_columns;

Name                                                                                Comments                                                                                         

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

OWNER                                                                         Owner of the constraint definition                                                               

CONSTRAINT_NAME                                               Name associated with the constraint definition                                                   

TABLE_NAME                                                        Name associated with table with constraint definition                                            

COLUMN_NAME                    Name associated with column or attribute of object column specified in the constraint definition 

POSITION                                                                      Original position of column or attribute in definition  

ORACLE的索引和限制詳解資料庫

Oracle的限制

* 如果某個限制隻作用于單獨的字段,即可以在字段級定義限制,也可以在表級定義限制,但如果某個限制作用于多個字段,

必須在表級定義限制

* 在定義限制時可以通過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限制時可以為它的索引

指定存儲位置和存儲參數

檢視Oracle的表中有哪些索引
檢視Oracle的表中有哪些索引
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      

View Code

定義了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:

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)      

删除限制

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的索引

    索引和對應的表應該位于不同的表空間中,oracle能夠并行讀取位于不同硬碟上的資料,可以避免産生I/O沖突

B樹索引:在B樹的葉節點中存儲索引字段的值與ROWID。

唯一索引和不唯一索引都隻是針對B樹索引而言.

Oracle最多允許包含32個字段的複合索引

索引建立政策

  1. 1.導入資料後再建立索引
  2. 2.不需要為很小的表建立索引
  3. 3.對于取值範圍很小的字段(比如性别字段)應當建立位圖索引
  4. 4.限制表中的索引的數目
  5. 5.為索引設定合适的PCTFREE值
  6. 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 索引對應的字段資訊

1. 查詢一張表裡面索引

select * from user_indexes where table_name=upper('tableName');      

2. 查詢被索引字段

select * from user_ind_columns where index_name=('indexName');      

3. 給某一字段建立索引

create index index_name on table_name(col_name);      

1.檢視所有使用者

select * from all_users; -------檢視所有的使用者

     select * from user_users; --------檢視目前使用者      

2.檢視使用者或角色系統權限:

select * from user_sys_privs; --------檢視目前使用者的權限      

3.檢視角色所包含的權限

select * from role_sys_privs;   -------      

4.檢視使用者對象權限

select * from all_tab_privs;   --------檢視所用的使用者的可操作表權限
     select * from user_tab_privs; --------檢視目前使用者的表可操作權限      

5.檢視使用者或角色所擁有的角色

select * from user_role_privs;   ------檢視目前使用者的角色

     select * from user_constraints where TABLE_NAME='?';    -----檢視某一個表的限制      

6.檢視使用者下的索引

1.  select  * from user_indexes-          -----檢視目前使用者下的所有索引

   2.  select  * from user_indexes where table_name='A';      -----檢視目前使用者下表A的索引
      (drop index index_name去掉索引) 

   3. select index_name,index_type,status,blevel from user_indexes where table_name = '?';  

           -----檢視某一個表的所有索引

   4.  select table_name, index_name, column_name, column_position from        user_ind_columns where  table_name='?';    ----檢視索引的構成      

 7. 建索引

create unique clustered index 索引名on 表名(字段1)  --單索引

       Create index 索引名 on 表名(字段1,字段2)  -------複合索引