Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to find the primary key of a table

 
Shobana Sukumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which table can i refer to find the primary key of a table?i need a tablename to be refered other than the desc command as i hav to access the table name's primary key value from a code....
 
Balaguru Krish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shobana Sukumar:
which table can i refer to find the primary key of a table?i need a tablename to be refered other than the desc command as i hav to access the table name's primary key value from a code....


In order to know the primary keys of a table,we have a table called USER_CONSTRAINTS.

Example :

select * from user_constraints where table_name = 'EMP' and constraint_type = 'P'
 
Balaguru Krish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to know the column name which is the primary key , we can use
USER_CONS_COLUMNS table.

Example:

SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = 'EMP_PRIMARY_KEY'
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balaguru Krish:
In order to know the column name which is the primary key , we can use
USER_CONS_COLUMNS table.

Example:

SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = 'EMP_PRIMARY_KEY'

That works on only a few databasees, maybe even only Oracle; it certainly doesn't work on many many other databases. Other databases have other DB-specific approaches; in general, you can use jva.sql.DatabaseMetaData methods to find such things.
 
saivenu madhav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am using DB2, i would like to know the primarykey of one table.

Could you please reply in SQL query format

The above didn't work for me..


ThanksInadvance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic