最近在学习oracle sql及相关的对象部分及函数部分,感觉oracle10g中还有很多东西是那样的陌生,在此提一下,以供学习参考。
1:约束的使用:以前用的很少
先来个试题:
Evaluate the following SQL statements executed in the given order: ALTER TABLE cust ADD CONSTR
AINT cust_id_pk PRIMARYKEY(cust_id) DEFERRABLE INITIALLY DEFERRED; INSERT INTO cust V
ALUES (1,'RAJ'); row 1 INSERT INTO cust VALUES (1,'SAM'); row 2
COMMIT; SET CONSTRAINT cust_id_pk IMMEDIATE; INSERT INTO cust VALUES (1,'LATA'); row 3
INSERT INTO cust VALUES (2,'KING'); row 4
COMMIT; Which rows would bemade permanent in the CUST table?
A. row 4 only
B. rows 2 and 4
C. rows 3 and 4
D. rows 1 and 4
解答:
延迟约束(Deferred Constraint)
延迟约束是在事务被提交时强制执行的约束.添加约束时可以通过Deferrable子句来指定约束为延迟约束. 约束一但创建以后, 就不能修改为Deferrable延迟约束.唯一办法: 删除该约束,只能在创建指定为延迟约束即可.
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1 --当前来指定上述创建的唯一约束为延迟约束 先删除已经创建唯一约束
2 --在创建中再次指定为延迟
3 alter table order_status
4 drop constraint order_status_uq
5
6 alert table order_status
7 add constraint order_status_uq unique (status) deferrable initially deferred;
注意上面再添加约束时. 可以将其标识为Initially Immediate 或Initially Deferred.
其中Initially Immediate意思是每次向表中添加数据,修改数据或是从表中删除数据时.都要检查这个约束.(这与约束默认行为相同). 而Initially Deferred.意思是只有事务被提交时才检查这个约束. 上述我们指定的Deferred . 即在只有事务提交时才检查该约束.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17069315/viewspace-690067/
,如需转载,请注明出处,否则将追究法律责任。