Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / May 2005

Tip: Looking for answers? Try searching our database.

PL/SQL code in constraints?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Randi W - 30 May 2005 13:43 GMT
I run Oracle 8.1.7. Is it possible to add calls to PL/SQL code from
constraints on a table?
Or is using a trigger the way to perform more control of data when coding is
needed?

Thanks for your answer,
Randi W.
sybrandb@yahoo.com - 30 May 2005 13:47 GMT
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
Mark C. Stock - 30 May 2005 15:28 GMT
> 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
Holger Baer - 30 May 2005 15:58 GMT
> 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)

Noons - 31 May 2005 10:22 GMT
> I run Oracle 8.1.7. Is it possible to add calls to PL/SQL code from
> constraints on a table?
> Or is using a trigger the way to perform more control of data when coding is
> needed?

If you are not after very complex constraints,
then you may try using a Function-Based Index to
achieve something similar.  Have a look at:
http://www.akadia.com/services/ora_function_based_index_1.html

Keep an eye on parameter "query_rewrite_enabled" in
init.ora, must be true for this to work.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.