-3

Can anyone please tell me how to get the primary key of a table in MySQL?

DineshDB
  • 5,998
  • 7
  • 33
  • 49
Anubhav Sarangi
  • 179
  • 1
  • 5
  • 15

1 Answers1

8

Try this:

Solution 1:

This is from Ajons Answer

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';

Solution 2:

This is from alexn

SHOW KEYS FROM tablename WHERE Key_name = 'PRIMARY'

Solution 3:

SHOW INDEX FROM presort.final_conf_score_mld_run2 
WHERE Key_name = 'PRIMARY';
DineshDB
  • 5,998
  • 7
  • 33
  • 49
  • None of them is working....Can you just help me with the query - if my table name is "final_conf_score_mld_run2" and database name is "presort", what should be the query according to solution 1? – Anubhav Sarangi Mar 19 '18 at 07:28
  • Also if we can view the schema of the table - it will show which column is PRIMARY KEY. How to view the schema? – Anubhav Sarangi Mar 19 '18 at 07:32
  • @AnubhavSarangi, Try the third one. – DineshDB Mar 19 '18 at 07:32
  • Solution 3 also not working.... Error - "FAILED: ParseException line 1:11 mismatched input 'FROM' expecting ON near 'INDEX' in show statement" – Anubhav Sarangi Mar 19 '18 at 07:34
  • 1
    @AnubhavSarangi Are you sure you are using mysql? Since all of them should work. – MBijen Mar 19 '18 at 07:35
  • Sorry @MBijen @DineshDB - the solution 3 worked.... slight change in query - SHOW INDEX FROM presort.final_conf_score_mld_run2 WHERE `Key_name` = 'PRIMARY'; – Anubhav Sarangi Mar 19 '18 at 07:37