Sometimes you want a list of all the associations in your database. You're supposed to know this from your Hibernate mappings, or from your ActiveRecord declarations, but sometimes you just need to dig this stuff out of a legacy database with its cryptic prehistoric naming conventions layered on top of one another like geological rock strata.
Anyway, I digress. As you might imagine, this is a simple select in MySQL, and a four-table join in Oracle.
Suppose you've had the original idea of building a brilliant e-commerce site. You have Clients, Orders, Products, and LineItems, with the usual associations.
This is the kind of output you want:
+-----------------------+-------------+ | foreign key | references | +-----------------------+-------------+ | orders.client_id | clients.id | | line_items.order_id | orders.id | | line_items.product_id | products.id | +-----------------------+-------------+
With MySQL:
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
referenced_table_name is not null;
With Oracle (9-ish, probably 10 too, it's ages since I've actually used this)
select
col.table_name || '.' || col.column_name as foreign_key,
rel.table_name || '.' || rel.column_name as references
from
user_tab_columns col
join user_cons_columns con
on col.table_name = con.table_name
and col.column_name = con.column_name
join user_constraints cc
on con.constraint_name = cc.constraint_name
join user_cons_columns rel
on cc.r_constraint_name = rel.constraint_name
and con.position = rel.position
where
cc.constraint_type = 'R'
I cry when I behold the beauty of Oracle's meta data model. But we'll hold off the religious wars for another day. I know it's not your fault you're using Oracle.
HTH.


21 comments:
Thanks, this is exactly what I needed.
Very very useful.
Thanks from Indonesia
Thanks, this is exactly what I needed.
Very helpful, thank you.
Extremely Helpful - just saved me a full day of analysis to find the Constraint details.
Thanks,
clear and concise...
thank you very much.
Thanks for this dude,
Sean
Maybe the oracle is slightly sophisticated than mysql :)
Thank you.Its very useful to me
I think you deserve a couple of wings or so...
Ty!!!
thanks man.. it s helpful.. I am also having the blog won oracle database www.blogskies.com visit it when you get time.. thx.
Thank you very much
beautiful-thanks!
thanks dude....
thx from Latvia
Clear, concise and right to the point. Thanks for this helpful tip and thanks Google for bringing your page to the top :). The search phrase I used was "mysql foreign key details" (without the double quotes).
Now does anyone know how to modify the data type of a column/field in MySQL which has a foreign key constraint to another table? A simple 'ALTER TABLE table_name MODIFY column_name new_column_type' throws an errorno: 150 due to the foreign key constraint.
Is there a way to get around this by doing something like cascading the changes to the other dependent tables?
Thanks,
Karthik
Just what I needed :)
Thanks :) one more to you growing list ;)
Thanks. It's very useful.
Post a Comment