Subject How to obtain the primary key column(s)
Author fabiano_bonin
Hi all,

If i create a table like this:

create table table1 (
id integer not null primary key,
column1 varchar(10) );

or like this:

create table table2 (
id1 integer not null,
id2 integer not null,
column1 varchar(10) );
alter table table2 add primary key (id1, id2);

How can i obtain, based on system tables, the columns used in the
primary key of both tables? I am using the query below and it works,
but i am worried if the index used by the primary key will always
begin with 'RDB$PRIMARY'. I'd like to know if there is a better way
to do that.

select
a.rdb$relation_name,
c.rdb$field_name,
from
rdb$relations a
inner join rdb$indices b on b.rdb$relation_name =
a.rdb$relation_name
inner join rdb$index_segments c on c.rdb$index_name =
b.rdb$index_name
where
( b.rdb$index_name like 'RDB$PRIMARY%' ) and
( a.rdb$relation_name in ('TABLE1', 'TABLE2') )
order by
a.rdb$relation_name, c.rdb$field_position

Thanks.