1、约束分类

主键约束(PRIMARY KEY):用于唯一标示表中的一个列,在一个表中的主键约束只能有一个,但是可以在一个主键约束中包含多个列,也称为联合约束或者联合主键。
外键约束(FOREIGN KEY):用于约束表与表之间关联关系。 
唯一约束(UNIQUE KEY):用于唯一标示表中的一个列,与主键约束不同,在一张表中可以多个唯一约束。
检查约束(CHECK):用于约束表中列的输入值得范围,比如在输入性别时,要求数据库中只能输入男或者女,就可以使用检查约束来约束该列。
非空约束(NOT NULL):表示该列一定有值,不能为空。
默认约束(DEFAULT):用于向列中插入默认值。
 
2、约束的命名

建议自己定义一套命名规则,否则使用系统生成的约束名,很难能把它和对应的表、字段联系起来。
非空约束     NN_表名_列名
唯一约束     UK_表名_列名
主键约束     PK_表名
外键约束     FK_表名_列名
条件约束     CK_表名_列名
默认约束     DF_表名_列名
 
3、主键约束 PRIMARY KEY

文档原文翻译如下
A table or view can have only one primary key.
一个表或视图有且只有一个主键
 
None of the columns in the primary key can be LOB, LONG, LONG RAW, VARRAY, NESTED TABLE, BFILE, REF, TIMESTAMP WITH TIME ZONE, or user-defined type. However, the primary key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE.
主键字段不能为LOB、LOG、LOG RAW、VARRAY、NESTED TABLE、BFILE、REF、TIMESTAMP WITH TIME ZONE或用户定义类型。然而它可以包含TIMESTAMP WITH LOCAL TIME ZONE类型的字段。
 
The size of the primary key cannot exceed approximately one database block.
主键大小不能超过一个数据块大小。
 
A composite primary key cannot have more than 32 columns.
主键组合键不能超过32列。
 
You cannot designate the same column or combination of columns as both a primary key and a unique key.
你不能指定一列或组合列既是主键又是唯一键。
 
You cannot specify a primary key when creating a subview in an inheritance hierarchy. The primary key can be specified only for the top-level (root) view.
创建一个继承层次结构中的子视图时,你不能指定一个主键。主键可以唯一指定的顶层(根)视图。
 
创建表的时候声明主键
--声明列是声明约束
create table student(
sid int not null primary key,
...
);
 
--或 通过 constraint 声明约束
--格式: constraint 约束名称 约束类型 (受约束列)
drop table student cascade constraints;
create table student(
sid int not null,
...
constraint pk_stu_sid primary key(sid)
)
 
通过alter语句添加主键约束
--格式: alter table 表名 add constraint 约束名称 约束类型 (受约束列);
alter table student add constraint pk_stu_sid primary key (sid);
 
4、外键 FOREGIN KEY

文档原文翻译
Restrictions on Foreign Key Constraints Foreign key constraints are subject to the following restrictions:
None of the columns in the foreign key can be of LOB, LONG, LONG RAW, VARRAY, NESTED TABLE, BFILE, REF, TIMESTAMP WITH TIME ZONE, or user-defined type. However, the primary key can contain a column of TIMESTAMP WITH LOCAL TIME ZONE.
外键字段不能为LOB, LONG, LONG RAW, VARRAY, NESTED TABLE, BFILE, REF, TIMESTAMP WITH TIME ZONE, or user-defined type类型,主键可以包含数据类型为TIMESTAMP WITH LOCAL TIME ZONE的字段。
 
The referenced unique or primary key constraint on the parent table or view must already be defined.
引用唯一或主键约束,必须是父表中已经定义的。
 
A composite foreign key cannot have more than 32 columns.
外键的组合列不能超过32列。
 
The child and parent tables must be on the same database. To enable referential integrity constraints across nodes of a distributed database, you must use database triggers. See CREATE TRIGGER.
字表和父表必须在同一个数据库。分布式数据库中,外键不能跨节点,但触发器可以
If either the child or parent object is a view, then the constraint is subject to all restrictions on view constraints. See "View Constraints".
 
You cannot define a foreign key constraint in a CREATE TABLE statement that contains an AS subquery clause. Instead, you must create the table without the constraint and then add it later with an ALTER TABLE statement.
你不能在CREATE TABLE语句中包含AS子查询子句定义一个外键约束。相反,你必须创建一个没有约束的表,然后添加ALTER TABLE语句。
 
创建表的时候声明外键
drop table clazz cascade constraints;
drop table student cascade constraints;
create table clazz(
cid int not null primary key,
bak_id int not null unique
);
create table student(
sid int not null primary key,
cid int ,
--foreign key(cid) references clazz(cid),
constraint fk_stu_cid foreign key(cid) references clazz(cid)
)
 
通过alter语句添加或者修改外键约束
alter table student add constraint fk_stu_cid2 foreign key(cid) references clazz(bak_id) [on delete cascade | set null];
-----------------------------
on delete set null: 当主表中的一行数据被删除时,Oracle系统会自动地将所有从表中依赖于它的数据记录的外键改成空值;
on delete cascade: 当主表中的一行数据被删除时,Oracle系统会自动地将所有从表中依赖于它的数据记录删除;
 
