13.1检查(check)约束
检查约束对输入列或者整个表中的值设置检查条件,以限制输入值,保证数据库数据的完整性。
比如只能输入“女”或“男”:
创建表时检查(check)约束
create table student
(
sid number(8,0),
name varchar2(20),
sex char(2) check(sex='男' or sex='女'),
birthday date,
address varchar2(50)
);
-----------------------------------------------------------
create table student
(
sid number(8,0),
name varchar2(20),
sex char(2),
birthday date,
address varchar2(50),
constraint ck_sex check(sex='男' or sex='女')
);
修改表时添加检查(check)约束
alter table student add constraint ck_sex check(sex='男' or sex='女');
直接将列级检查约束语句写在创建表中:
直接将表级检查约束语句写在创建表中:
禁止使用约束和彻底删除约束的写法: