加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

909422229__Oracle之Check约束实例详解

发布时间:2020-12-12 15:30:37 所属栏目:百科 来源:网络整理
导读:1. 目标 实例讲解在Oracle中如何使用CHECK约束(创建、启用、禁用和删除) 2. 什么是Check约束? CHECK约束指在表的列中增加额外的限制条件。 注: CHECK约束不能在VIEW中定义。CHECK约束只能定义的列必须包含在所指定的表中。CHECK约束不能包含子查询。 3. 创
1 2 3 4 5 6 7 CREATETABLE table_name(column1 datatype null/not,column2 datatype ...CONSTRAINTconstraint_name CHECK(column_name condition) [DISABLE]);

其中,DISABLE关键之是可选项。如果使用了DISABLE关键字,当CHECK约束被创建后,CHECK约束的限制条件不会生效。


3.2 示例1:数值范围验证

? 7 8 createtable tb_suppliersupplier_id number,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">supplier_name varchar2(50),monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">contact_name varchar2(60),/*定义CHECK约束,该约束在字段supplier_id被插入或者更新时验证,当条件不满足时触发。*/check_tb_supplier_id (supplier_id BETWEEN100 and9999) 验证:
在表中插入supplier_id满足条件和不满足条件两种情况:

? 5 --supplier_id满足check约束条件,此条记录能够成功插入 insert into tb_supplier values (200, 'dlt' 'stk' ); --supplier_id不满足check约束条件,此条记录能够插入失败,并提示相关错误如下 (1,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'david louis tian' 不满足条件的错误提示:

? 4 Error report - SQL Error: ORA-02290: check constraint (502351838.CHECK_TB_SUPPLIER_ID) violated 02290. 00000 - "check constraint (%s.%s) violated" *Cause: The being inserted do not satisfy the named check

3.3 示例2:强制插入列的字母为大写

? 8 9 tb_products
product_id number product_name varchar2(100) supplier_id number /*定义CHECK约束check_tb_products,用途是限制插入的产品名称必须为大写字母*/ check_tb_products (product_name = UPPER (product_name)) 验证:
在表中插入product_name满足条件和不满足条件两种情况:

4 --product_name满足check约束条件,此条记录能够成功插入
tb_products (2,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'LENOVO' '2' ); --product_name不满足check约束条件,此条记录能够插入失败,并提示相关错误如下 'iPhone' '1' ? 3 (502351838.CHECK_TB_PRODUCTS) violated "check constraint (%s.%s) violated" check

4. ALTER TABLE定义CHECK约束

4.1 语法

? 2 ALTER
table_nameADD constraint_name (column_name condition) [DISABLE];

4.2 示例准备 8 drop tb_supplier; --创建实例表 tb_supplier ( contact_name varchar2(60) );

4.3 创建CHECK约束

--创建check约束alter tb_supplieradd check_tb_suppliercheck(supplier_name IN('IBM''Microsoft'));

4.4 验证

--supplier_name满足check约束条件,此条记录能够成功插入'US'--supplier_name不满足check约束条件,此条记录能够插入失败,并提示相关错误如下'DELL''HO');不满足条件的错误提示: (502351838.CHECK_TB_SUPPLIER) violated

5. 启用CHECK约束

5.1 语法

2 ENABLE
constraint_name;

5.2 示例

? 9 10 11 12 13 --重建表和CHECK约束 /*定义CHECK约束,该约束尽在启用后生效*/ 9999) DISABLE ); --启用约束 tb_supplier ENABLE check_tb_supplier_id;

6. 禁用CHECK约束

6.1 语法

DISABLE constraint_name;

6.2 示例

--禁用约束 tb_supplier DISABLE 7. 约束详细信息查看
语句:

9 --查看约束的详细信息 select constraint_name, --约束名称 constraint_type,0)!important; background:none!important">--约束类型 table_name,0)!important; background:none!important">--约束所在的表 search_condition,0)!important; background:none!important">--约束表达式 status --是否启用 from user_constraints --[all_constraints|dba_constraints] where constraint_name= 'CHECK_TB_SUPPLIER_ID' ;

8. 删除CHECK约束

8.1 语法

DROP constraint_name;

8.2 示例

tb_supplier check_tb_supplier_id;---------------------------------------------------------------------------------------------------------

如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢!

转载地址:http://www.2cto.com/database/201411/351113.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

1. 目标

实例讲解在Oracle中如何使用CHECK约束(创建、启用、禁用和删除)

2. 什么是Check约束?

CHECK约束指在表的列中增加额外的限制条件。

注: CHECK约束不能在VIEW中定义。CHECK约束只能定义的列必须包含在所指定的表中。CHECK约束不能包含子查询。

3. 创建表时定义CHECK约束

3.1 语法:

?
    推荐文章
      热点阅读