简体   繁体   English

Sybase ASE:通过查询获取REAL PK和FK的列表

[英]Sybase ASE: Get list of REAL PK and FK via query

I have some Sybase ASE tables with primary and foreign keys and I want to get list of REAL PK and FK for these tables. 我有一些带有主键和外键的Sybase ASE表,我想获取这些表的REAL PK和FK列表。 And this information should be returned via SELECT query to system tables. 并且此信息应通过SELECT查询返回给系统表。 All queries which uses queries to 'syskeys' are not correct since syskeys contains only logical references for tables which is created via sp_foreignkey. 使用syskeys查询的所有查询都不正确,因为syskeys仅包含通过sp_foreignkey创建的表的逻辑引用。 For example I have below tables: 例如,我有如下表:

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

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

sp_primarykey  'tbl_pk3', 'col1'

And below query will return only 'tbl_pk3'. 而下面的查询将仅返回“ 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')

I know what 'sp_helpconstraint' procedure can help me, but I can't call it in SELECT. 我知道什么'sp_helpconstraint'过程可以帮助我,但是我不能在SELECT中调用它。 Maybe someone can help me with query to system tables to get actual information about primary and foreign keys for tables? 也许有人可以帮助我查询系统表,以获得有关表的主键和外键的实际信息?

Here are how it can be done 这是可以完成的

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         

I use Sybase ASA (not ASE) but you can try this query (it works on ASA). 我使用Sybase ASA(不是ASE),但是您可以尝试此查询(在ASA上有效)。

For foreign key: 对于外键:

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

For primary key: 对于主键:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM