Let’s assume we have Drupal instance with second database connection. Tables in that second database are shared between many systems and thus was not created using Drupal’s schema functionality. How do I get the name of the primary key, in a universal way that doesn’t dependent on the RDBMS? Is there a way that will be equally good for all databases?
For MySQL I can use db_query and:
SHOW INDEX FROM {table} WHERE Key_name = "PRIMARY"
and for SQL-92 compatible ones:
SELECT pk.TABLE_NAME, c.COLUMN_NAME primary_key FROM information_schema.table_constraints pk JOIN information_schema.key_column_usage c ON c.table_name = pk.table_name AND c.constraint_name = pk.constraint_name WHERE constraint_type = 'primary key'
Thanks to T I’s answer on stackoverflow.
What happens with Oracle or Apache Derby? I know I can find specific solutions, but I wonder if there is a Drupal way. I couldn’t find one outside schema.