简体   繁体   中英

SQL query to get primary keys for all tables in sybase ase 15.x along with column names

I'm using Sybase ASE 15.5 and a stranger to this database. Straight to the point--> I'm looking for a sql query that would help me get the primary keys for all tables in sybase along with the column names on which the primary key is declared. For example, if I have the following tables, organization having primary key PK_org_id on the column org_id org_alias having primary key PK_alias_id on the column alias_id org_temp having primary key PK_org_temp_id on the columns (org_id,org_name)

then the query should return me with:

  • Table_Name PK_Name Column_name
  • Organization PK_org_id org_id
  • Org_alias PK_alias_id alias_id
  • Org_temp PK_org_temp_id org_id,org_name

I've tried the below query:

select  o.name , i.name
from   sysobjects  o,   sysindexes   i   
where o.id=i.id  
and i.indid = 1  
and o.type = 'U'

but it only returns me the table name with its primary key. I want to have the column name too.

Please help!

Use the built_in function index_col(object_name, indexid, N [,owner_id]) . This lets you retrieve the Nth column of a particular index. Call it multiple times with different values for N, for example by joining it with master..spt_values where type = 'P' and supplying the number column as N.

If you don't want to code your own query, then look at the catalog procedures that are included with ASE. 'sp_pkeys' should give you what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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