如何获取Sybase中所有表的主键

时间:2015-01-30 12:28:30

标签: sql sybase sybase-ase data-dictionary

我需要从sybase ase数据库中的所有用户表创建数据字典。我无法安装Sybase PowerDesigner等工具,所以我必须使用SQL查询。

我通过论坛的帮助编写了这个查询,以显示表名,列名,数据类型,大小和非空约束。但我无法确定该列是否为主键,或者是群集主键的一部分。

    SELECT O.name as "Table",
            C.name as "Column", 
            C.length as "Length",
            T.name as "Datatype",
            C.status as "Allow Null",
            CASE C.status
                WHEN 8 THEN 'NULL' 
                WHEN 0 THEN 'NOT NULL' 
            END as "NULLS"
FROM        sysobjects O,              
            syscolumns C,
            systypes T
WHERE     O.id = C.id
AND       O.type = "U"             -- user tables only
AND       C.usertype = T.usertype 
ORDER BY  O.name, C.colid

这里的任何人都可以帮助我与必需的表进行必要的连接,以获得指示其主键状态的必需标志。我已经查看了syskeyssysindexes表格文档,但无法确定哪种状态对我的目的有益。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

    SELECT O.name as "Table",
            C.name as "Column", 
            C.length as "Length",
            T.name as "Datatype",
            C.status as "Allow Null",
            CASE C.status
                WHEN 8 THEN 'NULL' 
                WHEN 0 THEN 'NOT NULL' 
            END as "NULLS",
            ISNULL((select "Y" as PK_FLAG from syskeys K where O.id=K.id
            and K.type = 1 and (C.colid =K.key1 or C.colid =K.key2 OR C.colid =K.key3  OR C.colid =K.key4 OR C.colid =K.key5 OR C.colid =K.key6 OR C.colid =K.key7 Or C.colid =K.key8 )),"N") as Primary_Key_Flag
FROM        sysobjects O,              
            syscolumns C,
            systypes T
WHERE     O.id = C.id
AND       O.type = "U"             -- user tables only
AND       C.usertype = T.usertype 
ORDER BY  O.name, C.colid