Sybase

如何辨識具有指向 Sybase 中特定表的外鍵的表?

  • November 14, 2018

我正在尋找(最好)一個 SQL 語句,它為任何具有 Sybase 中給定表的外鍵的表選擇表/列名。我認為表格應該以某種方式成為可能,sys...但是對於 sybase 來說是全新的,我無法完全理解它。因此,我們非常感謝對正確方向的任何幫助。

編輯:為了完整起見:SELECT @@VERSION返回

‘Adaptive Server Enterprise/15.0.3/EBF 17156 ESD#3/P/Sun_svr4/OS 5.8/ase1503/2726/64-bit/FBO/Fri Feb 5 05:26:23 2010’

編輯 2

非常感謝您的建議,尤其是Andrew Bickerton 的評論。這使我能夠建構一個基本的 SQL 選擇語句,作為進一步發展的起點。

如果其他人對此感興趣,這裡是:

select 
 fko.name    "Foreign key name",
 par.name    "Referenced table name",
 fk1.name || ' -> ' || pk1.name "Reference 1",
 fk2.name || ' -> ' || pk2.name "Reference 2",
 fk3.name || ' -> ' || pk3.name "Reference 3",
 fk4.name || ' -> ' || pk4.name "Reference 4"
from 
 sysobjects      tab                                       join
 sysconstraints  con on tab.id        = con.tableid        join
 sysobjects      fko on con.constrid  = fko.id             join
 sysreferences   ref on con.constrid  = ref.constrid       join
 sysobjects      par on par.id        = ref.reftabid  left join
 ---- 1. Column
 syscolumns      fk1 on ref.fokey1    = fk1.colid and
                        ref.tableid   = fk1.id        left join
 syscolumns      pk1 on ref.refkey1   = pk1.colid and
                        ref.reftabid  = pk1.id        left join
 ---- 2. Column
 syscolumns      fk2 on ref.fokey2    = fk2.colid and
                        ref.tableid   = fk2.id        left join
 syscolumns      pk2 on ref.refkey2   = pk2.colid and
                        ref.reftabid  = pk2.id        left join
 ---- 3. Column
 syscolumns      fk3 on ref.fokey3    = fk3.colid and
                        ref.tableid   = fk3.id        left join
 syscolumns      pk3 on ref.refkey3   = pk3.colid and
                        ref.reftabid  = pk3.id        left join
 ---- 4. Column
 syscolumns      fk4 on ref.fokey4    = fk4.colid and
                        ref.tableid   = fk4.id        left join
 syscolumns      pk4 on ref.refkey4   = pk4.colid and
                        ref.reftabid  = pk4.id        -- Et cetera...
where
 tab.type = 'U'      and
 tab.name = 'tq84_f' and
 fko.type = 'RI'

自從我在 sybase 工作以來已經有一段時間了,但是(從記憶中)以下 sql 應該為您指明正確的方向:

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

您還可以線上查看sybase 書籍(系統表)

另一種方法是“sp_depends”

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

引用自:https://dba.stackexchange.com/questions/2837