【问题标题】:Sybase ASE: Get list of REAL PK and FK via querySybase ASE:通过查询获取 REAL PK 和 FK 列表
【发布时间】:2023-03-11 09:23:02
【问题描述】:

我有一些带有主键和外键的 Sybase ASE 表,我想获取这些表的 REAL PK 和 FK 列表。并且此信息应通过 SELECT 查询返回到系统表。 所有使用“syskeys”查询的查询都是不正确的,因为 syskeys 只包含通过 sp_foreignkey 创建的表的逻辑引用。 例如我有下表:

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 中调用它。 也许有人可以帮助我查询系统表以获取有关表的主键和外键的实际信息?

【问题讨论】:

  • 您可能想用一些所需输出的示例来更新您的问题(例如,约束名称?pk 的表名?fk 的表名?pk/fk 中的列名?) ;我还建议您查看 sp_helpconstraint (exec sybsystemprocs..sp_helptext sp_helpconstraint null,null,showsql) 的源代码 ...您应该能够使用此代码创建自己的 SELECT 以以您想要的格式显示约束数据

标签: sql primary-key sap-ase


【解决方案1】:

这是怎么做的

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         

【讨论】:

    【解决方案2】:

    我使用 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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2021-10-22
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      相关资源
      最近更新 更多