外键约束对DML与DDL的影响
insert: 只有操作是在子表一端时才可能产生违反引用完整性约束的问题。
delete: 只有操作是在父表这一端时可能产生违反引用完整性约束的问题。
update: 子表父表直接操作都可能违反引用完整性约束。
 
解决外键约束带来的完整性约束的问题
先更新子表的引用列为空,再更新父表的主键的列的值,然后把子表的引用列更新成新的父表的值;
使用on deleteset null、on delete cascade,先更新父表,然后将子表外键为空的记录更新为新的值。
注: 如果在外键定义中使用了on delete set null 或 on delete cascade,无论删除操作是在父表这一端还是子表这一端都不会产生违反引用完整性的问题,但是却留下了安全隐患(一般不这样)。
 
 
5、非空约束 NOT NULL

创建表的时候声明
drop table student cascade constraints;
create table student(
sid int not null
...
)
 
通过alter语句添加约束
drop table student cascade constraints;
create table student(
sid int
);
--非空约束比较特殊。
alter table student modify (sid constraint nk_stu_sid not null);-- 非空
alter table student modify(sid not null); -- 非空
alter table student modify(sid null); -- 可以为空
 
6、唯一约束

创建表的时候声明
drop table student cascade constraints;
create table student(
--sid int unique,
sid int,
constraint uk_stu_sid unique(sid)
);
 
通过alter语句添加约束
drop table student cascade constraints;
create table student(
sid int
);
alter table student add constraint uk_stu_sid unique(sid);
 
7、检查约束

创建表的时候声明约束
drop table student cascade constraints;
create table student(
sid integer not null,
--sno number(12) check(length(sno) between 4 and 12), --检查约束,输入长度(位数)4-12位
sno number(12),
constraint ck_stu_sno check(length(sno) between 4 and 12)
);
 
通过alter语句添加约束
drop table student cascade constraints;
create table student(
sid integer not null,
sno number(12)
);
alter table student add constraint ck_stu_sno check(length(sno) between 4 and 12);
 
8、默认约束

创建表的时候声明约束
drop table student cascade constraints;
create table student(
sid integer not null,
studyDate date default(sysdate) --默认约束
 
);
 
 
通过alter语句添加约束
drop table student cascade constraints;
create table student(
sid integer not null,
studyDate date
);
--默认约束同非空约束很像
alter table student modify(studyDate default sysdate);
 
9、约束的查询

存储约束的表
user_constraints:当前用户所创建的。
all_constraints:当前用户所创建的以及当前用户能访问的。
dba_constraints:整个数据库的。
 
存储约束与字段或表的关系的表(同上)
user_cons_columns:
all_cons_columns:
dba_cons_columns:
 
xxx_constraints表中的constraint_type的类型
C --- 表或者字段的检查约束 check
P --- 主键约束 primary key
R --- 外键约束 foreign Key
U --- 唯一约束 unique
还有其他 O,V 有兴趣可以查资料
 
查询某个表中的某个字段的约束
select
a.constraint_name,
a.constraint_type,
b.column_name
from user_constraints a, user_cons_columns b
where a.table_name = b.table_name
and a.constraint_name = b.constraint_name
and a. table_name = 'STUDENT'
and b.column_name = 'SID';
 
10、约束的管理(启用、禁用、重命名、删除)

禁用约束
alter table 表 disable constraint 约束名 [cascade];
----------------------------------------
drop table student cascade constraints;
create table student(
sid integer not null primary key,
cid int,
constraint uk_stu_cid unique(cid)
);
 
alter table student disable primary key cascade;
alter table student disable constraint uk_stu_cid cascade;
--参数CASCADE子句用来关闭存在有完整性关系的约束,比如DISABLE一个主键,如果没有CASCADE子句就会出错,此时使用CASCADE子句DISABLE主键可以将该主键与相关外键一起DISABLE掉。使用的情况:例如在数据库系统中大规模装入数据时,为了系统的效率您不得不牺牲数据的一致性来关闭一些约束,甚至删除一些约束将主表主键禁止的同时,也将禁止依赖于此主键的外键禁止了.
 
启用约束
alter table 表 enable constraint 约束名;
-------------------------------------------
drop table student cascade constraints;
create table student(
sid integer not null primary key,
cid int,
constraint uk_stu_cid unique(cid)
);
 
alter table student disable constraint uk_stu_cid cascade;
alter table student enable constraint uk_stu_cid ;
 
重命名
alter table 表 rename constraint old约束名称 to new约束名称;
---------------
drop table student cascade constraints;
create table student(
sid integer,
constraint uk_stu_sid unique(sid)
);
alter table student rename constraint uk_stu_sid to uk_stu_sid2;
 
删除约束
alter table student drop constraint 约束名称;
alter table student drop constraint uk_stu_sid2;