Results 1 to 4 of 4

Thread: How to get list of database's table's foreign keys?

  1. #1
    Join Date
    Oct 2007
    Posts
    27
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to get list of database's table's foreign keys?

    Hi,

    I build application which can read all tables on all databases on specified server (MySQL 5.0), so I don't know how selected table look like. I have to get list of all foreign keys (if is there any) for selected table. I found class QSqlRelationalTableModel, but there I must to setRelation with exact names of columns, but in my case I don't know which columns are fk and on which table's column pointing at. In database are defined fk's and when I open database in MySQL-Front I can see all relations. I have to see relations from my Qt application.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get list of database's table's foreign keys?

    You can query information_schema.table_constraints table.

  3. #3
    Join Date
    Jul 2008
    Location
    Spain
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get list of database's table's foreign keys?

    This PostgreSQL view helps me a lot when I want more information about foreign keys:

    Qt Code:
    1. CREATE OR REPLACE VIEW my_foreign_keys AS
    2. SELECT s1.constraint_name, s1.table_name, s1.column_name, s1.ordinal_position, s2.table_name_ref, s2.column_name_ref, s2.ordinal_position_ref
    3. FROM ( SELECT key_column_usage.constraint_name, key_column_usage.table_name, key_column_usage.column_name, columns.ordinal_position
    4. FROM information_schema.key_column_usage
    5. JOIN information_schema.columns USING (table_name, column_name)) s1
    6. JOIN ( SELECT constraint_column_usage.constraint_name, constraint_column_usage.table_name AS table_name_ref, constraint_column_usage.column_name AS column_name_ref, cols_ref.ordinal_position AS ordinal_position_ref
    7. FROM information_schema.constraint_column_usage
    8. JOIN information_schema.columns cols_ref ON cols_ref.table_name::text = constraint_column_usage.table_name::text AND cols_ref.column_name::text = constraint_column_usage.column_name::text) s2 ON s1.constraint_name::text = s2.constraint_name::text AND NOT s1.table_name::text = s2.table_name_ref::text;
    To copy to clipboard, switch view to plain text mode 

    The table obtained is:
    constraint_name | table_name | column_name | ordinal_position | table_name_ref | column_name_ref | ordinal_position_ref

    I am not a guru, so use it with care.
    Auryn
    Starting to learn the world of Qt

  4. #4
    Join Date
    Sep 2008
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to get list of database's table's foreign keys?

    If you are using a 4.x version of MySQL, you don't have the luxury of INFORMATION_SCHEMATA, but you can get at a table's foreign keys by issuing a SHOW CREATE TABLE xxx; query and parsing the return values for rows which look like CONSTRAINT xxx_ibfk_n FOREIGN KEY (`abc`) REFERENCES `yyy`(`def`), where abc is the foreign key column in your xxx table and def is the referenced column in your yyy table. With judicious use of Qt or STL container classes you can easily build the referential structure as a tree, which then lends itself very well to post-processing in order to extract the reference hierarchy. I have a home-brewed tool which uses this exact approach to document a corporate database (it goes one step further by generating LaTeX documentation and then calling pdflatex to build a document the developers can see via the intranet).
    Last edited by daggilli; 26th September 2008 at 06:03.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.