简体   繁体   English

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

[英]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.我已经浏览了syskeyssysindexes表文档,但无法找到对我的目的有益的状态。

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.

相关问题 SQL查询以获取sybase ase 15.x中所有表的主键以及列名 - SQL query to get primary keys for all tables in sybase ase 15.x along with column names 如何从特定数据库中的所有表中检索不属于主键和外键的所有列 - How to retrieve all the columns from all of the tables in a particular database which are not part of primary keys and foreign keys 数据库中所有表的种子主键 - Reseed Primary Keys from all tables on a database 如何在sybase中列出模式中的所有表? - How to list down all tables in a schema in sybase? 即使主键和外键不均匀,SQL 查询也能从多个表中获取所有行 - SQL query to get all the rows from multiple tables even it has uneven primary and foreign keys 发现 Sybase ASE 中的主键/唯一键 - Discover primary / unique keys in Sybase ASE 如何在2个不同的表中强制使用不同的主键? - How to enforce different primary keys in 2 different tables? TSQL / SQL-SERVER:如何在具有主键的快照复制中查找所有表 - TSQL / SQL-SERVER: How to find all tables in a snapshot replication that have primary keys 如何在Sybase中获取所有用户定义的数据类型? - How to get all user defined datatypes in Sybase? 导出数据库中的所有对象(表,主键/外键等)。 - Exporting all objects in database (tables,primary / foreign keys, etc..)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM