Given a table name, how do I extract a list of primary key columns and their datatypes from a plpgsql function?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
Beware of indexes where the column order differs from the table's column order. (i.e. if the primary key used columns 3, 2, and 1)
The following query is much more complex, but returns the columns in the proper order. (Remove the 'indisprimary' clause to get the same info for all indexes on a table)
The following
SQL
statement works for me:It is directly taken from here.
Preserving column order using
generate_subscripts
:To provide a straight bit of SQL, you can list the primary key columns and their types with:
The query above is very bad as it is really slow.
I would recommend this official version:
http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
if schema is needed the query is as follows