How do I identify tables that have a foreign key to a specific table in Sybase?

Been a while since I've worked in sybase but (from memory) the following sql should point you in the right direction:

select *
from sysobjects so inner join syscolumns sc on so.id = sc.id 
where sc.name = 'field name'

you can also check out the sybase books online (system tables)


Another aproach is "sp_depends"

Sybase URL: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sprocs/html/sprocs/sprocs68.htm


Note that the approach shown above (using sysconstraints and sysreferences) will only work if you have referential constraint. If you have just a foreign key defined but without constraint, you will not see it there. To get all foreign keys related to a given table, you can use syskeys:

select  rtrim(object_name(k.id)),
    rtrim(substring(col_name(k.depid, depkey1),sign(keycnt),30))
    +rtrim(substring(', '+col_name(k.depid, depkey2),sign(keycnt-1),30))
    +rtrim(substring(', '+col_name(k.depid, depkey3),sign(keycnt-2),30))
    +rtrim(substring(', '+col_name(k.depid, depkey4),sign(keycnt-3),30))
    +rtrim(substring(', '+col_name(k.depid, depkey5),sign(keycnt-4),30))
    +rtrim(substring(', '+col_name(k.depid, depkey6),sign(keycnt-5),30))
    +rtrim(substring(', '+col_name(k.depid, depkey7),sign(keycnt-6),30))
    +rtrim(substring(', '+col_name(k.depid, depkey8),sign(keycnt-7),30)),
    rtrim(substring(col_name(k.id, key1),sign(keycnt),30))
    +rtrim(substring(', '+col_name(k.id, key2),sign(keycnt-1),30))
    +rtrim(substring(', '+col_name(k.id, key3),sign(keycnt-2),30))
    +rtrim(substring(', '+col_name(k.id, key4),sign(keycnt-3),30))
    +rtrim(substring(', '+col_name(k.id, key5),sign(keycnt-4),30))
    +rtrim(substring(', '+col_name(k.id, key6),sign(keycnt-5),30))
    +rtrim(substring(', '+col_name(k.id, key7),sign(keycnt-6),30))
    +rtrim(substring(', '+col_name(k.id, key8),sign(keycnt-7),30))
from    syskeys k
where   k.type = 2
and     k.depid = object_id('report')

(replace report by the name of your table).

The first column return is the name of the table with a foreign key pointing to this table. The second column is the list of columns in this table. And the third columns is the list column names in the referencing table.

So if you have a table people_report with a column rep_key which is a foreign key to report.key, you'd get something like this:

people_report        key       rep_key