Subject RE: [firebird-support] RDB$CONSTRAINT_TYPE returning Null for Unique index
Author Thomas Ellis
Hi Werner,

A unique index is not a constraint a "unique constraint" is something
different that is why your query was returning NULL.

Here is a sample to give a loose description for each index "type" you
should be able to work something out from here.

SELECT
RDB$INDICES.RDB$INDEX_NAME AS "Index Name",
CASE WHEN ((RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE IS NULL) AND
(RDB$INDICES.RDB$UNIQUE_FLAG = 1)) THEN 'UNIQUE INDEX'
WHEN (RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE IS NULL) THEN
'NON-UNIQUE INDEX'
ELSE RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE END AS
"Constraint/Index Type"
FROM RDB$INDICES
LEFT OUTER JOIN RDB$RELATION_CONSTRAINTS
ON RDB$RELATION_CONSTRAINTS.RDB$INDEX_NAME = RDB$INDICES.RDB$INDEX_NAME
WHERE RDB$INDICES.RDB$RELATION_NAME = 'tablename'
ORDER BY RDB$INDICES.RDB$INDEX_NAME

When adding fields (INDEX_SEGMENTS) into the mix keep in mind partial keys,
their uniqueness cannot be fixed at a field level you need to look at the
index as a whole. Showing unique/primary key in conjunction with a single
field effectively means nothing.

Here is a sample SQL to show the fields of the indexes:

SELECT
RDB$INDICES.RDB$INDEX_NAME AS "Index Name",
RDB$INDEX_SEGMENTS.RDB$FIELD_NAME AS "Index Field",
CASE WHEN ((RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE IS NULL) AND
(RDB$INDICES.RDB$UNIQUE_FLAG = 1)) THEN 'UNIQUE INDEX'
WHEN (RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE IS NULL) THEN
'NON-UNIQUE INDEX'
ELSE RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_TYPE END AS
"Constraint/Index Type"
FROM RDB$INDICES
LEFT OUTER JOIN RDB$RELATION_CONSTRAINTS
ON RDB$RELATION_CONSTRAINTS.RDB$INDEX_NAME = RDB$INDICES.RDB$INDEX_NAME
LEFT OUTER JOIN RDB$INDEX_SEGMENTS
ON RDB$INDICES.RDB$INDEX_NAME = RDB$INDEX_SEGMENTS.RDB$INDEX_NAME
WHERE RDB$INDICES.RDB$RELATION_NAME = 'tablename'
ORDER BY RDB$INDICES.RDB$INDEX_NAME, RDB$INDEX_SEGMENTS.RDB$FIELD_POSITION;

Regards
Thomas Ellis