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
...
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_tb_supplier_id
(supplier_id
BETWEEN
100
and
9999)
验证:
在表中插入supplier_id满足条件和不满足条件两种情况:
?
5
insert
into
tb_supplier
values
(200,
'dlt'
'stk'
);
(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'
);
'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
(
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约束
/*定义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
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
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!