ORACLE的索引和约束详解
博客分类:
数据库
Oracle数据结构
Oracle的约束--
primary key, not null, unique,foreign key, check 约束
* 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,
必须在表级定义约束
* 在定义约束时可以通过CONSTRAINT关键字为约束命名,如果没有指定,ORACLE 将自动为约束建立默认的名称
PRIMARY KEY
定义primary key约束(单个字段)
create table employees (empno number(5) primary key,...)
指定约束名
create table employees (empno number(5) constraint emp_pk primary key,...)
定义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约束时可以为它的索引指定存储位置和存储参数
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)