Solved

How do I write a query  that will display the columns designated as primary keys  for each table  in a Derby 10.11  database?

Posted on 2015-01-12
7
335 Views
Last Modified: 2015-01-16
I am trying to find a way to write a select statement that will show all of the primary keys for each table in a  database.  The describe table utility provides some useful information but it does not, as far as I can tell, give any info pertaining to either primary or foreign keys.  The closest I have come to getting the results I am looking for is this select statement:

SELECT TA.TABLENAME, CO.COLUMNNAME, CO.COLUMNNUMBER,     CO.REFERENCEID, CN.TABLEID,
              CN.ISINDEX, CN.ISCONSTRAINT, KS.CONSTRAINTID, CON.CONSTRAINTID,
              CON.CONSTRAINTNAME, CON.SCHEMAID  

FROM SYS.SYSTABLES TA, SYS.SYSCOLUMNS CO, SYS.SYSCONGLOMERATES CN, SYS.SYSKEYS KS, SYS.SYSCONSTRAINTS CON

WHERE TA.TABLEID = CO.REFERENCEID AND
              CN.TABLEID = TA.TABLEID AND
              CN.CONGLOMERATEID = KS.CONGLOMERATEID AND
              KS.CONSTRAINTID = CON.CONSTRAINTID

The SYSKEYS appears to be the table that contains data relating to the primary keys for each table in a database.  That in conjunction with  the CONGLOMERATES, SYSCONSTRAINTS, SYSCOLUMNS and SYSTABLES would seem to be enough to get the output I am looking for but there are no fields in the SYSCOLUMNS table that will link either directly or indirectly to  a record in the SYSKEYS table.  The report produced from the query shown above lists the all of the columns in a table that relates to a record in the SYSKEYS table.  In other words I am only able to find the table that contains the primary key referenced in a row in the SYSKEYS table.  I am not able to drill down any further than that.
 
Does anybody have an idea what need to do to get this narrowed down to the actual column in a table that is the primary key for that table?    

Thank you.
0
Comment
Question by:srulison
  • 4
  • 3
7 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 40546812
So, first I look at SYSCONSTRAINTS.  You'll need to check TABLEID and TYPE to isolate the PK you want to describe, and get the CONSTRAINTID.  Match that to SYSKEYS to get the CONGLOMERATEID, which you can then follow back to SYSCONGLOMERATES.DESCRIPTOR.  So:
SELECT c.DESCRIPTOR FROM
  SYSCONSTRAINTS a 
    INNER JOIN SYSKEYS b ON a.CONSTRAINTID=b.CONSTRAINTID
    INNER JOIN SYSCONGLOMERATES c ON b.CONGLOMERATEID = c.CONGLOMERATEID
  WHERE a.TABLEID='my_table_name' AND a.TYPE='P'

Open in new window

Now comes the fun part - that DESCRIPTOR is supposed to be a CSV list of column *numbers* in the table.  You'll still need to pull the column list and isolate them somehow.
0
 

Author Comment

by:srulison
ID: 40547399
Well unfortunately the SQL client that I am using, Squirrel SQL returns unknown data type for the DESCRIPTOR field.  The data type shown in the table description is org.apache.derby.  catalog.IndexDescriptor.

As an FYI here is a link to all of the sys table layouts   http://db.apache.org/derby/docs/10.3/ref/rrefsistabs38369.html.  

Maybe it's just not feasible to get this information through a standard SQL statement.  I know that it can be done in a java application using the getMetaData() method, (see below) and that led me to believe that it should be  retrievable with a Select statement as well.  The fact that I am getting an unknown data  type on the DESCRIPTOR field is making me think twice about that.

DatabaseMetaData dm = conn.getMetaData( );
ResultSet rs = dm.getExportedKeys( "" , "" , "table1" );
while( rs.next( ) )
{    
  String pkey = rs.getString("PKCOLUMN_NAME");
  System.out.println("primary key = " + pkey);
}
0
 

Author Comment

by:srulison
ID: 40547427
Here are two images of select statements that I ran after reading your post.

Select-1.jpg
Select2.jpg
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
ID: 40549101
That's the same problem I ran into with my previous research.  The DESCRIPTOR field is not a SQL-92 standard data type - it is a data type specific to derby.  Even worse, a quote from the table definition page, for the column's type:
org.apache.derby.
catalog.IndexDescriptor:

This class is not part of the public API.
So, it's not something meant to be used externally, either.  I did find the interface page describing the DESCRIPTOR behind-the-scenes class, but it merely shows what is possible to access using Java libraries.  As a final hint, you can always download the SVN source for derby to find the implementation yourself.  I think that is the route you'll have to go.
0
 

Author Closing Comment

by:srulison
ID: 40550521
Unfortunately it does not appear that there is a way to query the columns designated as a primary key in an Apache Derby database.  That information appears to be contained in a field that is a hidden data type.  The gentleman that responded to my question did all he could to help me with this issue but it appears that there just isn't a way to retrieve this data through a normal SQL select statement.  I am therefore still giving him an A for providing the information that he did.  At least now I know the field where this information resides.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 40551448
To be a little more specific, I'm arguing this is not something that can be done outside of Java.  Inside a Java application, with the proprietary class available, it should be possible to not only return the data properly, but use it natively for whatever operations are required.
0
 

Author Comment

by:srulison
ID: 40553982
Sounds like the JAVA application is the only way to go.  Thank you again for your help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now