909422229__Oracle之Check约束实例详解

前端之家收集整理的这篇文章主要介绍了909422229__Oracle之Check约束实例详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 目标

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

2. 什么是Check约束?

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

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

3. 创建表时定义CHECK约束

3.1 语法:

?
1
2
3
4
5
6
7
CREATE TABLE table_name
(
column1 datatype null / not ,
column2 datatype sql color1" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
...
CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE]
);

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


3.2 示例1:数值范围验证

?
7
8
create table tb_supplier
supplier_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 BETWEEN 100 and 9999)
验证:
在表中插入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_name
ADD constraint_name (column_name condition) [DISABLE];

4.2 示例准备

8
drop tb_supplier;
--创建实例表
tb_supplier
(
sql spaces" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
contact_name varchar2(60)
);

4.3 创建CHECK约束

--创建check约束
alter tb_supplier
add check_tb_supplier
check (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约束
sql keyword" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
/*定义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

原文链接:https://www.f2er.com/oracle/211063.html

猜你在找的Oracle相关文章