Rewrite foreign_key_info to be just one query

Do all the data munging in SQL, and just spit the result straight out,
instead of three separate queries with lots of munging in perl-space.