`

Oracle的约束

阅读更多
Oracle数据库可以以两种声明方式创建6种类型的约束。
  1>. NOT NULL constraint 保证数据值不为空。
  2>. UNIQUE constraint 保证该列或组合的多列值唯一,可以有部分值为空。
  3>. PRIMARY KEY constraint 是NOT NULL 和 UNIQUE的组合使用,也就是将这两种约束使用到同一列

上。
  4>. FOREIGN KEY constraint 引用的值在另外一个表中也出现过。
  5>. CHECK constraint 保证数据值在指定的条件中。
  6>. REF constraint 引用其它的表或对象类型。

两种方式是:
  1>. 在列内部定义, inline (NOT NULL constraint必须在inline定义,其它可以在外部定义。)
  2>. 在列外部定义, out-of-line

-----------------------------------------------------------
外部约束Oracle语法:
out_of_line_constraint::=
[ CONSTRAINT constraint_name ]
{ UNIQUE (column [, column ]...)
| PRIMARY KEY (column [, column ]...)
| FOREIGN KEY (column [, column ]...)
     references_clause
| CHECK (condition)
}
[ constraint_state ]

references_clause::=
REFERENCES [ schema. ] { object_table | view }
[ (column [, column ]...) ]
[ON DELETE { CASCADE | SET NULL } ]
[ constraint_state ]

constraint_state::=
[ [ [ NOT ] DEFERRABLE ]
  [ INITIALLY { IMMEDIATE | DEFERRED } ]
| [ INITIALLY { IMMEDIATE | DEFERRED } ]
  [ [ NOT ] DEFERRABLE ]
]
[ RELY | NORELY ]
[ using_index_clause ]
[ ENABLE | DISABLE ]
[ VALIDATE | NOVALIDATE ]
[ exceptions_clause ]

-------------------------------------------------------------------
UNIQUE constraint 限制:
  1>. 以下数据类型不可以使用UNIQUE constraint:
LOB(CLOB, BLOB), LONG, LONG RAW, VARRAY, NESTED TABLE, OBJECT, REF, TIMESTAMP WITH TIME

ZONE, or user-defined type. 可以使用到TIMESTAMP WITH LOCAL TIME ZONE类型中。
  2>. 复合UNIQUE键不可以超过32列。
  3>. 不可以在同一列或多列上同时使用PRIMARY KEY, UNIQUE constraint.
  4>. You cannot specify a unique key when creating a subview in an inheritance hierarchy.

The unique key can be specified only for the top-level (root) view.

PRIMARY KEY constraint 限制:
  1>. 一个表或视图只可以有一个主键。
  2>. LOB(CLOB, BLOB), LONG, LONG RAW, VARRAY, NESTED TABLE, BFILE, REF, TIMESTAMP WITH TIME

ZONE, or user-defined type不可以是主键. TIMESTAMP WITH LOCAL TIME ZONE可以是主键。
  3>. 主键的长度不可以超过一个数据块。
  4>. 复合主键不能超过32列。
  5>. 不可以在一列或多列上同时有主键和唯一约束。
  6>. 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.

FOREIGN KEY constraint 限制:
  一个表或视图包含的外键被称为子对象,同时该表或视图包含的引用键称为父对象。外键的对象可以是

本身,这时子表(视图)和父表(视图)都其本身。如果没有指定父表(视图)中的外键,侧自动采用该

表的主键。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics