2017-08-29 310 views
1

我有一些帶有主鍵和外鍵的Sybase ASE表,我想獲得這些表的REAL PK和FK列表。這個信息應該通過SELECT查詢返回給系統表。 由於syskeys僅包含通過sp_foreignkey創建的表的邏輯引用,所以對「syskeys」使用查詢的所有查詢均不正確。 例如,我有如下表:Sybase ASE:通過查詢獲取REAL PK和FK的列表

create table tbl_pk1 
(col1 int primary key, 
col2 int); 

create table tbl_pk3 
(col1 int null, 
col2 int); 

sp_primarykey 'tbl_pk3', 'col1' 

而且下面的查詢將返回唯一的「tbl_pk3」。

select t.name from syskeys i INNER JOIN sysobjects t ON i.id = t.id where t.name in ('tbl_pk1', 'tbl_pk3') 

我知道什麼是「sp_helpconstraint可將」程序可以幫助我,但我不能把它SELECT。 也許有人可以幫助我查詢系統表以獲取有關表的主鍵和外鍵的實際信息?

+0

你可能想用一些例子來更新你的問題所需的輸出(例如,pk/fk中列的名稱?pk?表名的約束名稱?fk?名稱的表名);我還建議你看看sp_helpconstraint的源代碼('exec sybsystemprocs..sp_helptext sp_helpconstraint null,null,showsql')...你應該能夠使用這段代碼創建你自己的SELECT來顯示約束數據的格式是你想要的 – markp

回答

0

下面是它如何可以做到

select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 1) column_name, 1 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 1) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 2) column_name, 2 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 2) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 3) column_name, 3 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 3) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 4) column_name, 4 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 4) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 5) column_name, 5 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 5) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 6) column_name, 6 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 6) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 7) column_name, 7 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 7) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 8) column_name, 8 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 8) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 9) column_name, 9 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 9) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 10) column_name, 10 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 10) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 11) column_name, 11 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 11) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 12) column_name, 12 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 12) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 13) column_name, 13 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 13) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 14) column_name, 14 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 14) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 15) column_name, 15 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 15) is not null   union   
select user_name(o.uid) user_name, object_name(i.id) table_name, index_col(object_name(i.id), i.indid, 16) column_name, 16 column_id, i.name as con_name 
    from sysindexes i, sysobjects o where object_name(i.id) = 'tbl_pk_mult' and user_name(o.uid) = 'dbo' and status & 2048 = 2048 and i.id = o.id and index_col(object_name(i.id), i.indid, 16) is not null   
-1

我使用Sybase ASA(不是ASE),但可以嘗試此查詢(它適用於ASA)。

對於外鍵:

select f.* from sys.systable t 
join sys.SYSFOREIGNKEY f on t.table_id=f.primary_table_id 
where t.table_name='xxx' 

主鍵:

select c.* from sys.systable t 
join sys.SYSCONSTRAINT c on t.object_id=c.table_object_id 
where t.table_name='xxx' and c.constraint_type='P'