1,約束的分類。sql
約束分紅5類:1. not null,2.primary key,3.check,4.unique,5.foreign key。app
1.1 not null約束ide
默認狀況下,全部列的值均可以包含null值,當在列上定義not null約束後,列上面就必須得有值。not null約束還經常與其它的約束一塊兒組合起來使用,好比與unique約束一塊兒使用,就能夠保證新插入的列的數據不會與已經存在的數據發生衝突。須要在至關的列上面建立索引的時候,建議也在相關的列上面增長上not null約束,由於索引不會存放null記錄。ui
1.2 primary key約束this
主鍵約束其實就是not null約束與unique約束的一個組合,用來保證行記錄的惟一,不重複性。每張表只能有一個主鍵約束,在表設計的時候,咱們通常都每張表上面都要有主鍵約束。在建立主鍵的時候會本身的建立相應約束名的索引,在選擇主鍵約束的列的時候能夠參考下面指導:spa
1.選擇sequence的列作爲主鍵。設計
2.選擇列的值是惟一的,而且沒有null值的列。orm
3.主鍵的更通常不會發生修改,也僅僅用於標識行的惟一性,不用於其它的目的。索引
4.主鍵的列儘可能選擇值比較短的值或者是number的值。ip
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));
- create table test_cons_4 (id number);
- 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));
- create table test_cons_4 (id number);
- 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