Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  302 随笔 :: 26 文章 :: 82 评论 :: 0 Trackbacks
Constraint的简单操作
 
 
、约束的建立
 
    如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,必须在表级定义约束。
 
    在定义约束时可以通过CONSTRAINT关键字为约束命名,如果没有指定,ORACLE将自动为约束建立默认的名称。
 
 
1、定义primary key约束(单个字段)

    create table employees (empno number(5) primary key,...)
    注:直接定义字段级约束是不用显示标注constraint关键字的
 
2、指定约束名

    create table employees (empno number(5) constraint emp_pk primary key,...)
 
3、定义primary key约束(多个字段,在表级定义约束)

    create table employees 
    (empno number(5), 
    deptno number(3) not null, 
    constraint emp_pk primary key(empno,deptno) 
    using index tablespace indx 
    storage
    (initial 64K 
     next 64K )
    )
 
    ORACLE自动会为具有PRIMARY KEY约束的字段(主码字段)建立一个唯一索引和一个NOT NULL约束,定义PRIMARY KEY约束时可以为它的索引
 
4、指定存储位置和存储参数
 
alter table employees add primary key (empno)
alter table employees add constraint emp_pk primary key (empno)
alter table employees add constraint emp_pk primary key (empno,deptno)
 
 
二、not null约束
 
    只能在字段级定义NOT NULL约束,在同一个表中可以定义多个NOT NULL约束)

    alter table employees modify deptno not null/null
 
 
三、unique约束

    create table employees 
    ( empno number(5), 
    ename varchar2(15), 
    phone varchar2(15), 
    email varchar2(30) unique, 
    deptno number(3) not null, 
    constraint emp_ename_phone_uk unique (ename,phone) 
    )
 
    alter table employees 
    add constraint emp_uk unique(ename,phone) 
    using index tablespace indx
 
    定义了UNIQUE约束的字段中不能包含重复值,可以为一个或多个字段定义UNIQUE约束,因此,UNIQUE即可以在字段级也可以在表级定义, 
    在UNIQUED约束的字段上可以包含空值.
 
 
四、foreign key约束
 
1、foreign key的性质
 
    * 定义为FOREIGN KEY约束的字段中只能包含相应的其它表中的引用码字段的值或者NULL值 
    * 可以为一个或者多个字段的组合定义FOREIGN KEY约束 
    * 定义了FOREIGN KEY约束的外部码字段和相应的引用码字段可以存在于同一个表中,这种情况称为“自引用” 
    * 对同一个字段可以同时定义FOREIGN KEY约束和NOT NULL约束
 
    定义了FOREIGN KEY约束的字段称为“外部码字段”,被FORGIEN KEY约束引用的字段称为“引用码字段”,引用码必须是主码或唯一码,包含外部码的表称为子表,包含引用码的表称为父表。
 
    A: 
    create table employees 
    (....., 
    deptno number(3) NOT NULL, 
    constraint emp_deptno_fk foreign key (deptno) 
    references dept (deptno) 
    )
 
    如果子表中的外部码与主表中的引用码具有相同的名称,可以写成:
    B: 
    create table employees 
    (....., 
    deptno number(3) NOT NULL 
    constraint emp_deptno_fk references dept 
    )
 
    注意:上面的例子(B)中not null后面没有加逗号,因为这一句的contraint是跟在那一列deptno后面的,属于列定义,所以都无需指明列。而A例中的是表定义,需要指明那一列,所以要加逗号,不能在列后面定义,还可以写成:
 
    create table employees 
    (empno char(4), 
    deptno char(2) not null constraint emp_deptno_fk references dept, 
    ename varchar2(10) 
    )

    表定义contraint的只能写在最后,再看两个例子:
 
    create table employees 
    (empno number(5), 
    ename varchar2(10), 
    deptno char(2) not null constraint emp_deptno_fk references dept, 
    constraint emp_pk primary key(empno,ename) 
    )
 
    create table employees 
    ( empno number(5), 
    ename varchar2(15), 
    phone varchar2(15), 
    email varchar2(30) unique, 
    deptno number(3) not null, 
    constraint emp_pk primary key(empno,ename), 
    constraint emp_phone_uk unique (phone) 
    )
 
