Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:41:51+00:00 2026-05-28T05:41:51+00:00

This might sound a bit un-realistic, but I’ve recently had to study the database

  • 0

This might sound a bit un-realistic, but I’ve recently had to study the database structure of a system am maintaining remotely, and it hit me that there were some occassions when I wanted to find all columns that were linked to a given table (see, the original devs didn’t create explicit relations, but instead have relations encoded in the column names!).

For example, assuming all columns referring to a column parent.parent have the form table.somefield_parent as the means of documenting a relationship between the two fields, how do I find all such relationships (and the tables involved) using say the standard SQL commands / in-built functions?

Any solutions for MySQL or PostgreSQL are welcome.

NOTE :

  • am not looking for patterns in the data, but in the meta-data.
  • I know I can dump the db structure and search for the results there, but I don’t want to go out of the db-shell to find these relationships (assume am working in say psql, and don’t want to exit)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:41:51+00:00Added an answer on May 28, 2026 at 5:41 am

    In PostgreSQL you can query the system catalog tables.

    You can also query the information_schema, which consists of views providing the standard SQL way of displaying the information. Querying the system catalogs is usually much faster, though.

    To find the children of one particular parent column:

    SELECT n.nspname AS schema_name
         , c.relname AS table_name
         , a.attname AS column_name
    FROM   pg_catalog.pg_attribute a
    JOIN   pg_catalog.pg_class c ON c.oid = a.attrelid
    JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    WHERE  a.attname ~~ E'%\\_parent'
    AND    NOT a.attisdropped
    AND    c.relkind = 'r'
    AND    nspname !~~ E'pg\\_%';
    

    To find all related column according to the description:

    SELECT n.nspname AS parrent_schema
         , c.relname AS parrent_table
         , a.attname AS parrent_column
         , n1.nspname AS child_schema
         , c1.relname AS child_table
         , a1.attname AS child_column
    FROM   pg_catalog.pg_attribute a
    JOIN   pg_catalog.pg_class c ON c.oid = a.attrelid
    JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    JOIN   pg_catalog.pg_attribute a1 ON a.attname = substring(a1.attname, '_(.*?)$')
    JOIN   pg_catalog.pg_class c1 ON c1.oid = a1.attrelid
    JOIN   pg_catalog.pg_namespace n1 ON n1.oid = c1.relnamespace
    WHERE  c.relkind = 'r'
    AND    c1.relkind = 'r'
    AND    n.nspname !~~ E'pg\\_%'
    AND    n1.nspname !~~ E'pg\\_%'
    AND    NOT a.attisdropped
    AND    NOT a1.attisdropped
    -- AND    n.nspname = 'public'   -- to narrow it down to a specific schema
    -- AND    n1.nspname = 'public'   -- to narrow it down to a specific schema
    ORDER  BY 1,2,3,4,5,6
    

    This assumes that the parrent columns don’t have _ in the name.
    The key element is the join condition:

    a.attname = substring(a1.attname, '_(.*?)$')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound a bit of an odd question but I know what I
This might sound like a little bit of a crazy question, but how can
This might sound a bit dumb. I always had this impression that web.config should
Now this might sound simple, but I'm a bit mixed up. I am trying
This might sound a bit dumb but am confused. I know the strlen() would
this question might sound a little bit weired... But I will try to explain:
I know this question might sound a little bit crazy, but I tough that
This might sound a bit complicated, but what I want to do is find
This question might sound a bit stupid but here it goes. I have two
So this might sound a bit odd, but essentially I'm trying to render a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.