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