Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select primary key from table in mysql

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
US
I'm wanting to do a select and retrieve the column name for the primary key in a table. I did a search and found a thread that described doing this in Oracle, but I'm pretty sure that wouldn't work in mySQL.

I'm using mySQL 4.x if it helps.

Thanks for your time.
 
I would like to be able to select the primary key out of a table in a way that would work for ANY database. I realize there may be different methodologies for doing so for each particular database. I can work around this with some branching logic. But, I'll still need to know WHAT database I'm working against. As such, as an alternate to my original question, "Is there a any ANSI SQL way to retrieve the database type (mysql, oracle, etc) I'm working against?"

Thanks again.
 
There is a ANSI way, using the information_schema views. In this case information_schema.key_column_usage.

Neither Mysql nor Oracle supports information_schema views so you are out of luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top