不强制检查的约束:create table applicants(ap_no int not null,
gender char(1) not null,
constraint genderok
check (gender in ('M','F'))
not enforced
enable query optimization,
age int not null,
constraint ageok
check (age between 1 and 80)
not enforced
enable query optimization
);
[db2inst@www ~]$ db2 "insert into applicants values ('5','C','10')"
DB20000I The SQL command completed successfully.
强制检查的约束:
create table applicants(ap_no int not null,
gender char(1) not null,
constraint genderok
check (gender in ('M','F'))
not enforced not trusted
enable query optimization,
age int not null,
constraint ageok
check (age between 1 and 80)
not enforced
enable query optimization
);
[db2inst@www ~]$ db2 "insert into applicants values ('5','C','10')"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0545N The requested operation is not allowed because a row does not
satisfy the check constraint "DB2INST.APPLICANTS.GENDEROK". SQLSTATE=23513
enable query optimization影响查询结果,不符合约束的记录被过滤掉:
[db2inst@www ~]$ db2 "SELECT * FROM applicants WHERE GENDER='C'"
AP_NO GENDER AGE
----------- ------ -----------
0 record(s) selected.
禁用查询优化功能,查出实际结果:
[db2inst@www ~]$ db2 "alter table applicants alter check genderok disable query optimization"
DB20000I The SQL command completed successfully.
[db2inst@www ~]$ db2 "SELECT * FROM applicants WHERE GENDER='C'"
AP_NO GENDER AGE
----------- ------ -----------
5 C 10
1 record(s) selected.
结论:当应用程序能够保证插入表中的数据是对的,那么建议使用not enforced ,如果不能建议使用 enfored关键字
因为你建表时用了‘ENABLE QUERY OPTIMIZATION’,所以optimizor总是假设constraint为真。
用alter table disable query optimization结果就会不一样了。
如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!
赞0
添加新评论0 条评论