Search code examples
mysqlprimary-keyinformation-schema

MySQL - How to get primary key of a table?


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


Solution

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