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));
- 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
本文转自7343696 51CTO博客,原文链接:http://blog.51cto.com/luoping/1028793,如需转载请自行联系原作者