ORACLE- check 检查约束

1.检查约束(check)

某列取值范围限制、格式限制等

格式:check(字段名 like '约束条件')  或者 check(regexp_like(字段名,'约束条件'))

2.实战演练

1.检查性别只能是男或女

--第一种
create table test(         
    s_id number primary key,         
    sex varchar2(2) check(sex ='男' or sex='女')  
);    

--第二种
create table test1(         
    t_id number primary key,         
    sex varchar2(2)  
);  

alter table test1 add constraint chkk check (sex ='男' or sex='女');  
alter table test1 add constraint chkk check (sex in('男','女')); 

2.在一个范围中间

--第一种
create table test2(       
      t_id number primary key,       
      age number check(age between 12 and 30)
);

--第二种
create table test3(       
      t_id number primary key ,       
      age number
);

alter table test3 add constraint ch_test3 check(age>12 and age<30);
alter table test3 add constraint ch_test3 check(age between 12 and 30);  

3.长度大于等于某个值

--第一种
create table test4(       
    id number primary key ,       
    password varchar2(20) check(length(password)>=6)
);
--第二种
create table test5(       
    id number primary key ,       
    password varchar2(20)
);
alter table test5 add constraint check_test5 check(length(password)>=6);

以下内容仅写第一种方法(表中check约束)

4.数大于某个值

create table test6(       
      id number(10) primary key,      
      no number(10) check(no>1)
);

5.只能是8位字符,前两位是0,3~4位为数字,第 5 位为""下划线,6~8位为字母

create table test7(       
    id number(10) primary key ,       
    password varchar2(10)check((regexp_like(password,'^00[0-9]{2}[_][a-z,A-Z]{3}$') )and(length(password)=8))
);

insert into test7 values(1,'0012_jaa');

6.电子邮箱要含有@符号check

--第一种 like方法
create table test8(
id number(10) primary key,
email varchar2(10) check (email like '%@%')
);
insert into test8 values(1,'12@126.com');

--第二种 regexp_like方法
create table test9(
id number(10) primary key,
email varchar2(10) check ( regexp_like(email,'@'))
);
insert into test9 values(1,'12@126.com');

7.用check约束一列的首字母为’s’

--第一种方法 like
create table test10(
id number(10) primary key ,
name varchar2(10) check(name like 's%')
);
insert into test10 values(1,'same');

第二种方法 regexp_like
create table test11(
id number(10) primary key ,
name varchar2(10) check(regexp_like(name,'s','i') )
);
insert into test11 values(1,'same');

以下内容仅采用like方法,regexp_like方法由读者自行修改调试

8.检查约束前3位和后5位均为数字字符: (代码存在问题)--认为思路正确(求help)

create table test12(
id number(10) primary key,
wno varchar2(10)check(wno like '[0-9]{3}%[0-9]{5}')
);
insert into test12 values(4,'12324578');

3.思考题部分:

1.如何建立检查身份证的约束,身份证是18位,最后一位还有可能是X,进行建表、插入和查询
2.如何限制varchar字段不允许出现单引号的检查约束 
3.如何限制varchar字段不允许出现字符串的检查约束 

参考:

1.ORACLE-检查约束(check)_锦城花楼的博客-CSDN博客_oracle check约束

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的小羽儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值