Oracle数据库约束 oracle数据库实例
创始人
2024-07-30 05:11:00
0

Oracle数据库是大家平时的工作中经常会用到的,本文将为大家带来Oracle数据库约束的讲解,希望对大家能够有所帮助。

最近一张表上有两列字段,要求这两列要么都有值,要么都为空,简单的table定义没办法实现这种要求,需要利用Oracle的constraint(约束)机制。约束主要是用来保证数据的完整性。

可以从TOAD的设置上,很容易看到约束分为4种,分别是主键(Primary Key),检查(Check),唯一性(Unique),外键(Foreign Key)。另外还有两种是NOT NULL和REF,REF就是其中的一列或者几列是另外一张表中的值。

下面是对着6中的详细介绍。

NOT NULL constraint prohibits a database value from being null.

Unique constraint prohibits multiple rows from having the same value in the same column or combination of columns but allows some values to be null.

Primary Key constraint combines a NOT NULL constraint and a unique constraint in a single declaration. It prohibits multiple rows from having the same value in the same column or combination of columns and prohibits values from being null.

Foreign Key constraint requires values in one table to match values in another table.

Check constraint requires a value in the database to comply with a specified condition.

REF column by definition references an object in another object type or in a relational table. A REF constraint lets you further describe the relationship between the REF column and the object it references.

toad create constraint step 1

对于我们的要求,符合的是Check,可以通过增加一个条件是( A is null and B is null ) or ( A is not null and B is not null) 的约束来实现。

对于约束,主要的状态有两种,一个是Status on Creation,也就是在表中增加数据或修改数据时是否使用约束,可选值是Enabled和Disabled;另外一种是Validation,它表示是否对表中现有的数据是否进行验证,可选值是Validate和NoValidate。

toad create constraint step 2

对于上面这两种状态,有4种组合,下面是对着四种的详细介绍。

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.

约束的设置还有一个延迟性设置,默认是非延迟的,也就是Initially Immediate,这种情况下任何的修改都会进行校验,另外一种是延迟的,也就是Intially Deferred,会在所有修改完成后commit的时候校验,进而引发回滚。

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...