Oracle认证辅导:Oracle的约束和索引Oracle认证考试
文章作者 100test 发表时间 2009:11:20 16:54:24
来源 100Test.Com百考试题网
"tbbnc">
	
  Oracle的约束
  * 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,
  必须在表级定义约束
  * 在定义约束时可以通过CONSTRAINT关键字为约束命名,如果没有指定,ORACLE将自动为约束建立默认的名称
  定义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)
  not null约束(只能在字段级定义NOT NULL约束,在同一个表中可以定义多个NOT NULL约束)
  alter table employees modify deptno not null/null
  unique约束
  create table employees
  ( empno number(5),
  ename varchar2(15),
  phone varchar2(15),
  email varchar2(30) unique,
  deptno number(3) not null,
  constraint emp_ename_phone_uk unique (ename,phone)
  )