19

I'm, generating a SQL query like this in PHP:

$sql = sprintf("UPDATE %s SET %s = %s WHERE %s = %s", ...);

Since almost every part of this query is dynamic I need a way to determine the table's primary key dynamically, so that I'd have a query like this:

$sql = sprintf("UPDATE %s SET %s=%s WHERE PRIMARY_KEY = %s", ...);

Is there a MySQL keyword for a table's primary key, or a way to get it?

I've used the information_schema DB before to find information like this, but it'd be nice if I didn't have to resort to that.

joshwbrick
  • 5,882
  • 9
  • 48
  • 72

5 Answers5

31
SHOW INDEX FROM <tablename>

You want the row where Key_name = PRIMARY

http://dev.mysql.com/doc/refman/5.0/en/show-index.html

You'll probably want to cache the results -- it takes a while to run SHOW statements on all the tables you might need to work with.

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
  • 14
    Thanks, that is great. I ended up using this: SHOW KEYS FROM WHERE Key_name = 'PRIMARY' – joshwbrick May 21 '09 at 17:13
  • 1
    This returns the full row... if you want just the column name, see Jake Sully's answer below. – Andrew Feb 11 '16 at 17:53
  • As the guy who wrote this answer, I have to agree: Jake Sully's answer is the best one so far. @macinjosh I'd recommend un-accepting my answer, and accepting Jake's instead. – Frank Farmer Mar 10 '16 at 23:53
20

It might be not advised but works just fine:

SHOW INDEX FROM <table_name> WHERE Key_name = 'PRIMARY';

The solid way is to use information_schema:

SELECT k.COLUMN_NAME
FROM information_schema.table_constraints t
LEFT JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
    AND t.table_schema=DATABASE()
    AND t.table_name='owalog';

As presented on the mysql-list. However its a few times slower from the first solution.

lukmdo
  • 7,489
  • 5
  • 30
  • 23
  • Hi, I know this is an old post but I just wanted to ask why you said the first method is not advised. The accepted answer from [this duplicate question](https://stackoverflow.com/questions/2341278/php-get-primary-key-of-table) says you don't always have access to the information_schema so it suggests the first method. Plus you said yourself that the second method is a few times slower than the first solution. If that is the case, why is the second method solid? Thanks. – Cave Johnson Mar 09 '16 at 18:18
  • As pointed in http://mysql-0v34c10ck.blogspot.com/2011/05/better-way-to-get-primary-key-columns.html it can not be used as mysql variable or think view etc but if the first case works in your scenario go for it @Andrew – lukmdo Mar 10 '16 at 00:42
18

A better way to get Primary Key columns:

SELECT `COLUMN_NAME`
FROM `information_schema`.`COLUMNS`
WHERE (`TABLE_SCHEMA` = 'dbName')
  AND (`TABLE_NAME` = 'tableName')
  AND (`COLUMN_KEY` = 'PRI');

From http://mysql-0v34c10ck.blogspot.com/2011/05/better-way-to-get-primary-key-columns.html

Jake Sully
  • 191
  • 1
  • 3
0

Also

SHOW INDEX FROM <table_name> WHERE Key_name = 'PRIMARY';

Is equivalent to

SHOW KEYS FROM <table_name> WHERE Key_name = 'PRIMARY';

enter image description here

Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

Based on @jake-sully and @lukmdo answers, making a merge of their code, I finished with the following snippet:

SELECT `COLUMN_NAME`
FROM `information_schema`.`COLUMNS`
WHERE (`TABLE_SCHEMA` = DATABASE())
AND (`TABLE_NAME` = '<tablename>')
AND (`COLUMN_KEY` = 'PRI');

Hope it could help someone

Ivano
  • 72
  • 6