Oracle数据库的约束和索引_Oracle数据库_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

Oracle的约束和索引

作者:黑客防线网安Oracle维护基地 来源:黑客防线网安Oracle维护基地 浏览次数:0

本篇关键词:Oracle数据库Oracle教程
黑客防线网安网讯:    Oracle的约束  * 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约束,但如果某个约束作用于多个字段,  必须在表级定义约束  * 在定义约束时可以通...
    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)
  )
  alter table employees
  add constraint emp_uk unique(ename,phone)
  using index tablespace indx
  定义了UNIQUE约束的字段中不能包含重复值,可以为一个或多个字段定义UNIQUE约束,因此,UNIQUE即可以在字段级也可以在表级定义,
  在UNIQUED约束的字段上可以包含空值.
  foreign key约束
  * 定义为FOREIGN KEY约束的字段中只能包含相应的其它表中的引用码字段的值或者NULL值
  * 可以为一个或者多个字段的组合定义FOREIGN KEY约束
  * 定义了FOREIGN KEY约束的外部码字段和相应的引用码字段可以存在于同一个表中,这种情况称为"自引用"
  * 对同一个字段可以同时定义FOREIGN KEY约束和NOT NULL约束
   定义了FOREIGN KEY约束的字段称为"外部码字段",被FORGIEN KEY约束引用的字段称为"引用码字段",引用码必须是主码或唯一码,包含外部码的表称为子表,
  包含引用码的表称为父表.
  A:
  create table employees
  (.....,
  deptno number(3) NOT NULL,
  constraint emp_deptno_fk foreign key (deptno)
  references dept (deptno)
  )
  如果子表中的外部码与主表中的引用码具有相同的名称,可以写成:
  B:
  create table employees
  (.....,
  deptno number(3) NOT NULL
  constraint emp_deptno_fk references dept
  )
  注意:
  上面的例子(B)中not null后面没有加逗号,因为这一句的contraint是跟在那一列deptno后面的,属于列定义,所以都无需指明列而A例中的是表定义,需要指明那一列,所以要加逗号,不能在列后面定义,还可以写成:
  create table employees
  (empno char(4),
  deptno char(2)
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-13210-1.html
网站维护教程更新时间:2012-03-23 00:52:14  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer