Comments on: Putting Analytical Functions to good use – find tables with multiple foreign keys to the same referenced table Friends of Oracle and Java Wed, 24 Jun 2015 09:59:44 +0000 hourly 1 By: Xaprb Mon, 16 Oct 2006 14:29:08 +0000 For those working on MySQL, I built a similar feature into a Perl script I recently wrote for MySQL. It’s published on MySQL Forge at

By: Lucas Jellema Sun, 15 Oct 2006 15:40:15 +0000 Roland,

If all I needed is the name of the table with multiple foreign keys then of course you are right. However, I also want to know the names of the foreign keys. I believe that necessitates the use of the LAG and LEAD functions.

You are right that filtering on constraint_type is not really necessary.

Thanks for your comment.

best regards,


By: Roland Bouman Sun, 15 Oct 2006 10:43:33 +0000 Hi Lucas,

nice usage of LAG and LEAD. I think these analytical functions are very powerful and quite useful from time to time.

But couldn’t the original problem, “Table Definitions having multiple foreign keys to the same referenced table”, be solved using a simpler (and probably faster) method?

select fk.table_name
from user_constraints fk
, user_constraints pk
where fk.r_constraint_name = pk.constraint_name
and fk.constraint_type = ‘R’
group by fk.table_name
, pk.table_name
having count(*) > 1

(The fk.constraint_type = ‘R’ condition is not really necessary, because r_constraint_name will be NULL for any but foreign key constraints, but it does tend to perform better when it is included)

Roland Bouman

By: Lucas Jellema Thu, 12 Oct 2006 08:44:16 +0000 Pete,

Thanks for the compliment and of course you can! I like that.

best regards,


By: Pete_S Wed, 11 Oct 2006 18:43:09 +0000 Lucas
A nice example; can I cite it my beginers giude to Analytics paper at UK OUG in November?