天天看點

ORACLE 限制的基礎知識介紹

1,限制的分類。

      限制分成5類:1. not null,2.primary key,3.check,4.unique,5.foreign key。

     1.1 not null限制

            預設情況下,所有列的值都可以包含null值,當在列上定義not null限制後,列上面就必須得有值。not null限制還常常與其它的限制一起組合起來使用,比如與unique限制一起使用,就可以保證新插入的列的資料不會與已經存在的資料發生沖突。需要在相當的列上面建立索引的時候,建議也在相關的列上面增加上not null限制,因為索引不會存放null記錄。

    1.2 primary key限制

           主鍵限制其實就是not null限制與unique限制的一個組合,用來保證行記錄的唯一,不重複性。每張表隻能有一個主鍵限制,在表設計的時候,我們一般都每張表上面都要有主鍵限制。在建立主鍵的時候會自己的建立相應限制名的索引,在選擇主鍵限制的列的時候可以參考下面指導:

           1.選擇sequence的列做為主鍵。

           2.選擇列的值是唯一的,并且沒有null值的列。

           3.主鍵的更一般不會發生修改,也僅僅用于辨別行的唯一性,不用于其它的目的。

           4.主鍵的列盡量選擇值比較短的值或者是number的值。

   1.3 unique限制

          unique限制是保證值的記錄不會出現相同的值,但是noll值不受權限,建立unique限制的時候,會自己建立限制名的索引。

   1.4 check限制

          檢查限制用于檢查值在插入時是否滿足指定的條件,比如值要求大于10小于100.

   1.5 foreign key限制

          當2個表,當A表中的列的值必須在B表中的列的值時候,可以定義外鍵限制。父表相關的值上面有主鍵或者唯一性限制。不過很多公司要求不能使用外鍵,讓開發自己用程式來判斷。

2,限制的定義

      限制可以在表建立的時候指定,也可以在表建立完成後通過alter指令來建立,下面是每一種限制建立的文法。

   2.1 not null限制 

      create table test_cons (id number constraint cons_test_cons_id_nonull not null); 

      create table test_cons_1(id number not null); 

      alter table test_cons_2 modify id not null; 

      2.2 primary key 

create table test_cons_1 (id number primary key); 

create table test_cons_2 (id number constraint cons_2 primary key); 

create table test_cons_3 (id number,constraint cons_3 primary key(id)); 

create table test_cons_4 (id number); 

alter table test_cons_4 add constraint cons_4 primary key(id); 

    2.3 unique 

create table test_cons_1 (id number unique); 

create table test_cons_2 (id number constraint cons_2 unique); 

create table test_cons_3 (id number,constraint cons_3 unique(id)); 

alter table test_cons_4 add constraint cons_4 unique(id); 

 2.4 check 

create table test_cons_1 (id number check (id > 10 and id <100)); 

create table test_cons_2 (id number constraint cons_2 check (id > 10 and id <100)); 

create table test_cons_3 (id number,constraint cons_3 check (id > 10 and id <100)); 

alter table test_cons_4 add constraint cons_4 check (id > 10 and id <100); 

2.5 外鍵限制 

create table test_cons_1 (id number,constraint cons_1 foreign key (id) references test_cons(id)); 

alter table test_cons_4 add constraint cons_4 foreign key (id) references test_cons(id); 

3,限制的維護

3.1 限制更改名字 

alter table test_cons rename constraint cons_id to cons_1_id; 

3.2 啟用與禁用限制 

alter table test_cons disable constraint cons_1_id; 

alter table test_cons disable constraint cons_1_id keep index; 

alter table test_cons disable primary key cascade; 

alter table test_cons enable constraint cons_1_id; 

alter table test_cons enable novalidate constraint cons_1_id; 

alter table test_cons enable  validate constraint cons_1_id; 

3.3  修改與删除限制 

alter table test_cons modify constraint cons_1_id novalidate; 

alter table test_cons modify constraint cons_1_id validate; 

alter table test_cons drop constraint cons_1_id keep index; 

alter table test_cons drop constraint cons_1_id; 

4,限制的Exception

If exceptions exist when a constraint is validated, an error is returned and the integrity constraint remains novalidated. When a statement is not successfully executed because integrity constraint exceptions exist, the statement is rolled back. If exceptions exist, you cannot validate the constraint until all exceptions to the constraint are either updated or deleted.

You must create an appropriate exceptions report table to accept information from the EXCEPTIONS option of the ENABLE clause before enabling the constraint. You can create an exception table by executing the UTLEXCPT.SQL script or the UTLEXPT1.SQL script.

Both of these scripts create a table named EXCEPTIONS. You can create additional exceptions tables with different names by modifying and resubmitting the script.

The following statement attempts to validate the PRIMARY KEY of the dept table, and if exceptions exist, information is inserted into a table named EXCEPTIONS:

ALTER TABLE dept ENABLE PRIMARY KEY EXCEPTIONS INTO EXCEPTIONS;

SELECT * FROM EXCEPTIONS;

The following exceptions are shown:

fROWID               OWNER      TABLE_NAME      CONSTRAINT

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

AAAAZ9AABAAABvqAAB  SCOTT      DEPT            SYS_C00610 

AAAAZ9AABAAABvqAAG  SCOTT      DEPT            SYS_C00610

SELECT deptno, dname, loc FROM dept, EXCEPTIONS

    WHERE EXCEPTIONS.constraint = 'SYS_C00610'

    AND dept.rowid = EXCEPTIONS.row_id;

5,限制的Defer

When the database checks a constraint, it signals an error if the constraint is not satisfied. You can defer checking the validity of constraints until the end of a transaction.

When you issue the SET CONSTRAINTS statement, the SET CONSTRAINTS mode lasts for the duration of the transaction, or until another SET CONSTRAINTS statement resets the mode.

Set All Constraints Deferred

Within the application being used to manipulate the data, you must set all constraints deferred before you actually begin processing any data. Use the following DML statement to set all deferrable constraints deferred:

SET CONSTRAINTS ALL DEFERRED;

Check the Commit (Optional)

You can check for constraint violations before committing by issuing the SET CONSTRAINTS ALL IMMEDIATE statement just before issuing the COMMIT. If there are any problems with a constraint, this statement fails and the constraint causing the error is identified. If you commit while constraints are violated, the transaction is rolled back and you receive an error message

               本文轉自7343696 51CTO部落格,原文連結:http://blog.51cto.com/luoping/1028793,如需轉載請自行聯系原作者