Constraints signify relationships between entities.
Consequently it is not possible to add any procedural logic.
You'll need to use triggers.
However note, you should avoid replacing constraints completely by
triggers.
Enforcing integrity by means of triggers is about 8 times slower.
--
Sybrand Bakker
Senior Oracle DBA
> Constraints signify relationships between entities.
> Consequently it is not possible to add any procedural logic.
[quoted text clipped - 6 lines]
> Sybrand Bakker
> Senior Oracle DBA
The OP may have been refering to CHECK constraints, which may seem to
provide an opportunity for PL/SQL calls. However, a specific restriction on
CHECK constraints is that they cannot call user-defined functions.
++ mcs
> Constraints signify relationships between entities.
> Consequently it is not possible to add any procedural logic.
> You'll need to use triggers.
> However note, you should avoid replacing constraints completely by
> triggers.
... especially if even the Oracle documentation doesn't get it right:
<quote>
You can use both triggers and integrity constraints to define and enforce any type of integrity rule.
</quote>
This is simply not true.
Consider the following:
The business rule is to make sure that every value must be unique (ok, that's not hard to achieve,
but you just can't with a trigger):
SQL> create table demo (id int);
Table created.
SQL> create trigger trg_demo
2 before insert on demo for each row
3 declare
4 l_cnt pls_integer;
5 begin
6 select count (*) into l_cnt from demo where :new.id = id;
7 if l_cnt > 0 then raise_application_error(-20000, 'Count larger 1 encountered');
8 end if;
9 end;
10 /
Trigger created.
SQL> insert into demo values (1);
1 row created.
Now in a second session, issue the same insert statement.
Commit both.
No error raised.
But now we have 2 lines although we wanted to enforce only one?!
Baseline as far as I'm concerned:
If you can enforce integrity with a constraint, do it with a constraint.
If you can't, then make sure that the read consistency you get with oracle
doesn't get in the way.
> Enforcing integrity by means of triggers is about 8 times slower.
Now that's an interesting number. Would you care to elaborate on that?
Holger
sybrandb@yahoo.com - 30 May 2005 16:08 GMT
Conducted some tests years ago, on 7.3.4, where because of third party
sw (Vision Jade) all RI was implemented by means of triggers.
Our gut-feeling was this was slower, but we couldn't convince customer.
Consequently we set up a test, with triggers migrated to the usual
declarative constraints.
This resulted in the factor 8 slower for triggers.
--
Sybrand Bakker
Senior Oracle DBA
DA Morgan - 30 May 2005 17:43 GMT
> Conducted some tests years ago, on 7.3.4, where because of third party
> sw (Vision Jade) all RI was implemented by means of triggers.
[quoted text clipped - 6 lines]
> Sybrand Bakker
> Senior Oracle DBA
In no way critical of what you did ... if you have that suite of tests
handy I would very much like to try them in 10gR1.
Thanks.

Signature
Daniel A. Morgan
http://www.psoug.org
damorgan@x.washington.edu
(replace x with u to respond)
Sybrand Bakker - 30 May 2005 19:07 GMT
>In no way critical of what you did ... if you have that suite of tests
>handy I would very much like to try them in 10gR1.
No chance, as they were made on behalf of a customer.
--
Sybrand Bakker, Senior Oracle DBA
DA Morgan - 30 May 2005 22:03 GMT
>>In no way critical of what you did ... if you have that suite of tests
>>handy I would very much like to try them in 10gR1.
[quoted text clipped - 3 lines]
> --
> Sybrand Bakker, Senior Oracle DBA
Too bad. I am in finals week right now and don't have time to write
it. But I do have students that could run it. Anyone else have something
they could make available?
Thanks.

Signature
Daniel A. Morgan
http://www.psoug.org
damorgan@x.washington.edu
(replace x with u to respond)