Board index » delphi » Database Primary Key

Database Primary Key

Hello everyone:

I was wandering, if you have a table name is there anyway to find the
primary key of the table?
I am using interbase.  I saw in the interbase docs that you can do
select RDB$... from Relations....  But I really do not know if this is
the right track.  Thanks for any help that you can give me.

Trac

 

Re:Database Primary Key


Quote
Trac Taylor wrote:

> I was wandering, if you have a table name is there anyway to find the
> primary key of the table?
> I am using interbase.  

SELECT
  S.RDB$FIELD_NAME
FROM
  RDB$RELATION_CONSTRAINTS RC
  INNER JOIN RDB$INDEX_SEGMENTS S
    ON S.RDB$INDEX_NAME = RC.RDB$INDEX_NAME
WHERE
  RC.RDB$RELATION_NAME = :Table_name
  AND RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY
  S.RDB$FIELD_POSITION

        Keep in mind that a table might have no primary key, or the primary key
may have more than one field, in which case this query will give you
more than one record back.

        HTH,

        -Craig

--
Craig Stuntz            Vertex Systems Corporation
Senior Developer        http://www.vertexsoftware.com

Re:Database Primary Key


Hi Craig:

I tried the code you gave me in the sqlExplorer that comes with delphi.  I
substituted a table name that I know has a primary key and the query return
nothing.  Do you know why this is?  Is this type of query possible to run in the
sqlExplorer?

Thanks

Trac

Quote
Craig Stuntz wrote:
> Trac Taylor wrote:

> > I was wandering, if you have a table name is there anyway to find the
> > primary key of the table?
> > I am using interbase.

> SELECT
>   S.RDB$FIELD_NAME
> FROM
>   RDB$RELATION_CONSTRAINTS RC
>   INNER JOIN RDB$INDEX_SEGMENTS S
>     ON S.RDB$INDEX_NAME = RC.RDB$INDEX_NAME
> WHERE
>   RC.RDB$RELATION_NAME = :Table_name
>   AND RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
> ORDER BY
>   S.RDB$FIELD_POSITION

>         Keep in mind that a table might have no primary key, or the primary key
> may have more than one field, in which case this query will give you
> more than one record back.

>         HTH,

>         -Craig

> --
> Craig Stuntz            Vertex Systems Corporation
> Senior Developer        http://www.vertexsoftware.com

Re:Database Primary Key


Quote
Trac Taylor wrote:

> I tried the code you gave me in the sqlExplorer that comes with delphi.  I
> substituted a table name that I know has a primary key and the query return
> nothing.  Do you know why this is?  Is this type of query possible to run in the
> sqlExplorer?

        Yes; I ran it in SQL explorer before I posted.  The table name will be
case sensitive and needs to be in quotes -- are your sure the case is
correct?

        If it still doesn't work for you, post the SQL exactly as you're
entering it in SQL explorer, and the DDL for the table in question.
FWIW, I'm using IB 5.6.

        HTH,

        -Craig

--
Craig Stuntz            Vertex Systems Corporation
Senior Developer        http://www.vertexsoftware.com

Re:Database Primary Key


Hey Craig:

Yes, you were right (ofcourse) I did not have the proper case for the table name.

Thanks alot, I have been trying to find this info for a few months now.

Trac

Quote
Craig Stuntz wrote:
> Trac Taylor wrote:

> > I tried the code you gave me in the sqlExplorer that comes with delphi.  I
> > substituted a table name that I know has a primary key and the query return
> > nothing.  Do you know why this is?  Is this type of query possible to run in the
> > sqlExplorer?

>         Yes; I ran it in SQL explorer before I posted.  The table name will be
> case sensitive and needs to be in quotes -- are your sure the case is
> correct?

>         If it still doesn't work for you, post the SQL exactly as you're
> entering it in SQL explorer, and the DDL for the table in question.
> FWIW, I'm using IB 5.6.

>         HTH,

>         -Craig

> --
> Craig Stuntz            Vertex Systems Corporation
> Senior Developer        http://www.vertexsoftware.com

Other Threads