标签:check约束 oracle check constraint oracle约束
Oracle | PL/SQL Check约束用法详解
CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not null, ... CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE] );
其中,DISABLE关键之是可选项。如果使用了DISABLE关键字,当CHECK约束被创建后,CHECK约束的限制条件不会生效。
create table tb_supplier ( supplier_id number, supplier_name varchar2(50), contact_name varchar2(60), /*定义CHECK约束,该约束在字段supplier_id被插入或者更新时验证,当条件不满足时触发。*/ CONSTRAINT check_tb_supplier_id CHECK (supplier_id BETWEEN 100 and 9999) );
验证:
在表中插入supplier_id满足条件和不满足条件两种情况:
--supplier_id满足check约束条件,此条记录能够成功插入 insert into tb_supplier values(200, 'dlt','stk'); --supplier_id不满足check约束条件,此条记录能够插入失败,并提示相关错误如下 insert into tb_supplier values(1, 'david louis tian','stk');
不满足条件的错误提示:
Error report - SQL Error: ORA-02290: check constraint (502351838.CHECK_TB_SUPPLIER_ID) violated 02290. 00000 - "check constraint (%s.%s) violated" *Cause: The values being inserted do not satisfy the named check
create table tb_products ( product_id number not null, product_name varchar2(100) not null, supplier_id number not null, /*定义CHECK约束check_tb_products,用途是限制插入的产品名称必须为大写字母*/ CONSTRAINT check_tb_products CHECK (product_name = UPPER(product_name)) );
验证:
在表中插入product_name满足条件和不满足条件两种情况:
--product_name满足check约束条件,此条记录能够成功插入 insert into tb_products values(2, 'LENOVO','2'); --product_name不满足check约束条件,此条记录能够插入失败,并提示相关错误如下 insert into tb_products values(1, 'iPhone','1');
不满足条件的错误提示:
SQL Error: ORA-02290: check constraint (502351838.CHECK_TB_PRODUCTS) violated 02290. 00000 - "check constraint (%s.%s) violated" *Cause: The values being inserted do not satisfy the named check
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE];
其中,DISABLE关键之是可选项。如果使用了DISABLE关键字,当CHECK约束被创建后,CHECK约束的限制条件不会生效。
drop table tb_supplier; --创建实例表 create table tb_supplier ( supplier_id number, supplier_name varchar2(50), contact_name varchar2(60) );
--创建check约束 alter table tb_supplier add constraint check_tb_supplier check (supplier_name IN ('IBM','LENOVO','Microsoft'));
--supplier_name满足check约束条件,此条记录能够成功插入 insert into tb_supplier values(1, 'IBM','US'); --supplier_name不满足check约束条件,此条记录能够插入失败,并提示相关错误如下 insert into tb_supplier values(1, 'DELL','HO');不满足条件的错误提示:
SQL Error: ORA-02290: check constraint (502351838.CHECK_TB_SUPPLIER) violated 02290. 00000 - "check constraint (%s.%s) violated" *Cause: The values being inserted do not satisfy the named check
ALTER TABLE table_name ENABLE CONSTRAINT constraint_name;
drop table tb_supplier; --重建表和CHECK约束 create table tb_supplier ( supplier_id number, supplier_name varchar2(50), contact_name varchar2(60), /*定义CHECK约束,该约束尽在启用后生效*/ CONSTRAINT check_tb_supplier_id CHECK (supplier_id BETWEEN 100 and 9999) DISABLE ); --启用约束 ALTER TABLE tb_supplier ENABLE CONSTRAINT check_tb_supplier_id;
6.1 语法
ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
6.2 示例
--禁用约束 ALTER TABLE tb_supplier DISABLE CONSTRAINT check_tb_supplier_id;
7. 约束详细信息查看
语句:
--查看约束的详细信息 select constraint_name,--约束名称 constraint_type,--约束类型 table_name,--约束所在的表 search_condition,--约束表达式 status--是否启用 from user_constraints--[all_constraints|dba_constraints] where constraint_name='CHECK_TB_SUPPLIER_ID';
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
ALTER TABLE tb_supplier DROP CONSTRAINT check_tb_supplier_id;
如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢!
联系方式:david.louis.tian@outlook.com
版权@:转载请标明出处, 否则追究法律责任!标签:check约束 oracle check constraint oracle约束
原文地址:http://blog.csdn.net/jssg_tzw/article/details/40985081