File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes how to get the table constraints? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "how to get the table constraints?" Watch "how to get the table constraints?" New topic

how to get the table constraints?

Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
I am trying to create a java component. I want to get all the constraints set for a database table. I am able to get the primary key constraint,but how to get the check constraints? Does java hae any api to get all the check constraints available on a table?

craig a chapman

Joined: Jun 24, 2005
Posts: 27
The only way I know to get table constraints is via ResultSet.getMetaData() The DatabaseMetaData interface allows you to get all sorts of information about your tables.

Hope that helps.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Excuse me, but I am confused. Please clarify your answer for me.

The "getMetaData()" method (of interface java.sql.ResultSet) returns an instance of java.sql.ResultSetMetaData -- and not an instance of java.sql.DatabaseMetaData. It seems to me like you are saying that you use the "getMetaData()" method to obtain an instance of "DatabaseMetaData", but this is not correct, so please clarify.

In any case, as Sripathi mentioned, there is nothing in the javadocs (that I could find) that returns the check constraints for database table columns. Perhaps you could provide some code that demonstrates how to do this?

Good Luck,
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Presuming your database supports this you can query its meta data to get constraints. For example in SQL Server you might write:

As I think you've also noticed, you can get Primary Key (and Foreign Key) constraints in a database independent way using DatabaseMetaData's methods.
[ July 13, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
yes,there is nothing in the Java api that can give the constraints set on a table. I have already used the DatabasemetaData and am able to get the primary keys, foriegn keys. But what I need is the unique keys and constraints in the table.
The only way I see now is to query the system catalog tables and get the information needed. But that would make the component database dependent which is not what I need.

I was looking at the eclipse api. There is something called as rdb component in eclipse where they have all the methods that I need.

Method Summary
org.eclipse.emf.common.util.EList getConstraints()
java.util.List getForeignKeys()
PrimaryKey getPrimaryKey()
java.util.List getUniqueConstraints()

Wonderful!!! isnt it? But no example code is given there and not much information is available. If anyone has sample code on how to use it that would be great.
craig a chapman

Joined: Jun 24, 2005
Posts: 27
Sorry fellas, I was getting my wires crossed there. I meant ResultSetMetaData not DatabaseMetaData. In any case I didn't answer the question as it appears I misunderstood it.
I agree. Here's the link:
subject: how to get the table constraints?
It's not a secret anymore!