0

Is it possible to check (with a query) if a primary key of an Informix table exists? I have to check this from Java code via the EntityManager from javax.persistence.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Chris311
  • 3,239
  • 7
  • 39
  • 77
  • 1
    Yes, it is certainly possible to determine whether a table has a primary key in Informix (see the [question](http://stackoverflow.com/questions/19196184/query-to-check-if-primary-key-exists-on-the-table-in-informix) referenced by [perdomoff](https://stackoverflow.com/users/4344081/perdomoff) in his [answer](http://stackoverflow.com/a/33217940/15168). How you do it with `javax.persistence` and the EntityManager is a separate issue which requires more knowledge of Java than I have. – Jonathan Leffler Oct 20 '15 at 07:37

2 Answers2

1

Chris311, See the link below where similar question is asked and solved:

Query to check if primary key exists on the table in informix

The user first looks for index name for the PK (pk_idx column) then, checks the index columns (look for the same index name of the PK constraint).

Community
  • 1
  • 1
Perdomoff
  • 921
  • 2
  • 7
  • 24
0

Any database support special commands that retrieve information about the database schema itself and/or allow to change it.

For example MySQL supports command SHOW CREATE TABLE (see here). Informix' version of similar command is called info. Please take a look on the following discussion for details: Informix SQL - List all fields & tables

Java persistence API can run any command that is supported by current database including management commands, so you can run info command too. I'd recommend you to start from playing with this command using any UI or command line tool that runs commands against your database. Once your command works try to run it from java.

Community
  • 1
  • 1
AlexR
  • 109,181
  • 14
  • 116
  • 194
  • Note that the Informix INFO statement is only understood by the DB-Access program. It translates the INFO statement into one or more queries against the system catalog. – Jonathan Leffler Oct 20 '15 at 07:34