频道栏目
首页 > 资讯 > Oracle > 正文

Oracle数据库如何创建约束?创建约束的SQL语句和正则约束的SQL语句讲解

17-10-21        来源:[db:作者]  
收藏   我要投稿

Oracle数据库中的约束有五种约束,分别是:

实体完整性:主键约束、唯一性约束(可以为空)

域完整性:check约束

引用完整性:Foreign KEY约束

默认约束(默认值)

SQL语句创建约束需要一个关键字:Costraint

创建、删除约束的SQL语句:

表名是test

1、添加主键约束(非空,不重复)(给test表的id这一列添加主键约束)

alter table test add constraint test_pk primary key(id)

2、添加唯一性约束(不重复,可以为空)(给test表name这一列添加唯一性约束)

alter table test add constraint test_unique_name unique(name)

3、添加外键约束(保证外键的值取关联表里关联字段的值)(给表test的列cus_no设为外键,关联表cus_info的列cus_no)

alter table test add constraint test_cus_no_fk_1 foreign key(cus_no) references cus_info (cus_no)

4、完整性约束(代表一个字段或者多个字段的取值是有限定范围的)

alter table test add constraint test_name_check check(name in ('a','b','c') )

5、 删除约束

alter table test drop constraint test_unique_name

6、 修改约束名称

alter table test rename constraint test_name_check to test_name_check2

7、禁用约束

alter table test disable constraint test_name_check2

8、启用约束

alter table test enable constraint test_name_check2

关于check约束的一些练习:

1、性别只能是男和女

alter table cus_linkman add constraint lkm_sex_check check(lkm_sex in ('男','女'));

2、手机号必须是11位数字,开头是1

alter table cus_linkman

add constraint lkm_mobile_check check(REGEXP_LIKE(lkm_mobile,'[1][1234567890]{10}'));

3、电话号码必须是(010)-1234567,区号3,4位,座机号7,8位

alter table cus_linkman

add constraint lkm_tel_check

check(REGEXP_LIKE(lkm_tel,'\([0][1234567890]{2,3}\)-[1234567890]{7,8}'));

4、验证必须是电话号码:例:(010)-6666666

alter table test2

add constraint test2_check

check(REGEXP_LIKE(test,'\([0][0123456789]{2,3}\)-[0123456789]{7,8}'))

相关TAG标签
上一篇:android存储方式之Sqlite存储方式讲解
下一篇:编写hibernate时与inverse属性的关系
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站