Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JDBC 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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
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: 1121

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.
subject: how to get the table constraints?
Similar Threads
delay validation in hibernate till commit
Authenticating Users
How To Know oracle table information
Using JDBC versus Stored Procedures
creating backup tables