Oracle 约束

一、什么是约束

约束是表级的强制规定

(A 有以下五种约束:

  1.NOT NULL       非空   

  • 在insert 操作时必须填写且非空(not null) 约束只能定义在列上

  2.UNIQUE             唯一

  • 数据不能够重复
  • 数据能为空(null)
  • 多个空值是被允许的

  3.PRIMARY KEY       主键

  • 默认唯一且不为空

  4.FOREIGN KEY       外键

  • FOREIGN KEY: 在表级指定子表中的列
  • REFERENCES: 标示在父表中的列
  • ON DELETE CASCADE(级联删除): 当父表中的列被删除时,子表中相对应的列也被删除
  • ON DELETE SET NULL(级联置空): 子表中相应的列置空

  5.CHECK                   检查

(B 表级约束和列级约束

作用范围:

               ①列级约束只能作用在一个列上

               ②表级约束可以作用在多个列上(当然表级约束也  

        可以作用在一个列上)

定义方式:列约束必须跟在列的定义后面,表约束不与列一起,而是单独定义。

非空(not null) 约束只能定义在列上

**********************在建表的时候添加约束
举例子
关于逗号,最后一个行不用逗号
--
---------------------列级约束--------------------------- 方式一:

create table test ( id varchar2(3) constraint test_pk PRIMARY KEY , name varchar(10) constraint test_un unique, birthday date constraint test_un1 unique, age number(3) constraint test_ck check(age <115 and age >0), grade varchar2(10)constraint test_null not null, class varchar2(10)constraint test_fk foreign key (class) )
方式二:
create table test 
(
   id        varchar2(3)     PRIMARY KEY ,
   name      varchar(10)     unique,   
   birthday  date            unique,   
   age       number(3)       check(age <115 and age >0),
   grade     varchar2(10)    not null
  
)

关于constraint 给约束命名, 如果不指定约束名 ,Oracle server 自动按照 SYS_Cn 的格式指定约束名,不命名也可以
----------------------表级约束---------------------------
create table CLASS_01  -----外键表
(

   class     varchar2(10) constraint CLASS_01_NOTNULL  unique

)

方式一: create table test ( id varchar2(3) , name varchar(10) , birthday date , age number(3) , grade varchar2(10) constraint test_null not null, class varchar2(10), constraint test_pk PRIMARY KEY(id), constraint test_un unique(name,birthday), ----可多个字段一起约束 constraint test_ck check(age <115 and age >0), constraint test_fk foreign key (class) references CLASS_01(class)-----外键要和外键表的字段数据类型字段一样 ---- 非空(not null) 约束只能定义在列上-------
)
关于外键:
FOREIGN
KEY:    在表级指定子表中的列 REFERENCES:     标示在父表中的列 ON DELETE CASCADE (级联删除): 当父表中的列被删除时,子表中相对应的列也被删除 ON DELETE SET NULL(级联置空): 子表中相应的列置空 constraint test_fk foreign key (class) references CLASS_01(class) ON DELETE CASCADE constraint test_fk foreign key (class) references CLASS_01(class) ON DELETE SET NULL
约束名 外键字段 外键表 参考字段

 

 
方式二:
create table test 
(
   id        varchar2(3) ,                    
   name      varchar(10) ,
   birthday  date        ,         
   age       number(3)   ,
   grade     varchar2(10) constraint test_null   not null,
   class     varchar2(10),  
   
     PRIMARY KEY(id),
     unique(name,birthday),  ----可多个字段一起约束 
     check(age <115 and age >0),
     foreign key (class) references CLASS_01(class)-----外键要和外键表的字段数据类型字段一样
   ---- 非空(not null) 约束只能定义在列上-------
  
)

关于constraint 给约束命名, 如果不指定约束名 ,Oracle server 自动按照 SYS_Cn 的格式指定约束名,不命名也可以

 

**********************在已有表中的时候添加约束

 

未添加约束表
create
table test( id varchar2(3), name varchar(10) , birthday date , age number(3) , grade varchar2(10), class varchar2(10) )
为上表添加以下约束
create table test 
(
   id        varchar2(3) ,                    
   name      varchar(10) ,
   birthday  date        ,         
   age       number(3)   ,
   grade     varchar2(10) constraint test_null   not null,
   class     varchar2(10),  
   
   constraint test_pk     PRIMARY KEY(id),
   constraint test_un     unique(name,birthday),  
   constraint test_ck     check(age <115 and age >0),
   constraint test_fk     foreign key (class) references CLASS_01(class)

  
)

-------------------not null----------------------------------- alter table test modify( grade varchar2(10) not null); ------------------------其他约束---------------------------------------------- alter table test add  constraint test_pk PRIMARY KEY(id)
alter table test add constraint test_un unique(name,birthday)
alter table test add  constraint test_ck check(age <115 and age >0)
alter table test add constraint test_fk foreign key (class) references CLASS_01(class)

 
注意:
使用 ALTER TABLE 语句:
添加或删除约束,但是不能修改约束
有效化或无效化约束
添加 NOT NULL 约束要使用 MODIFY 语句
语法: ALTER TABLE table ADD [CONSTRAINT constraint] type (column);
 
 


 

 

**********************删除约束&&约束无效化&&激活约束

 

---------------------------删除约束
alter
table test drop CONSTRAINT test_pk ;

alter table test
drop  CONSTRAINT SYS_C0011228 ; 
alter table test
drop CONSTRAINT test_un ;

alter table test
drop CONSTRAINT test_ck ;

alter table test
drop CONSTRAINT test_fk ;




 


---------------------------使约束无效化
在ALTER TABLE 语句中使用 DISABLE 子句将约束无效化。  
但是不可以对主键用
alter table test DISABLE CONSTRAINT test_un ;
alter table test
DISABLE  CONSTRAINT SYS_C0011228 ; 
alter table test 
DISABLE
CONSTRAINT test_ck ;

alter table test
DISABLE
CONSTRAINT
test_fk ;

-----------------------------激活约束

ENABLE 子句可将当前无效的约束激活

alter
table test ENABLE CONSTRAINT test_un ; alter table test ENABLE CONSTRAINT test_ck ; alter table test ENABLE CONSTRAINT test_fk ; 当定义和激活UNIQUE 或 定义 PRIMARY KEY 约束时系统会自动创建UNIQUE 或 PRIMARY KEY索引

**********************查询定义约束的列

查询数据字典视图 USER_CONS_COLUMNS
SELECT    constraint_name as 约束名, column_name as 约束列
FROM      user_cons_columns
WHERE     lower(table_name) = 'test';

lower() 函数将大写变小写 , 我定义表是小写 test 属于关键字 ,oracle 中的关键字不区分大小写
但是 'test' 属于字符,区分大小写,所以用lower()转为小写 table_name = 'TEST'; 也可

grade 是系统定义的约束名,其他是自定义的约束名)



 

 

 

 

posted @ 2020-10-06 09:33  摸索ing  阅读(158)  评论(0)    收藏  举报