ORACLE 您所在的位置:网站首页 oracle中有效的列名 ORACLE

ORACLE

2023-11-27 20:12| 来源: 网络整理| 查看: 265

ORACLE-检查约束(check)

1.检查约束 ( check )   某列取值范围限制、格式限制等

2.检查只能是男或者女 create table test29( id number primary key, sex varchar2(2) check(sex in (‘男,女’)) );

create table test30( id number primary key, sex varchar2(2) check(sex ='男' or sex='女') ); create table test31( id number primary key, sex varchar2(2) ); alter table test31 add constraint chkk check (sex ='男' or sex='女'); alter table test31 add constraint chkk check (sex in('男','女'));

3.在一个范围中间 create table test32( id number primary key, age number check(age>0 and age0 and age1) );

create table test38( id number(10) primary key, no number(10) ); alter table test38 add constraint ch_test38 check(no>1);

6.只能是8位字符,前两位是 0 ,3~4位为数字,第 5 位为""下划线,6~8位为字母 create table test39( id number(10) primary key, password varchar2(20) check((password like '00[0-9][0-9]/[a-z,A-Z][a-z,A-Z][a-z,A-Z]%’ escape ‘/’)and(length(password)=8) ) ); insert into test39 values (1,‘0011_aaa’);

create table test40( id number(10) primary key , password varchar2(10)check((password like '00[0-9][0-9][_][a-z,A-Z][a-z,A-Z][a-z,A-Z]%')and(length(password)=8) )); ); alter table test40 modify password varchar2(10)check((password like '00[0-9][0-9][_][a-z,A-Z][a-z,A-Z][a-z,A-Z]%')and(length(password)>1) insert into test40 values(1,'0012_abc');

7.电子邮箱要含有@符号check(字段 like ‘%@%’) create table test41( id number(10) primary key, email varchar2(10) check (email like ‘%@%’) ); insert into test41 values(1,‘[email protected]’);

8.SQL中用check约束一列的首字母为’s’check(字段 like ‘s%’) create table test42( id number(10) primary key , name varchar2(10) check(name like ‘s%’) ); insert into test42 values(1,‘sname’);

9.检查约束前3位和后8位均为数字字符:check(字段 like ‘[0-9][0-9][0-9]%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]’) create table test43( id number(10) primary key, no varchar2(10)check(no like ‘[0-9][0-9][0-9]%[0-9][0-9][0-9][0-9][0-9]’) ); insert into test43 values(1,‘12345678’);

10.如何建立检查身份证的约束,身份证是18位,最后一位还有可能是X create table test44( id number(10) primary key, no values(18) check( length(no)=18 and right(no,17)like ‘[0-9]’ or right (no,17) like ‘x’ ) ); insert into test44 values (1,‘12345678912345678x’);

select 身份证号 from 表名where len(身份证号) = 18 and (right(身份证号,17) like '[0-9]'or right(身份证号,17) like ‘x’)

11.如何设置区号由0-9之间的数字组成CONSTRAINT quhao CHECK (quhao LIKE ‘[0-9][0-9][0-9]’ or quhao LIKE ‘[0-9][0-9][0-9][0-9]’ or quhao LIKE ‘[0-9][0-9][0-9][0-9][0-9]’));

解释:quhao LIKE '[0-9]…[0-9]'的号码由表示n位从0到9中的数组成。 quhao LIKE ‘[0-9][0-9][0-9]’ 表示3位的区号,如北京010; quhao LIKE '[0-9][0-9][0-9][0-9]'表示4位的区号,如三门峡0398; quhao LIKE '[0-9][0-9][0-9][0-9][0-9]'表示5位的区号,如香港00852

12.最后回复时间 TLastClickT 发贴时间 TTime最后回复时间 必须晚于 发贴时间 并且小于等于当前时间 使用GetDate()函数获取当前时间 设计表在TLastClickT上右击选择约束,新建, 填入([TLastClickT] > [TTime] and [TLastClickT] < GetDate()) 或者TiastReply(回帖时间)大于Ttime(发帖时间) 在创表的同时创建表的时候应该还没有回帖吧,为什么要用默认值? 可以添加一个约束 alter table topic alter column add check(TlastReply is null or TlastReply > Ttime)

13.定义前八位为数字或者 -一共是15位,为CHAR型 alter table 表名add constraint chk check(字段 like’[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%’), constraint chklen check(len(字段)=15)

14.如何限制varchar字段不允许出现单引号的检查约束 !!! 设表为TALBENAME,不能有引号的字段为FIELDNAME 则: ALTER TABLE tablename ADD CONSTRAINT CK_fieldname CHECK (not fieldname like ‘%’’%’)

15.在表中的某列中通过检查约束,让其有某些固定的值 check(sid like ‘bd[0-9][0-9][0-9][0-9][0-9][0-9]’) add const ck_num check(num like ‘[1][2] [4][_] [0-9][0-9] [0-9][a-z]’)

16.如何限制varchar字段不允许出现字符串的检查约束 !!! 设表名为TABLENAME,VARCHAR类型的字段为VAR_FIELD.则有: ALTER TABLE [dbo].[TABLENAME] ADD CONSTRAINT [CK_TABLENAME] CHECK (isnumeric([VAR_FIELD]) = 1) 这样,在VAR_FIELD只要出现字段一旦出现非数字内容就会报告错误。

17.电话号码的格式必须为xxxx-xxxxxxxx或手机号11位 alter 表名 add constraint ck_字段名 check (字段 like ‘[0-9][0-9][0-9][0-9]_[0-9]…’ or length(字段)=11)

18.身份证号是18位且唯一的 alter 表名 add constraint ck_字段名 check (len(字段名)=18 ), constraint uk_字段名 unique(字段名)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有