[英]How to get primary keys for all tables in Sybase
I have got a requirement of creating data dictionary from all the user tables in a sybase ase database.我需要从 sybase ase 数据库中的所有用户表创建数据字典。 I can't install tools like Sybase PowerDesigner, and so I have to make it possible using SQL queries.我无法安装 Sybase PowerDesigner 之类的工具,因此我必须使用 SQL 查询使其成为可能。
I have composed this query by taking the help of forums, to show the table name, column name, datatype, size and not null constraint.我在论坛的帮助下编写了这个查询,以显示表名、列名、数据类型、大小和非空约束。 But I am unable to find whether the column is a Primary key or not or is a part of clustered Primary key.但我无法找到该列是主键还是集群主键的一部分。
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
Can anyone here help me to do the necessary join with the required tables to get the required flag indicating its Primary Key status.这里的任何人都可以帮助我对所需的表进行必要的连接,以获得指示其主键状态的所需标志。 I already went through the syskeys and sysindexes table documentation but am unable to find which status there would be beneficial for my purpose.我已经浏览了syskeys和sysindexes表文档,但无法找到对我的目的有益的状态。
Please try below Code :请尝试以下代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.