Search code examples
javadatabaseoracle-databasejdbcmetadata

How to get VIEW references (base tables) through JDBC?


When I create VIEW, database engine pases my query and store it some how. Can I then find out what tables are used by one view? I understand that different databases may be differently. May have some sort of level of abstraction throw JDBC.

Something tells me that this is done through getTables(?):

Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);        
DatabaseMetaData md = con.getMetaData();
md.getTables(null, null, null, null);

If it can not be. Are there solution in particular for Oracle database (like view/referenses in plsql developer)?


Solution

  • Oracle:

    Select * from dictionary 
    

    Returns many system tables/functions you have access to which contain valuable structure information.

    in that list you will see

    Select * from ALL_Views (which contains the source SQL for the view) which could be parsed for table names.

    Though I think there's an all_Dependencies or all_References view that will contain required references to tables in seperate fields which would be easier to parse I'm looking for that now...

    Select * from All_dependencies WHERE Name = 'YOUR_VIEWNAME' and Referenced_Type='TABLE' will return all referenced tables.

    Though I'm not sure how those features work with Linked tables. so if you're referencing external tables though a linked server... those features may not work.