关于oracle中的约束

oracle中约束用constraint表示:

主要分为五个约束:

1.primary key 主键约束

2.not null 非空约束

3.unique 唯一约束

4.check 自定义约束

5.foreign key 外键约束

下面是创建表中各个约束的sql语句例子:

create table users(

  id varchar2(10),

  name varchar2(10) not null,

  address varchar2(30) unique,

  age number(3) check(age>0),

  temp_id varchar2(10),

  constraint users_id_pk primary key(id),

  constraint users_id_fk foreign key(temp_id) references temp(id)

)

可以手动添加约束:

alter table users add constraint users_age_check check(age<200);

对于有外键约束的表来说,当删除依赖表中的数据时,主表总有两种方式,一种是级联删除中跟外键相关的数据,第二种是将主表中外键的值设置为null;

约束的sql语句具体为

constraint users_id_fk foreign key(temp_id) references temp(id) on delete cascade;

constraint users_id_fk foreign key(temp_id) references temp(id) on delete set null;

查询表中的约束信息可以通过表user_constraints

select * from user_constraints where table_name='USERS';

原文地址:https://www.cnblogs.com/qingtianyu/p/3542473.html