约束

约束

数据库中的约束类型

  • 非空约束(NOTNULL)

  • 唯一性约束(UNIQUE)

  • 主键约束(PRIMARYKEY)

  • 外键约束(FOREIGNKEY)

  • 用户自定义约束(CHECK)

[外链图片转存失败(img-HGQLcLvp-1564747347856)(C:\Users\ad\AppData\Roaming\Typora\typora-user-images\1564733936764.png)]

非空约束(NOTNULL)

create table dept80(id number, name varchar2(20) not null, salary number(8,2) constraint dept_nn not null);
alter table dept80 modify location_id not null;

唯一性约束(UNIQUE)

create table dept80(id number, name varchar2(20) not null, salary number(8,2), constraint dept_unique UNIQUE(id));
alter table dept80 modify(name unique)

删除

alter table dept80 drop constraint DEPT_UNIQUE;

主键约束(PRIMARYKEY)

create table dept80(id number, name varchar2(20) not null, salary number(8,2), constraint dept_unique PRIMARY KEY(id));
create table dept80(id number, name varchar2(20) not null, salary number(8,2), constraint dept_unique PRIMARY KEY(id, name));
alter table dept80 modify(id PRIMARY KEY)

外键约束(FOREIGNKEY)

create table dept40(id number, d_id number, constraint dept_40_fk foreign key (d_id) references departments(department_id));
alter table dept40 add constraint dept_40_fk foreign key (d_id) references departments(department_id));

用户自定义约束(CHECK)

create table dept30 (id number, salary number(8, 2) constraint dep30_ck check(salary > 0));
alter table dept50 add constraint dept50_ck check(salary) > 1000

查看约束

select constraint_name, constraint_type, search_condition from user_constraints where table_name = 'DEPT';

禁用约束

alter table employees
disable constraint emp_emp_id_pk cascade

启用约束

alter table employees
enable constraint emp_emp_id_pk;

e constraint emp_emp_id_pk cascade




## 启用约束

alter table employees
enable constraint emp_emp_id_pk;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值