设为首页 收藏本站
查看: 1424|回复: 0

[经验分享] CHIL-ORACLE

[复制链接]

尚未签到

发表于 2017-12-11 14:44:51 | 显示全部楼层 |阅读模式
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 age<120)  );
  

create table test33(  id
number primary key,  age
number check(age between 12 and 30)  );
  

create table test34(  id
number primary key ,  age
number  );
  

alter table test34 add constraint ch_test34 check(age>0 and age<120);alter table test34 add constraint ch_test34 check(age between 12 and 30);  

  

4.长度大于某个值create table test35(  id
number primary key,  password
varchar2(10) check(length(password)=6)  );
create table test36(  id
number primary key ,  password
varchar2(20)  );
alter table test36 add constraint check_test36 check(length(password)=6);  

  

5.数大于某个值create table test37(  id
number(10)primary key ,  no
number(10) check(no>1)  );
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,'12@126.com');
  

  
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>  

  
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(字段名)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-422998-1-1.html 上篇帖子: Oracle数据库自动启动Shell脚本 下篇帖子: oracle sql 基础(二):数据操纵语言(select 语句)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表