---
title: "Data Integirty"
author: "edwards0307"
date: "2011-12-01T17:47:31Z"
tags: []
categories: ["数据库"]
slug: "data-integirty"
draft: false
lastmod: "2011-12-01T17:47:31Z"
id: 1288064
url: "https://killdbs.iteye.com/blog/1288064"
---

1.Constraints

 

type: Not null ,PK,FK,Check,Unique

 

SQL&gt; create table mm(  
  2  id number constraint con\_pk primary key,  
  3  name char(10) not null,  
  4  uniqueid number unique,  
  5  sal number check(sal&gt;100));

 

  alter table mm  modify  name not null;

  alter table mm  add primary key(id);

  alter table mm  add unique(name);

  alter table mm  add check(sal&gt;1000);

  alter table mm  add foreign key(id) references tt(id);

  alter table mm  add constraint  pk\_cons primary key(id);

 

  alter table mm  drop primary key cascade;

  alter table mm  drop unique(name) cascade

  alter table mm  drop constraint pk\_cons;

 

Constraint States:

 

■ ENABLE VALIDATE is the same as ENABLE. The constraint is checked and is  
guaranteed to hold for all rows.  
■ ENABLE NOVALIDATE means that the constraint is checked, but it does not have to  
be true for all rows. This allows existing rows to violate the constraint, while  
ensuring that all new or modified rows are valid.  
In an ALTER TABLE statement, ENABLE NOVALIDATE resumes constraint checking  
on disabled constraints without first validating all data in the table.  
■ DISABLE NOVALIDATE is the same as DISABLE. The constraint is not checked and  
is not necessarily true.  
■ DISABLE VALIDATE disables the constraint, drops the index on the constraint,  
and disallows any modification of the constrained columns. When any constraint is moved from the NOVALIDATE state to the VALIDATE state, all data must be checked. (This can be very slow.) However, moving from VALIDATE to NOVALIDATE simply forgets that the data was ever checked.Moving a single constraint from the ENABLE NOVALIDATE state to the ENABLE VALIDATE state does not block reads, writes, or other DDL statements. It can be done in parallel.

 

alter table mm enable validate constraint pk\_con;

alter table mm disable novalidate primary key;

 

 

Constraint Check:

 

deferred: after commit,check;

nodeferred: immdeite checck;

 

set  constraints pk\_emp deferred/immediate

alter session set constraints=deferred/immediate