List:General Discussion« Previous MessageNext Message »
From:Dan Nelson Date:January 12 2007 7:00am
Subject:Re: Primary key
View as plain text  
In the last episode (Jan 11), Steffan A. Cline said:
> Is there anyway to do a SELECT or DESCRIBE or SHOW statement which can
> return the primary key field of a specified table?
> 
> Pseudo code: select primary_key_field_name from mytable.

SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
  WHERE TABLE_SCHEMA='myschema' AND TABLE_NAME='mytable' 
  AND CONSTRAINT_NAME='PRIMARY'
  ORDER BY ORDINAL_POSITION;

For multi-column indexes, you will get multiple rows back.

You should also use "show create table mytable" or "show keys from
mytable" but you'll have to do extra parsing.

-- 
	Dan Nelson
	dnelson@stripped
Thread
Primary keySteffan A. Cline12 Jan
  • Re: Primary keyDan Nelson12 Jan
    • Re: Primary keySteffan A. Cline12 Jan