2、添加foreign key约束(多字段/表级)

    alter table employees 
    add constraint emp_jobs_fk foreign key (job,deptno) 
    references jobs (jobid,deptno) 
    on delete cascade
 --注意格式
 
    更改foreign key约束定义的引用行为(delete cascade/delete set null/delete no action),默认是delete no action
 
    引用行为(当主表中一条记录被删除时,确定如何处理字表中的外部码字段):

    delete cascade : 删除子表中所有的相关记录 
    delete set null : 将所有相关记录的外部码字段值设置为NULL 
    delete no action: 不做任何操作
 
    先删除原来的外键约束,再添加约束

    ALTER TABLE employees DROP CONSTRAINT emp_deptno_fk; 
    ALTER TABLE employees ADD CONSTRAINT emp_deptno_fk FOREIGN KEY(deptno) REFERENCES dept(deptno) ON DELETE CASCADE;
 
 
五、check约束

    * 在CHECK约束的表达式中必须引用到表中的一个或多个字段,并且表达式的计算结果必须是一个布尔值 
    * 可以在表级或字段级定义 
    * 对同一个字段可以定义多个CHECK约束,同时也可以定义NOT NULL约束
  
    create table employees 
    (sal number(7,2) 
    constraint emp_sal_ck1 check (sal > 0) 
    )
 
    alter table employees 
    add constraint emp_sal_ck2 check (sal < 20000)
 
 
六、删除约束
 
    alter table dept drop unique (dname,loc) --指定约束的定义内容 
    alter table dept drop constraint dept_dname_loc_uk --指定约束名
 
    注:contraint是不能直接用drop删除的
 
 
    删除约束时,默认将同时删除约束所对应的索引,如果要保留索引,用KEEP INDEX关键字

    alter table employees drop primary key keep index
 
 
    如果要删除的约束正在被其它约束引用,通过ALTER TABLE..DROP语句中指定CASCADE关键字能够同时删除引用它的约束
 
    利用下面的语句在删除DEPT表中的PRIMARY KEY约束时,同时将删除其它表中引用这个约束的FOREIGN KEY约束:

    alter table dept drop primary key cascade
 
 
七、禁用/激活约束

    alter table employees disable/enable unique email 
    alter table employees disable/enable constraint emp_ename_pk 
    alter tabel employees modify constraint emp_pk disable/enable 
    alter tabel employees modify constraint emp_ename_phone_uk disable/enable
 
    注:禁用/激活约束会引起删除和重建索引的操作
 
    如果有FOREIGN KEY约束正在引用UNIQUE或PRIMARY KEY约束,则无法禁用这些UNIQUE或PRIMARY KEY约束,这时可以先禁用FOREIGN KEY约束,然后再禁用UNIQUE或PRIMARY KEY约束;或者可以在ALTER TABLE...DISABLE 语句中指定CASCADE关键字,这样将在禁用UNIQUE或PRIMARY KEY约束的同时禁用那些引用它们的FOREIGN KEY约束,如:
    alter table employees disable primary key cascade
 
 
八、约束数据字典
 
 
    使用以下数据字典查看Constraint信息:
    all_constraints |dba_constraints |user_constraints
 
    包括了constraints的名称、类型、状态
    constraint _typeC(CHECK约束)、P(主码约束)、R(外部码约束)、U(唯一码约束)
    status:enabled | disable
    deferrable:deferrable | not deferrable
    deferred:immediate | deferred
    validated:validated | not validated
    index_name:对应的索引名称
 

    若需要查看constraint建立在哪个字段上,使用以下数据字典:
    all_cons_columns |dba_cons_columns |user_cons_columns




-The End-

posted on 2008-12-23 20:16 decode360-3 阅读(1332) 评论(0)  编辑  收藏 所属分类: Oracle

只有注册用户登录后才能发表评论。


网站导航: