Aaron Cooper wrote:
> Hi All,
> 
> Can't find anything on this in the docs. Does anyone know of a way
> off the top of their head how you can return the name of a table's
> primary key using SQL.
> 
> I've figured out how to get foreign keys using the
> REFERENCED_COLUMN_NAME constant, but there doesn't appear to be any
> such constant for Primaries.

If your DB supports INFORMATION_SCHEMA you can use something along the
lines of the following:

SELECT TABLE_NAME, GROUP_CONCAT(COLUMN_NAME) AS PRI_KEY_COLUMNS
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_KEY = 'PRI'
AND TABLE_SCHEMA = 'mydatabase'
AND TABLE_NAME = 'mytable'
GROUP BY TABLE_NAME;

Replace mydatabase and mytable with the relevant database and table names.

Regards,
Paul

--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to