Data Integirty

本文深入探讨了SQL中约束的概念,包括NOT NULL、PRIMARY KEY、FOREIGN KEY、CHECK和UNIQUE约束的创建与修改,以及如何启用、禁用、设置约束检查的延迟或立即执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Constraints

 

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

 

SQL> 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>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>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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值