Subject Re: determining if a table has a primary key
Author Milan Babuskov
--- Ed Dressel wrote:
> What's the SQL to get the primary key of a table?

select i.rdb$field_name
from rdb$relation_constraints r
join rdb$index_segments i on r.rdb$index_name=i.rdb$index_name
where r.rdb$relation_name = 'YOUR_TABLE'
and r.rdb$constraint_type='PRIMARY KEY'
order by r.rdb$constraint_name, i.rdb$field_position


> While I'm
> asking--the SQL for getting foreign keys as well.


select i.rdb$field_name, c.rdb$update_rule,
c.rdb$delete_rule, r2.rdb$relation_name,
i2.rdb$field_name
from rdb$relation_constraints r
join rdb$index_segments i
on r.rdb$index_name = i.rdb$index_name
join rdb$ref_constraints c
on r.rdb$constraint_name = c.rdb$constraint_name
join rdb$relation_constraints r2
on r2.rdb$constraint_name = c.RDB$CONST_NAME_UQ
join rdb$index_segments i2
on i2.rdb$index_name = r2.rdb$index_name
where r.rdb$relation_name='SALES'
and r.rdb$constraint_type='FOREIGN KEY'
order by 1, i.rdb$field_position


--

Milan Babuskov
http://www.guacosoft.com